Skip to content

Commit

Permalink
fix failing tracer, profiling, stats, and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Dec 24, 2024
1 parent 2325f7a commit e299ca5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
31 changes: 23 additions & 8 deletions tests/integration/test_debug.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions tests/integration/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
16 changes: 9 additions & 7 deletions tests/integration/test_trace_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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"})
Expand Down
38 changes: 31 additions & 7 deletions tests/profiling/test_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,36 +232,61 @@ 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

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

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

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

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

prof = profiler.Profiler(tracer=t)
t.configure(hostname="foobar")
_check_url(prof, "http://foobar:8126", os.environ.get("DD_API_KEY"))
Expand All @@ -276,11 +301,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"))

Expand Down
5 changes: 4 additions & 1 deletion tests/tracer/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e299ca5

Please sign in to comment.