Skip to content

Commit

Permalink
refactor(o11y): compose otel layer
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 6, 2024
1 parent e517716 commit 3562f3f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/synd_o11y/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub mod health_check;
pub mod opentelemetry;
pub mod tracing_subscriber;

pub use opentelemetry::OpenTelemetryGuard;
pub use tracing_subscriber::opentelemetry_layer;

/// Request id key for opentelemetry baggage
pub const REQUEST_ID_KEY: &str = "request.id";

Expand Down
22 changes: 22 additions & 0 deletions crates/synd_o11y/src/tracing_subscriber/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
use std::borrow::Cow;

use tracing::Subscriber;
use tracing_subscriber::{registry::LookupSpan, Layer};

pub mod audit;
pub mod otel_log;
pub mod otel_metrics;
pub mod otel_trace;

pub fn opentelemetry_layer<S>(
endpoint: impl Into<String>,
service_name: impl Into<Cow<'static, str>>,
service_version: impl Into<Cow<'static, str>>,
trace_sampler_ratio: f64,
) -> impl Layer<S>
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
let endpoint = endpoint.into();
let resource = crate::opentelemetry::resource(service_name, service_version);

otel_trace::layer(endpoint.clone(), resource.clone(), trace_sampler_ratio)
.and_then(otel_metrics::layer(endpoint.clone(), resource.clone()))
.and_then(otel_log::layer(endpoint, resource))
}

0 comments on commit 3562f3f

Please sign in to comment.