-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_span_events.py
More file actions
205 lines (170 loc) · 8.6 KB
/
test_span_events.py
File metadata and controls
205 lines (170 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import json
import pytest
from utils import scenarios, features, rfc
from utils.docker_fixtures.spec.trace import find_span, find_trace
from utils.docker_fixtures import TestAgentAPI
from .conftest import APMLibrary
@rfc("https://docs.google.com/document/d/1cVod_VI7Yruq8U9dfMRFJd7npDu-uBpste2IB04GyaQ")
@scenarios.parametric
@features.span_events
class Test_Span_Events:
def _test_span_with_native_event(
self, _library_env: dict[str, str], test_agent: TestAgentAPI, test_library: APMLibrary
):
"""Test adding a span event, with attributes, to an active span.
Assumes native format where all values are serialized according to their original type.
"""
time0 = 123450 # microseconds
name0 = "event_name"
attributes0 = {
"string": "bar",
"bool": True,
"int": 1,
"double": 2.3,
"str_arr": ["a", "b", "c"],
"bool_arr": [True, False],
"int_arr": [5, 6],
"double_arr": [1.1, 2.2],
}
time1 = 123451 # microseconds
name1 = "other_event"
attributes1 = {"bool": False, "int": 0, "double": 0.0}
with test_library, test_library.otel_start_span("test") as s:
s.add_event(name0, timestamp=time0, attributes=attributes0)
s.add_event(name1, timestamp=time1, attributes=attributes1)
traces = test_agent.wait_for_num_traces(1)
trace = find_trace(traces, s.trace_id)
assert len(trace) == 1
span = find_span(trace, s.span_id)
span_events = span["span_events"]
assert len(span_events) == 2
event = span_events[0]
assert event["time_unix_nano"] == time0 * 1000 # microseconds to nanoseconds
assert event["name"] == name0
assert event["attributes"].get("string") == {"type": 0, "string_value": "bar"}
assert event["attributes"].get("bool") == {"type": 1, "bool_value": True}
assert event["attributes"].get("int") == {"type": 2, "int_value": 1}
assert event["attributes"].get("double") == {"type": 3, "double_value": 2.3}
assert event["attributes"].get("str_arr") == {
"type": 4,
"array_value": {
"values": [
{"type": 0, "string_value": "a"},
{"type": 0, "string_value": "b"},
{"type": 0, "string_value": "c"},
]
},
}
assert event["attributes"].get("bool_arr") == {
"type": 4,
"array_value": {"values": [{"type": 1, "bool_value": True}, {"type": 1, "bool_value": False}]},
}
assert event["attributes"].get("int_arr") == {
"type": 4,
"array_value": {"values": [{"type": 2, "int_value": 5}, {"type": 2, "int_value": 6}]},
}
assert event["attributes"].get("double_arr") == {
"type": 4,
"array_value": {"values": [{"type": 3, "double_value": 1.1}, {"type": 3, "double_value": 2.2}]},
}
event = span_events[1]
assert event["time_unix_nano"] == time1 * 1000
assert event["name"] == name1
assert event["attributes"].get("bool") == {"type": 1, "bool_value": False}
assert event["attributes"].get("int") == {"type": 2, "int_value": 0}
assert isinstance(event["attributes"].get("int").get("int_value"), int)
assert event["attributes"].get("double") == {"type": 3, "double_value": 0.0}
assert isinstance(event["attributes"].get("double").get("double_value"), float)
def _test_span_with_meta_event(
self, _library_env: dict[str, str], test_agent: TestAgentAPI, test_library: APMLibrary
):
"""Test adding a span event, with attributes, to an active span.
Assumes meta format where all values are strings.
"""
time0 = 123450 # microseconds
name0 = "event_name"
attributes0 = {
"string": "bar",
"bool": "true",
"int": "1",
"double": "2.3",
"str_arr": ["a", "b", "c"],
"bool_arr": ["true", "false"],
"int_arr": ["5", "6"],
"double_arr": ["1.1", "2.2"],
}
time1 = 123451 # microseconds
name1 = "other_event"
attributes1 = {"bool": "false", "int": "0", "double": "0.0"}
with test_library, test_library.otel_start_span("test") as s:
s.add_event(name0, timestamp=time0, attributes=attributes0)
s.add_event(name1, timestamp=time1, attributes=attributes1)
traces = test_agent.wait_for_num_traces(1)
trace = find_trace(traces, s.trace_id)
assert len(trace) == 1
span = find_span(trace, s.span_id)
span_events = json.loads(span.get("meta", {}).get("events"))
assert len(span_events) == 2
event = span_events[0]
assert event["time_unix_nano"] == time0 * 1000 # microseconds to nanoseconds
assert event["name"] == name0
assert event["attributes"].get("string") == "bar"
assert event["attributes"].get("bool") == "true"
assert event["attributes"].get("int") == "1"
assert event["attributes"].get("double") == "2.3"
assert event["attributes"].get("str_arr") == ["a", "b", "c"]
assert event["attributes"].get("bool_arr") == ["true", "false"]
assert event["attributes"].get("int_arr") == ["5", "6"]
assert event["attributes"].get("double_arr") == ["1.1", "2.2"]
event = span_events[1]
assert event["time_unix_nano"] == time1 * 1000 # microseconds to nanoseconds
assert event["name"] == name1
assert event["attributes"].get("bool") == "false"
assert event["attributes"].get("int") == "0"
assert isinstance(event["attributes"].get("int"), str)
assert event["attributes"].get("double") == "0.0"
assert isinstance(event["attributes"].get("double"), str)
@pytest.mark.parametrize("library_env", [{"DD_TRACE_API_VERSION": "v0.7", "DD_TRACE_NATIVE_SPAN_EVENTS": "1"}])
def test_span_with_event_v07(self, library_env: dict[str, str], test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test adding a span event in the v0.7 format, which support the native attribute representation."""
self._test_span_with_native_event(library_env, test_agent, test_library)
@pytest.mark.parametrize("library_env", [{"DD_TRACE_API_VERSION": "v0.4", "DD_TRACE_NATIVE_SPAN_EVENTS": "1"}])
def test_span_with_event_v04(self, library_env: dict[str, str], test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test adding a span event in the v0.4 format, which support the native attribute representation."""
self._test_span_with_native_event(library_env, test_agent, test_library)
@pytest.mark.parametrize("library_env", [{"DD_TRACE_API_VERSION": "v0.5", "DD_TRACE_NATIVE_SPAN_EVENTS": "1"}])
def test_span_with_event_v05(self, library_env: dict[str, str], test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test adding a span event in the v0.5 format, which does not support the native attribute representation.
Thus span events are serialized as span tags, and attribute values all strings.
"""
self._test_span_with_meta_event(library_env, test_agent, test_library)
@pytest.mark.parametrize("library_env", [{"DD_TRACE_API_VERSION": "v0.4", "DD_TRACE_NATIVE_SPAN_EVENTS": "1"}])
def test_span_with_invalid_event_attributes(self, test_agent: TestAgentAPI, test_library: APMLibrary):
"""Test adding a span event, with invalid attributes, to an active span.
Span events with invalid attributes should be discarded.
Valid attributes should be kept.
"""
with test_library, test_library.otel_start_span("test") as s:
s.add_event(
"name",
timestamp=123450,
attributes={
"int": 1,
"invalid_arr1": [1, "a"],
"invalid_arr2": [[1]],
"invalid_int1": 2 << 65,
"invalid_int2": -2 << 65,
"string": "bar",
},
)
traces = test_agent.wait_for_num_traces(1)
trace = find_trace(traces, s.trace_id)
assert len(trace) == 1
span = find_span(trace, s.span_id)
span_events = span["span_events"]
assert len(span_events) == 1
event = span_events[0]
assert event["name"] == "name"
assert len(event["attributes"]) == 2
assert event["attributes"].get("int").get("int_value") == 1
assert event["attributes"].get("string").get("string_value") == "bar"