async def test_override_service_name(redis_client): with override_config("aioredis", dict(service_name="myaioredis")): val = await redis_client.get("cheese") assert val is None await redis_client.set("cheese", "my-cheese") val = await redis_client.get("cheese") if isinstance(val, bytes): val = val.decode() assert val == "my-cheese"
def test_analytics_with_rate_snapshot(tracer): with override_config( "mariadb", dict(analytics_enabled=True, analytics_sample_rate=0.5)): with get_connection(tracer) as connection: cursor = connection.cursor() cursor.execute("SELECT 1") rows = cursor.fetchall() assert len(rows) == 1
def test_snowflake_analytics_without_rate(client): add_snowflake_query_response( rowtype=["TEXT"], rows=[("4.30.2", )], ) with override_config("snowflake", dict(analytics_enabled=True)): with client.cursor() as cur: res = cur.execute("select current_version();") assert res == cur assert cur.fetchone() == ("4.30.2", )
def test_snowflake_settings_override(client): add_snowflake_query_response( rowtype=["TEXT"], rows=[("4.30.2", )], ) with override_config("snowflake", dict(service="my-snowflake-svc")): with client.cursor() as cur: res = cur.execute("select current_version();") assert res == cur assert cur.fetchone() == ("4.30.2", )
def test_service_name_can_be_overriden(tracer, test_spans): 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 = 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_spans): """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 = test_spans.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
async def test_traced_client_analytics(tracer): with override_config( "aiobotocore", dict(analytics_enabled=True, analytics_sample_rate=0.5)): with aiobotocore_client("ec2", tracer) as ec2: await ec2.describe_instances() traces = tracer.pop_traces() assert traces span = traces[0][0] assert span.get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 0.5
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_split_by_domain(snapshot_context): """ When split_by_domain is configure We set the service name to the <host>:<port> """ url = get_url("/status/200") with override_config("httpx", {"split_by_domain": True}): with snapshot_context(): resp = httpx.get(url) assert resp.status_code == 200 with snapshot_context(): async with httpx.AsyncClient() as client: resp = await client.get(url) assert resp.status_code == 200
async def test_configure_service_name(snapshot_context): """ When setting ddtrace.config.httpx.service_name directly We use the value from ddtrace.config.httpx.service_name """ url = get_url("/status/200") with override_config("httpx", {"service_name": "test-httpx-service-name"}): with snapshot_context(): resp = httpx.get(url) assert resp.status_code == 200 with snapshot_context(): async with httpx.AsyncClient() as client: resp = await client.get(url) assert resp.status_code == 200
def test_httpx_service_name(tracer, test_spans): """ When using split_by_domain We set the span service name as a text type and not binary """ client = httpx.Client() Pin.override(client, tracer=tracer) with override_config("httpx", {"split_by_domain": True}): resp = client.get(get_url("/status/200")) assert resp.status_code == 200 traces = test_spans.pop_traces() assert len(traces) == 1 spans = traces[0] assert len(spans) == 1 assert isinstance(spans[0].service, six.text_type)
async def test_distributed_tracing_disabled(): """ When distributed_tracing is disabled We do not add distributed tracing headers to outbound requests """ url = get_url("/headers") 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"] with override_config("httpx", {"distributed_tracing": False}): resp = httpx.get(url) assert_request_headers(resp) async with httpx.AsyncClient() as client: resp = await client.get(url) assert_request_headers(resp)
def test_configure_from_settings(): pin = Pin(tracer=Tracer()) 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.agent_url == "http://host-test:1234"
def test_query_many_fetchall_snapshot(tracer): with override_config("mariadb", dict(trace_fetch_methods=True)): with get_connection(tracer) as connection: # tests that the executemany method is correctly wrapped. tracer.enabled = False cursor = connection.cursor() cursor.execute(""" create table if not exists dummy ( dummy_key VARCHAR(32) PRIMARY KEY, dummy_value TEXT NOT NULL)""") tracer.enabled = True stmt = "INSERT INTO dummy (dummy_key, dummy_value) VALUES (%s, %s)" data = [ ("foo", "this is foo"), ("bar", "this is bar"), ] cursor.executemany(stmt, data) query = "SELECT dummy_key, dummy_value FROM dummy ORDER BY dummy_key" cursor.execute(query) rows = cursor.fetchall() assert len(rows) == 2
def test_distributed_tracing(tracer, test_spans): 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 = test_spans.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 = test_spans.pop() assert len(spans) == 4 root = spans[0] assert root.name == "wsgi.request" assert root.trace_id != 4321 assert root.parent_id != 1234
def test_sync_worker_config_service(queue): job = queue.enqueue(job_add1, 10) with override_config("rq_worker", dict(service="my-worker-svc")): worker = rq.SimpleWorker([queue], connection=queue.connection) worker.work(burst=True) assert job.result == 11
async def test_analytics_with_rate(snapshot_context, traced_yaaredis): with override_config( "yaaredis", dict(analytics_enabled=True, analytics_sample_rate=0.5)): with snapshot_context(): await traced_yaaredis.get("cheese")
async def test_analytics_with_rate(snapshot_context): with override_config("aredis", dict(analytics_enabled=True, analytics_sample_rate=0.5)): with snapshot_context(): r = aredis.StrictRedis(port=REDIS_CONFIG["port"]) await r.get("cheese")