-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_otel_env_vars.py
More file actions
222 lines (197 loc) · 9.21 KB
/
test_otel_env_vars.py
File metadata and controls
222 lines (197 loc) · 9.21 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pytest
from utils import context, scenarios, features
from .conftest import APMLibrary
@scenarios.parametric
@features.open_tracing_api
class Test_Otel_Env_Vars:
@pytest.mark.parametrize(
"library_env",
[
{
"DD_SERVICE": "service",
"OTEL_SERVICE_NAME": "otel_service",
"DD_TRACE_LOG_LEVEL": "error", # Node.js uses DD_TRACE_LOG_LEVEL
"DD_LOG_LEVEL": "error",
"DD_TRACE_DEBUG": "false",
"OTEL_LOG_LEVEL": "debug",
"DD_TRACE_SAMPLE_RATE": "0.5",
"DD_TRACE_SAMPLING_RULES": '[{"sample_rate":0.5}]',
"OTEL_TRACES_SAMPLER": "traceidratio",
"OTEL_TRACES_SAMPLER_ARG": "0.1",
"DD_TRACE_ENABLED": "true",
"OTEL_TRACES_EXPORTER": "none",
"DD_RUNTIME_METRICS_ENABLED": "true",
"OTEL_METRICS_EXPORTER": "none",
"DD_TAGS": "foo:bar,baz:qux",
"OTEL_RESOURCE_ATTRIBUTES": "foo=otel_bar,baz=otel_qux",
"DD_TRACE_PROPAGATION_STYLE": "b3,tracecontext",
"OTEL_PROPAGATORS": "datadog,tracecontext",
}
],
)
def test_dd_env_var_take_precedence(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_service"] == "service"
# Some languages do not support the DD_TRACE_LOG_LEVEL env var
# Here we can test that the OTEL_LOG_LEVEL is not used
assert resp["dd_log_level"] != "debug"
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.5
assert resp["dd_trace_enabled"] == "true"
tags = resp["dd_tags"]
assert isinstance(tags, (str, list))
assert "foo:bar" in tags
assert "baz:qux" in tags
assert "foo:otel_bar" not in tags
assert "baz:otel_qux" not in tags
assert resp["dd_trace_debug"] == "false"
if context.library != "java":
assert resp["dd_trace_propagation_style"] == "b3,tracecontext"
else:
assert resp["dd_trace_propagation_style"] == "b3multi,tracecontext"
if context.library != "php":
assert resp["dd_runtime_metrics_enabled"]
@pytest.mark.parametrize(
"library_env",
[
{
"OTEL_SERVICE_NAME": "otel_service",
"OTEL_TRACES_SAMPLER": "traceidratio",
"OTEL_TRACES_SAMPLER_ARG": "0.1",
"OTEL_METRICS_EXPORTER": "none",
"OTEL_RESOURCE_ATTRIBUTES": "foo=bar1,baz=qux1",
"OTEL_PROPAGATORS": "b3,tracecontext",
"DD_TRACE_OTEL_ENABLED": "true",
}
],
)
def test_otel_env_vars_set(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_service"] == "otel_service"
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.1
assert resp["dd_trace_enabled"] == "true"
tags = resp["dd_tags"]
assert isinstance(tags, (str, list))
assert "foo:bar1" in tags
assert "baz:qux1" in tags
if context.library in ("dotnet", "php", "golang"):
assert resp["dd_trace_propagation_style"] == "b3 single header,tracecontext"
elif context.library == "java":
assert resp["dd_trace_propagation_style"] == "b3single,tracecontext"
else:
assert resp["dd_trace_propagation_style"] == "b3,tracecontext"
if context.library != "php":
assert resp["dd_runtime_metrics_enabled"] == "false"
@pytest.mark.parametrize("library_env", [{"OTEL_LOG_LEVEL": "error"}])
def test_otel_log_level_env(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_log_level"] == "error"
@pytest.mark.parametrize(
"library_env",
[
{
"OTEL_RESOURCE_ATTRIBUTES": "deployment.environment=test1,service.name=test2,service.version=5,foo=bar1,baz=qux1",
"DD_TRACE_OTEL_ENABLED": "true",
}
],
)
def test_otel_attribute_mapping(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_service"] == "test2"
assert resp["dd_env"] == "test1"
assert resp["dd_version"] == "5"
tags = resp["dd_tags"]
assert isinstance(tags, (str, list))
assert "foo:bar1" in tags
assert "baz:qux1" in tags
@pytest.mark.parametrize("library_env", [{"OTEL_TRACES_SAMPLER": "always_on", "DD_TRACE_OTEL_ENABLED": "true"}])
def test_otel_traces_always_on(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 1.0
@pytest.mark.parametrize("library_env", [{"OTEL_TRACES_SAMPLER": "always_off", "DD_TRACE_OTEL_ENABLED": "true"}])
def test_otel_traces_always_off(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.0
@pytest.mark.parametrize(
"library_env",
[{"OTEL_TRACES_SAMPLER": "traceidratio", "OTEL_TRACES_SAMPLER_ARG": "0.1", "DD_TRACE_OTEL_ENABLED": "true"}],
)
def test_otel_traces_traceidratio(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.1
@pytest.mark.parametrize(
"library_env", [{"OTEL_TRACES_SAMPLER": "parentbased_always_on", "DD_TRACE_OTEL_ENABLED": "true"}]
)
def test_otel_traces_parentbased_on(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 1.0
@pytest.mark.parametrize(
"library_env", [{"OTEL_TRACES_SAMPLER": "parentbased_always_off", "DD_TRACE_OTEL_ENABLED": "true"}]
)
def test_otel_traces_parentbased_off(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.0
@pytest.mark.parametrize(
"library_env",
[
{
"OTEL_TRACES_SAMPLER": "parentbased_traceidratio",
"OTEL_TRACES_SAMPLER_ARG": "0.1",
"DD_TRACE_OTEL_ENABLED": "true",
}
],
)
def test_otel_traces_parentbased_ratio(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert isinstance(resp["dd_trace_sample_rate"], (float, str, bool, int))
assert float(resp["dd_trace_sample_rate"]) == 0.1
@pytest.mark.parametrize("library_env", [{"OTEL_TRACES_EXPORTER": "none", "DD_TRACE_OTEL_ENABLED": "true"}])
def test_otel_traces_exporter_none(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_enabled"] == "false"
@pytest.mark.parametrize("library_env", [{"OTEL_LOG_LEVEL": "debug", "DD_TRACE_OTEL_ENABLED": "true"}])
def test_otel_log_level_to_debug_mapping(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_debug"] == "true"
# If dd_log_level is set it must be consistent with dd_trace_debug
assert (resp["dd_log_level"] == "debug") or (resp["dd_log_level"] is None)
@pytest.mark.parametrize("library_env", [{"DD_TRACE_OTEL_ENABLED": "true", "OTEL_SDK_DISABLED": "true"}])
def test_dd_trace_otel_enabled_takes_precedence(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_otel_enabled"] == "true"
@pytest.mark.parametrize("library_env", [{"OTEL_SDK_DISABLED": "true", "DD_TRACE_OTEL_ENABLED": None}])
def test_otel_sdk_disabled_set(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_otel_enabled"] == "false"
@pytest.mark.parametrize("library_env", [{"OTEL_TRACES_SAMPLER": "always_on", "DD_TRACE_OTEL_ENABLED": "true"}])
def test_dd_trace_sample_ignore_parent_true(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_sample_ignore_parent"] == "true"
@pytest.mark.parametrize(
"library_env", [{"OTEL_TRACES_SAMPLER": "parentbased_always_off", "DD_TRACE_OTEL_ENABLED": "true"}]
)
def test_dd_trace_sample_ignore_parent_false(self, test_library: APMLibrary):
with test_library as t:
resp = t.config()
assert resp["dd_trace_sample_ignore_parent"] == "false"