Skip to content

Commit

Permalink
fix: apply fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fhsgoncalves committed Dec 17, 2024
1 parent 2a963af commit dc86d83
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/axum/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use std::{
task::{Context, Poll},
};
use tower::{Layer, Service};
use tracing::field::Empty;
use tracing::Span;
use tracing_opentelemetry_instrumentation_sdk::http as otel_http;

Expand Down
16 changes: 8 additions & 8 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl From<SpanId> for DatadogId {
}

fn lookup_trace_info<S>(span_ref: &SpanRef<S>) -> Option<TraceInfo>
where
S: Subscriber + for<'a> LookupSpan<'a>,
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
span_ref.extensions().get::<OtelData>().map(|o| {
let trace_id = if o.parent_cx.has_active_span() {
Expand All @@ -62,18 +62,18 @@ fn lookup_trace_info<S>(span_ref: &SpanRef<S>) -> Option<TraceInfo>
pub struct DatadogFormatter;

impl<S, N> FormatEvent<S, N> for DatadogFormatter
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &Event<'_>,
) -> std::fmt::Result
where
S: Subscriber + for<'a> LookupSpan<'a>,
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
let meta = event.metadata();

Expand Down Expand Up @@ -159,4 +159,4 @@ mod tests {

assert_eq!(datadog_id.0, 6359193864645272721);
}
}
}
32 changes: 20 additions & 12 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use std::env;
use crate::formatter::DatadogFormatter;
use crate::shutdown::TracerShutdown;
use crate::tracer::build_tracer;
use opentelemetry::trace::TraceError;
use std::env;
use tracing::Subscriber;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::{EnvFilter, Layer, Registry};
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::util::SubscriberInitExt;
use crate::formatter::DatadogFormatter;
use crate::shutdown::TracerShutdown;
use crate::tracer::build_tracer;
use tracing_subscriber::{EnvFilter, Layer, Registry};

fn loglevel_filter_layer(dd_enabled: bool) -> EnvFilter {
let log_level = env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());

// `axum_tracing_opentelemetry` should be a level info to emit opentelemetry trace & span
let axum_tracing_log_level = env::var("AXUM_TRACING_LOG_LEVEL").unwrap_or_else(|_| if dd_enabled { "trace".to_string() } else { "off".to_string() });
let axum_tracing_log_level = env::var("AXUM_TRACING_LOG_LEVEL").unwrap_or_else(|_| {
if dd_enabled {
"trace".to_string()
} else {
"off".to_string()
}
});

// `otel::setup` set to debug to log detected resources, configuration read and infered
let otel_log_level = env::var("OTEL_LOG_LEVEL").unwrap_or_else(|_| "debug".to_string());
Expand All @@ -27,9 +33,12 @@ fn loglevel_filter_layer(dd_enabled: bool) -> EnvFilter {
EnvFilter::from_default_env()
}

fn log_layer<S>(dd_enabled: bool, non_blocking: NonBlocking) -> Box<dyn Layer<S> + Send + Sync + 'static>
where
S: Subscriber + for<'a> LookupSpan<'a>,
fn log_layer<S>(
dd_enabled: bool,
non_blocking: NonBlocking,
) -> Box<dyn Layer<S> + Send + Sync + 'static>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
if dd_enabled {
Box::new(
Expand All @@ -44,7 +53,6 @@ fn log_layer<S>(dd_enabled: bool, non_blocking: NonBlocking) -> Box<dyn Layer<S>
}

pub fn init() -> Result<(WorkerGuard, TracerShutdown), TraceError> {

let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());

let dd_enabled = env::var("DD_ENABLED").map(|s| s == "true").unwrap_or(false);
Expand All @@ -62,5 +70,5 @@ pub fn init() -> Result<(WorkerGuard, TracerShutdown), TraceError> {
.with(telemetry_layer)
.init();

Ok((guard, TracerShutdown{}))
Ok((guard, TracerShutdown {}))
}
12 changes: 6 additions & 6 deletions src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
//! to send traces to the Datadog agent in batches over gRPC.
//!
//! It also contains a convenience function to build a layer with the tracer.
use std::env;
use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, Tracer};
use opentelemetry_sdk::trace;
use opentelemetry::global;
use std::time::Duration;
pub use opentelemetry::trace::{TraceError, TraceId, TraceResult};
use opentelemetry_datadog::{ApiVersion, DatadogPropagator};
use opentelemetry_sdk::trace;
use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, Tracer};
use std::env;
use std::time::Duration;
use tracing::Subscriber;
use tracing_opentelemetry::{OpenTelemetryLayer, PreSampledTracer};
use tracing_subscriber::registry::LookupSpan;
Expand All @@ -19,9 +19,9 @@ pub fn build_tracer() -> TraceResult<Tracer> {
let service_name = env::var("DD_SERVICE")
.map_err(|_| <&str as Into<TraceError>>::into("missing DD_SERVICE"))?;


let dd_host = env::var("DD_AGENT_HOST").unwrap_or("localhost".to_string());
let dd_port = env::var("DD_AGENT_PORT").ok()
let dd_port = env::var("DD_AGENT_PORT")
.ok()
.and_then(|it| it.parse::<i32>().ok())
.unwrap_or(8126);

Expand Down

0 comments on commit dc86d83

Please sign in to comment.