Skip to content

Commit 26a2e44

Browse files
committed
Update code example for v0.2 in README
1 parent 54e73a3 commit 26a2e44

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ and Datadog.
1616
# Features
1717

1818
`ddtrace` has the following features:
19+
1920
1. tracing: utilities for building an OpenTelemetry tracer/layer that sends traces to the Datadog agent
20-
2. log correlation: a log formatter that converts the trace ID and span ID to the Datadog native format and injects them into the `dd.trace_id` and `dd.span_id` fields
21+
2. log correlation: a log formatter that converts the trace ID and span ID to the Datadog native format and injects them
22+
into the `dd.trace_id` and `dd.span_id` fields
2123
([more information](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/opentelemetry/))
2224
3. propagation: a utility function to set the Datadog propagator as the global propagator
23-
4. axum (enabled via the `axum` feature): re-exposing the functionality of [axum-tracing-opentelemetry](https://github.com/davidB/axum-tracing-opentelemetry)
25+
4. axum (enabled via the `axum` feature): re-exposing the functionality
26+
of [axum-tracing-opentelemetry](https://github.com/davidB/axum-tracing-opentelemetry)
2427

2528
# A Complete Example
2629

@@ -32,17 +35,18 @@ use std::net::SocketAddr;
3235
use std::time::Duration;
3336

3437
use axum::{routing::get, Router};
35-
use ddtrace::axum::opentelemetry_tracing_layer;
38+
use ddtrace::axum::OtelAxumLayer;
39+
use ddtrace::error::Error;
3640
use ddtrace::formatter::DatadogFormatter;
3741
use ddtrace::set_global_propagator;
38-
use ddtrace::tracer::{build_layer, TraceResult};
42+
use ddtrace::tracer::build_layer;
3943
use tracing_subscriber::layer::SubscriberExt;
4044
use tracing_subscriber::util::SubscriberInitExt;
4145

4246
#[tokio::main]
43-
async fn main() -> TraceResult<()> {
47+
async fn main() -> Result<(), Error> {
4448
let service_name = std::env::var("DD_SERVICE").unwrap_or("my-service".to_string());
45-
let tracing_layer = build_layer(&service_name)?;
49+
let (tracing_layer, _guard) = build_layer(service_name)?;
4650
tracing_subscriber::registry()
4751
.with(tracing_subscriber::EnvFilter::new(
4852
std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
@@ -58,16 +62,13 @@ async fn main() -> TraceResult<()> {
5862

5963
let app = Router::new()
6064
.route("/", get(root))
61-
.layer(opentelemetry_tracing_layer())
65+
.layer(OtelAxumLayer::default())
6266
.route("/health", get(health));
6367

6468
let addr = SocketAddr::from(([0, 0, 0, 0], 3025));
69+
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
70+
axum::serve(listener, app).await.unwrap();
6571
tracing::info!("listening on {}", addr);
66-
axum::Server::bind(&addr)
67-
.serve(app.into_make_service())
68-
.with_graceful_shutdown(ddtrace::axum::shutdown_signal())
69-
.await
70-
.unwrap();
7172

7273
Ok(())
7374
}
@@ -109,13 +110,14 @@ to set up the agent.
109110
# Further Context and Rationale
110111

111112
## Exporting Traces
113+
112114
For traces, the official Datadog agent
113115
[can ingest OTel trace data](https://docs.datadoghq.com/opentelemetry/)
114-
with the correct environment variable settings. The traces can be sent
116+
with the correct environment variable settings. The traces can be sent
115117
via either HTTP or gRPC. More information on this can be found
116118
[here](https://docs.datadoghq.com/opentelemetry/otlp_ingest_in_the_agent/?tab=docker).
117119

118-
OpenTelemetry has an official Rust crate with extensions for major
120+
OpenTelemetry has an official Rust crate with extensions for major
119121
formats/providers. This includes a Datadog exporter. We have found
120122
this exporter to be less reliable than the standard OTel exporter
121123
sending data to the OTel endpoint of the Datadog agent, though.
@@ -124,7 +126,8 @@ This crate builds on the OTel exporter.
124126
## Propagation
125127

126128
Two commonly used propagation standards are `B3` (OpenZipkin's propagation style)
127-
and Jaeger. OpenTelemetry [supports both](https://opentelemetry.io/docs/reference/specification/context/api-propagators/#propagators-distribution).
129+
and Jaeger.
130+
OpenTelemetry [supports both](https://opentelemetry.io/docs/reference/specification/context/api-propagators/#propagators-distribution).
128131

129132
Most Datadog SDK's support both `B3` and the Datadog native propagation style.
130133
For example, the Python `ddtrace` library supports `B3` but it
@@ -134,8 +137,8 @@ For ease of integration with services written in other languages that use the of
134137
we opted for sticking with Datadog-style propagation over `B3`. This is set via the
135138
`set_global_propagator` function.
136139

137-
138140
# Reqwest Propagation
141+
139142
The Python library takes care of propagation of the trace context automatically.
140143
Unfortunately, we need to do this manually in Rust.
141144

@@ -154,7 +157,7 @@ use reqwest_tracing::TracingMiddleware;
154157
async fn main() {
155158
set_global_propagator();
156159
client = get_http_client();
157-
160+
158161
// configure tracing, setup your app and inject the client
159162
}
160163

@@ -166,4 +169,5 @@ fn get_http_client() -> ClientWithMiddleware {
166169
```
167170

168171
[crates-badge]: https://img.shields.io/crates/v/ddtrace.svg
172+
169173
[docs-badge]: https://docs.rs/ddtrace/badge.svg

0 commit comments

Comments
 (0)