-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: setup tracing with opentelemetry for TestApp
Signed-off-by: hamz2a <[email protected]>
- Loading branch information
Showing
9 changed files
with
513 additions
and
564 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::time::Duration; | ||
|
||
use derivative::Derivative; | ||
use opentelemetry::trace::TracerProvider; | ||
use opentelemetry::KeyValue; | ||
use opentelemetry_sdk::export::trace::SpanExporter; | ||
use opentelemetry_sdk::propagation::TraceContextPropagator; | ||
use opentelemetry_sdk::resource::EnvResourceDetector; | ||
use opentelemetry_sdk::resource::SdkProvidedResourceDetector; | ||
use opentelemetry_sdk::resource::TelemetryResourceDetector; | ||
use opentelemetry_sdk::Resource; | ||
use tracing_subscriber::layer::SubscriberExt; | ||
use tracing_subscriber::Layer; | ||
use url::Url; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum Stream { | ||
Stderr, | ||
Stdout, | ||
} | ||
|
||
#[derive(Debug, Derivative)] | ||
#[derivative(Default)] | ||
pub struct Telemetry { | ||
#[derivative(Default(value = r#""osrd-editoast".into()"#))] | ||
pub service_name: String, | ||
#[derivative(Default(value = r#"Url::parse("http://localhost:4317").unwrap()"#))] | ||
pub endpoint: Url, | ||
} | ||
|
||
pub struct TracingConfig { | ||
pub stream: Stream, | ||
pub telemetry: Option<Telemetry>, | ||
} | ||
|
||
impl Default for TracingConfig { | ||
fn default() -> Self { | ||
TracingConfig { | ||
stream: Stream::Stdout, | ||
telemetry: Some(Telemetry::default()), | ||
} | ||
} | ||
} | ||
|
||
pub fn create_tracing_subscriber<T: SpanExporter + 'static>( | ||
tracing_config: TracingConfig, | ||
exporter: T, | ||
) -> impl tracing::Subscriber { | ||
let env_filter_layer = tracing_subscriber::EnvFilter::builder() | ||
// Set the default log level to 'info' | ||
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) | ||
.from_env_lossy(); | ||
let fmt_layer = tracing_subscriber::fmt::layer() | ||
.pretty() | ||
.with_file(true) | ||
.with_line_number(false); | ||
let fmt_layer = if tracing_config.stream == Stream::Stderr { | ||
fmt_layer.with_writer(std::io::stderr).boxed() | ||
} else { | ||
fmt_layer.boxed() | ||
}; | ||
// https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#runtime-configuration-with-layers | ||
let telemetry_layer = match tracing_config.telemetry { | ||
None => None, | ||
Some(telemetry) => { | ||
let resource = Resource::new(vec![KeyValue::new( | ||
opentelemetry_semantic_conventions::resource::SERVICE_NAME, | ||
telemetry.service_name.clone(), | ||
)]) | ||
.merge(&Resource::from_detectors( | ||
Duration::from_secs(10), | ||
vec![ | ||
Box::new(SdkProvidedResourceDetector), | ||
Box::new(TelemetryResourceDetector), | ||
Box::new(EnvResourceDetector::new()), | ||
], | ||
)); | ||
let otlp_tracer = opentelemetry_sdk::trace::TracerProvider::builder() | ||
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio) | ||
.with_resource(resource) | ||
.build() | ||
.tracer("osrd-editoast"); | ||
let layer = tracing_opentelemetry::OpenTelemetryLayer::new(otlp_tracer); | ||
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new()); | ||
Some(layer) | ||
} | ||
}; | ||
|
||
tracing_subscriber::registry() | ||
.with(telemetry_layer) | ||
.with(env_filter_layer) | ||
.with(fmt_layer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters