Skip to content

Commit 638fffc

Browse files
lciancoolguyzone
andauthored
feat(rust): document OTEL integration (#13674)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR Documents the newly released OTEL integration for the Rust SDK. Content is largely the same as the crate docs available here https://crates.io/crates/sentry-opentelemetry Closes getsentry/sentry-rust#781 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [X] Urgent deadline (GA date, etc.): 13/05 (this has been already released, we should add docs ASAP) <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [ ] None: Not urgent, can wait up to 1 week+ --------- Co-authored-by: Alex Krawiec <[email protected]>
1 parent 350714a commit 638fffc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/platforms/rust/common/tracing/instrumentation/automatic-instrumentation.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,73 @@ fn main_span2() {
4949
}
5050
```
5151

52+
## OpenTelemetry Integration
53+
54+
The Rust SDK offers an integration for the OpenTelemetry ecosystem.
55+
The integration can automatically capture Sentry [spans/transactions](/concepts/key-terms/tracing/distributed-tracing/#traces-transactions-and-spans) for every span created through the OpenTelemetry API, with support for [distributed tracing](/concepts/key-terms/tracing/distributed-tracing/).
56+
The integration also captures errors with the correct span and trace associations.
57+
58+
For this integration to work as intended, only the OpenTelemetry tracing API should be used to manage spans.
59+
Mixing it with the Sentry tracing API will result in incorrectly nested spans.
60+
61+
To get started using the OpenTelemetry integration for the Sentry Rust SDK, add the necessary dependencies to your `Cargo.toml`:
62+
```toml {filename:Cargo.toml}
63+
[dependencies]
64+
sentry = "{{@inject packages.version('sentry.rust') }}"
65+
opentelemetry = { version = "0.29.1", features = ["trace"] }
66+
opentelemetry_sdk = { version = "0.29.0", features = ["trace"] }
67+
```
68+
69+
Initialize the SDK, then create and register the `SentryPropagator` and `SentrySpanProcessor`:
70+
71+
```rust
72+
use opentelemetry::{
73+
global,
74+
trace::{TraceContextExt, Tracer},
75+
KeyValue,
76+
};
77+
use opentelemetry_sdk::trace::SdkTracerProvider;
78+
use sentry::integrations::opentelemetry as sentry_opentelemetry;
79+
80+
// Initialize the Sentry SDK
81+
let _guard = sentry::init((
82+
"___PUBLIC_DSN___",
83+
sentry::ClientOptions {
84+
release: sentry::release_name!(),
85+
// Enable capturing of traces; set this a to lower value in production.
86+
traces_sample_rate: 1.0,
87+
..sentry::ClientOptions::default()
88+
},
89+
));
90+
91+
// Register the Sentry propagator for distributed tracing
92+
global::set_text_map_propagator(sentry_opentelemetry::SentryPropagator::new());
93+
94+
let tracer_provider = SdkTracerProvider::builder()
95+
// Register the Sentry span processor to send OpenTelemetry spans to Sentry
96+
.with_span_processor(sentry_opentelemetry::SentrySpanProcessor::new())
97+
.build();
98+
99+
global::set_tracer_provider(tracer_provider);
100+
```
101+
102+
Use the OpenTelemetry API to create spans. They will be captured by Sentry:
103+
104+
```rust
105+
let tracer = global::tracer("tracer");
106+
// Creates a Sentry span (transaction) with the name set to "example"
107+
tracer.in_span("example", |_| {
108+
// Creates a Sentry child span with the name set to "child"
109+
tracer.in_span("child", |cx| {
110+
// OTEL span attributes are captured as data attributes on the Sentry span
111+
cx.span().set_attribute(KeyValue::new("my", "attribute"));
112+
113+
// Captures a Sentry error message and associates it with the ongoing child span
114+
sentry::capture_message("Everything is on fire!", sentry::Level::Error);
115+
});
116+
});
117+
```
118+
52119
## HTTP Instrumentation
53120

54121
The Sentry SDK offers an integration for the `tower` ecosystem which can automatically continue a trace from an incoming HTTP request.

0 commit comments

Comments
 (0)