forked from DataDog/dd-trace-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_debug.py
479 lines (386 loc) · 14.5 KB
/
test_debug.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
from datetime import datetime
import json
import logging
import os
import re
import subprocess
import sys
from typing import List
from typing import Optional
import mock
import pytest
import ddtrace
from ddtrace import Span
from ddtrace.internal import debug
from ddtrace.internal.compat import PY2
from ddtrace.internal.compat import PY3
from ddtrace.internal.writer import TraceWriter
import ddtrace.sampler
from tests.subprocesstest import SubprocessTestCase
from tests.subprocesstest import run_in_subprocess
from .test_integration import AGENT_VERSION
pytestmark = pytest.mark.skipif(AGENT_VERSION == "testagent", reason="The test agent doesn't support startup logs.")
def re_matcher(pattern):
pattern = re.compile(pattern)
class Match:
def __eq__(self, other):
return pattern.match(other)
return Match()
def test_standard_tags():
f = debug.collect(ddtrace.tracer)
date = f.get("date")
assert isinstance(date, str)
if sys.version_info >= (3, 7, 0):
# Try to parse the date-time, only built-in way to parse
# available in Python 3.7+
date = datetime.fromisoformat(date)
os_name = f.get("os_name")
assert isinstance(os_name, str)
os_version = f.get("os_version")
assert isinstance(os_version, str)
is_64_bit = f.get("is_64_bit")
assert isinstance(is_64_bit, bool)
arch = f.get("architecture")
assert isinstance(arch, str)
vm = f.get("vm")
assert isinstance(vm, str)
pip_version = f.get("pip_version")
assert isinstance(pip_version, str)
version = f.get("version")
assert isinstance(version, str)
lang = f.get("lang")
assert lang == "python"
in_venv = f.get("in_virtual_env")
assert in_venv is True
lang_version = f.get("lang_version")
if sys.version_info == (3, 7, 0):
assert "3.7" in lang_version
elif sys.version_info == (3, 6, 0):
assert "3.6" in lang_version
elif sys.version_info == (2, 7, 0):
assert "2.7" in lang_version
agent_url = f.get("agent_url")
assert agent_url == "http://localhost:8126"
assert "agent_error" in f
agent_error = f.get("agent_error")
assert agent_error is None
assert f.get("env") == ""
assert f.get("is_global_tracer") is True
assert f.get("tracer_enabled") is True
assert f.get("sampler_type") == "DatadogSampler"
assert f.get("priority_sampler_type") == "RateByServiceSampler"
assert f.get("service") == ""
assert f.get("dd_version") == ""
assert f.get("debug") is False
assert f.get("enabled_cli") is False
assert f.get("analytics_enabled") is False
assert f.get("log_injection_enabled") is False
assert f.get("health_metrics_enabled") is False
assert f.get("runtime_metrics_enabled") is False
assert f.get("priority_sampling_enabled") is True
assert f.get("sampler_rules") == []
assert f.get("global_tags") == ""
assert f.get("tracer_tags") == ""
icfg = f.get("integrations")
assert icfg["django"] == "N/A"
assert icfg["flask"] == "N/A"
def test_debug_post_configure():
tracer = ddtrace.Tracer()
tracer.configure(
hostname="0.0.0.0",
port=1234,
priority_sampling=True,
)
f = debug.collect(tracer)
agent_url = f.get("agent_url")
assert agent_url == "http://0.0.0.0:1234"
assert f.get("is_global_tracer") is False
assert f.get("tracer_enabled") is True
agent_error = f.get("agent_error")
# Error code can differ between Python version
assert re.match("^Agent not reachable.*Connection refused", agent_error)
# Tracer doesn't support re-configure()-ing with a UDS after an initial
# configure with normal http settings. So we need a new tracer instance.
tracer = ddtrace.Tracer()
tracer.configure(uds_path="/file.sock")
f = debug.collect(tracer)
agent_url = f.get("agent_url")
assert agent_url == "unix:///file.sock"
agent_error = f.get("agent_error")
assert re.match("^Agent not reachable.*No such file or directory", agent_error)
class TestGlobalConfig(SubprocessTestCase):
@run_in_subprocess(
env_overrides=dict(
DD_AGENT_HOST="0.0.0.0",
DD_TRACE_AGENT_PORT="4321",
DD_TRACE_ANALYTICS_ENABLED="true",
DD_TRACE_HEALTH_METRICS_ENABLED="true",
DD_LOGS_INJECTION="true",
DD_ENV="prod",
DD_VERSION="123456",
DD_SERVICE="service",
DD_TAGS="k1:v1,k2:v2",
)
)
def test_env_config(self):
f = debug.collect(ddtrace.tracer)
assert f.get("agent_url") == "http://0.0.0.0:4321"
assert f.get("analytics_enabled") is True
assert f.get("health_metrics_enabled") is True
assert f.get("log_injection_enabled") is True
assert f.get("priority_sampling_enabled") is True
assert f.get("env") == "prod"
assert f.get("dd_version") == "123456"
assert f.get("service") == "service"
assert f.get("global_tags") == "k1:v1,k2:v2"
assert f.get("tracer_tags") in ["k1:v1,k2:v2", "k2:v2,k1:v1"]
assert f.get("tracer_enabled") is True
icfg = f.get("integrations")
assert icfg["django"] == "N/A"
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:1234",
)
)
def test_trace_agent_url(self):
f = debug.collect(ddtrace.tracer)
assert f.get("agent_url") == "http://0.0.0.0:1234"
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://localhost:8126",
DD_TRACE_STARTUP_LOGS="1",
)
)
def test_tracer_loglevel_info_connection(self):
tracer = ddtrace.Tracer()
logging.basicConfig(level=logging.INFO)
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock.call(logging.INFO, re_matcher("- DATADOG TRACER CONFIGURATION - ")) in mock_logger.mock_calls
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:1234",
DD_TRACE_STARTUP_LOGS="1",
)
)
def test_tracer_loglevel_info_no_connection(self):
tracer = ddtrace.Tracer()
logging.basicConfig(level=logging.INFO)
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
# Python 2 logs will go to stderr directly since there's no log handler
if PY3:
assert mock.call(logging.INFO, re_matcher("- DATADOG TRACER CONFIGURATION - ")) in mock_logger.mock_calls
assert mock.call(logging.WARNING, re_matcher("- DATADOG TRACER DIAGNOSTIC - ")) in mock_logger.mock_calls
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:1234",
)
)
def test_tracer_loglevel_info_no_connection_py2_handler(self):
tracer = ddtrace.Tracer()
logging.basicConfig()
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
if PY2:
assert mock_logger.mock_calls == []
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:1234",
DD_TRACE_STARTUP_LOGS="0",
)
)
def test_tracer_log_disabled_error(self):
tracer = ddtrace.Tracer()
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:8126",
DD_TRACE_STARTUP_LOGS="0",
)
)
def test_tracer_log_disabled(self):
tracer = ddtrace.Tracer()
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
@run_in_subprocess(
env_overrides=dict(
DD_TRACE_AGENT_URL="http://0.0.0.0:8126",
)
)
def test_tracer_info_level_log(self):
logging.basicConfig(level=logging.INFO)
tracer = ddtrace.Tracer()
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
def test_runtime_metrics_enabled_via_manual_start(ddtrace_run_python_code_in_subprocess):
_, _, status, _ = ddtrace_run_python_code_in_subprocess(
"""
import ddtrace
from ddtrace.internal import debug
from ddtrace.runtime import RuntimeMetrics
f = debug.collect(ddtrace.tracer)
assert f.get("runtime_metrics_enabled") is False
RuntimeMetrics.enable()
f = debug.collect(ddtrace.tracer)
assert f.get("runtime_metrics_enabled") is True
RuntimeMetrics.disable()
f = debug.collect(ddtrace.tracer)
assert f.get("runtime_metrics_enabled") is False
""",
)
assert status == 0
def test_runtime_metrics_enabled_via_env_var_start(monkeypatch, ddtrace_run_python_code_in_subprocess):
# default, no env variable set
_, _, status, _ = ddtrace_run_python_code_in_subprocess(
"""
import ddtrace
from ddtrace.internal import debug
f = debug.collect(ddtrace.tracer)
assert f.get("runtime_metrics_enabled") is False
""",
)
assert status == 0
# Explicitly set env variable
monkeypatch.setenv("DD_RUNTIME_METRICS_ENABLED", "true")
_, _, status, _ = ddtrace_run_python_code_in_subprocess(
"""
import ddtrace
from ddtrace.internal import debug
f = debug.collect(ddtrace.tracer)
assert f.get("runtime_metrics_enabled") is True
""",
)
assert status == 0
def test_to_json():
info = debug.collect(ddtrace.tracer)
json.dumps(info)
def test_agentless(monkeypatch):
monkeypatch.setenv("AWS_LAMBDA_FUNCTION_NAME", "something")
tracer = ddtrace.Tracer()
info = debug.collect(tracer)
assert info.get("agent_url") == "AGENTLESS"
def test_custom_writer():
tracer = ddtrace.Tracer()
class CustomWriter(TraceWriter):
def recreate(self):
# type: () -> TraceWriter
return self
def stop(self, timeout=None):
# type: (Optional[float]) -> None
pass
def write(self, spans=None):
# type: (Optional[List[Span]]) -> None
pass
def flush_queue(self):
# type: () -> None
pass
tracer._writer = CustomWriter()
info = debug.collect(tracer)
assert info.get("agent_url") == "CUSTOM"
def test_different_samplers():
tracer = ddtrace.Tracer()
tracer.configure(sampler=ddtrace.sampler.RateSampler())
info = debug.collect(tracer)
assert info.get("sampler_type") == "RateSampler"
def test_startup_logs_sampling_rules():
tracer = ddtrace.Tracer()
sampler = ddtrace.sampler.DatadogSampler(rules=[ddtrace.sampler.SamplingRule(sample_rate=1.0)])
tracer.configure(sampler=sampler)
f = debug.collect(tracer)
assert f.get("sampler_rules") == ["SamplingRule(sample_rate=1.0, service='NO_RULE', name='NO_RULE')"]
sampler = ddtrace.sampler.DatadogSampler(
rules=[ddtrace.sampler.SamplingRule(sample_rate=1.0, service="xyz", name="abc")]
)
tracer.configure(sampler=sampler)
f = debug.collect(tracer)
assert f.get("sampler_rules") == ["SamplingRule(sample_rate=1.0, service='xyz', name='abc')"]
def test_error_output_ddtracerun_debug_mode():
p = subprocess.Popen(
["ddtrace-run", "python", "tests/integration/hello.py"],
env=dict(
DD_TRACE_AGENT_URL="http://localhost:8126", DD_TRACE_DEBUG="true", DD_CALL_BASIC_CONFIG="true", **os.environ
),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
assert b"Test success" in p.stdout.read()
assert b"DATADOG TRACER CONFIGURATION" in p.stderr.read()
assert b"DATADOG TRACER DIAGNOSTIC - Agent not reachable" not in p.stderr.read()
# No connection to agent, debug mode enabled
p = subprocess.Popen(
["ddtrace-run", "python", "tests/integration/hello.py"],
env=dict(
DD_TRACE_AGENT_URL="http://localhost:4321", DD_TRACE_DEBUG="true", DD_CALL_BASIC_CONFIG="true", **os.environ
),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
assert b"Test success" in p.stdout.read()
stderr = p.stderr.read()
assert b"DATADOG TRACER CONFIGURATION" in stderr
assert b"DATADOG TRACER DIAGNOSTIC - Agent not reachable" in stderr
def test_error_output_ddtracerun():
# Connection to agent, debug mode disabled
p = subprocess.Popen(
["ddtrace-run", "python", "tests/integration/hello.py"],
env=dict(DD_TRACE_AGENT_URL="http://localhost:8126", DD_TRACE_DEBUG="false", **os.environ),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
assert b"Test success" in p.stdout.read()
stderr = p.stderr.read()
assert b"DATADOG TRACER CONFIGURATION" not in stderr
assert b"DATADOG TRACER DIAGNOSTIC - Agent not reachable" not in stderr
# No connection to agent, debug mode disabled
p = subprocess.Popen(
["ddtrace-run", "python", "tests/integration/hello.py"],
env=dict(DD_TRACE_AGENT_URL="http://localhost:4321", DD_TRACE_DEBUG="false", **os.environ),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
assert b"Test success" in p.stdout.read()
stderr = p.stderr.read()
assert b"DATADOG TRACER CONFIGURATION" not in stderr
assert b"DATADOG TRACER DIAGNOSTIC - Agent not reachable" not in stderr
def test_debug_span_log():
p = subprocess.Popen(
["python", "-c", 'import os; print(os.environ);import ddtrace; ddtrace.tracer.trace("span").finish()'],
env=dict(
DD_TRACE_AGENT_URL="http://localhost:8126", DD_TRACE_DEBUG="true", DD_CALL_BASIC_CONFIG="true", **os.environ
),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
p.wait()
stderr = p.stderr.read()
assert b"finishing span name='span'" in stderr
def test_partial_flush_log():
tracer = ddtrace.Tracer()
tracer.configure(
partial_flush_enabled=True,
partial_flush_min_spans=300,
)
f = debug.collect(tracer)
partial_flush_enabled = f.get("partial_flush_enabled")
partial_flush_min_spans = f.get("partial_flush_min_spans")
assert partial_flush_enabled is True
assert partial_flush_min_spans == 300
@pytest.mark.subprocess(
env=dict(
DD_TRACE_PARTIAL_FLUSH_ENABLED="true",
DD_TRACE_PARTIAL_FLUSH_MIN_SPANS="2",
)
)
def test_partial_flush_log_subprocess():
from ddtrace import tracer
assert tracer._partial_flush_enabled is True
assert tracer._partial_flush_min_spans == 2