Skip to content

Commit 769c00e

Browse files
committed
OTEL-2540 add unit tests for traces
1 parent 01997b2 commit 769c00e

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

exporter/exporterhelper/traces.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88
"encoding/json"
99
"errors"
1010

11-
"go.uber.org/zap"
12-
1311
"go.opentelemetry.io/otel/trace"
12+
"go.uber.org/zap"
1413

1514
"go.opentelemetry.io/collector/component"
1615
"go.opentelemetry.io/collector/consumer"

exporter/exporterhelper/traces_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,40 @@ func checkWrapSpanForTraces(t *testing.T, sr *tracetest.SpanRecorder, tracer tra
477477
require.Containsf(t, sd.Attributes(), attribute.KeyValue{Key: internal.ItemsFailed, Value: attribute.Int64Value(failedToSendSpans)}, "SpanData %v", sd)
478478
}
479479
}
480+
481+
func TestSerializableToLink(t *testing.T) {
482+
sl := SerializableLink{
483+
TraceID: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
484+
SpanID: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
485+
TraceFlags: byte(trace.FlagsSampled),
486+
TraceState: TraceStateSerializable{
487+
Members: []struct {
488+
Key string `json:"key"`
489+
Value string `json:"value"`
490+
}{
491+
{
492+
Key: "@key", // @ is not allowed in trace state keys
493+
Value: "value",
494+
},
495+
},
496+
},
497+
}
498+
res := serializableToLink(sl)
499+
// TraceState will be empty due to error in inserting Member Key "@key"
500+
assert.Equal(t, trace.TraceState{}, res.SpanContext.TraceState())
501+
}
502+
503+
func TestTracesEncoding_Unmarshal_InvalidJSON(t *testing.T) {
504+
// Create invalid JSON bytes
505+
invalidJSON := []byte(`{invalid json}`)
506+
507+
// Create tracesEncoding instance
508+
encoding := tracesEncoding{}
509+
510+
// Attempt to unmarshal invalid JSON
511+
req, err := encoding.Unmarshal(invalidJSON)
512+
513+
// Verify error is returned and request is nil
514+
require.Error(t, err)
515+
assert.Nil(t, req)
516+
}

0 commit comments

Comments
 (0)