Ejemplo n.º 1
0
def test_distributed_tracing_disabled_env():
    """
    When disabling distributed tracing via env variable
        We do not add distributed tracing headers to outbound requests
    """

    import asyncio
    import sys

    import httpx

    from ddtrace.contrib.httpx import patch
    from tests.contrib.httpx.test_httpx import get_url

    patch()
    url = get_url("/headers")

    async def test():
        def assert_request_headers(response):
            data = response.json()
            assert "X-Datadog-Trace-Id" not in data["headers"]
            assert "X-Datadog-Parent-Id" not in data["headers"]
            assert "X-Datadog-Sampling-Priority" not in data["headers"]

        resp = httpx.get(url)
        assert_request_headers(resp)

        async with httpx.AsyncClient() as client:
            resp = await client.get(url)
            assert_request_headers(resp)

    if sys.version_info >= (3, 7, 0):
        asyncio.run(test())
    else:
        asyncio.get_event_loop().run_until_complete(test())
Ejemplo n.º 2
0
def test_configure_global_service_name_env():
    """
    When only setting DD_SERVICE
        We use the value from DD_SERVICE for the service name
    """

    import asyncio
    import sys

    import httpx

    from ddtrace.contrib.httpx import patch
    from tests.contrib.httpx.test_httpx import get_url
    from tests.utils import snapshot_context

    patch()
    url = get_url("/status/200")

    async def test():
        token = "tests.contrib.httpx.test_httpx.test_configure_global_service_name_env"
        with snapshot_context(token=token):
            httpx.get(url)

        with snapshot_context(token=token):
            async with httpx.AsyncClient() as client:
                await client.get(url)

    if sys.version_info >= (3, 7, 0):
        asyncio.run(test())
    else:
        asyncio.get_event_loop().run_until_complete(test())
Ejemplo n.º 3
0
def test_configure_global_service_name_env(run_python_code_in_subprocess):
    """
    When only setting DD_SERVICE
        We use the value from DD_SERVICE for the service name
    """
    code = """
import asyncio

import httpx

from ddtrace.contrib.httpx import patch
from tests.contrib.httpx.test_httpx import get_url
from tests.utils import snapshot_context

patch()
url = get_url("/status/200")

async def test():
    token = "tests.contrib.httpx.test_httpx.test_configure_global_service_name_env"
    with snapshot_context(token=token):
        httpx.get(url)

    with snapshot_context(token=token):
        async with httpx.AsyncClient() as client:
            await client.get(url)

asyncio.get_event_loop().run_until_complete(test())
    """
    env = os.environ.copy()
    env["DD_SERVICE"] = "global-service-name"
    out, err, status, pid = run_python_code_in_subprocess(code, env=env)
    assert status == 0, err
    assert err == b""
Ejemplo n.º 4
0
def test_distributed_tracing_disabled_env(run_python_code_in_subprocess):
    """
    When disabling distributed tracing via env variable
        We do not add distributed tracing headers to outbound requests
    """
    code = """
import asyncio
import sys

import httpx

from ddtrace.contrib.httpx import patch
from tests.contrib.httpx.test_httpx import get_url, override_config

patch()
url = get_url("/headers")

async def test():
    def assert_request_headers(response):
        data = response.json()
        assert "X-Datadog-Trace-Id" not in data["headers"]
        assert "X-Datadog-Parent-Id" not in data["headers"]
        assert "X-Datadog-Sampling-Priority" not in data["headers"]

    resp = httpx.get(url)
    assert_request_headers(resp)

    async with httpx.AsyncClient() as client:
        resp = await client.get(url)
        assert_request_headers(resp)

if sys.version_info >= (3, 7, 0):
    asyncio.run(test())
else:
    asyncio.get_event_loop().run_until_complete(test())
    """
    env = os.environ.copy()
    env["DD_HTTPX_DISTRIBUTED_TRACING"] = "false"
    out, err, status, pid = run_python_code_in_subprocess(code, env=env)
    assert status == 0, err
    assert err == b""
Ejemplo n.º 5
0
def test_configure_service_name_env(run_python_code_in_subprocess):
    """
    When setting DD_HTTPX_SERVICE_NAME env variable
        When DD_SERVICE is also set
            We use the value from DD_HTTPX_SERVICE_NAME
    """
    code = """
import asyncio
import sys

import httpx

from ddtrace.contrib.httpx import patch
from tests.contrib.httpx.test_httpx import get_url
from tests.utils import snapshot_context

patch()
url = get_url("/status/200")

async def test():
    token = "tests.contrib.httpx.test_httpx.test_configure_service_name_env"
    with snapshot_context(token=token):
        httpx.get(url)

    with snapshot_context(token=token):
        async with httpx.AsyncClient() as client:
            await client.get(url)

if sys.version_info >= (3, 7, 0):
    asyncio.run(test())
else:
    asyncio.get_event_loop().run_until_complete(test())
    """
    env = os.environ.copy()
    env["DD_HTTPX_SERVICE_NAME"] = "env-overridden-service-name"
    env["DD_SERVICE"] = "global-service-name"
    out, err, status, pid = run_python_code_in_subprocess(code, env=env)
    assert status == 0, err
    assert err == b""
Ejemplo n.º 6
0
def patch_httpx():
    patch()
    try:
        yield
    finally:
        unpatch()