Skip to content

Commit 36bee93

Browse files
authored
Use reflect to construct a Record in logtest (open-telemetry#5275)
* Use reflect to construct Record * Fix merge * Fix merge
1 parent e0ed6a3 commit 36bee93

File tree

5 files changed

+49
-76
lines changed

5 files changed

+49
-76
lines changed

exporters/otlp/otlplog/otlploghttp/internal/transform/log_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var (
109109
TraceID: trace.TraceID(traceIDA),
110110
SpanID: trace.SpanID(spanIDA),
111111
TraceFlags: trace.TraceFlags(flagsA),
112-
InstrumentationScope: scope,
112+
InstrumentationScope: &scope,
113113
Resource: res,
114114
}.NewRecord())
115115

@@ -123,7 +123,7 @@ var (
123123
TraceID: trace.TraceID(traceIDA),
124124
SpanID: trace.SpanID(spanIDA),
125125
TraceFlags: trace.TraceFlags(flagsA),
126-
InstrumentationScope: scope,
126+
InstrumentationScope: &scope,
127127
Resource: res,
128128
}.NewRecord())
129129

@@ -137,7 +137,7 @@ var (
137137
TraceID: trace.TraceID(traceIDB),
138138
SpanID: trace.SpanID(spanIDB),
139139
TraceFlags: trace.TraceFlags(flagsB),
140-
InstrumentationScope: scope,
140+
InstrumentationScope: &scope,
141141
Resource: res,
142142
}.NewRecord())
143143

@@ -151,7 +151,7 @@ var (
151151
TraceID: trace.TraceID(traceIDB),
152152
SpanID: trace.SpanID(spanIDB),
153153
TraceFlags: trace.TraceFlags(flagsB),
154-
InstrumentationScope: scope,
154+
InstrumentationScope: &scope,
155155
Resource: res,
156156
}.NewRecord())
157157

exporters/stdout/stdoutlog/exporter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func getRecord(now time.Time) sdklog.Record {
294294
"https://example.com/custom-resource-schema",
295295
attribute.String("foo", "bar"),
296296
),
297-
InstrumentationScope: instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"},
297+
InstrumentationScope: &instrumentation.Scope{Name: "name", Version: "version", SchemaURL: "https://example.com/custom-schema"},
298298
DroppedAttributes: 10,
299299
}
300300

