You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there have been struggling to get trace/span ids into logging for grafana from traces and saw your example in this issue: tokio-rs/tracing#1531
I could not get the custom json formatter to work with my existing setup and think I might have made some mistakes in my setup would love to get your thoughts as you seem to understand this much better than I do!
This is where I do most of the setting up and have copied your formatter exactly.
use opentelemetry::{propagation::TextMapCompositePropagator, trace::{FutureExt, TraceError}, KeyValue};
use opentelemetry_otlp::{ExportConfig, Protocol};
use opentelemetry_sdk::{
propagation::{BaggagePropagator, TraceContextPropagator},
trace, Resource, logs
};
use opentelemetry_semantic_conventions::resource;
use tracing::{info, subscriber, Subscriber};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, registry::LookupSpan, Layer};
use crate::tracing_funcs::log_format::Json;
pub fn init_propagator() -> Result<(), TraceError> {
let composite_propagator = TextMapCompositePropagator::new(vec![
Box::new(BaggagePropagator::new()),
Box::new(TraceContextPropagator::new()),
]);
opentelemetry::global::set_text_map_propagator(composite_propagator);
Ok(())
}
fn init_trace_pipeline(export_config: ExportConfig, resource: Resource) -> Result<trace::Tracer, TraceError> {
use opentelemetry_otlp::WithExportConfig;
let span_exporter = opentelemetry_otlp::new_exporter()
.http()
.with_export_config(export_config);
let pipeline = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(span_exporter)
.with_trace_config(
trace::config()
.with_resource(resource)
.with_sampler(trace::Sampler::AlwaysOn)
.with_id_generator(trace::RandomIdGenerator::default()),
);
pipeline.install_batch(opentelemetry_sdk::runtime::Tokio)
}
fn build_otlp_layer<S>() -> Result<OpenTelemetryLayer<S, trace::Tracer>, TraceError>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
let export_config = create_exporter_config();
let resource = create_resource();
let otlp_tracer = init_trace_pipeline(export_config, resource)?;
init_propagator()?;
Ok(tracing_opentelemetry::layer()
.with_error_records_to_exceptions(true)
.with_tracer(otlp_tracer))
}
pub fn init_subscriber() -> Result<(), axum::BoxError> {
let subscriber = tracing_subscriber::fmt::layer()
.json()
.with_current_span(true)
.event_format(Json);
let register = tracing_subscriber::registry()
.with(build_otlp_layer()?)
.with(build_loglevel_filter_layer())
.with(subscriber);
info!("started otlp logging & tracing");
tracing::subscriber::set_global_default(register)?;
Ok(())
}
pub fn build_loglevel_filter_layer() -> tracing_subscriber::filter::EnvFilter {
std::env::set_var(
"RUST_LOG",
format!(
"{},otel::tracing=trace,otel=debug",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var("OTEL_LOG_LEVEL"))
.unwrap_or_else(|_| "info".to_string())
),
);
EnvFilter::from_default_env()
}
fn create_resource() -> Resource {
let service_name = std::env::var("APPLICATION_NAME").unwrap_or("".into());
let pod_namespace = std::env::var("POD_NAMESPACE").unwrap_or("".into());
let host_name = std::env::var("HOSTNAME").unwrap_or("".into());
Resource::new(vec![
KeyValue::new("compose_service", "service"),
KeyValue::new(resource::SERVICE_NAME, service_name),
KeyValue::new("pod.namespace", pod_namespace),
KeyValue::new("pod.hostname", host_name),
KeyValue::new(resource::TELEMETRY_SDK_LANGUAGE, "rust"),
KeyValue::new(resource::TELEMETRY_SDK_NAME, "opentelemetry"),
KeyValue::new(resource::TELEMETRY_SDK_VERSION, "0.22.1"),
])
}
fn create_exporter_config() -> ExportConfig {
ExportConfig {
endpoint: "endpoint".to_string(),
protocol: Protocol::HttpBinary,
..Default::default()
}
}
The text was updated successfully, but these errors were encountered:
Hi there have been struggling to get trace/span ids into logging for grafana from traces and saw your example in this issue: tokio-rs/tracing#1531
I could not get the custom json formatter to work with my existing setup and think I might have made some mistakes in my setup would love to get your thoughts as you seem to understand this much better than I do!
This is where I do most of the setting up and have copied your formatter exactly.
The text was updated successfully, but these errors were encountered: