← Back to Pull requests

[CONTP-2125] Add opt-in APM tracing for DDA and DDAI reconciles #3537

Closed
unknown closed this pull request without merging on Oct 13, 2026 16:02 UTC
View on GitHub
unknown unknown commented on Sep 30, 2026 14:48 UTC
Author

What does this PR do?

Adds opt-in APM tracing (dd-trace-go v2) to the DDA and DDAI reconcile loops.

  • Enable with --tracing-enabled or DD_TRACING_ENABLED=true. Off by default.

  • One root span per reconcile: datadogagent.reconcile / datadogagentinternal.reconcile, tagged with kind, name, namespace and reconcileID.

  • Child spans:

  • - DDA: manageExperiment, manageRevision, reconcileProfiles, manageDDADependenciesWithDDAI, createOrUpdateDDAI, updateStatusIfNeeded
    - DDAI: applyAndCleanupDependencies, reconcileComponent / Cleanup (tagged agent.component), reconcileV2Agent, cleanupExtraneousResources, updateStatusIfNeeded
  • Kubernetes API requests made with the reconcile context are traced (cached reads don't reach the API server). By default, only 5xx responses count as errors (override with DD_TRACE_HTTP_CLIENT_ERROR_STATUSES).

  • Logs written through the context logger (ctrl.LoggerFrom(ctx)) include dd.trace_id and the current span's dd.span_id, in dd-trace-go's trace ID format so they link to traces. Logs still written through the base logger (r.log) don't; moving them is tracked in CONTP-2142.

  • The profiler moves to dd-trace-go v2 and the v1 dependency is removed. Its service name is unchanged (manager unless DD_SERVICE is set).
  • Motivation

    CONTP-2125. Existing metrics show total reconcile duration and error counts, but not which step inside a reconcile is slow or failing. Tracing breaks each reconcile into spans, including the Kubernetes API calls, so a slow or failed step can be found for a specific reconcile. This builds on the prototype in #2744.

    Kubernetes audit IDs

    The apiserver uses a client-supplied Audit-ID header verbatim as the request's audit ID, and generates a UUID per request when it is absent. The dd-trace-go client-go integration sets that header to the trace ID, which is fine when a trace makes one API call but not here: a reconcile is one trace making dozens, so every one of them would land in the audit log under a single ID, and a request's audit events could no longer be paired with its response.

    This PR wraps the transport directly instead and sets the header to <trace ID>-<span ID>. The ID stays unique per request, and still identifies the reconcile that issued it, so the audit-log correlation the integration was built for is kept. The span keeps the integration's kubernetes.audit_id tag, set to the same value, for the reverse lookup. Verified on a kind cluster with audit logging: 23 API calls produced 23 distinct audit IDs with two audit events each, against 4 IDs covering up to 8 events before the change.

    Worth raising upstream separately, since it affects any dd-trace-go user making more than one API call per trace.

    DatadogAgent log fields

    Moving the reconcile logs onto the context logger is what carries the trace IDs, but it also pulls in controller-runtime's default fields, so the object is now reported three times on each line: nested DatadogAgent, flat namespace/name, and our datadogagent.

    Deduplicating means dropping one of those three names, and each option removes a log attribute that may be in use today — flat namespace/name on one side, @datadogagent. on the other. Renaming a shipped log attribute is out of scope here, so this PR leaves the duplication in place. CONTP-2162 covers picking one shape and applying it across all controllers, which today use three different ones (DDA, DDAI and DatadogGenericResource all differ).

    The one thing this PR does fix is the controllers.DatadogAgent logger name, which the switch to the context logger had dropped, breaking logger:controllers.DatadogAgent queries. It is set in Reconcile the same way DatadogAgentInternal does it.

    No log field is renamed or removed relative to main. Reconcile lines gain reconcileID, the trace IDs when tracing is on, and controller-runtime's duplicate object fields.

    Additional Notes

  • Users need to point the tracer at an Agent: DD_AGENT_HOST (e.g. from status.hostIP), DD_TRACE_AGENT_URL, or the APM socket at /var/run/datadog/apm.socket. The Helm chart sets none of these.

  • No cluster-identifying span tags are set. kube_cluster_name reaches spans as a host tag via the node Agent, so APM search by cluster works without the tracer duplicating it.

  • The binary grows by about 7.5 MB (+7%).

  • When tracing is disabled, the tracer isn't started, API calls aren't wrapped, and no Audit-ID header is sent. The main cost is binary size.

  • If DD_TRACE_ENABLED=false is set alongside --tracing-enabled, no spans are created and logs aren't tagged with trace IDs.

  • Not traced: calls that don't use the reconcile context, e.g. the OpenShift etcd secret copy in control plane monitoring.

  • A reconcile that panics is recorded as a successful span (controller-runtime recovers the panic after the spans finish).

  • Log trace IDs are 128-bit hex only when DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED is on (the default) and the ID has upper bits set. Otherwise they are 64-bit decimal, so pipelines that only parse decimal IDs still correlate.
  • Minimum Agent Versions

    Agent: N/A

  • Cluster Agent: N/A
  • Describe your test plan

  • Unit tests in pkg/trace cover spans, tags, errors, log fields, the transport, and a tracer that isn't running. TestWrapTransport asserts audit IDs are unique per request, prefixed with the trace ID, within the apiserver's 64-character limit, and absent on untraced requests; it fails against the previous transport wrapper.

  • TestReconcileTracing runs a DDA → DDAI reconcile and checks the span tree.

  • Manual check on a kind cluster with apiserver audit logging enabled: compared reconcile log lines and audit IDs across main, this branch, and tracing off. Confirmed no trace IDs or modified audit IDs with the flag off, and one audit ID per request with it on.

  • Manual check: deploy with tracing enabled next to an Agent with APM. Change a DatadogAgent, then confirm the traces show up in APM under datadog-operator.
  • Checklist

  • PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies

  • PR has a milestone or the qa/skip-qa label

  • All commits are signed (see: [signing commits][1])
  • [1]: https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: khewonc Review duration: 18794m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3537"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%