def test_configure_from_settings(tracer): pin = Pin.get_from(django) with override_config("django", dict()): assert "ddtrace.contrib.django" in django.conf.settings.INSTALLED_APPS assert hasattr(django.conf.settings, "DATADOG_TRACE") configure_from_settings(pin, config.django, django.conf.settings.DATADOG_TRACE) assert config.django.service_name == "django-test" assert config.django.cache_service_name == "cache-test" assert config.django.database_service_name_prefix == "db-test-" assert config.django.distributed_tracing_enabled is True assert config.django.instrument_databases is True assert config.django.instrument_caches is True assert config.django.analytics_enabled is True assert config.django.analytics_sample_rate is True # TODO: uncomment when figured out why setting this is not working # assert config.django.trace_query_string is True assert pin.tracer.enabled is True assert pin.tracer.tags["env"] == "env-test" assert pin.tracer.writer.api.hostname == "host-test" assert pin.tracer.writer.api.port == 1234
def test_service_name_can_be_overriden(tracer): with override_config("wsgi", dict(service_name="test-override-service")): app = TestApp(wsgi.DDWSGIMiddleware(application, tracer=tracer)) response = app.get("/") assert response.status_code == 200 spans = tracer.writer.pop_traces() assert len(spans) > 0 span = spans[0][0] assert span.service == "test-override-service"
def test_service_can_be_overridden(client, tracer, test_spans): with override_config("fastapi", dict(service_name="test-override-service")): response = client.get("/", headers={"sleep": "False"}) assert response.status_code == 200 spans = test_spans.pop_traces() assert len(spans) > 0 span = spans[0][0] assert span.service == "test-override-service"
async def test_basic_app(tracer, client, integration_config, integration_http_config): """Test Sanic Patching""" with override_http_config("sanic", integration_http_config): with override_config("sanic", integration_config): headers = [ (http_propagation.HTTP_HEADER_PARENT_ID, "1234"), (http_propagation.HTTP_HEADER_TRACE_ID, "5678"), ] response = await client.get("/hello", params=[("foo", "bar")], headers=headers) assert _response_status(response) == 200 assert await _response_json(response) == {"hello": "world"} spans = tracer.writer.pop_traces() assert len(spans) == 1 assert len(spans[0]) == 2 request_span = spans[0][0] assert request_span.name == "sanic.request" assert request_span.error == 0 assert request_span.get_tag("http.method") == "GET" assert re.search("/hello$", request_span.get_tag("http.url")) assert request_span.get_tag("http.status_code") == "200" assert request_span.resource == "GET /hello" sleep_span = spans[0][1] assert sleep_span.name == "tests.contrib.sanic.test_sanic.random_sleep" assert sleep_span.parent_id == request_span.span_id if integration_config.get("service"): assert request_span.service == integration_config["service"] else: assert request_span.service == "sanic" if integration_http_config.get("trace_query_string"): assert request_span.get_tag("http.query.string") == "foo=bar" else: assert request_span.get_tag("http.query.string") is None if integration_config.get("analytics_enabled"): analytics_sample_rate = integration_config.get( "analytics_sample_rate") or 1.0 assert request_span.get_metric( ANALYTICS_SAMPLE_RATE_KEY) == analytics_sample_rate else: assert request_span.get_metric(ANALYTICS_SAMPLE_RATE_KEY) is None if integration_config.get("distributed_tracing", True): assert request_span.parent_id == 1234 assert request_span.trace_id == 5678 else: assert request_span.parent_id is None assert request_span.trace_id is not None and request_span.trace_id != 5678
def test_distributed_tracing(tracer): app = TestApp(wsgi.DDWSGIMiddleware(application, tracer=tracer)) resp = app.get("/", headers={ "X-Datadog-Parent-Id": "1234", "X-Datadog-Trace-Id": "4321" }) assert config.wsgi.distributed_tracing is True assert resp.status == "200 OK" assert resp.status_int == 200 spans = tracer.writer.pop() assert len(spans) == 4 root = spans[0] assert root.name == "wsgi.request" assert root.trace_id == 4321 assert root.parent_id == 1234 with override_config("wsgi", dict(distributed_tracing=False)): app = TestApp(wsgi.DDWSGIMiddleware(application, tracer=tracer)) resp = app.get("/", headers={ "X-Datadog-Parent-Id": "1234", "X-Datadog-Trace-Id": "4321" }) assert config.wsgi.distributed_tracing is False assert resp.status == "200 OK" assert resp.status_int == 200 spans = tracer.writer.pop() assert len(spans) == 4 root = spans[0] assert root.name == "wsgi.request" assert root.trace_id != 4321 assert root.parent_id != 1234