Skip to content

Commit

Permalink
have log and tracing from otel global context
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jan 16, 2024
1 parent a3befe7 commit 741e17e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tracing-subscriber = { version = "0.3.18", default-features = false, features =
"env-filter",
"fmt",
"json",
"ansi"
] }
serde = { version = "1.0.195", features = ["derive"], optional = true }
serde_json = { version = "1.0.111", optional = true }
Expand Down
53 changes: 40 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// which is licensed under CC0 1.0 Universal
// https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk/blob/d3609ac2cc699d3a24fbf89754053cc8e938e3bf/LICENSE

use std::cmp::max;

use opentelemetry_sdk::{
propagation::{
BaggagePropagator, TextMapCompositePropagator, TraceContextPropagator,
},
resource::{OsResourceDetector, ResourceDetector},
Resource,
};
use std::cmp::max;
use tracing::Subscriber;
use tracing_subscriber::fmt::writer::MakeWriterExt;

use opentelemetry::{propagation::TextMapPropagator, trace::TraceError};
use tracing_subscriber::{
filter::LevelFilter,
fmt::{format::FmtSpan, writer::MakeWriterExt},
layer::SubscriberExt,
filter::LevelFilter, fmt::format::FmtSpan, layer::SubscriberExt,
registry::LookupSpan, Layer,
};

pub mod middleware;
Expand Down Expand Up @@ -109,6 +109,32 @@ impl ResourceDetector for ServiceInfoDetector {
}
}

pub fn build_logger_text<S>(
log_level: tracing::Level,
) -> Box<dyn Layer<S> + Send + Sync + 'static>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
if cfg!(debug_assertions) {
Box::new(
tracing_subscriber::fmt::layer()
.pretty()
.with_line_number(true)
.with_thread_names(true)
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_timer(tracing_subscriber::fmt::time::uptime()),
)
} else {
Box::new(
tracing_subscriber::fmt::layer()
.json()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_writer(std::io::stdout.with_max_level(log_level)),
)
}
}

pub fn init_tracing_with_fallbacks(
log_level: tracing::Level,
fallback_service_name: &'static str,
Expand All @@ -119,17 +145,18 @@ pub fn init_tracing_with_fallbacks(
let otel_tracer =
otlp::init_tracer(otel_rsrc, otlp::identity).expect("setup of Tracer");
init_propagator().expect("setup of propagator");
let otel_layer = tracing_opentelemetry::layer().with_tracer(otel_tracer);

let fmt_layer = tracing_subscriber::fmt::layer()
.json()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_writer(std::io::stdout.with_max_level(log_level));

let level_filter: LevelFilter = max(log_level, tracing::Level::INFO).into();
let subscriber = tracing_subscriber::registry()
.with(fmt_layer)
.with(level_filter)
.with(build_logger_text(log_level));
let _guard = tracing::subscriber::set_default(subscriber);
tracing::info!("init logging & tracing");

let otel_layer = tracing_opentelemetry::layer().with_tracer(otel_tracer);

let subscriber = tracing_subscriber::registry()
.with(build_logger_text(log_level))
.with(otel_layer)
.with(level_filter);
tracing::subscriber::set_global_default(subscriber).unwrap();
Expand Down

0 comments on commit 741e17e

Please sign in to comment.