Skip to content

Commit cd5ae54

Browse files
committed
Add basic documentation
1 parent 0cd2063 commit cd5ae54

6 files changed

Lines changed: 57 additions & 28 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ddtrace"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
authors = ["David Steiner <david_j_steiner@yahoo.co.nz", "Fergus Strangways-Dixon <fergusdixon101@gmail.com>"]
55
edition = "2021"
66
license = "MIT"
@@ -9,6 +9,7 @@ readme = "README.md"
99

1010
[features]
1111
axum = ["dep:axum", "dep:axum-tracing-opentelemetry"]
12+
reqwest = ["dep:reqwest"]
1213

1314
[dependencies]
1415
axum = { version = "^0.6.10", optional = true }
@@ -17,6 +18,7 @@ chrono = "^0.4.24"
1718
opentelemetry = { version = "^0.18.0", features = ["rt-tokio"] }
1819
opentelemetry-datadog = "^0.6.0"
1920
opentelemetry-otlp = { version = "^0.11.0" }
21+
reqwest = { version = "^0.11.16", optional = true }
2022
serde = { version = "^1.0.156", features = ["derive"] }
2123
serde_json = "^1.0.95"
2224
tracing = "^0.1.37"

src/axum.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Axum utilities.
2+
//!
3+
//! This module re-exposes the middleware layers provided by the
4+
//! [`axum-tracing-opentelemetry`] project.
5+
//!
6+
//! [`axum-tracing-opentelemetry`]: https://github.com/davidB/axum-tracing-opentelemetry
7+
8+
pub use axum_tracing_opentelemetry::opentelemetry_tracing_layer;
9+
pub use axum_tracing_opentelemetry::opentelemetry_tracing_layer_grpc;

src/formatter.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! An event formatter to emit events in a way that Datadog can correlate them with traces.
2+
//!
3+
//! Datadog's trace ID and span ID format is different from the OpenTelemetry standard.
4+
//! Using this formatter, the trace ID is converted to the correct format.
5+
//! It also adds the trace ID to the `dd.trace_id` field and the span ID to the
6+
//! `dd.span_id` field, which is where Datadog looks for these by default
7+
//! (although the path to the trace ID can be overridden in Datadog).
18
use std::io;
29

310
use chrono::Utc;
@@ -11,9 +18,9 @@ use tracing_subscriber::fmt::format::Writer;
1118
use tracing_subscriber::fmt::{FmtContext, FormatEvent, FormatFields};
1219
use tracing_subscriber::registry::{LookupSpan, SpanRef};
1320

