5
5
package logtest // import "go.opentelemetry.io/otel/sdk/log/logtest"
6
6
7
7
import (
8
- "context"
9
- "slices"
8
+ "reflect"
10
9
"time"
10
+ "unsafe"
11
11
12
12
"go.opentelemetry.io/otel/log"
13
13
"go.opentelemetry.io/otel/sdk/instrumentation"
@@ -33,75 +33,44 @@ type RecordFactory struct {
33
33
TraceFlags trace.TraceFlags
34
34
35
35
Resource * resource.Resource
36
- InstrumentationScope instrumentation.Scope
36
+ InstrumentationScope * instrumentation.Scope
37
37
38
- DroppedAttributes int
38
+ DroppedAttributes int
39
+ AttributeValueLengthLimit int
40
+ AttributeCountLimit int
39
41
}
40
42
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
103
69
}
104
70
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 ))
107
76
}
0 commit comments