Skip to content

Datadog

Application Kit provides Datadog APM (Application Performance Monitoring) integration for tracing and observability. The integration automatically configures service names, environment tags, and version information based on your Bender manifest.

Configuration

The configure_datadog() function should be called during application startup. It configures:

  • Service Name: Derived from the manifest name, optionally suffixed with a worker name
  • Environment: From ENVIRONMENT or DATADOG_ENV environment variables
  • Version: From VERSION or DD_VERSION environment variables

For FastAPI applications, get_fastapi_app() calls configure_datadog() automatically. For Django applications, call it in your settings or WSGI/ASGI entry point.

Version Compatibility

The integration supports ddtrace versions 2.x through 4.1.x:

  • v3.x+: Uses simplified patch_all() approach
  • v2.x: Explicit tracer configuration with RuntimeMetrics

Tracing is only enabled when get_environment() returns a non-None value (i.e., when ENVIRONMENT or DATADOG_ENV is set).

application_kit.datadog.configure_datadog

configure_datadog(worker_name=None)

Setups datadog integration.

  • service name will be computed from current application name optionally appending worker_name to it.
  • checks that worker_name is declared in the workers section of the manifest if not None
PARAMETER DESCRIPTION
worker_name

The worker_name to use.

TYPE: str | None DEFAULT: None

RETURNS DESCRIPTION
str | None

The datadog service name that was computed

Source code in application_kit/datadog/__init__.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def configure_datadog(
    worker_name: Annotated[str | None, Doc("The worker_name to use.")] = None,
) -> Annotated[str | None, Doc("The datadog service name that was computed")]:
    """Setups datadog integration.

    - service name will be computed from current application name optionally appending worker_name to it.
    - checks that worker_name is declared in the workers section of the manifest if not None
    """

    major_ddtrace_version = int(ddtrace.__version__.split(".")[0])

    if major_ddtrace_version < 3:
        from ddtrace.runtime import RuntimeMetrics

        service_name = None
        datadog_configuration = get_datadog_configuration(worker_name)

        if datadog_configuration is not None:
            service_name = datadog_configuration.service_name

            logger.info(
                f"Configuring datadog with service : '{service_name}' with environment "
                f"'{datadog_configuration.environment}'."
            )

            ddtrace.trace.tracer.configure(hostname="dd-agent", port=8126, enabled=True)  # type:ignore

            ddtrace.config.django["service_name"] = service_name

            ddtrace.config.http.trace_query_string = True

            ddtrace.config.service = service_name

            ddtrace.trace.tracer.set_tags(
                {
                    "env": datadog_configuration.environment,
                    "version": datadog_configuration.version,
                    "service": service_name,
                }
            )
            ddtrace.trace.tracer._services = {service_name}  # type:ignore
            ddtrace.patch_all()
            RuntimeMetrics.enable(dogstatsd_url="udp://dd-agent:8125", tracer=ddtrace.trace.tracer)
        else:
            ddtrace.trace.tracer.configure(enabled=False)  # type:ignore
    else:
        service_name = get_service_name(worker_name)
        ddtrace.patch_all()

    return service_name