chore: clean up tracing subscriber setup
Some checks failed
ci/woodpecker/push/ci Pipeline failed

This commit is contained in:
Daniel Lundin 2024-12-25 00:11:50 +01:00
parent b1f701ddf2
commit 706b6787d3
Signed by: dln
SSH key fingerprint: SHA256:dQy1Xj3UiqJYpKR5ggQ2bxgz4jCH8IF+k3AB8o0kmdI
2 changed files with 43 additions and 27 deletions

View file

@ -6,12 +6,15 @@ mod patagia_api;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {}
struct Cli {
#[arg(long = "telemetry-otlp-endpoint", default_value = "http://localhost:4317", value_name = "OTEL_EXPORTER_OTLP_ENDPOINT")]
otlp_endpoint: Option<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
let _args = Cli::parse();
let _tracing = instrumentation::init_tracing_subscriber()?;
let args = Cli::parse();
let _tracing = instrumentation::init_tracing(args.otlp_endpoint.as_ref())?;
tracing::info!("Patagia Agent");

View file

@ -4,6 +4,7 @@ use opentelemetry::{trace::TracerProvider as _, KeyValue};
use opentelemetry_otlp::{WithExportConfig, WithTonicConfig};
use opentelemetry_sdk::{
metrics::{MeterProviderBuilder, PeriodicReader, SdkMeterProvider},
propagation::TraceContextPropagator,
runtime,
trace::{RandomIdGenerator, Sampler, TracerProvider},
Resource,
@ -74,32 +75,44 @@ fn init_tracer_provider(otel_endpoint: &String) -> Result<TracerProvider> {
// Initialize tracing-subscriber and return TracingGuard for opentelemetry-related termination processing
pub fn init_tracing(otel_endpoint: Option<&String>) -> Result<TracingGuard> {
let sub = tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(tracing_subscriber::fmt::layer());
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(""));
match otel_endpoint {
None => {
sub.init();
Ok(TracingGuard {
meter_provider: None,
tracer_provider: None,
})
}
let fmt_layer = tracing_subscriber::fmt::layer().with_writer(std::io::stderr);
Some(otel_endpoint) => {
let meter_provider = init_meter_provider(otel_endpoint)?;
let tracer_provider = init_tracer_provider(otel_endpoint)?;
let tracer = tracer_provider.tracer("tracing-otel-subscriber");
sub.with(MetricsLayer::new(meter_provider.clone()))
.with(OpenTelemetryLayer::new(tracer))
.init();
Ok(TracingGuard {
meter_provider: Some(meter_provider),
tracer_provider: Some(tracer_provider),
})
}
}
let meter_provider = match otel_endpoint {
Some(endpoint) => Some(init_meter_provider(endpoint)?),
None => None,
};
let metrics_layer = match meter_provider {
Some(ref p) => Some(MetricsLayer::new(p.to_owned())),
None => None,
};
let tracer_provider = match otel_endpoint {
Some(endpoint) => Some(init_tracer_provider(endpoint)?),
None => None,
};
let trace_layer = match tracer_provider {
Some(ref p) => Some(OpenTelemetryLayer::new(p.tracer("tracing-otel-subscriber"))),
None => None,
};
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer)
.with(metrics_layer)
.with(trace_layer)
.init();
Ok(TracingGuard {
meter_provider,
tracer_provider,
})
}
pub struct TracingGuard {