forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proto_span_exporter.py
More file actions
179 lines (156 loc) · 6.14 KB
/
test_proto_span_exporter.py
File metadata and controls
179 lines (156 loc) · 6.14 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
# 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 unittest
from unittest.mock import patch
from opentelemetry.exporter.otlp.proto.http import Compression
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
DEFAULT_COMPRESSION,
DEFAULT_ENDPOINT,
DEFAULT_TIMEOUT,
DEFAULT_TRACES_EXPORT_PATH,
OTLPSpanExporter,
)
from opentelemetry.sdk.environment_variables import (
OTEL_EXPORTER_OTLP_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION,
OTEL_EXPORTER_OTLP_ENDPOINT,
OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT,
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,
)
OS_ENV_ENDPOINT = "os.env.base"
OS_ENV_CERTIFICATE = "os/env/base.crt"
OS_ENV_HEADERS = "envHeader1=val1,envHeader2=val2"
OS_ENV_TIMEOUT = "30"
# pylint: disable=protected-access
class TestOTLPSpanExporter(unittest.TestCase):
def test_constructor_default(self):
exporter = OTLPSpanExporter()
self.assertEqual(
exporter._endpoint, DEFAULT_ENDPOINT + DEFAULT_TRACES_EXPORT_PATH
)
self.assertEqual(exporter._certificate_file, True)
self.assertEqual(exporter._timeout, DEFAULT_TIMEOUT)
self.assertIs(exporter._compression, DEFAULT_COMPRESSION)
self.assertEqual(exporter._headers, {})
@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE: "traces/certificate.env",
OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: Compression.Deflate.value,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.endpoint.env",
OTEL_EXPORTER_OTLP_TRACES_HEADERS: "tracesEnv1=val1,tracesEnv2=val2,traceEnv3===val3==",
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: "40",
},
)
def test_exporter_traces_env_take_priority(self):
exporter = OTLPSpanExporter()
self.assertEqual(exporter._endpoint, "https://traces.endpoint.env")
self.assertEqual(exporter._certificate_file, "traces/certificate.env")
self.assertEqual(exporter._timeout, 40)
self.assertIs(exporter._compression, Compression.Deflate)
self.assertEqual(
exporter._headers,
{
"tracesenv1": "val1",
"tracesenv2": "val2",
"traceenv3": "==val3==",
},
)
@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.endpoint.env",
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
},
)
def test_exporter_constructor_take_priority(self):
exporter = OTLPSpanExporter(
endpoint="example.com/1234",
certificate_file="path/to/service.crt",
headers={"testHeader1": "value1", "testHeader2": "value2"},
timeout=20,
compression=Compression.NoCompression,
)
self.assertEqual(exporter._endpoint, "example.com/1234")
self.assertEqual(exporter._certificate_file, "path/to/service.crt")
self.assertEqual(exporter._timeout, 20)
self.assertIs(exporter._compression, Compression.NoCompression)
self.assertEqual(
exporter._headers,
{"testHeader1": "value1", "testHeader2": "value2"},
)
@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_CERTIFICATE: OS_ENV_CERTIFICATE,
OTEL_EXPORTER_OTLP_COMPRESSION: Compression.Gzip.value,
OTEL_EXPORTER_OTLP_HEADERS: OS_ENV_HEADERS,
OTEL_EXPORTER_OTLP_TIMEOUT: OS_ENV_TIMEOUT,
},
)
def test_exporter_env(self):
exporter = OTLPSpanExporter()
self.assertEqual(exporter._certificate_file, OS_ENV_CERTIFICATE)
self.assertEqual(exporter._timeout, int(OS_ENV_TIMEOUT))
self.assertIs(exporter._compression, Compression.Gzip)
self.assertEqual(
exporter._headers, {"envheader1": "val1", "envheader2": "val2"}
)
@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT},
)
def test_exporter_env_endpoint_without_slash(self):
exporter = OTLPSpanExporter()
self.assertEqual(
exporter._endpoint,
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
)
@patch.dict(
"os.environ",
{OTEL_EXPORTER_OTLP_ENDPOINT: OS_ENV_ENDPOINT + "/"},
)
def test_exporter_env_endpoint_with_slash(self):
exporter = OTLPSpanExporter()
self.assertEqual(
exporter._endpoint,
OS_ENV_ENDPOINT + f"/{DEFAULT_TRACES_EXPORT_PATH}",
)
@patch.dict(
"os.environ",
{
OTEL_EXPORTER_OTLP_HEADERS: "envHeader1=val1,envHeader2=val2,missingValue"
},
)
def test_headers_parse_from_env(self):
with self.assertLogs(level="WARNING") as cm:
_ = OTLPSpanExporter()
self.assertEqual(
cm.records[0].message,
"Header doesn't match the format: missingValue.",
)