@@ -6,6 +6,8 @@ package loghouseprocessor
6
6
import (
7
7
"testing"
8
8
9
+ "go.opentelemetry.io/collector/pdata/pcommon"
10
+
9
11
"github.com/stretchr/testify/assert"
10
12
"go.opentelemetry.io/collector/pdata/plog"
11
13
)
@@ -244,23 +246,43 @@ func Test_processLine(t *testing.T) {
244
246
})
245
247
}
246
248
247
- // func assertAttr(t *testing.T, expected, key string, log *plog.LogRecord) {
248
- // val, ok := log.Attributes().Get(key)
249
- // assert.True(t, ok)
250
- // assert.Equal(t, expected, val.Str())
251
- // }
252
- // TODO for testing that metrics get updated
253
- // func getCounter() metric.Int64Counter {
254
- // meterProvider := sdkmetric.NewMeterProvider()
255
- //
256
- // meter := meterProvider.Meter("xoyo-logs")
257
- // observedLogsCtr, err := meter.Int64Counter(
258
- // "loghouse_observed_logs",
259
- // metric.WithDescription("Number of log records that were not routed to some or all exporters"),
260
- // )
261
- // if err != nil {
262
- // panic(err)
263
- // }
264
- // return observedLogsCtr
265
- //
266
- // }
249
+ func Test_promoteTraceAndSpan (t * testing.T ) {
250
+ type expect struct {
251
+ trace pcommon.TraceID
252
+ span pcommon.SpanID
253
+ }
254
+ tests := []struct {
255
+ name string
256
+ args map [string ]any
257
+ expected expect
258
+ }{
259
+ {name : "missing both" , args : map [string ]any (nil ), expected : expect {
260
+ trace : pcommon.TraceID {},
261
+ span : pcommon.SpanID {},
262
+ }},
263
+ {name : "have both" , args : map [string ]any {"traceId" : "00000000000000000000000000000001" , "spanId" : "0000000000000001" }, expected : expect {
264
+ trace : pcommon.TraceID {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x01 },
265
+ span : pcommon.SpanID {0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x01 },
266
+ }},
267
+ {name : "one passed it, so ignore" , args : map [string ]any {"traceId" : "00000000000000000000000000000001" }, expected : expect {
268
+ trace : pcommon.TraceID {},
269
+ span : pcommon.SpanID {},
270
+ }},
271
+ {name : "garbage, also ignore" , args : map [string ]any {"traceId" : "00000000000000000000000000000001" , "spanId" : "blah" }, expected : expect {
272
+ trace : pcommon.TraceID {},
273
+ span : pcommon.SpanID {},
274
+ }},
275
+ }
276
+ for _ , tt := range tests {
277
+ t .Run (tt .name , func (t * testing.T ) {
278
+ l := plog .NewLogRecord ()
279
+ err := l .Attributes ().FromRaw (tt .args )
280
+ if err != nil {
281
+ t .Fatalf ("failed to parse from the args" )
282
+ }
283
+ promoteTraceAndSpan (& l )
284
+ assert .Equal (t , tt .expected .span , l .SpanID ())
285
+ assert .Equal (t , tt .expected .trace , l .TraceID ())
286
+ })
287
+ }
288
+ }
0 commit comments