Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions datadog_checks_base/datadog_checks/base/utils/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def configure_tracer(tracer, self_check):
"""
apm_tracing_enabled = False
context_provider = None
custom_writer = None
try:
integration_tracing, integration_tracing_exhaustive = tracing_enabled()
if integration_tracing or integration_tracing_exhaustive:
Expand All @@ -128,22 +129,40 @@ def configure_tracer(tracer, self_check):
dd_trace_id = self_check.instances[0].get("dd_trace_id", None)
dd_parent_id = self_check.instances[0].get("dd_parent_span_id", None)

if dd_trace_id and dd_parent_id:
# 1 means the trace will be dropped
if dd_trace_id and dd_parent_id and dd_trace_id != 1:
from ddtrace.context import Context

apm_tracing_enabled = True
context_provider = Context(
trace_id=dd_trace_id,
span_id=dd_parent_id,
)

# We send the traces to the telemetry endpoint instead of the regular intake endpoint
# dd_trace_id and dd_parent_id are set by the datadog-agent itself in case of a internal observability
from ddtrace.internal.writer import HTTPWriter
custom_writer = HTTPWriter(
intake_url=f"instrumentation-telemetry-intake.{datadog_agent.get_config('site', 'datadoghq.com')}/api/v2/apmtelemetry",
headers={
"dd-api-key": datadog_agent.get_config('api_key'),
"content-type": "application/json",
"dd-telemetry-api-version": "v2",
"dd-telemetry-origin": "integrations",
"dd-telemetry-request-type": "traces",
"dd-client-library-language": "python",
}
)

except (ValueError, TypeError, AttributeError, ImportError):
raise
pass

try:
# Update the tracer configuration to make sure we trace only if we really need to
tracer.configure(
appsec_enabled=False,
enabled=apm_tracing_enabled,
writer=custom_writer,
)

# If the current trace context is not set or is set to an empty trace_id, activate the context provider
Expand Down
Loading