Debian Codegraph @

trace/internal/telemetry/traces_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package telemetry
5
6 import (
7 "testing"
8 "time"
9 )
10
11 func TestTracesEncoding(t *testing.T) {
12 traces := &Traces{
13 ResourceSpans: []*ResourceSpans{{}},
14 }
15
16 t.Run("CamelCase", runJSONEncodingTests(traces, []byte(`{
17 "resourceSpans": [
18 {
19 "resource": {}
20 }
21 ]
22 }`)))
23
24 t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(traces, []byte(`{
25 "resource_spans": [
26 {
27 "resource": {}
28 }
29 ]
30 }`)))
31 }
32
33 func TestResourceSpansEncoding(t *testing.T) {
34 rs := &ResourceSpans{
35 Resource: Resource{
36 Attrs: []Attr{String("key", "val")},
37 },
38 ScopeSpans: []*ScopeSpans{{}},
39 SchemaURL: schema100,
40 }
41
42 t.Run("CamelCase", runJSONEncodingTests(rs, []byte(`{
43 "resource": {
44 "attributes": [
45 {
46 "key": "key",
47 "value": {
48 "stringValue": "val"
49 }
50 }
51 ]
52 },
53 "scopeSpans": [
54 {
55 "scope": null
56 }
57 ],
58 "schemaUrl": "http://go.opentelemetry.io/schema/v1.0.0"
59 }`)))
60
61 t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(rs, []byte(`{
62 "resource": {
63 "attributes": [
64 {
65 "key": "key",
66 "value": {
67 "string_value": "val"
68 }
69 }
70 ]
71 },
72 "scope_spans": [
73 {
74 "scope": null
75 }
76 ],
77 "schema_url": "http://go.opentelemetry.io/schema/v1.0.0"
78 }`)))
79 }
80
81 func TestScopeSpansEncoding(t *testing.T) {
82 ss := &ScopeSpans{
83 Scope: &Scope{Name: "scope"},
84 Spans: []*Span{{
85 TraceID: [16]byte{0x1},
86 SpanID: [8]byte{0x2},
87 Name: "A",
88 StartTime: y2k,
89 EndTime: y2k.Add(time.Second),
90 }, {
91 TraceID: [16]byte{0x1},
92 SpanID: [8]byte{0x3},
93 Name: "B",
94 StartTime: y2k.Add(time.Second),
95 EndTime: y2k.Add(2 * time.Second),
96 }},
97 SchemaURL: schema100,
98 }
99
100 t.Run("CamelCase", runJSONEncodingTests(ss, []byte(`{
101 "scope": {
102 "name": "scope"
103 },
104 "spans": [
105 {
106 "traceId": "01000000000000000000000000000000",
107 "spanId": "0200000000000000",
108 "name": "A",
109 "startTimeUnixNano": 946684800000000000,
110 "endTimeUnixNano": 946684801000000000
111 },
112 {
113 "traceId": "01000000000000000000000000000000",
114 "spanId": "0300000000000000",
115 "name": "B",
116 "startTimeUnixNano": 946684801000000000,
117 "endTimeUnixNano": 946684802000000000
118 }
119 ],
120 "schemaUrl": "http://go.opentelemetry.io/schema/v1.0.0"
121 }`)))
122
123 t.Run("SnakeCase/Unmarshal", runJSONUnmarshalTest(ss, []byte(`{
124 "scope": {
125 "name": "scope"
126 },
127 "spans": [
128 {
129 "trace_id": "01000000000000000000000000000000",
130 "span_id": "0200000000000000",
131 "name": "A",
132 "start_time_unix_nano": 946684800000000000,
133 "end_time_unix_nano": 946684801000000000
134 },
135 {
136 "trace_id": "01000000000000000000000000000000",
137 "span_id": "0300000000000000",
138 "name": "B",
139 "start_time_unix_nano": 946684801000000000,
140 "end_time_unix_nano": 946684802000000000
141 }
142 ],
143 "schema_url": "http://go.opentelemetry.io/schema/v1.0.0"
144 }`)))
145 }