For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/tracing/trace_collection/dynamic_instrumentation.md. A documentation index is available at /llms.txt.

Dynamic Instrumentation

This product is not supported for your selected Datadog site. ().

Language

Language


Overview

Dynamic Instrumentation lets you add metrics, spans, and span tags to running production systems without restarts or code changes, including in third-party libraries.

To use autocomplete and search, opt in to the preview.

Note: Dynamic Instrumentation is not compatible with Azure App Services or serverless environments.

Requirements

Datadog SDK

Datadog configuration

Permissions

The following permissions are required to use Dynamic Instrumentation:

  • Dynamic Instrumentation Read Configuration (debugger_read): Required to access the Dynamic Instrumentation page.
  • One of the following write permissions:
    • Dynamic Instrumentation Write Configuration (debugger_write): Required to create or modify instrumentations in any environment.
    • Dynamic Instrumentation Write Pre-Prod (debugger_write_preprod): Required to create or modify instrumentations in known pre-production environments only (such as staging or QA).

For more information about roles and how to assign roles to users, see Role Based Access Control.

Enable Dynamic Instrumentation

Dynamic Instrumentation and Live Debugger share the same enablement state per service and environment: enabling or disabling one also enables or disables the other. The two products have separate permissions and Settings pages.

You can enable Dynamic Instrumentation in-app or manually.

Enable in-app (recommended)

Enable Dynamic Instrumentation in-app in one of two ways:

  • On the Dynamic Instrumentation Settings page, enable the service and environment.
  • Create an instrumentation. Dynamic Instrumentation is enabled automatically on the selected service and environment.

Enable manually

Follow these steps if your SDK is below the in-app minimum, or if you want to manage enablement outside the Datadog UI.

  1. If you don't already have APM enabled, set DD_APM_ENABLED=true and configure the Agent to listen on port 8126/TCP.

  2. Download dd-java-agent.jar:

    wget -O dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'
    
    curl -Lo dd-java-agent.jar 'https://dtdg.co/latest-java-tracer'
    
    ADD 'https://dtdg.co/latest-java-tracer' dd-java-agent.jar
    
  3. Start your service with Dynamic Instrumentation enabled. The -javaagent argument must come before -jar:

    java \
        -javaagent:dd-java-agent.jar \
        -Ddd.service=<YOUR_SERVICE> \
        -Ddd.env=<YOUR_ENVIRONMENT> \
        -Ddd.version=<YOUR_VERSION> \
        -Ddd.dynamic.instrumentation.enabled=true \
        -jar <YOUR_SERVICE>.jar <YOUR_SERVICE_FLAGS>
    
    export DD_SERVICE=<YOUR_SERVICE>
    export DD_ENV=<YOUR_ENV>
    export DD_VERSION=<YOUR_VERSION>
    export DD_DYNAMIC_INSTRUMENTATION_ENABLED=true
    java \
        -javaagent:dd-java-agent.jar \
        -jar <YOUR_SERVICE>.jar <YOUR_SERVICE_FLAGS>
    

    Note: On JDK 18 and earlier, classes compiled with the -parameters flag (default in Spring 6+, Spring Boot 3+, and Scala) may fail to instrument with the error Method Parameters detected.

  4. After you start your service, open the Dynamic Instrumentation page.

  1. If you don't already have APM enabled, set DD_APM_ENABLED=true and configure the Agent to listen on port 8126/TCP.

  2. Install ddtrace:

    pip install ddtrace
    
  3. Enable Dynamic Instrumentation with environment variables or in code:

    export DD_SERVICE=<YOUR_SERVICE>
    export DD_ENV=<YOUR_ENV>
    export DD_VERSION=<YOUR_VERSION>
    export DD_DYNAMIC_INSTRUMENTATION_ENABLED=true
    ddtrace-run python -m myapp.py
    
    import os
    
    os.environ["DD_DYNAMIC_INSTRUMENTATION_ENABLED"] = "true"
    
    import ddtrace.auto  # this must be imported as soon as possible
    
  4. After you start your service, open the Dynamic Instrumentation page.

  1. If you don't already have APM enabled, set DD_APM_ENABLED=true and configure the Agent to listen on port 8126/TCP.

  2. Start your service with the following environment variables set:

    DD_SERVICE=<YOUR_SERVICE>
    DD_ENV=<YOUR_ENV>
    DD_VERSION=<YOUR_VERSION>
    DD_DYNAMIC_INSTRUMENTATION_ENABLED=true
    
  3. After you start your service, open the Dynamic Instrumentation page.

  1. If you don't already have APM enabled, set DD_APM_ENABLED=true and configure the Agent to listen on port 8126/TCP.

  2. Start your service with the following environment variables set:

    DD_SERVICE=<YOUR_SERVICE>
    DD_ENV=<YOUR_ENV>
    DD_VERSION=<YOUR_VERSION>
    DD_DYNAMIC_INSTRUMENTATION_ENABLED=true
    
  3. After you start your service, open the Dynamic Instrumentation page.

