Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
You want to extend the ddtrace library’s functionality.
You need finer control over instrumenting your applications.
The ddtrace library provides several techniques to help you achieve these goals. The following sections demonstrate how to use the OpenTelemetry API for custom instrumentation to use with Datadog.
The dd.tags property allows you to set tags across all generated spans for an application. This is useful for grouping stats for your applications, data centers, or any other tags you would like to see in Datadog.
If you aren’t using a supported framework instrumentation, or you would like additional depth in your application’s traces, you may want to add custom instrumentation to your code for complete flame graphs or to measure execution times for pieces of code.
If modifying application code is not possible, use the environment variable dd.trace.methods to detail these methods.
If you have existing @Trace or similar annotations, or prefer to use annotations to complete any incomplete traces within Datadog, use Trace Annotations.
Traces may also be created using the OpenTelemetry @WithSpan annotation as described in Trace annotations.
Add @WithSpan to methods to have them be traced when running OpenTelemetry and the dd-java-agent.jar. If the Agent is not attached, this annotation has no effect on your application.
OpenTelemetry’s @WithSpan annotation is provided by the opentelemetry-instrumentation-annotations dependency.
importio.opentelemetry.instrumentation.annotations.WithSpan;publicclassSessionManager{@WithSpanpublicstaticvoidsaveSession(){// your method implementation here}}
To manually create new spans within the current trace context:
importio.opentelemetry.api.OpenTelemetry;importio.opentelemetry.api.trace.Span;importio.opentelemetry.api.trace.Tracer;importio.opentelemetry.context.Scope;importio.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;importio.opentelemetry.sdk.OpenTelemetrySdk;importio.opentelemetry.sdk.resources.Resource;importio.opentelemetry.sdk.trace.SdkTracerProvider;importio.opentelemetry.sdk.trace.export.BatchSpanProcessor;importio.opentelemetry.semconv.ResourceAttributes;importjava.util.concurrent.TimeUnit;publicclassExample{publicvoiddoSomething(){Tracertracer=GlobalOpenTelemetry.getTracer("my-scope","0.1.0");Spanspan=tracer.spanBuilder("my-resource").startSpan();try(Scopescope=span.makeCurrent()){// do some work}catch(Throwablet){span.recordException(t);throwt;}finally{span.end();}}}
Adding span events requires SDK version 1.40.0 or higher.
You can add span events using the addEvent API. This method requires a name parameter and optionally accepts attributes and timestamp parameters. The method creates a new span event with the specified properties and associates it with the corresponding span.
Name [required]: A string representing the event’s name.
Attributes [optional]: Zero or more key-value pairs with the following properties:
The key must be a non-empty string.
The value can be either:
A primitive type: string, Boolean, or number.
A homogeneous array of primitive type values (for example, an array of strings).
Nested arrays and arrays containing elements of different data types are not allowed.
Timestamp [optional]: A UNIX timestamp representing the event’s occurrence time. Expects an Instant object.
The following examples demonstrate different ways to add events to a span:
AttributeseventAttributes=Attributes.builder().put(AttributeKey.longKey("int_val"),1L).put(AttributeKey.stringKey("string_val"),"two").put(AttributeKey.longArrayKey("int_array"),Arrays.asList(3L,4L)).put(AttributeKey.stringArrayKey("string_array"),Arrays.asList("5","6")).put(AttributeKey.booleanArrayKey("bool_array"),Arrays.asList(true,false)).build();span.addEvent("Event With No Attributes");span.addEvent("Event With Some Attributes",eventAttributes);
Read the OpenTelemetry specification for more information.
To record exceptions, use the recordException API. This method requires an exception parameter and optionally accepts a UNIX timestamp parameter. It creates a new span event that includes standardized exception attributes and associates it with the corresponding span.
The following examples demonstrate different ways to record exceptions:
Both the tracing client and Datadog Agent offer additional configuration options for context propagation. You can also exclude specific resources from sending traces to Datadog if you don’t want those traces to be included in calculated metrics, such as traces related to health checks.
You can configure the propagation of context for distributed traces by injecting and extracting headers. Read Trace Context Propagation for information.
Traces can be excluded based on their resource name, to remove synthetic traffic such as health checks from reporting traces to Datadog. This and other security and fine-tuning configurations can be found on the Security page or in Ignoring Unwanted Resources.