sdk/log/logtest/example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func ExampleRecordFactory() {
2020
exp := exporter{os.Stdout}
2121
rf := logtest.RecordFactory{
22-
InstrumentationScope: instrumentation.Scope{Name: "myapp"},
22+
InstrumentationScope: &instrumentation.Scope{Name: "myapp"},
2323
}
2424

2525
rf.Body = logapi.StringValue("foo")

sdk/log/logtest/factory.go

+37-68
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest"
66

77
import (
8-
"context"
9-
"slices"
8+
"reflect"
109
"time"
10+
"unsafe"
1111

1212
"go.opentelemetry.io/otel/log"
1313
"go.opentelemetry.io/otel/sdk/instrumentation"
@@ -33,75 +33,44 @@ type RecordFactory struct {
3333
TraceFlags trace.TraceFlags
3434

3535
Resource *resource.Resource
36-
InstrumentationScope instrumentation.Scope
36+
InstrumentationScope *instrumentation.Scope
3737

38-
DroppedAttributes int
38+
DroppedAttributes int
39+
AttributeValueLengthLimit int
40+
AttributeCountLimit int
3941
}
4042

41-
// NewRecord returns a log record.
42-
func (b RecordFactory) NewRecord() sdklog.Record {
43-
var record sdklog.Record
44-
p := processor(func(r sdklog.Record) {
45-
r.SetTimestamp(b.Timestamp)
46-
r.SetObservedTimestamp(b.ObservedTimestamp)
47-
r.SetSeverity(b.Severity)
48-
r.SetSeverityText(b.SeverityText)
49-
r.SetBody(b.Body)
50-
r.SetAttributes(slices.Clone(b.Attributes)...)
51-
52-
// Generate dropped attributes.
53-
for i := 0; i < b.DroppedAttributes; i++ {
54-
r.AddAttributes(log.KeyValue{})
55-
}
56-
57-
r.SetTraceID(b.TraceID)
58-
r.SetSpanID(b.SpanID)
59-
r.SetTraceFlags(b.TraceFlags)
60-
61-
record = r
62-
})
63-
64-
attributeCountLimit := -1
65-
if b.DroppedAttributes > 0 {
66-
// Make sure that we can generate dropped attributes.
67-
attributeCountLimit = len(b.Attributes)
68-
}
69-
70-
res := b.Resource
71-
if res == nil {
72-
res = resource.Empty()
73-
}
74-
75-
provider := sdklog.NewLoggerProvider(
76-
sdklog.WithResource(res),
77-
sdklog.WithAttributeCountLimit(attributeCountLimit),
78-
sdklog.WithAttributeValueLengthLimit(-1),
79-
sdklog.WithProcessor(p),
80-
)
81-
82-
l := provider.Logger(b.InstrumentationScope.Name,
83-
log.WithInstrumentationVersion(b.InstrumentationScope.Version),
84-
log.WithSchemaURL(b.InstrumentationScope.SchemaURL),
85-
)
86-
l.Emit(context.Background(), log.Record{}) // This executes the processor function.
87-
return record
88-
}
89-
90-
type processor func(r sdklog.Record)
91-
92-
func (p processor) OnEmit(ctx context.Context, r sdklog.Record) error {
93-
p(r)
94-
return nil
95-
}
96-
97-
func (processor) Enabled(context.Context, sdklog.Record) bool {
98-
return true
99-
}
100-
101-
func (processor) Shutdown(ctx context.Context) error {
102-
return nil
43+
// NewRecord returns a [sdklog.Record] configured from the values of f.
44+
func (f RecordFactory) NewRecord() sdklog.Record {
45+
// r needs to be addressable for set() below.
46+
r := new(sdklog.Record)
47+
48+
// Set to unlimited so attributes are set exactly.
49+
set(r, "attributeCountLimit", -1)
50+
set(r, "attributeValueLengthLimit", -1)
51+
52+
r.SetTimestamp(f.Timestamp)
53+
r.SetObservedTimestamp(f.ObservedTimestamp)
54+
r.SetSeverity(f.Severity)
55+
r.SetSeverityText(f.SeverityText)
56+
r.SetBody(f.Body)
57+
r.SetAttributes(f.Attributes...)
58+
r.SetTraceID(f.TraceID)
59+
r.SetSpanID(f.SpanID)
60+
r.SetTraceFlags(f.TraceFlags)
61+
62+
set(r, "resource", f.Resource)
63+
set(r, "scope", f.InstrumentationScope)
64+
set(r, "dropped", f.DroppedAttributes)
65+
set(r, "attributeCountLimit", f.AttributeCountLimit)
66+
set(r, "attributeValueLengthLimit", f.AttributeValueLengthLimit)
67+
68+
return *r
10369
}
10470

105-
func (processor) ForceFlush(context.Context) error {
106-
return nil
71+
func set(r *sdklog.Record, name string, value any) {
72+
rVal := reflect.ValueOf(r).Elem()
73+
rf := rVal.FieldByName(name)
74+
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
75+
rf.Set(reflect.ValueOf(value))
10776
}

sdk/log/logtest/factory_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
"go.opentelemetry.io/otel/trace"
1919
)
2020

21+
func TestRecordFactoryEmpty(t *testing.T) {
22+
assert.Equal(t, sdklog.Record{}, RecordFactory{}.NewRecord())
23+
}
24+
2125
func TestRecordFactory(t *testing.T) {
2226
now := time.Now()
2327
observed := now.Add(time.Second)
@@ -49,7 +53,7 @@ func TestRecordFactory(t *testing.T) {
4953
SpanID: spanID,
5054
TraceFlags: traceFlags,
5155
DroppedAttributes: dropped,
52-
InstrumentationScope: scope,
56+
InstrumentationScope: &scope,
5357
Resource: r,
5458
}.NewRecord()
5559

@@ -82,7 +86,7 @@ func TestRecordFactoryMultiple(t *testing.T) {
8286
Timestamp: now,
8387
Attributes: attrs,
8488
DroppedAttributes: 1,
85-
InstrumentationScope: scope,
89+
InstrumentationScope: &scope,
8690
}
8791

8892
record1 := f.NewRecord()

0 commit comments

Comments
 (0)