diff --git a/exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go b/exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go index 8a3e85f0a76..25613601b1c 100644 --- a/exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go +++ b/exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go @@ -109,7 +109,7 @@ var ( TraceID: trace.TraceID(traceIDA), SpanID: trace.SpanID(spanIDA), TraceFlags: trace.TraceFlags(flagsA), - InstrumentationScope: scope, + InstrumentationScope: &scope, Resource: res, }.NewRecord()) @@ -123,7 +123,7 @@ var ( TraceID: trace.TraceID(traceIDA), SpanID: trace.SpanID(spanIDA), TraceFlags: trace.TraceFlags(flagsA), - InstrumentationScope: scope, + InstrumentationScope: &scope, Resource: res, }.NewRecord()) @@ -137,7 +137,7 @@ var ( TraceID: trace.TraceID(traceIDB), SpanID: trace.SpanID(spanIDB), TraceFlags: trace.TraceFlags(flagsB), - InstrumentationScope: scope, + InstrumentationScope: &scope, Resource: res, }.NewRecord()) @@ -151,7 +151,7 @@ var ( TraceID: trace.TraceID(traceIDB), SpanID: trace.SpanID(spanIDB), TraceFlags: trace.TraceFlags(flagsB), - InstrumentationScope: scope, + InstrumentationScope: &scope, Resource: res, }.NewRecord()) diff --git a/exporters/stdout/stdoutlog/exporter_test.go b/exporters/stdout/stdoutlog/exporter_test.go index 45ccf2023cf..606fba40b3f 100644 --- a/exporters/stdout/stdoutlog/exporter_test.go +++ b/exporters/stdout/stdoutlog/exporter_test.go @@ -294,7 +294,7 @@ func getRecord(now time.Time) sdklog.Record { "https://example.com/custom-resource-schema", attribute.String("foo", "bar"), ), - InstrumentationScope: instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"}, + InstrumentationScope: &instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"}, DroppedAttributes: 10, } diff --git a/metric/asyncfloat64.go b/metric/asyncfloat64.go index c7234f4bc8d..cf23db77803 100644 --- a/metric/asyncfloat64.go +++ b/metric/asyncfloat64.go @@ -39,7 +39,7 @@ type Float64ObservableCounter interface { } // Float64ObservableCounterConfig contains options for asynchronous counter -// instruments that record int64 values. +// instruments that record float64 values. type Float64ObservableCounterConfig struct { description string unit string @@ -97,7 +97,7 @@ type Float64ObservableUpDownCounter interface { } // Float64ObservableUpDownCounterConfig contains options for asynchronous -// counter instruments that record int64 values. +// counter instruments that record float64 values. type Float64ObservableUpDownCounterConfig struct { description string unit string @@ -154,7 +154,7 @@ type Float64ObservableGauge interface { } // Float64ObservableGaugeConfig contains options for asynchronous counter -// instruments that record int64 values. +// instruments that record float64 values. type Float64ObservableGaugeConfig struct { description string unit string diff --git a/metric/syncfloat64.go b/metric/syncfloat64.go index 5420d546eb7..1b00a7f6c15 100644 --- a/metric/syncfloat64.go +++ b/metric/syncfloat64.go @@ -28,7 +28,7 @@ type Float64Counter interface { } // Float64CounterConfig contains options for synchronous counter instruments that -// record int64 values. +// record float64 values. type Float64CounterConfig struct { description string unit string @@ -81,7 +81,7 @@ type Float64UpDownCounter interface { } // Float64UpDownCounterConfig contains options for synchronous counter -// instruments that record int64 values. +// instruments that record float64 values. type Float64UpDownCounterConfig struct { description string unit string @@ -133,8 +133,8 @@ type Float64Histogram interface { Record(ctx context.Context, incr float64, options ...RecordOption) } -// Float64HistogramConfig contains options for synchronous counter instruments -// that record int64 values. +// Float64HistogramConfig contains options for synchronous histogram +// instruments that record float64 values. type Float64HistogramConfig struct { description string unit string diff --git a/metric/syncint64.go b/metric/syncint64.go index 0dcbf06db99..ec96c7f1816 100644 --- a/metric/syncint64.go +++ b/metric/syncint64.go @@ -133,7 +133,7 @@ type Int64Histogram interface { Record(ctx context.Context, incr int64, options ...RecordOption) } -// Int64HistogramConfig contains options for synchronous counter instruments +// Int64HistogramConfig contains options for synchronous histogram instruments // that record int64 values. type Int64HistogramConfig struct { description string diff --git a/sdk/log/logtest/example_test.go b/sdk/log/logtest/example_test.go index 68376a726c1..7bfdb38df3b 100644 --- a/sdk/log/logtest/example_test.go +++ b/sdk/log/logtest/example_test.go @@ -19,7 +19,7 @@ import ( func ExampleRecordFactory() { exp := exporter{os.Stdout} rf := logtest.RecordFactory{ - InstrumentationScope: instrumentation.Scope{Name: "myapp"}, + InstrumentationScope: &instrumentation.Scope{Name: "myapp"}, } rf.Body = logapi.StringValue("foo") diff --git a/sdk/log/logtest/factory.go b/sdk/log/logtest/factory.go index 86b75d89317..f9f32aafac8 100644 --- a/sdk/log/logtest/factory.go +++ b/sdk/log/logtest/factory.go @@ -5,9 +5,9 @@ package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest" import ( - "context" - "slices" + "reflect" "time" + "unsafe" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/sdk/instrumentation" @@ -33,75 +33,44 @@ type RecordFactory struct { TraceFlags trace.TraceFlags Resource *resource.Resource - InstrumentationScope instrumentation.Scope + InstrumentationScope *instrumentation.Scope - DroppedAttributes int + DroppedAttributes int + AttributeValueLengthLimit int + AttributeCountLimit int } -// NewRecord returns a log record. -func (b RecordFactory) NewRecord() sdklog.Record { - var record sdklog.Record - p := processor(func(r sdklog.Record) { - r.SetTimestamp(b.Timestamp) - r.SetObservedTimestamp(b.ObservedTimestamp) - r.SetSeverity(b.Severity) - r.SetSeverityText(b.SeverityText) - r.SetBody(b.Body) - r.SetAttributes(slices.Clone(b.Attributes)...) - - // Generate dropped attributes. - for i := 0; i < b.DroppedAttributes; i++ { - r.AddAttributes(log.KeyValue{}) - } - - r.SetTraceID(b.TraceID) - r.SetSpanID(b.SpanID) - r.SetTraceFlags(b.TraceFlags) - - record = r - }) - - attributeCountLimit := -1 - if b.DroppedAttributes > 0 { - // Make sure that we can generate dropped attributes. - attributeCountLimit = len(b.Attributes) - } - - res := b.Resource - if res == nil { - res = resource.Empty() - } - - provider := sdklog.NewLoggerProvider( - sdklog.WithResource(res), - sdklog.WithAttributeCountLimit(attributeCountLimit), - sdklog.WithAttributeValueLengthLimit(-1), - sdklog.WithProcessor(p), - ) - - l := provider.Logger(b.InstrumentationScope.Name, - log.WithInstrumentationVersion(b.InstrumentationScope.Version), - log.WithSchemaURL(b.InstrumentationScope.SchemaURL), - ) - l.Emit(context.Background(), log.Record{}) // This executes the processor function. - return record -} - -type processor func(r sdklog.Record) - -func (p processor) OnEmit(ctx context.Context, r sdklog.Record) error { - p(r) - return nil -} - -func (processor) Enabled(context.Context, sdklog.Record) bool { - return true -} - -func (processor) Shutdown(ctx context.Context) error { - return nil +// NewRecord returns a [sdklog.Record] configured from the values of f. +func (f RecordFactory) NewRecord() sdklog.Record { + // r needs to be addressable for set() below. + r := new(sdklog.Record) + + // Set to unlimited so attributes are set exactly. + set(r, "attributeCountLimit", -1) + set(r, "attributeValueLengthLimit", -1) + + r.SetTimestamp(f.Timestamp) + r.SetObservedTimestamp(f.ObservedTimestamp) + r.SetSeverity(f.Severity) + r.SetSeverityText(f.SeverityText) + r.SetBody(f.Body) + r.SetAttributes(f.Attributes...) + r.SetTraceID(f.TraceID) + r.SetSpanID(f.SpanID) + r.SetTraceFlags(f.TraceFlags) + + set(r, "resource", f.Resource) + set(r, "scope", f.InstrumentationScope) + set(r, "dropped", f.DroppedAttributes) + set(r, "attributeCountLimit", f.AttributeCountLimit) + set(r, "attributeValueLengthLimit", f.AttributeValueLengthLimit) + + return *r } -func (processor) ForceFlush(context.Context) error { - return nil +func set(r *sdklog.Record, name string, value any) { + rVal := reflect.ValueOf(r).Elem() + rf := rVal.FieldByName(name) + rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem() + rf.Set(reflect.ValueOf(value)) } diff --git a/sdk/log/logtest/factory_test.go b/sdk/log/logtest/factory_test.go index dc9662cd633..63e48de8b5c 100644 --- a/sdk/log/logtest/factory_test.go +++ b/sdk/log/logtest/factory_test.go @@ -18,6 +18,10 @@ import ( "go.opentelemetry.io/otel/trace" ) +func TestRecordFactoryEmpty(t *testing.T) { + assert.Equal(t, sdklog.Record{}, RecordFactory{}.NewRecord()) +} + func TestRecordFactory(t *testing.T) { now := time.Now() observed := now.Add(time.Second) @@ -49,7 +53,7 @@ func TestRecordFactory(t *testing.T) { SpanID: spanID, TraceFlags: traceFlags, DroppedAttributes: dropped, - InstrumentationScope: scope, + InstrumentationScope: &scope, Resource: r, }.NewRecord() @@ -82,7 +86,7 @@ func TestRecordFactoryMultiple(t *testing.T) { Timestamp: now, Attributes: attrs, DroppedAttributes: 1, - InstrumentationScope: scope, + InstrumentationScope: &scope, } record1 := f.NewRecord()