forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_otlp_trace_exporter.py
499 lines (446 loc) · 19 KB
/
test_otlp_trace_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# 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 os
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
from unittest import TestCase
from unittest.mock import Mock, PropertyMock, patch
from google.protobuf.duration_pb2 import Duration
from google.rpc.error_details_pb2 import RetryInfo
from grpc import ChannelCredentials, Compression, StatusCode, server
from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import (
ExportTraceServiceRequest,
ExportTraceServiceResponse,
)
from opentelemetry.proto.collector.trace.v1.trace_service_pb2_grpc import (
TraceServiceServicer,
add_TraceServiceServicer_to_server,
)
from opentelemetry.proto.common.v1.common_pb2 import (
AnyValue,
InstrumentationLibrary,
KeyValue,
)
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as OTLPResource,
)
from opentelemetry.proto.trace.v1.trace_pb2 import (
InstrumentationLibrarySpans,
ResourceSpans,
)
from opentelemetry.proto.trace.v1.trace_pb2 import Span as OTLPSpan
from opentelemetry.proto.trace.v1.trace_pb2 import Status
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE,
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_TRACES_HEADERS,
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
)
from opentelemetry.sdk.resources import Resource as SDKResource
from opentelemetry.sdk.trace import Status as SDKStatus
from opentelemetry.sdk.trace import StatusCode as SDKStatusCode
from opentelemetry.sdk.trace import TracerProvider, _Span
from opentelemetry.sdk.trace.export import (
SimpleSpanProcessor,
SpanExportResult,
)
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
THIS_DIR = os.path.dirname(__file__)
class TraceServiceServicerUNAVAILABLEDelay(TraceServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.UNAVAILABLE)
context.send_initial_metadata(
(("google.rpc.retryinfo-bin", RetryInfo().SerializeToString()),)
)
context.set_trailing_metadata(
(
(
"google.rpc.retryinfo-bin",
RetryInfo(
retry_delay=Duration(seconds=4)
).SerializeToString(),
),
)
)
return ExportTraceServiceResponse()
class TraceServiceServicerUNAVAILABLE(TraceServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.UNAVAILABLE)
return ExportTraceServiceResponse()
class TraceServiceServicerSUCCESS(TraceServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.OK)
return ExportTraceServiceResponse()
class TraceServiceServicerALREADY_EXISTS(TraceServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.ALREADY_EXISTS)
return ExportTraceServiceResponse()
class TestOTLPSpanExporter(TestCase):
def setUp(self):
tracer_provider = TracerProvider()
self.exporter = OTLPSpanExporter(insecure=True)
tracer_provider.add_span_processor(SimpleSpanProcessor(self.exporter))
self.tracer = tracer_provider.get_tracer(__name__)
self.server = server(ThreadPoolExecutor(max_workers=10))
self.server.add_insecure_port("[::]:4317")
self.server.start()
event_mock = Mock(
**{
"timestamp": 1591240820506462784,
"attributes": OrderedDict([("a", 1), ("b", False)]),
}
)
type(event_mock).name = PropertyMock(return_value="a")
self.span = _Span(
"a",
context=Mock(
**{
"trace_state": OrderedDict([("a", "b"), ("c", "d")]),
"span_id": 10217189687419569865,
"trace_id": 67545097771067222548457157018666467027,
}
),
resource=SDKResource(OrderedDict([("a", 1), ("b", False)])),
parent=Mock(**{"span_id": 12345}),
attributes=OrderedDict([("a", 1), ("b", True)]),
events=[event_mock],
links=[
Mock(
**{
"context.trace_id": 1,
"context.span_id": 2,
"attributes": OrderedDict([("a", 1), ("b", False)]),
"kind": OTLPSpan.SpanKind.SPAN_KIND_INTERNAL, # pylint: disable=no-member
}
)
],
instrumentation_info=InstrumentationInfo(
name="name", version="version"
),
)
self.span.start()
self.span.end()
def tearDown(self):
self.server.stop(None)
@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "collector:4317",
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE: THIS_DIR
+ "/fixtures/test.cert",
OTEL_EXPORTER_OTLP_TRACES_HEADERS: "key1=value1,key2=value2",
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: "10",
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: "gzip",
},
)
@patch("opentelemetry.exporter.otlp.exporter.OTLPExporterMixin.__init__")
def test_env_variables(self, mock_exporter_mixin):
OTLPSpanExporter()
self.assertTrue(len(mock_exporter_mixin.call_args_list) == 1)
_, kwargs = mock_exporter_mixin.call_args_list[0]
self.assertEqual(kwargs["endpoint"], "collector:4317")
self.assertEqual(kwargs["headers"], "key1=value1,key2=value2")
self.assertEqual(kwargs["timeout"], 10)
self.assertEqual(kwargs["compression"], Compression.Gzip)
self.assertIsNotNone(kwargs["credentials"])
self.assertIsInstance(kwargs["credentials"], ChannelCredentials)
@patch("opentelemetry.exporter.otlp.exporter.ssl_channel_credentials")
@patch("opentelemetry.exporter.otlp.exporter.secure_channel")
@patch("opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter._stub")
# pylint: disable=unused-argument
def test_no_credentials_error(
self, mock_ssl_channel, mock_secure, mock_stub
):
OTLPSpanExporter(insecure=False)
self.assertTrue(mock_ssl_channel.called)
@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_TRACES_HEADERS: "key1=value1,key2=value2"},
)
@patch("opentelemetry.exporter.otlp.exporter.ssl_channel_credentials")
@patch("opentelemetry.exporter.otlp.exporter.secure_channel")
# pylint: disable=unused-argument
def test_otlp_headers_from_env(self, mock_ssl_channel, mock_secure):
exporter = OTLPSpanExporter()
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key1", "value1"), ("key2", "value2"))
)
exporter = OTLPSpanExporter(
headers=(("key3", "value3"), ("key4", "value4"))
)
# pylint: disable=protected-access
self.assertEqual(
exporter._headers, (("key3", "value3"), ("key4", "value4"))
)
# pylint: disable=no-self-use
@patch("opentelemetry.exporter.otlp.exporter.insecure_channel")
@patch.dict("os.environ", {OTEL_EXPORTER_OTLP_COMPRESSION: "gzip"})
def test_otlp_exporter_otlp_compression_envvar(
self, mock_insecure_channel
):
"""Just OTEL_EXPORTER_OTLP_COMPRESSION should work"""
OTLPSpanExporter(insecure=True)
mock_insecure_channel.assert_called_once_with(
"localhost:4317", compression=Compression.Gzip
)
# pylint: disable=no-self-use
@patch("opentelemetry.exporter.otlp.exporter.insecure_channel")
@patch.dict("os.environ", {OTEL_EXPORTER_OTLP_COMPRESSION: "gzip"})
def test_otlp_exporter_otlp_compression_kwarg(self, mock_insecure_channel):
"""Specifying kwarg should take precedence over env"""
OTLPSpanExporter(insecure=True, compression=Compression.NoCompression)
mock_insecure_channel.assert_called_once_with(
"localhost:4317", compression=Compression.NoCompression
)
# pylint: disable=no-self-use
@patch("opentelemetry.exporter.otlp.exporter.insecure_channel")
@patch.dict("os.environ", {})
def test_otlp_exporter_otlp_compression_unspecified(
self, mock_insecure_channel
):
"""No env or kwarg should be NoCompression"""
OTLPSpanExporter(insecure=True)
mock_insecure_channel.assert_called_once_with(
"localhost:4317", compression=Compression.NoCompression
)
# pylint: disable=no-self-use
@patch("opentelemetry.exporter.otlp.exporter.insecure_channel")
@patch.dict(
"os.environ", {OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: "gzip"},
)
def test_otlp_exporter_otlp_compression_precendence(
self, mock_insecure_channel
):
"""OTEL_EXPORTER_OTLP_TRACES_COMPRESSION as higher priority than
OTEL_EXPORTER_OTLP_COMPRESSION
"""
OTLPSpanExporter(insecure=True)
mock_insecure_channel.assert_called_once_with(
"localhost:4317", compression=Compression.Gzip
)
@patch("opentelemetry.exporter.otlp.exporter.ssl_channel_credentials")
@patch("opentelemetry.exporter.otlp.exporter.secure_channel")
# pylint: disable=unused-argument
def test_otlp_headers(self, mock_ssl_channel, mock_secure):
exporter = OTLPSpanExporter()
# pylint: disable=protected-access
self.assertIsNone(exporter._headers, None)
@patch("opentelemetry.exporter.otlp.exporter.expo")
@patch("opentelemetry.exporter.otlp.exporter.sleep")
def test_unavailable(self, mock_sleep, mock_expo):
mock_expo.configure_mock(**{"return_value": [1]})
add_TraceServiceServicer_to_server(
TraceServiceServicerUNAVAILABLE(), self.server
)
self.assertEqual(
self.exporter.export([self.span]), SpanExportResult.FAILURE
)
mock_sleep.assert_called_with(1)
@patch("opentelemetry.exporter.otlp.exporter.expo")
@patch("opentelemetry.exporter.otlp.exporter.sleep")
def test_unavailable_delay(self, mock_sleep, mock_expo):
mock_expo.configure_mock(**{"return_value": [1]})
add_TraceServiceServicer_to_server(
TraceServiceServicerUNAVAILABLEDelay(), self.server
)
self.assertEqual(
self.exporter.export([self.span]), SpanExportResult.FAILURE
)
mock_sleep.assert_called_with(4)
def test_success(self):
add_TraceServiceServicer_to_server(
TraceServiceServicerSUCCESS(), self.server
)
self.assertEqual(
self.exporter.export([self.span]), SpanExportResult.SUCCESS
)
def test_failure(self):
add_TraceServiceServicer_to_server(
TraceServiceServicerALREADY_EXISTS(), self.server
)
self.assertEqual(
self.exporter.export([self.span]), SpanExportResult.FAILURE
)
def test_translate_spans(self):
expected = ExportTraceServiceRequest(
resource_spans=[
ResourceSpans(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_spans=[
InstrumentationLibrarySpans(
instrumentation_library=InstrumentationLibrary(
name="name", version="version"
),
spans=[
OTLPSpan(
# pylint: disable=no-member
name="a",
start_time_unix_nano=self.span.start_time,
end_time_unix_nano=self.span.end_time,
trace_state="a=b,c=d",
span_id=int.to_bytes(
10217189687419569865, 8, "big"
),
trace_id=int.to_bytes(
67545097771067222548457157018666467027,
16,
"big",
),
parent_span_id=(
b"\000\000\000\000\000\00009"
),
kind=(
OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
),
attributes=[
KeyValue(
key="a",
value=AnyValue(int_value=1),
),
KeyValue(
key="b",
value=AnyValue(bool_value=True),
),
],
events=[
OTLPSpan.Event(
name="a",
time_unix_nano=1591240820506462784,
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=False
),
),
],
)
],
status=Status(code=0, message=""),
links=[
OTLPSpan.Link(
trace_id=int.to_bytes(
1, 16, "big"
),
span_id=int.to_bytes(2, 8, "big"),
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=False
),
),
],
)
],
)
],
)
],
),
]
)
# pylint: disable=protected-access
self.assertEqual(expected, self.exporter._translate_data([self.span]))
def _check_translated_status(
self,
translated: ExportTraceServiceRequest,
code_expected: Status,
deprecated_code_expected: Status,
):
status = (
translated.resource_spans[0]
.instrumentation_library_spans[0]
.spans[0]
.status
)
self.assertEqual(
status.code, code_expected,
)
self.assertEqual(
status.deprecated_code, deprecated_code_expected,
)
def test_span_status_translate(self):
# pylint: disable=protected-access,no-member
unset = SDKStatus(status_code=SDKStatusCode.UNSET)
ok = SDKStatus(status_code=SDKStatusCode.OK)
error = SDKStatus(status_code=SDKStatusCode.ERROR)
unset_translated = self.exporter._translate_data(
[_create_span_with_status(unset)]
)
ok_translated = self.exporter._translate_data(
[_create_span_with_status(ok)]
)
error_translated = self.exporter._translate_data(
[_create_span_with_status(error)]
)
self._check_translated_status(
unset_translated,
Status.STATUS_CODE_UNSET,
Status.DEPRECATED_STATUS_CODE_OK,
)
self._check_translated_status(
ok_translated,
Status.STATUS_CODE_OK,
Status.DEPRECATED_STATUS_CODE_OK,
)
self._check_translated_status(
error_translated,
Status.STATUS_CODE_ERROR,
Status.DEPRECATED_STATUS_CODE_UNKNOWN_ERROR,
)
def _create_span_with_status(status: SDKStatus):
span = _Span(
"a",
context=Mock(
**{
"trace_state": OrderedDict([("a", "b"), ("c", "d")]),
"span_id": 10217189687419569865,
"trace_id": 67545097771067222548457157018666467027,
}
),
parent=Mock(**{"span_id": 12345}),
instrumentation_info=InstrumentationInfo(
name="name", version="version"
),
)
span.set_status(status)
return span