Skip to content

Commit d3edc7d

Browse files
committed
update JSON tags to match OTel API Spec [OTEL-2540]
1 parent 24dec4a commit d3edc7d

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
3434
- `go.opentelemetry.io/otel/sdk/log/logtest` is now a separate Go module. (#6466)
3535
- `Recorder` in `go.opentelemetry.io/otel/log/logtest` no longer separately stores records emitted by loggers with the same instrumentation scope. (#6507)
3636
- Improve performance of `BatchProcessor` in `go.opentelemetry.io/otel/sdk/log` by not exporting when exporter cannot accept more. (#6569, #6641)
37+
- Update JSON tags for `TraceId`, `SpanId`, and `IsRemote` in `go.opentelemetry.io/otel/trace` to match [OpenTelemetry API specification for SpanContext items](https://github.com/open-telemetry/opentelemetry-specification/blob/815598814f3cf461ad5493ccbddd53633fb5cf24/specification/trace/api.md#spancontext) (#6738)
3738

3839
### Deprecated
3940

trace/trace.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ func (tf *TraceFlags) UnmarshalJSON(data []byte) error {
230230

231231
// SpanContextConfig contains mutable fields usable for constructing
232232
// an immutable SpanContext.
233+
// JSON tags match OTel API Specification:
234+
// https://github.com/open-telemetry/opentelemetry-specification/blob/815598814f3cf461ad5493ccbddd53633fb5cf24/specification/trace/api.md#spancontext
233235
type SpanContextConfig struct {
234-
TraceID TraceID
235-
SpanID SpanID
236-
TraceFlags TraceFlags
237-
TraceState TraceState
238-
Remote bool
236+
TraceID TraceID `json:"TraceId"`
237+
SpanID SpanID `json:"SpanId"`
238+
TraceFlags TraceFlags `json:"TraceFlags"`
239+
TraceState TraceState `json:"TraceState"`
240+
Remote bool `json:"IsRemote"`
239241
}
240242

241243
// NewSpanContext constructs a SpanContext using values from the provided

trace/trace_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestSpanContextMarshalJSON(t *testing.T) {
151151
name: "SpanContext.MarshalJSON() returns json with partial data",
152152
tid: [16]byte{1},
153153
sid: [8]byte{42},
154-
want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"00","TraceState":"","Remote":false}`),
154+
want: []byte(`{"TraceId":"01000000000000000000000000000000","SpanId":"2a00000000000000","TraceFlags":"00","TraceState":"","IsRemote":false}`),
155155
},
156156
{
157157
name: "SpanContext.MarshalJSON() returns json with full data",
@@ -162,7 +162,7 @@ func TestSpanContextMarshalJSON(t *testing.T) {
162162
tstate: TraceState{list: []member{
163163
{Key: "foo", Value: "1"},
164164
}},
165-
want: []byte(`{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","Remote":true}`),
165+
want: []byte(`{"TraceId":"01000000000000000000000000000000","SpanId":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","IsRemote":true}`),
166166
},
167167
} {
168168
t.Run(testcase.name, func(t *testing.T) {
@@ -761,7 +761,7 @@ func TestSpanContextUnmarshalJSON(t *testing.T) {
761761
}{
762762
{
763763
name: "valid full SpanContext",
764-
input: `{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","Remote":true}`,
764+
input: `{"TraceId":"01000000000000000000000000000000","SpanId":"2a00000000000000","TraceFlags":"01","TraceState":"foo=1","IsRemote":true}`,
765765
want: NewSpanContext(SpanContextConfig{
766766
TraceID: TraceID{0x01},
767767
SpanID: SpanID{0x2a},
@@ -772,20 +772,20 @@ func TestSpanContextUnmarshalJSON(t *testing.T) {
772772
},
773773
{
774774
name: "valid partial SpanContext",
775-
input: `{"TraceID":"01000000000000000000000000000000","SpanID":"2a00000000000000"}`,
775+
input: `{"TraceId":"01000000000000000000000000000000","SpanId":"2a00000000000000"}`,
776776
want: NewSpanContext(SpanContextConfig{
777777
TraceID: TraceID{0x01},
778778
SpanID: SpanID{0x2a},
779779
}),
780780
},
781781
{
782782
name: "invalid TraceID",
783-
input: `{"TraceID":"00000000000000000000000000000000","SpanID":"2a00000000000000"}`,
783+
input: `{"TraceId":"00000000000000000000000000000000","SpanId":"2a00000000000000"}`,
784784
wantErr: true,
785785
},
786786
{
787787
name: "invalid JSON",
788-
input: `{"TraceID:123}`,
788+
input: `{"TraceId:123}`,
789789
wantErr: true,
790790
},
791791
}

trace/tracestate.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,18 @@ func (ts TraceState) MarshalJSON() ([]byte, error) {
222222
}
223223

224224
// UnmarshalJSON implements the json.Unmarshaler interface for TraceState.
225-
// It expects the JSON to be a string (as produced by MarshalJSON), parses
226-
// it via ParseTraceState, and replaces the receiver’s contents.
225+
// It expects the JSON to be a string (as produced by MarshalJSON).
227226
func (ts *TraceState) UnmarshalJSON(data []byte) error {
228-
// 1) Unmarshal the JSON payload into a Go string.
229227
var raw string
230228
if err := json.Unmarshal(data, &raw); err != nil {
231229
return err
232230
}
233231

234-
// 2) Parse that string into a TraceState.
235232
parsed, err := ParseTraceState(raw)
236233
if err != nil {
237234
return err
238235
}
239236

240-
// 3) Overwrite the receiver with the parsed result.
241237
*ts = parsed
242238
return nil
243239
}

0 commit comments

Comments
 (0)