14-
pub struct TraceInfo {
15-
pub trace_id: String,
16-
pub span_id: String,
21+
struct TraceInfo {
22+
trace_id: String,
23+
span_id: String,
1724
}
1825

1926
fn convert_trace_id(id: TraceId) -> String {
@@ -24,14 +31,14 @@ fn convert_trace_id(id: TraceId) -> String {
2431
}
2532

2633
fn convert_span_id(id: SpanId) -> String {
27-
let dd_id = u64::from_be_bytes(id.to_bytes().try_into().unwrap());
34+
let dd_id = u64::from_be_bytes(id.to_bytes());
2835

2936
dd_id.to_string()
3037
}
3138

3239
fn lookup_trace_info<S>(span_ref: &SpanRef<S>) -> Option<TraceInfo>
33-
where
34-
S: Subscriber + for<'a> LookupSpan<'a>,
40+
where
41+
S: Subscriber + for<'a> LookupSpan<'a>,
3542
{
3643
span_ref.extensions().get::<OtelData>().map(|o| TraceInfo {
3744
trace_id: convert_trace_id(o.parent_cx.span().span_context().trace_id()),
@@ -40,21 +47,21 @@ fn lookup_trace_info<S>(span_ref: &SpanRef<S>) -> Option<TraceInfo>
4047
}
4148

4249
// mostly stolen from here: https://github.com/tokio-rs/tracing/issues/1531
43-
pub struct TraceIdFormat;
50+
pub struct DatadogFormatter;
4451

45-
impl<S, N> FormatEvent<S, N> for TraceIdFormat
46-
where
47-
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
48-
N: for<'writer> FormatFields<'writer> + 'static,
52+
impl<S, N> FormatEvent<S, N> for DatadogFormatter
53+
where
54+
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
55+
N: for<'writer> FormatFields<'writer> + 'static,
4956
{
5057
fn format_event(
5158
&self,
5259
ctx: &FmtContext<'_, S, N>,
5360
mut writer: Writer<'_>,
5461
event: &Event<'_>,
5562
) -> std::fmt::Result
56-
where
57-
S: Subscriber + for<'a> LookupSpan<'a>,
63+
where
64+
S: Subscriber + for<'a> LookupSpan<'a>,
5865
{
5966
let meta = event.metadata();
6067

@@ -81,12 +88,12 @@ impl<S, N> FormatEvent<S, N> for TraceIdFormat
8188
}
8289
}
8390

84-
pub struct WriteAdaptor<'a> {
91+
struct WriteAdaptor<'a> {
8592
fmt_write: &'a mut dyn std::fmt::Write,
8693
}
8794

8895
impl<'a> WriteAdaptor<'a> {
89-
pub fn new(fmt_write: &'a mut dyn std::fmt::Write) -> Self {
96+
fn new(fmt_write: &'a mut dyn std::fmt::Write) -> Self {
9097
Self { fmt_write }
9198
}
9299
}

src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
mod formatter;
1+
//! Utilities to integrate Rust services with Datadog using [`opentelemetry`],
2+
//! [`tracing`], and other open source libraries.
3+
//!
4+
//! This is an opinionated crate providing the building blocks for a setup that
5+
//! works with Datadog. It has been tested with services using [`axum`] hosted
6+
//! on AWS ECS, with propagation working when requests are made to other services
7+
//! using [`reqwest`].
8+
9+
#[cfg(feature = "axum")]
10+
pub mod axum;
11+
pub mod formatter;
212
mod propagator;
3-
mod tracer;
13+
pub mod tracer;
414

5-
pub use formatter::TraceIdFormat;
615
pub use propagator::set_global_propagator;
7-
pub use tracer::{build_tracer, build_layer};
8-
pub use opentelemetry::trace::{TraceError, TraceResult};
9-
10-
#[cfg(feature = "axum")]
11-
pub use axum_tracing_opentelemetry::opentelemetry_tracing_layer;

src/propagator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Sets the Datadog propagator as the global propagator.
12
pub fn set_global_propagator() {
23
let propagator = opentelemetry_datadog::DatadogPropagator::new();
34
opentelemetry::global::set_text_map_propagator(propagator);

src/tracer.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
use std::time::Duration;
2-
use opentelemetry::KeyValue;
3-
use opentelemetry::trace::{TraceResult};
4-
use opentelemetry::sdk::{Resource, trace};
1+
//! Trace and layer builders to export traces to the Datadog agent.
2+
//!
3+
//! This module contains a function that builds a tracer with an exporter
4+
//! to send traces to the Datadog agent in batches over gRPC.
5+
//!
6+
//! It also contains a convenience function to build a layer with the tracer.
57
use opentelemetry::sdk::trace::{RandomIdGenerator, Sampler, Tracer};
8+
use opentelemetry::sdk::{trace, Resource};
9+
pub use opentelemetry::trace::{TraceError, TraceResult};
10+
use opentelemetry::KeyValue;
611
use opentelemetry_otlp::WithExportConfig;
12+
use std::time::Duration;
713
use tracing::Subscriber;
814
use tracing_opentelemetry::{OpenTelemetryLayer, PreSampledTracer};
915
use tracing_subscriber::registry::LookupSpan;

0 commit comments

Comments
 (0)