forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_zipkin_exporter.py
352 lines (310 loc) · 12.2 KB
/
test_zipkin_exporter.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import unittest
from unittest.mock import MagicMock, patch
from opentelemetry import trace as trace_api
from opentelemetry.exporter.zipkin import ZipkinSpanExporter
from opentelemetry.sdk import trace
from opentelemetry.sdk.trace import Resource
from opentelemetry.sdk.trace.export import SpanExportResult
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.trace import TraceFlags
class MockResponse:
def __init__(self, status_code):
self.status_code = status_code
self.text = status_code
class TestZipkinSpanExporter(unittest.TestCase):
def setUp(self):
# create and save span to be used in tests
context = trace_api.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=0x00000000DEADBEF0,
is_remote=False,
)
self._test_span = trace.Span("test_span", context=context)
self._test_span.start()
self._test_span.end()
def tearDown(self):
if "OTEL_EXPORTER_ZIPKIN_ENDPOINT" in os.environ:
del os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"]
def test_constructor_env_var(self):
"""Test the default values assigned by constructor."""
url = "https://foo:9911/path"
os.environ["OTEL_EXPORTER_ZIPKIN_ENDPOINT"] = url
service_name = "my-service-name"
port = 9911
exporter = ZipkinSpanExporter(service_name)
ipv4 = None
ipv6 = None
self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.url, url)
self.assertEqual(exporter.port, port)
def test_constructor_default(self):
"""Test the default values assigned by constructor."""
service_name = "my-service-name"
port = 9411
exporter = ZipkinSpanExporter(service_name)
ipv4 = None
ipv6 = None
url = "http://localhost:9411/api/v2/spans"
self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.port, port)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.url, url)
def test_constructor_explicit(self):
"""Test the constructor passing all the options."""
service_name = "my-opentelemetry-zipkin"
port = 15875
ipv4 = "1.2.3.4"
ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
url = "https://opentelemetry.io:15875/myapi/traces?format=zipkin"
exporter = ZipkinSpanExporter(
service_name=service_name, url=url, ipv4=ipv4, ipv6=ipv6,
)
self.assertEqual(exporter.service_name, service_name)
self.assertEqual(exporter.port, port)
self.assertEqual(exporter.ipv4, ipv4)
self.assertEqual(exporter.ipv6, ipv6)
self.assertEqual(exporter.url, url)
# pylint: disable=too-many-locals
def test_export(self):
span_names = ("test1", "test2", "test3", "test4")
trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
span_id = 0x34BF92DEEFC58C92
parent_id = 0x1111111111111111
other_id = 0x2222222222222222
base_time = 683647322 * 10 ** 9 # in ns
start_times = (
base_time,
base_time + 150 * 10 ** 6,
base_time + 300 * 10 ** 6,
base_time + 400 * 10 ** 6,
)
durations = (50 * 10 ** 6, 100 * 10 ** 6, 200 * 10 ** 6, 300 * 10 ** 6)
end_times = (
start_times[0] + durations[0],
start_times[1] + durations[1],
start_times[2] + durations[2],
start_times[3] + durations[3],
)
span_context = trace_api.SpanContext(
trace_id,
span_id,
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
parent_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
other_context = trace_api.SpanContext(
trace_id, other_id, is_remote=False
)
event_attributes = {
"annotation_bool": True,
"annotation_string": "annotation_test",
"key_float": 0.3,
}
event_timestamp = base_time + 50 * 10 ** 6
event = trace.Event(
name="event0",
timestamp=event_timestamp,
attributes=event_attributes,
)
link_attributes = {"key_bool": True}
link = trace_api.Link(
context=other_context, attributes=link_attributes
)
otel_spans = [
trace.Span(
name=span_names[0],
context=span_context,
parent=parent_context,
events=(event,),
links=(link,),
),
trace.Span(
name=span_names[1], context=parent_context, parent=None
),
trace.Span(name=span_names[2], context=other_context, parent=None),
trace.Span(name=span_names[3], context=other_context, parent=None),
]
otel_spans[0].start(start_time=start_times[0])
otel_spans[0].resource = Resource({})
# added here to preserve order
otel_spans[0].set_attribute("key_bool", False)
otel_spans[0].set_attribute("key_string", "hello_world")
otel_spans[0].set_attribute("key_float", 111.22)
otel_spans[0].end(end_time=end_times[0])
otel_spans[1].start(start_time=start_times[1])
otel_spans[1].resource = Resource(
attributes={"key_resource": "some_resource"}
)
otel_spans[1].end(end_time=end_times[1])
otel_spans[2].start(start_time=start_times[2])
otel_spans[2].set_attribute("key_string", "hello_world")
otel_spans[2].resource = Resource(
attributes={"key_resource": "some_resource"}
)
otel_spans[2].end(end_time=end_times[2])
otel_spans[3].start(start_time=start_times[3])
otel_spans[3].resource = Resource({})
otel_spans[3].end(end_time=end_times[3])
otel_spans[3].instrumentation_info = InstrumentationInfo(
name="name", version="version"
)
service_name = "test-service"
local_endpoint = {"serviceName": service_name, "port": 9411}
exporter = ZipkinSpanExporter(service_name)
expected = [
{
"traceId": format(trace_id, "x"),
"id": format(span_id, "x"),
"name": span_names[0],
"timestamp": start_times[0] // 10 ** 3,
"duration": durations[0] // 10 ** 3,
"localEndpoint": local_endpoint,
"kind": None,
"tags": {
"key_bool": "False",
"key_string": "hello_world",
"key_float": "111.22",
},
"annotations": [
{
"timestamp": event_timestamp // 10 ** 3,
"value": "event0",
}
],
"debug": True,
"parentId": format(parent_id, "x"),
},
{
"traceId": format(trace_id, "x"),
"id": format(parent_id, "x"),
"name": span_names[1],
"timestamp": start_times[1] // 10 ** 3,
"duration": durations[1] // 10 ** 3,
"localEndpoint": local_endpoint,
"kind": None,
"tags": {"key_resource": "some_resource"},
"annotations": None,
},
{
"traceId": format(trace_id, "x"),
"id": format(other_id, "x"),
"name": span_names[2],
"timestamp": start_times[2] // 10 ** 3,
"duration": durations[2] // 10 ** 3,
"localEndpoint": local_endpoint,
"kind": None,
"tags": {
"key_string": "hello_world",
"key_resource": "some_resource",
},
"annotations": None,
},
{
"traceId": format(trace_id, "x"),
"id": format(other_id, "x"),
"name": span_names[3],
"timestamp": start_times[3] // 10 ** 3,
"duration": durations[3] // 10 ** 3,
"localEndpoint": local_endpoint,
"kind": None,
"tags": {
"otel.instrumentation_library.name": "name",
"otel.instrumentation_library.version": "version",
},
"annotations": None,
},
]
mock_post = MagicMock()
with patch("requests.post", mock_post):
mock_post.return_value = MockResponse(200)
status = exporter.export(otel_spans)
self.assertEqual(SpanExportResult.SUCCESS, status)
mock_post.assert_called_with(
url="http://localhost:9411/api/v2/spans",
data=json.dumps(expected),
headers={"Content-Type": "application/json"},
)
# pylint: disable=too-many-locals
def test_zero_padding(self):
"""test that hex ids starting with 0
are properly padded to 16 or 32 hex chars
when exported
"""
span_names = "testZeroes"
trace_id = 0x0E0C63257DE34C926F9EFCD03927272E
span_id = 0x04BF92DEEFC58C92
parent_id = 0x0AAAAAAAAAAAAAAA
start_time = 683647322 * 10 ** 9 # in ns
duration = 50 * 10 ** 6
end_time = start_time + duration
span_context = trace_api.SpanContext(
trace_id,
span_id,
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
parent_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
otel_span = trace.Span(
name=span_names[0], context=span_context, parent=parent_context,
)
otel_span.start(start_time=start_time)
otel_span.resource = Resource({})
otel_span.end(end_time=end_time)
service_name = "test-service"
local_endpoint = {"serviceName": service_name, "port": 9411}
exporter = ZipkinSpanExporter(service_name)
# Check traceId are properly lowercase 16 or 32 hex
expected = [
{
"traceId": "0e0c63257de34c926f9efcd03927272e",
"id": "04bf92deefc58c92",
"name": span_names[0],
"timestamp": start_time // 10 ** 3,
"duration": duration // 10 ** 3,
"localEndpoint": local_endpoint,
"kind": None,
"tags": {},
"annotations": None,
"debug": True,
"parentId": "0aaaaaaaaaaaaaaa",
}
]
mock_post = MagicMock()
with patch("requests.post", mock_post):
mock_post.return_value = MockResponse(200)
status = exporter.export([otel_span])
self.assertEqual(SpanExportResult.SUCCESS, status)
mock_post.assert_called_with(
url="http://localhost:9411/api/v2/spans",
data=json.dumps(expected),
headers={"Content-Type": "application/json"},
)
@patch("requests.post")
def test_invalid_response(self, mock_post):
mock_post.return_value = MockResponse(404)
spans = []
exporter = ZipkinSpanExporter("test-service")
status = exporter.export(spans)
self.assertEqual(SpanExportResult.FAILURE, status)