|
| 1 | +--- |
| 2 | +title: OpenTelemetry OTLP |
| 3 | +sdk: sentry.java |
| 4 | +description: "Using OpenTelemetry OTLP export with Sentry." |
| 5 | +sidebar_order: 300 |
| 6 | +--- |
| 7 | + |
| 8 | +Use the `sentry-opentelemetry-otlp` module when you want OpenTelemetry to handle tracing (with spans exported via OTLP to Sentry) while Sentry handles errors, logs, and metrics. Sentry reads trace and span IDs from the OpenTelemetry `Context` so that all Sentry events are correlated with your OpenTelemetry traces. |
| 9 | + |
| 10 | +Unlike the <PlatformLink to="/opentelemetry/setup/agent">Agent</PlatformLink> and <PlatformLink to="/opentelemetry/setup/agentless">Agentless</PlatformLink> integrations, this module does not use OpenTelemetry for scope storage or span creation within the Sentry SDK. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +<PlatformSection notSupported={["java.spring-boot"]}> |
| 15 | + |
| 16 | +```groovy {tabTitle:Gradle} |
| 17 | +implementation 'io.sentry:sentry-opentelemetry-otlp:{{@inject packages.version('sentry.java', '8.0.0') }}' |
| 18 | +``` |
| 19 | + |
| 20 | +```xml {tabTitle:Maven} |
| 21 | +<dependency> |
| 22 | + <groupId>io.sentry</groupId> |
| 23 | + <artifactId>sentry-opentelemetry-otlp</artifactId> |
| 24 | + <version>{{@inject packages.version('sentry.java', '8.0.0') }}</version> |
| 25 | +</dependency> |
| 26 | +``` |
| 27 | + |
| 28 | +This includes the OpenTelemetry SDK autoconfiguration and OTLP exporter as transitive dependencies. If you configure the OpenTelemetry SDK manually without autoconfiguration, you can exclude `opentelemetry-sdk-extension-autoconfigure` to prevent it from discovering and activating components via SPI. |
| 29 | + |
| 30 | +</PlatformSection> |
| 31 | + |
| 32 | +<PlatformSection supported={["java.spring-boot"]}> |
| 33 | + |
| 34 | +```groovy {tabTitle:Gradle} |
| 35 | +implementation 'io.sentry:sentry-opentelemetry-otlp-spring:{{@inject packages.version('sentry.java', '8.0.0') }}' |
| 36 | +``` |
| 37 | + |
| 38 | +```xml {tabTitle:Maven} |
| 39 | +<dependency> |
| 40 | + <groupId>io.sentry</groupId> |
| 41 | + <artifactId>sentry-opentelemetry-otlp-spring</artifactId> |
| 42 | + <version>{{@inject packages.version('sentry.java', '8.0.0') }}</version> |
| 43 | +</dependency> |
| 44 | +``` |
| 45 | + |
| 46 | +This includes `sentry-opentelemetry-otlp` and the OpenTelemetry Spring Boot starter as transitive dependencies. |
| 47 | + |
| 48 | +</PlatformSection> |
| 49 | + |
| 50 | +## Setup |
| 51 | + |
| 52 | +You need to configure both OpenTelemetry (to export spans via OTLP to Sentry) and Sentry (to read trace/span IDs from OpenTelemetry). |
| 53 | + |
| 54 | +The OTLP endpoint and authentication details are shown in the code examples below. You can also find the **OTLP Traces Endpoint** and **OTLP Traces Endpoint Headers** in your Sentry project under **Settings > Projects > [Project] > Client Keys (DSN)**. |
| 55 | + |
| 56 | +<PlatformSection notSupported={["java.spring-boot"]}> |
| 57 | + |
| 58 | +Initialize OpenTelemetry using `AutoConfiguredOpenTelemetrySdk`, then initialize Sentry with the `OpenTelemetryOtlpEventProcessor`: |
| 59 | + |
| 60 | +```java {tabTitle:Java} |
| 61 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 62 | +import io.sentry.Sentry; |
| 63 | +import io.sentry.opentelemetry.otlp.OpenTelemetryOtlpEventProcessor; |
| 64 | + |
| 65 | +// 1. Configure OpenTelemetry |
| 66 | +AutoConfiguredOpenTelemetrySdk.builder() |
| 67 | + .setResultAsGlobal() |
| 68 | + .addPropertiesSupplier(() -> { |
| 69 | + Map<String, String> properties = new HashMap<>(); |
| 70 | + properties.put("otel.traces.exporter", "otlp"); |
| 71 | + properties.put("otel.exporter.otlp.traces.endpoint", "___OTLP_TRACES_URL___"); |
| 72 | + properties.put("otel.exporter.otlp.traces.protocol", "http/protobuf"); |
| 73 | + properties.put("otel.exporter.otlp.traces.headers", "x-sentry-auth=sentry sentry_key=___PUBLIC_KEY___"); |
| 74 | + properties.put("otel.propagators", "sentry"); |
| 75 | + properties.put("otel.logs.exporter", "none"); |
| 76 | + properties.put("otel.metrics.exporter", "none"); |
| 77 | + return properties; |
| 78 | + }) |
| 79 | + .build(); |
| 80 | + |
| 81 | +// 2. Initialize Sentry |
| 82 | +Sentry.init(options -> { |
| 83 | + options.setDsn("___PUBLIC_DSN___"); |
| 84 | + // Link Sentry events to OpenTelemetry traces |
| 85 | + options.addEventProcessor(new OpenTelemetryOtlpEventProcessor()); |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +</PlatformSection> |
| 90 | + |
| 91 | +<PlatformSection supported={["java.spring-boot"]}> |
| 92 | + |
| 93 | +Add the following to your `application.properties`: |
| 94 | + |
| 95 | +```properties {tabTitle:application.properties} |
| 96 | +sentry.dsn=___PUBLIC_DSN___ |
| 97 | + |
| 98 | +otel.propagators=sentry |
| 99 | +otel.traces.exporter=otlp |
| 100 | +otel.exporter.otlp.traces.endpoint=___OTLP_TRACES_URL___ |
| 101 | +otel.exporter.otlp.traces.protocol=http/protobuf |
| 102 | +otel.exporter.otlp.traces.headers=x-sentry-auth=sentry sentry_key=___PUBLIC_KEY___ |
| 103 | +otel.logs.exporter=none |
| 104 | +otel.metrics.exporter=none |
| 105 | +``` |
| 106 | + |
| 107 | +Register the `OpenTelemetryOtlpEventProcessor` as a Spring bean to link Sentry events to OpenTelemetry traces: |
| 108 | + |
| 109 | +```java {tabTitle:Java} |
| 110 | +import io.sentry.opentelemetry.otlp.OpenTelemetryOtlpEventProcessor; |
| 111 | +import org.springframework.context.annotation.Bean; |
| 112 | +import org.springframework.context.annotation.Configuration; |
| 113 | + |
| 114 | +@Configuration |
| 115 | +public class SentryConfig { |
| 116 | + @Bean |
| 117 | + public OpenTelemetryOtlpEventProcessor openTelemetryOtlpEventProcessor() { |
| 118 | + return new OpenTelemetryOtlpEventProcessor(); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +</PlatformSection> |
0 commit comments