Skip to content

OpenTelemetry API support (3.0)

Trask Stalnaker edited this page May 18, 2021 · 7 revisions

⚠️ This feature is in preview

⚠️ 3.0.3-BETA or later is required in order to use OpenTelemetry 1.0.0

Enabling this feature

To enable this preview feature, add the following to your applicationinsights.json file:

{
  "preview": {
    "openTelemetryApiSupport": true
  }
}

You can also enable this preview feature by setting the environment variable APPLICATIONINSIGHTS_PREVIEW_OTEL_API_SUPPORT to true (which will then take precedence over value specified in the json configuration).

Adding OpenTelemetry API to your application

<dependencies>
  <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-api</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

Adding custom dimensions to your request telemetry

Span.current().setAttribute("mydimension", "myvalue");

Note: if your code is inside of a Spring controller, Span.current() will reference the InProc dependency that represents the controller call. In this case, you will need to add your code to a web filter that runs before the Spring controller, at which time Span.current() will refer to the request.

Note: if you set an attribute named enduser.id, the value will be stored in the user_Id column in the Application Insights Logs table instead of as a custom dimension.

Adding a top-level span

The easiest way to start a top-level span is to use the @WithSpan annotation from

<dependencies>
  <dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-extension-annotations</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

and place it on your method that represents the top-level operation, e.g.

@WithSpan(kind = SpanKind.SERVER)
public void operation() {
  ...
}

This will cause a request telemetry to be generated for each invocation of operation(), and all auto-collected dependencies and logging that occurs during the execution of the operation will be correlated with the top-level request telemetry.