Skip to content

Commit c03319e

Browse files
authored
Verify autosdk traces.json with BATS (#1409)
* Verify traces.json with bats * Update span kind of expected traces.json * Format expected traces.json Sort the fields, remove time fields, and redact the time fields. * Fix the redact_json function Do not fail if spans do not have attributes. Redact trace IDs for links. Remove timestamps for events. * Redact exception.stacktrace attr in traces.json * Redact exception.stacktrace attr in redact_json
1 parent 3ad4e6d commit c03319e

File tree

3 files changed

+64
-52
lines changed

3 files changed

+64
-52
lines changed

internal/test/e2e/autosdk/traces.json

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,27 @@
4747
}
4848
]
4949
},
50+
"schemaUrl": "https://opentelemetry.io/schemas/1.26.0",
5051
"scopeSpans": [
5152
{
53+
"schemaUrl": "https://some_schema",
5254
"scope": {
5355
"name": "go.opentelemetry.io/auto/internal/test/e2e/autosdk",
5456
"version": "v1.23.42"
5557
},
5658
"spans": [
5759
{
58-
"traceId": "xxxxx",
59-
"spanId": "xxxxx",
60-
"parentSpanId": "xxxxx",
6160
"flags": 256,
61+
"kind": 1,
6262
"name": "sig",
63-
"kind": 3,
64-
"startTimeUnixNano": "946684800000010000",
65-
"endTimeUnixNano": "946684800000110000",
66-
"status": {}
63+
"parentSpanId": "xxxxx",
64+
"spanId": "xxxxx",
65+
"status": {},
66+
"traceId": "xxxxx"
6767
},
6868
{
69-
"traceId": "xxxxx",
70-
"spanId": "xxxxx",
71-
"parentSpanId": "xxxxx",
72-
"flags": 256,
73-
"name": "Run",
74-
"kind": 1,
75-
"startTimeUnixNano": "946684800500000000",
76-
"endTimeUnixNano": "946684801000000000",
7769
"events": [
7870
{
79-
"timeUnixNano": "946684802000000000",
80-
"name": "exception",
8171
"attributes": [
8272
{
8373
"key": "impact",
@@ -103,24 +93,22 @@
10393
"stringValue": "xxxxx"
10494
}
10595
}
106-
]
107-
}
108-
],
109-
"links": [
110-
{
111-
"traceId": "xxxxx",
112-
"spanId": "xxxxx",
113-
"attributes": [
114-
{
115-
"key": "data",
116-
"value": {
117-
"stringValue": "Hello World"
118-
}
119-
}
12096
],
121-
"flags": 256
97+
"name": "exception"
12298
}
12399
],
100+
"flags": 256,
101+
"kind": 1,
102+
"name": "main",
103+
"parentSpanId": "",
104+
"spanId": "xxxxx",
105+
"status": {
106+
"code": 2,
107+
"message": "application error"
108+
},
109+
"traceId": "xxxxx"
110+
},
111+
{
124112
"attributes": [
125113
{
126114
"key": "user",
@@ -135,27 +123,32 @@
135123
}
136124
}
137125
],
138-
"status": {}
139-
},
140-
{
141-
"traceId": "xxxxx",
142-
"spanId": "xxxxx",
143-
"parentSpanId": "",
144126
"flags": 256,
145-
"name": "main",
146-
"kind": 3,
147-
"startTimeUnixNano": "946684800000000000",
148-
"endTimeUnixNano": "946684805000000000",
149-
"status": {
150-
"message": "application error",
151-
"code": 2
152-
}
127+
"kind": 2,
128+
"links": [
129+
{
130+
"attributes": [
131+
{
132+
"key": "data",
133+
"value": {
134+
"stringValue": "Hello World"
135+
}
136+
}
137+
],
138+
"flags": 256,
139+
"spanId": "xxxxx",
140+
"traceId": "xxxxx"
141+
}
142+
],
143+
"name": "Run",
144+
"parentSpanId": "xxxxx",
145+
"spanId": "xxxxx",
146+
"status": {},
147+
"traceId": "xxxxx"
153148
}
154-
],
155-
"schemaUrl": "https://some_schema"
149+
]
156150
}
157-
],
158-
"schemaUrl": "https://opentelemetry.io/schemas/1.26.0"
151+
]
159152
}
160153
]
161154
}

internal/test/e2e/autosdk/verify.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,8 @@ SCOPE="go.opentelemetry.io/auto/internal/test/e2e/autosdk"
152152
got=$(span_links ${SCOPE} "Run" | jq ".attributes[] | select(.key == \"data\").value.stringValue")
153153
assert_equal "$got" '"Hello World"'
154154
}
155+
156+
@test "autosdk :: expected (redacted) trace output" {
157+
redact_json
158+
assert_equal "$(git --no-pager diff ${BATS_TEST_DIRNAME}/traces.json)" ""
159+
}

internal/test/test_helpers/utilities.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,36 @@ redact_json() {
129129
jq --sort-keys '
130130
del(
131131
.resourceSpans[].scopeSpans[].spans[].startTimeUnixNano,
132-
.resourceSpans[].scopeSpans[].spans[].endTimeUnixNano
132+
.resourceSpans[].scopeSpans[].spans[].endTimeUnixNano,
133+
.resourceSpans[].scopeSpans[].spans[].events[]?.timeUnixNano
133134
)
134135
| .resourceSpans[].scopeSpans[].spans[].traceId|= (if
135136
. // "" | test("^[A-Fa-f0-9]{32}$") then "xxxxx" else (. + "<-INVALID")
136137
end)
138+
| .resourceSpans[].scopeSpans[].spans[].links[]?.traceId |= (if
139+
. // "" | test("^[A-Fa-f0-9]{32}$") then "xxxxx" else (. + "<-INVALID")
140+
end)
137141
| .resourceSpans[].scopeSpans[].spans[].spanId|= (if
138142
. // "" | test("^[A-Fa-f0-9]{16}$") then "xxxxx" else (. + "<-INVALID")
139143
end)
144+
| .resourceSpans[].scopeSpans[].spans[].links[]?.spanId |= (if
145+
. // "" | test("^[A-Fa-f0-9]{16}$") then "xxxxx" else (. + "<-INVALID")
146+
end)
140147
| .resourceSpans[].scopeSpans[].spans[].parentSpanId|= (if
141148
. // "" | test("^[A-Fa-f0-9]{16}$") then "xxxxx" else (. + "")
142149
end)
143-
| .resourceSpans[].scopeSpans[].spans[].attributes[] |= if
150+
| .resourceSpans[].scopeSpans[].spans[].attributes[]? |= if
144151
(.key == "network.peer.port") then .value.intValue |= (if
145152
. // "" | test("^[1-9][0-9]{0,4}$") then "xxxxx" else (. + "")
146153
end) else .
147154
end
155+
| .resourceSpans[].scopeSpans[].spans[].events[]?.attributes[]? |= if
156+
(.key == "exception.stacktrace")
157+
then
158+
.value.stringValue |= "xxxxx"
159+
else
160+
.
161+
end
148162
| .resourceSpans[].scopeSpans|=sort_by(.scope.name)
149163
| .resourceSpans[].scopeSpans[].spans|=sort_by(.kind)
150164
' > ${BATS_TEST_DIRNAME}/traces.json

0 commit comments

Comments
 (0)