|
| 1 | +--- |
| 2 | +title: OTLP Integration |
| 3 | +--- |
| 4 | + |
| 5 | +This document outlines how to build a dedicated `OTLPIntegration` in SDKs that makes it easier for users using [OpenTelemetry](https://opentelemetry.io/) for instrumentation to send their spans and traces to Sentry's new [OTLP ingestion endpoint](https://docs.sentry.io/concepts/otlp/). |
| 6 | + |
| 7 | +## Background |
| 8 | + |
| 9 | +### Existing OpenTelemetry Support |
| 10 | + |
| 11 | +Some of our SDKs (Node, Java) already shipped a complete Performance powered by OpenTelemetry (POTEL) system following the [OpenTelemetry Support spec](../opentelemetry). |
| 12 | + |
| 13 | +Some other SDKs (Python, Ruby, Elixir) implement a simpler system with **only** the `SpanProcessor` and `Propagator` components. |
| 14 | + |
| 15 | +While building the POTEL prototype for Python, we questioned if the complexity we were adding was justified and the architecture was scalable and decided to stop with POTEL for other backend SDKs. |
| 16 | + |
| 17 | +### OTLP Ingestion Endpoint |
| 18 | + |
| 19 | +Concurrently to the above SDK work, we also shipped first-class support for ingestion OTLP Traces and Logs. This opened up the possibility of a much cleaner system that allows users to setup OpenTelemetry with Sentry seamlessly while preserving behavior - **Trace Connectedness** that makes Sentry valuable. |
| 20 | + |
| 21 | +## Integration Spec |
| 22 | + |
| 23 | +The new integration MUST be called `OTLPIntegration` with the following signature: |
| 24 | + |
| 25 | +```python |
| 26 | +OTLPIntegration(setup_otlp_exporter=True, setup_propagator=True) |
| 27 | +``` |
| 28 | + |
| 29 | +The arguments `setup_otlp_exporter` and `setup_propagator` MUST be booleans that default to `True` and can be turned off if needed by the user. |
| 30 | + |
| 31 | +It MUST consist of and setup the following components: |
| 32 | + |
| 33 | +* A `SpanExporter` that automatically configures the OTLP ingestion endpoint from the DSN |
| 34 | +* A `Propagator` that ensures Distributed Tracing works with other upstream and downstream services using Sentry SDKs |
| 35 | +* An `external_propagation_context` that extracts the active `trace_id` and `span_id` from the OpenTelemetry SDK |
| 36 | + |
| 37 | +### SpanExporter |
| 38 | + |
| 39 | +IF `setup_otlp_exporter` is `True`, the integration MUST do the following: |
| 40 | + |
| 41 | +* Take the existing `TracerProvider` from OpenTelemetry if it exists, otherwise create a new one |
| 42 | +* Construct the OTLP Traces `endpoint` from the DSN |
| 43 | +* Add the `X-Sentry-Auth` header to a `headers` dictionary |
| 44 | +* Instantiate a `OTLPSpanExporter` from OpenTelemetry with the `endpoint` and `headers` |
| 45 | +* Instantiate a `BatchSpanProcessor` with the above `OTLPSpanExporter` |
| 46 | +* Add the `BatchSpanProcessor` to the `TracerProvider` |
| 47 | + |
| 48 | +See the [Reference Python implementation](https://github.com/getsentry/sentry-python/blob/c68c3d6b5152b879bc107f76d9a9c78e95792235/sentry_sdk/integrations/otlp.py#L36-L55). |
| 49 | + |
| 50 | +IF `setup_otlp_exporter` is `False`, the integration MUST skip this entire step so users can configure their exporters OR collectors manually if they wish to do so. |
| 51 | + |
| 52 | +### Propagator |
| 53 | + |
| 54 | +The `Propagator` implementation is the same as in the earlier [OpenTelemetry Spec](../opentelemetry/#step-2-implement-the-sentrypropagator-on-your-sdk-add-sentrypropagator). |
| 55 | + |
| 56 | +IF `setup_propagator` is `True`, the integration MUST setup the `Propagator`. |
| 57 | + |
| 58 | +The way to setup the propagator is dependent on the language ecosystem. The Python implementation uses `set_global_textmap`. |
| 59 | + |
| 60 | +IF `setup_propagator` is `False`, the integration MUST skip setting up the `Propagator` and leave it to user to configure manually if they wish to do so. |
| 61 | + |
| 62 | +### External Propagation Context |
| 63 | + |
| 64 | +This specification introduces a new `external_propagation_context` concept that is a global function that adds a new source of `trace_id` and `span_id` to be used in other Sentry event payloads. |
| 65 | + |
| 66 | +This is to ensure that all other Sentry events such as Errors, Check-Ins, Logs and Metrics have the correct `TraceContext` (for Errors and Check-Ins) or `trace_id, span_id` attributes (for Logs and Metrics) and can be linked correctly to the relevant Trace and Span originating from OpenTelemetry. |
| 67 | + |
| 68 | +#### Scope |
| 69 | + |
| 70 | +The `Scope` implementation MUST add two new methods: |
| 71 | + |
| 72 | +* `register_external_propagation_context(fn)` that takes a function and stores it as a global |
| 73 | +* `get_external_propagation_context` that when called calls the above stored global function and returns a `(trace_id, span_id)` tuple or `None` |
| 74 | + |
| 75 | +Further, the scope's `get_trace_context` method should use this `get_external_propagation_context` in preference to the local `propagation_context` on the scope to fill in the `trace_id` and `span_id` in the respective event payloads. |
| 76 | + |
| 77 | +It is RECOMMENDED to centralize this logic for fetching the active `trace_id` and `span_id` for Logs and Metrics if that is not already the case in your SDK. |
| 78 | + |
| 79 | +See the [Reference Python implementation](https://github.com/getsentry/sentry-python/blob/c68c3d6b5152b879bc107f76d9a9c78e95792235/sentry_sdk/scope.py#L587-L610). |
| 80 | + |
| 81 | +#### Integration |
| 82 | + |
| 83 | +The Integration them MUST call `register_external_propagation_context` with a function that fetches the `(trace_id, span_id)` tuple from OpenTelemetry. |
| 84 | + |
| 85 | +See the [Reference Python implementation](https://github.com/getsentry/sentry-python/blob/c68c3d6b5152b879bc107f76d9a9c78e95792235/sentry_sdk/integrations/otlp.py#L23-L33). |
| 86 | + |
| 87 | +## Comparison to POTEL |
| 88 | + |
| 89 | +Note the difference in components above to the components in the POTEL implementation: |
| 90 | + |
| 91 | +* A `SpanProcessor` for processing and packaging the Spans that OpenTelemetry emits |
| 92 | +* A `Propagator` that ensures Distributed Tracing works with other upstream and downstream services using Sentry SDKs |
| 93 | +* A `Sampler` that works with Sentry's `traces_sample_rate` and `traces_sampler` |
| 94 | +* Bidirectional syncing between Sentry's `Scope`s and OpenTelemetry's `Context` so that spans can be started in either API and interleaved freely while preserving the span tree |
0 commit comments