Skip to content

Commit 4169384

Browse files
committed
replace serializableTraceState with tracestate
1 parent f3b4373 commit 4169384

2 files changed

Lines changed: 31 additions & 69 deletions

File tree

exporter/exporterhelper/traces.go

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,81 +46,49 @@ func NewTracesQueueBatchSettings() QueueBatchSettings {
4646
}
4747
}
4848

49-
// TraceStateSerializable represents a serializable version of TraceState
50-
type TraceStateSerializable struct {
51-
Members []struct {
52-
Key string `json:"key"`
53-
Value string `json:"value"`
54-
} `json:"members"`
49+
type SerializableLink struct {
50+
TraceID [16]byte `json:"trace_id"`
51+
SpanID [8]byte `json:"span_id"`
52+
TraceFlags byte `json:"trace_flags"`
53+
TraceState trace.TraceState `json:"trace_state"`
5554
}
5655

57-
// traceStateToSerializable converts a TraceState to a serializable format
58-
func traceStateToSerializable(ts trace.TraceState) TraceStateSerializable {
59-
result := TraceStateSerializable{
60-
Members: make([]struct {
61-
Key string `json:"key"`
62-
Value string `json:"value"`
63-
}, 0, ts.Len()),
56+
func (sl *SerializableLink) UnmarshalJSON(data []byte) error {
57+
type Alias SerializableLink // Prevent recursion
58+
aux := &struct {
59+
TraceState string `json:"trace_state"`
60+
*Alias
61+
}{
62+
Alias: (*Alias)(sl),
6463
}
65-
66-
ts.Walk(func(key, value string) bool {
67-
result.Members = append(result.Members, struct {
68-
Key string `json:"key"`
69-
Value string `json:"value"`
70-
}{
71-
Key: key,
72-
Value: value,
73-
})
74-
return true
75-
})
76-
77-
return result
78-
}
79-
80-
// serializableToTraceState converts a serializable format back to TraceState
81-
func serializableToTraceState(ts TraceStateSerializable) (trace.TraceState, error) {
82-
var result trace.TraceState
83-
var err error
84-
85-
for _, m := range ts.Members {
86-
result, err = result.Insert(m.Key, m.Value)
64+
if err := json.Unmarshal(data, &aux); err != nil {
65+
return err
66+
}
67+
if aux.TraceState != "" {
68+
ts, err := trace.ParseTraceState(aux.TraceState)
8769
if err != nil {
88-
return trace.TraceState{}, err
70+
return err
8971
}
72+
sl.TraceState = ts
9073
}
91-
92-
return result, nil
93-
}
94-
95-
// Update SerializableLink to use the new TraceState serialization
96-
type SerializableLink struct {
97-
TraceID [16]byte `json:"trace_id"`
98-
SpanID [8]byte `json:"span_id"`
99-
TraceFlags byte `json:"trace_flags"`
100-
TraceState TraceStateSerializable `json:"trace_state"`
74+
return nil
10175
}
10276

10377
func linkToSerializable(l trace.Link) SerializableLink {
10478
return SerializableLink{
10579
TraceID: l.SpanContext.TraceID(),
10680
SpanID: l.SpanContext.SpanID(),
10781
TraceFlags: byte(l.SpanContext.TraceFlags()),
108-
TraceState: traceStateToSerializable(l.SpanContext.TraceState()),
82+
TraceState: l.SpanContext.TraceState(),
10983
}
11084
}
11185

11286
func serializableToLink(sl SerializableLink) trace.Link {
113-
ts, err := serializableToTraceState(sl.TraceState)
114-
if err != nil {
115-
// If there's an error parsing the trace state, use an empty one
116-
ts = trace.TraceState{}
117-
}
118-
11987
sc := trace.NewSpanContext(trace.SpanContextConfig{
12088
TraceID: sl.TraceID,
12189
SpanID: sl.SpanID,
12290
TraceFlags: trace.TraceFlags(sl.TraceFlags),
123-
TraceState: ts,
91+
TraceState: sl.TraceState,
12492
})
12593
return trace.Link{
12694
SpanContext: sc,
@@ -143,13 +111,13 @@ func newTracesRequest(td ptrace.Traces, links []trace.Link) Request {
143111

144112
type tracesEncoding struct{}
145113

146-
type tracesWithLinks struct {
114+
type tracesWithSpanContexts struct {
147115
Traces []byte `json:"traces"`
148116
Links []SerializableLink `json:"links"`
149117
}
150118

151119
func (tracesEncoding) Unmarshal(bytes []byte) (Request, error) {
152-
var twl tracesWithLinks
120+
var twl tracesWithSpanContexts
153121
if err := json.Unmarshal(bytes, &twl); err != nil {
154122
return nil, err
155123
}
@@ -174,7 +142,7 @@ func (tracesEncoding) Marshal(req Request) ([]byte, error) {
174142
for i, l := range tr.links {
175143
serializableLinks[i] = linkToSerializable(l)
176144
}
177-
twl := tracesWithLinks{
145+
twl := tracesWithSpanContexts{
178146
Traces: tracesBytes,
179147
Links: serializableLinks,
180148
}

exporter/exporterhelper/traces_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,25 +479,19 @@ func checkWrapSpanForTraces(t *testing.T, sr *tracetest.SpanRecorder, tracer tra
479479
}
480480

481481
func TestSerializableToLink(t *testing.T) {
482+
ts, err := trace.TraceState{}.Insert("key", "value")
483+
if err != nil {
484+
t.Fatalf("Error inserting trace state: %v", err)
485+
}
482486
sl := SerializableLink{
483487
TraceID: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
484488
SpanID: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
485489
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-
},
490+
TraceState: ts,
497491
}
498492
res := serializableToLink(sl)
499493
// TraceState will be empty due to error in inserting Member Key "@key"
500-
assert.Equal(t, trace.TraceState{}, res.SpanContext.TraceState())
494+
assert.Equal(t, ts, res.SpanContext.TraceState())
501495
}
502496

503497
func TestTracesEncoding_Unmarshal_InvalidJSON(t *testing.T) {

0 commit comments

Comments
 (0)