diff --git a/tests/integration/test_debug.py b/tests/integration/test_debug.py index e699db36d6b..c27481c23ab 100644 --- a/tests/integration/test_debug.py +++ b/tests/integration/test_debug.py @@ -1,10 +1,8 @@ -from datetime import datetime import json import logging import os import re import subprocess -import sys from typing import List from typing import Optional @@ -36,7 +34,14 @@ def __eq__(self, other): return Match() +@pytest.mark.subprocess() def test_standard_tags(): + from datetime import datetime + import sys + + import ddtrace + from ddtrace.internal import debug + f = debug.collect(ddtrace.tracer) date = f.get("date") @@ -94,7 +99,7 @@ def test_standard_tags(): assert f.get("tracer_enabled") is True assert f.get("sampler_type") == "DatadogSampler" assert f.get("priority_sampler_type") == "N/A" - assert f.get("service") == "tests.integration" + assert f.get("service") == "ddtrace_subprocess_dir" assert f.get("dd_version") == "" assert f.get("debug") is False assert f.get("enabled_cli") is False @@ -110,8 +115,13 @@ def test_standard_tags(): assert icfg["flask"] == "N/A" +@pytest.mark.subprocess() def test_debug_post_configure(): - tracer = ddtrace.Tracer() + import re + + from ddtrace import tracer + from ddtrace.internal import debug + tracer.configure( hostname="0.0.0.0", port=1234, @@ -122,16 +132,21 @@ def test_debug_post_configure(): 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("is_global_tracer") is True 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() + +@pytest.mark.subprocess() +def test_debug_post_configure_uds(): + import re + + from ddtrace import tracer + from ddtrace.internal import debug + tracer.configure(uds_path="/file.sock") f = debug.collect(tracer) diff --git a/tests/integration/test_sampling.py b/tests/integration/test_sampling.py index 902b430bbc8..707999b74b1 100644 --- a/tests/integration/test_sampling.py +++ b/tests/integration/test_sampling.py @@ -19,6 +19,9 @@ def snapshot_parametrized_with_writers(f): def _patch(writer, tracer): + old_sampler = tracer._sampler + old_writer = tracer._writer + old_tags = tracer._tags if writer == "sync": writer = AgentWriter( tracer.agent_trace_url, @@ -29,11 +32,13 @@ def _patch(writer, tracer): writer._headers = tracer._writer._headers else: writer = tracer._writer - tracer.configure(writer=writer) try: return f(writer, tracer) finally: - tracer.shutdown() + tracer.flush() + # Reset tracer configurations to avoid leaking state between tests + tracer.configure(sampler=old_sampler, writer=old_writer) + tracer._tags = old_tags wrapped = snapshot(include_tracer=True, token_override=f.__name__)(_patch) return pytest.mark.parametrize( diff --git a/tests/integration/test_trace_stats.py b/tests/integration/test_trace_stats.py index 0fd7695fc23..e4ffde3d81b 100644 --- a/tests/integration/test_trace_stats.py +++ b/tests/integration/test_trace_stats.py @@ -5,12 +5,12 @@ import mock import pytest -from ddtrace import Tracer from ddtrace.constants import SPAN_MEASURED_KEY from ddtrace.ext import http from ddtrace.internal.processor.stats import SpanStatsProcessorV06 from ddtrace.sampler import DatadogSampler from ddtrace.sampler import SamplingRule +from tests.utils import DummyTracer from tests.utils import override_global_config from .test_integration import AGENT_VERSION @@ -21,9 +21,8 @@ @pytest.fixture def stats_tracer(): - # type: (float) -> Generator[Tracer, None, None] with override_global_config(dict(_trace_compute_stats=True)): - tracer = Tracer() + tracer = DummyTracer() yield tracer tracer.shutdown() @@ -70,7 +69,7 @@ def test_compute_stats_default_and_configure(run_python_code_in_subprocess, envv """Ensure stats computation can be enabled.""" # Test enabling via `configure` - t = Tracer() + t = DummyTracer() assert not t._compute_stats assert not any(isinstance(p, SpanStatsProcessorV06) for p in t._span_processors) t.configure(compute_stats_enabled=True) @@ -100,14 +99,16 @@ def test_compute_stats_default_and_configure(run_python_code_in_subprocess, envv assert status == 0, out + err -def test_apm_opt_out_compute_stats_and_configure(run_python_code_in_subprocess): +@pytest.mark.subprocess(err=b"WARNING:root:IAST not enabled but native module is being loaded\n") +def test_apm_opt_out_compute_stats_and_configure(): """ Ensure stats computation is disabled, but reported as enabled, if APM is opt-out. """ + from ddtrace import tracer as t + from ddtrace.internal.processor.stats import SpanStatsProcessorV06 # Test via `configure` - t = Tracer() assert not t._compute_stats assert not any(isinstance(p, SpanStatsProcessorV06) for p in t._span_processors) t.configure(appsec_enabled=True, appsec_standalone_enabled=True) @@ -116,8 +117,9 @@ def test_apm_opt_out_compute_stats_and_configure(run_python_code_in_subprocess): assert not t._compute_stats # but it's reported as enabled assert t._writer._headers.get("Datadog-Client-Computed-Stats") == "yes" - t.configure(appsec_enabled=False, appsec_standalone_enabled=False) + +def test_apm_opt_out_compute_stats_and_configure_env(run_python_code_in_subprocess): # Test via environment variable env = os.environ.copy() env.update({"DD_EXPERIMENTAL_APPSEC_STANDALONE_ENABLED": "true", "DD_APPSEC_ENABLED": "true"}) diff --git a/tests/profiling/test_profiler.py b/tests/profiling/test_profiler.py index 7f98bbf6aa8..879a50afd54 100644 --- a/tests/profiling/test_profiler.py +++ b/tests/profiling/test_profiler.py @@ -232,36 +232,66 @@ def _check_url(prof, url, api_key, endpoint_path="profiling/v1/input"): pytest.fail("Unable to find HTTP exporter") +@pytest.mark.subprocess() def test_tracer_url(): - t = ddtrace.Tracer() + import os + + from ddtrace import tracer as t + from ddtrace.profiling import profiler + from tests.profiling.test_profiler import _check_url + t.configure(hostname="foobar") prof = profiler.Profiler(tracer=t) _check_url(prof, "http://foobar:8126", os.environ.get("DD_API_KEY")) +@pytest.mark.subprocess() def test_tracer_url_https(): - t = ddtrace.Tracer() + import os + + from ddtrace import tracer as t + from ddtrace.profiling import profiler + from tests.profiling.test_profiler import _check_url + t.configure(hostname="foobar", https=True) prof = profiler.Profiler(tracer=t) _check_url(prof, "https://foobar:8126", os.environ.get("DD_API_KEY")) +@pytest.mark.subprocess() def test_tracer_url_uds_hostname(): - t = ddtrace.Tracer() + import os + + from ddtrace import tracer as t + from ddtrace.profiling import profiler + from tests.profiling.test_profiler import _check_url + t.configure(hostname="foobar", uds_path="/foobar") prof = profiler.Profiler(tracer=t) _check_url(prof, "unix://foobar/foobar", os.environ.get("DD_API_KEY")) +@pytest.mark.subprocess() def test_tracer_url_uds(): - t = ddtrace.Tracer() + import os + + from ddtrace import tracer as t + from ddtrace.profiling import profiler + from tests.profiling.test_profiler import _check_url + t.configure(uds_path="/foobar") prof = profiler.Profiler(tracer=t) _check_url(prof, "unix:///foobar", os.environ.get("DD_API_KEY")) +@pytest.mark.subprocess() def test_tracer_url_configure_after(): - t = ddtrace.Tracer() + import os + + from ddtrace import tracer as t + from ddtrace.profiling import profiler + from tests.profiling.test_profiler import _check_url + prof = profiler.Profiler(tracer=t) t.configure(hostname="foobar") _check_url(prof, "http://foobar:8126", os.environ.get("DD_API_KEY")) @@ -276,11 +306,10 @@ def test_env_no_api_key(): def test_env_endpoint_url(): import os - import ddtrace + from ddtrace import tracer as t from ddtrace.profiling import profiler from tests.profiling.test_profiler import _check_url - t = ddtrace.Tracer() prof = profiler.Profiler(tracer=t) _check_url(prof, "http://foobar:123", os.environ.get("DD_API_KEY")) diff --git a/tests/tracer/test_tracer.py b/tests/tracer/test_tracer.py index 0978e3ae92b..21972d44492 100644 --- a/tests/tracer/test_tracer.py +++ b/tests/tracer/test_tracer.py @@ -2106,8 +2106,11 @@ def test_multiple_tracer_instances(): import ddtrace assert ddtrace.tracer is not None - assert len(warns) == 0 + for w in warns: + # Ensure the warning is not about multiple tracer instances is not logged when importing ddtrace + assert "Support for multiple Tracer instances is deprecated" not in str(w.message) + warns.clear() t = ddtrace.Tracer() assert t is ddtrace.tracer assert len(warns) == 1