Injecting DDTrace with DataKit Operator¶
DataKit Operator injects DDTrace automatic instrumentation when a Pod is created. It supports Java, Python, PHP, and Node.js. The Operator adds a datakit-lib-init init Container and a shared /datadog-lib volume to the Pod, then modifies the startup environment of all regular application containers. Existing Pods are not modified and must be recreated for injection to take effect.
Usage¶
Install DataKit Operator, enable the DDTrace input in DataKit, and make sure application Pods can reach DataKit on its HTTP port (default 9529).
Choose an injection mode¶
Set the selectors in admission_inject_v2.ddtraces in the Operator YAML. namespace_selectors matches the namespace and label_selectors matches existing Pod labels. Configure at least one list. The Pod must match an entry in each configured list. See selector syntax.
| Scenario | Operator rule | Application workload |
|---|---|---|
| Centrally managed injection | Use check_annotation: false (the field default) and set the version in image. |
Leave the workload YAML unchanged. The rule’s image is used even if the Pod has a version annotation. |
| Opt in per workload | Keep the selectors and set check_annotation: true on the same rule. |
Add or retain the language-specific version annotation under spec.template.metadata.annotations on the target Deployment. Pods outside the selector scope cannot match this rule even with the annotation. |
Supported Runtimes and Images¶
| Language | Application runtime | Default image |
|---|---|---|
| Java | JVM supported by the DDTrace Java Agent | pubrepo.guance.com/datakit-operator/dd-lib-java-init:v1.60.4-ext |
| Python | Python 3.7 | pubrepo.guance.com/datakit-operator/dd-lib-python-init:v2.21.12 |
| Python | Python 3.8 | pubrepo.guance.com/datakit-operator/dd-lib-python-init:v3.19.7 |
| Python | Python 3.9 to 3.14 | pubrepo.guance.com/datakit-operator/dd-lib-python-init:v4.8.3 |
| PHP | Linux GNU or musl | pubrepo.guance.com/datakit-operator/dd-lib-php-init:1.13.1 |
| Node.js | Node.js 16 | pubrepo.guance.com/datakit-operator/dd-lib-js-init:4.55.0 |
| Node.js | Node.js 18 to 25 | pubrepo.guance.com/datakit-operator/dd-lib-js-init:5.102.0 |
The image version must be compatible with the language runtime in the application container. In offline environments, synchronize the image to a private registry and specify its full address in the rule's image field.
Set php_loader_flavor to linux-gnu for glibc or linux-musl for musl (for example, Alpine). The Operator cannot detect libc and uses linux-gnu for missing or invalid values.
Rule Configuration¶
Add a rule to admission_inject_v2.ddtraces in the Operator, keeping existing rules in order. This Java rule selects Pods in default that already have app=java-demo.
The release template sets ddtraces to []. With those defaults, add a rule first. A Pod annotation alone cannot enable DDTrace.
{
"admission_inject_v2": {
"ddtraces": [
{
"name": "ddtrace-java",
"language": "java",
"namespace_selectors": ["^default$"],
"label_selectors": ["app=java-demo"],
"check_annotation": false,
"image": "pubrepo.guance.com/datakit-operator/dd-lib-java-init:v1.60.4-ext",
"envs": {
"DD_AGENT_HOST": "datakit-service.datakit.svc.cluster.local",
"DD_TRACE_AGENT_PORT": "9529",
"DD_JMXFETCH_STATSD_HOST": "datakit-service.datakit.svc.cluster.local",
"DD_JMXFETCH_STATSD_PORT": "8125",
"DD_SERVICE": "{fieldRef:metadata.labels['app']}",
"POD_NAME": "{fieldRef:metadata.name}",
"POD_NAMESPACE": "{fieldRef:metadata.namespace}",
"NODE_NAME": "{fieldRef:spec.nodeName}",
"DD_TAGS": "pod_name:$(POD_NAME),pod_namespace:$(POD_NAMESPACE),host:$(NODE_NAME)"
},
"resources": {
"requests": {
"cpu": "100m",
"memory": "64Mi"
},
"limits": {
"cpu": "500m",
"memory": "512Mi"
}
}
}
]
}
}
For another language, copy the rule and update name, language, the selectors, and image. Choose the image for the runtime from the image table. Remove the Java-only DD_JMXFETCH_STATSD_HOST and DD_JMXFETCH_STATSD_PORT. For PHP, set php_loader_flavor for the application’s libc.
| Field | Description |
|---|---|
name |
Rule name, recommended for locating relevant logs |
language |
Required; one of java, python, php, or nodejs |
namespace_selectors |
Array of Namespace regular expressions |
label_selectors |
Array of Pod Label Selectors |
check_annotation |
Whether the Pod must provide a version Annotation for the corresponding language; defaults to false |
image |
Required; image for the language library init Container |
envs |
Environment variables injected into all regular application containers |
resources |
Resource configuration for the init Container; defaults are used when missing or invalid |
php_loader_flavor |
PHP only; linux-gnu or linux-musl |
Injection Method¶
| Language | Operator changes to application containers |
|---|---|
| Java | Mounts /datadog-lib and appends to JAVA_TOOL_OPTIONS the value -javaagent:/datadog-lib/dd-java-agent.jar |
| Python | Mounts /datadog-lib and prepends /datadog-lib/ to PYTHONPATH |
| PHP | Mounts /datadog-lib, configures DD_LOADER_PACKAGE_PATH and PHP_INI_SCAN_DIR, and loads dd_library_loader.ini |
| Node.js | Mounts /datadog-lib and appends to NODE_OPTIONS the value --require=/datadog-lib/node_modules/dd-trace/init |
The Operator preserves existing startup options. If conflicting options prevent injection, check the Operator log.
Ordinary environment variables in a rule do not overwrite variables with the same name in an application container. DD_TAGS is an exception: when both values are ordinary strings, the Operator merges the tags.
Annotations and Versions¶
To disable DDTrace for one workload, set admission.datakit/ddtrace.enabled: "false" on its Pod template. With "true", the Pod must still meet the rule’s selector and version annotation requirements. To disable all tracing probes, set admission.datakit/tracing.enabled: "false".
When a rule sets check_annotation: true, the Pod must also provide the version Annotation for the corresponding language:
| Language | Version Annotation |
|---|---|
| Java | admission.datakit/java-lib.version |
| Python | admission.datakit/python-lib.version |
| PHP | admission.datakit/php-lib.version |
| Node.js | admission.datakit/nodejs-lib.version |
A nonempty version value replaces the tag in image, keeping its registry and name.
For example, to opt in Java workloads with app=java-demo in default individually, set check_annotation to true in the example rule and set the version on the target Deployment’s Pod template.
How DDTrace interacts with Beacon and OTel¶
Each Pod receives at most one tracing probe. The Operator checks Beacon → OTel → DDTrace, moving to the next probe only if the current one has no eligible rule. To use DDTrace on workloads that also qualify for Beacon or OTel, narrow every higher-priority rule that selects them. Alternatively, set admission.datakit/beacon.enabled: "false" and admission.datakit/otel.enabled: "false" on the Pod template as needed.
For each probe, the Operator uses the first rule in configuration order whose selectors and annotation requirements the Pod meets.
Deployment Example¶
This Deployment matches the Java rule above. Replace example/java-demo:1.0.0 with your Java application image before applying it.
apiVersion: apps/v1
kind: Deployment
metadata:
name: java-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: java-demo
template:
metadata:
labels:
app: java-demo
spec:
containers:
- name: app
image: example/java-demo:1.0.0
To disable DDTrace injection for this workload, add the following annotation to its Pod template.
Verification and Troubleshooting¶
If you edited the ConfigMap directly, restart the Operator first. Recreate the application Pods, then list the init containers for each new Pod.
kubectl -n default get pods -l app=java-demo \
-o custom-columns='POD:.metadata.name,INIT:.spec.initContainers[*].name'
Look for datakit-lib-init in the INIT column to confirm injection. Send a request to the application, then filter the trace list in APM by the service name java-demo.
For missing injection or init container failures, see General troubleshooting.
- The init Container succeeds but no Trace appears: confirm that the application process preserves the injected startup environment and can reach
DD_AGENT_HOSTandDD_TRACE_AGENT_PORT. - PHP fails to start: confirm whether the application image uses glibc or musl and set the correct
php_loader_flavor.
DDTrace is a tracing project initiated and open-sourced by Datadog. Copyright belongs to Datadog and other contributors. Guance supports DDTrace data ingestion. Use of DDTrace is subject to the applicable project's open-source license.