Configure Dynamic Instrumentation

Configure Dynamic Instrumentation using the following environment variables:

Environment variableTypeDescription
DD_DYNAMIC_INSTRUMENTATION_ENABLEDBooleanSet to true to enable Dynamic Instrumentation.
DD_SERVICEStringThe service name, for example, web-backend.
DD_ENVStringThe environment name, for example, production.
DD_VERSIONStringThe version of your service.
DD_TAGSStringTags to apply to produced data. Must be a list of <key>:<value> separated by commas such as: layer:api,team:intake.

Note: You can also set -Ddd.dynamic.instrumentation.enabled=true instead of DD_DYNAMIC_INSTRUMENTATION_ENABLED.

Explore Dynamic Instrumentation

Dynamic Instrumentation can help you understand what your application is doing at runtime. By adding an instrumentation at a specific code location, you can capture additional telemetry from your application without the need to change code or redeploy it.

Using instrumentations

An instrumentation allows you to collect additional telemetry from specific points in your code without halting the execution of the program.

Dynamic spans, span tags, and metrics are a UI-based alternative to adding custom instrumentation directly to your source code. Datadog receives the instrumentation configurations you define and dynamically applies them to the running service without requiring restarts.

Datadog captures and processes spans, tags, and metrics generated by Dynamic Instrumentation like other telemetry from the running application. Unlike manual custom instrumentation, Dynamic Instrumentation does not require code changes, deployments, or service restarts. To stop collecting data, disable the instrumentation in Datadog.

To redact sensitive data from instrumentations, see Sensitive data scrubbing.

Creating an instrumentation

All instrumentation types require the same initial setup:

  1. Go to the Dynamic Instrumentation page.
  2. Click Create Instrumentation in the top right, or click the three-dot menu on a service and select Add an instrumentation for this service.
  3. If the fields are not prefilled, choose service, runtime, environment, and version (optional).
  4. Specify where to set the instrumentation in the source code by selecting either a class and method, or a specific line of code in a file. When autocomplete and search are available, use them to find files, methods, or symbols.
    Specify where to set the instrumentation in the source code by selecting a class and method. When autocomplete and search are available, use them to find methods or symbols.

For the best experience, set up Source Code Integration to view code directly in Datadog and select instrumentation locations as you would with breakpoints in an IDE.

For creation steps specific to each instrumentation type, see the following sections.

Dynamic log instrumentations are supported in Live Debugger. Use Live Debugger to capture logs and variable snapshots in real time from running applications.

Creating dynamic metrics

A dynamic metric emits a metric when it executes. To create a dynamic metric:

  1. Select Metric as the instrumentation type.
  2. Complete the generic instrumentation setup (choose service, environment, and location).
  3. Specify a name for the metric, which is prefixed with dynamic.instrumentation.metric.probe..
  4. Select a metric type (count, gauge, or histogram).
  5. Choose the value of the metric using the Dynamic Instrumentation expression language. You can use any numeric value from the execution context, such as a method parameter, local variable, a class field, or an expression that yields a numeric value. For count metrics, this is optional, and if you omit it, every invocation increments the count by one.

Metric instrumentations are automatically enabled on all service instances that match the configured environment and version. Metric instrumentations are not rate limited and execute every time the method

or line
is invoked.

Dynamic Instrumentation supports the following metric types:

  • Count: Counts how many times a given method
    or line
    is executed. Can be combined with metric expressions to use the value of a variable to increment the count.
  • Gauge: Generates a gauge based on the last value of a variable. This metric requires a metric expression.
  • Histogram: Generates a statistical distribution of a variable. This metric requires a metric expression.

Creating dynamic spans

A dynamic span emits a span when a method is executed. To create a dynamic span:

  1. Select Span as the instrumentation type.
  2. Complete the generic instrumentation setup (choose service, environment, version, and location).

You can use a dynamic span as an alternative to creating spans with Custom Instrumentation. If the method throws an exception, the details of the exception are associated with the newly created span's error tag.

Creating dynamic span tags

A dynamic span tag adds a tag value to an existing span. You can add a tag to either the active span or the service entry span.

Note: Internal spans are not indexed by default and so might not be searchable in APM.

To create a dynamic span tag:

  1. Select Span Tag as the instrumentation type.
  2. Complete the generic instrumentation setup (choose service, environment, version, and location).
  3. Specify a name for the tag.
  4. Specify the value of the tag using the Dynamic Instrumentation expression language.
  5. Optionally define a condition using the Dynamic Instrumentation expression language. The tag is added only when the expression evaluates to true.
  6. Optionally add additional tags, each with their own name, expression, and optional condition.

You can use a dynamic span tag as an alternative to using Custom Instrumentation to add tags in code.

Further reading