Skip to content

Commit

Permalink
editoast: setup tracing with opentelemetry for TestApp
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Dec 24, 2024
1 parent 5af6621 commit 5c49d18
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
10 changes: 10 additions & 0 deletions editoast/src/client/telemetry_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ pub struct TelemetryConfig {
pub telemetry_endpoint: Url,
}

#[cfg(test)]
impl From<crate::views::test_app::OpentelemetryConfig> for TelemetryConfig {
fn from(_value: crate::views::test_app::OpentelemetryConfig) -> Self {
Self {
telemetry_kind: TelemetryKind::Opentelemetry,
..Default::default()
}
}
}

#[derive(Default, ValueEnum, Debug, Derivative, Clone, strum::Display)]
#[strum(serialize_all = "lowercase")]
pub enum TelemetryKind {
Expand Down
42 changes: 30 additions & 12 deletions editoast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ use client::user::UserCommand;
use client::Client;
use client::Color;
use client::Commands;
use client::TelemetryConfig;
use editoast_models::DbConnectionPoolV2;
use models::RollingStockModel;
use opentelemetry::trace::TracerProvider as _;
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 tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;
pub use views::AppState;

use models::prelude::*;
Expand All @@ -50,9 +55,6 @@ use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;
use tracing::error;
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt as _;
use tracing_subscriber::Layer as _;
pub use valkey_utils::ValkeyClient;
pub use valkey_utils::ValkeyConnection;

Expand All @@ -66,12 +68,23 @@ pub use valkey_utils::ValkeyConnection;
/// - we *expect* a webserver to output logging information, so since it's an expected
/// output (and not extra information), it should be on stdout
#[derive(Debug, PartialEq)]
enum EditoastMode {
pub enum EditoastMode {
Webservice,
Cli,
}

fn init_tracing(mode: EditoastMode, telemetry_config: &client::TelemetryConfig) {
#[cfg(test)]
impl From<views::test_app::WebserviceMode> for EditoastMode {
fn from(_value: views::test_app::WebserviceMode) -> Self {
Self::Webservice
}
}

pub fn create_tracing_subscriber<T: SpanExporter + 'static>(
mode: EditoastMode,
telemetry_config: TelemetryConfig,
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())
Expand All @@ -89,11 +102,6 @@ fn init_tracing(mode: EditoastMode, telemetry_config: &client::TelemetryConfig)
let telemetry_layer = match telemetry_config.telemetry_kind {
client::TelemetryKind::None => None,
client::TelemetryKind::Opentelemetry => {
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint(telemetry_config.telemetry_endpoint.as_str())
.build()
.expect("failed to build a span exporter");
let resource = Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
telemetry_config.service_name.clone(),
Expand All @@ -116,11 +124,11 @@ fn init_tracing(mode: EditoastMode, telemetry_config: &client::TelemetryConfig)
Some(layer)
}
};

tracing_subscriber::registry()
.with(telemetry_layer)
.with(env_filter_layer)
.with(fmt_layer)
.init();
}

impl EditoastMode {
Expand Down Expand Up @@ -151,7 +159,17 @@ async fn main() {

async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
let client = Client::parse();
init_tracing(EditoastMode::from_client(&client), &client.telemetry_config);
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint(client.telemetry_config.telemetry_endpoint.as_str())
.build()
.expect("failed to build a span exporter");
create_tracing_subscriber(
EditoastMode::from_client(&client),
client.telemetry_config,
exporter,
)
.init();

let pg_config = client.postgres_config;
let db_pool =
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod train_schedule;
pub mod work_schedules;

#[cfg(test)]
mod test_app;
pub mod test_app;

use ::core::str;
use std::collections::HashSet;
Expand Down
31 changes: 22 additions & 9 deletions editoast/src/views/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ use axum_tracing_opentelemetry::middleware::OtelAxumLayer;
use dashmap::DashMap;
use editoast_models::DbConnectionPoolV2;
use editoast_osrdyne_client::OsrdyneClient;
use futures::future::BoxFuture;
use opentelemetry_sdk::export::trace::ExportResult;
use opentelemetry_sdk::export::trace::SpanData;
use opentelemetry_sdk::export::trace::SpanExporter;
use serde::de::DeserializeOwned;
use tower_http::trace::TraceLayer;
use url::Url;

use crate::{
core::{mocking::MockingClient, CoreClient},
create_tracing_subscriber,
generated_data::speed_limit_tags_config::SpeedLimitTagIds,
infra_cache::InfraCache,
map::MapLayers,
Expand All @@ -26,6 +31,15 @@ use axum_test::TestServer;

use super::{authentication_middleware, CoreConfig, OsrdyneConfig, PostgresConfig, ServerConfig};

#[derive(Debug)]
pub struct NoopSpanExporter;

impl SpanExporter for NoopSpanExporter {
fn export(&mut self, _: Vec<SpanData>) -> BoxFuture<'static, ExportResult> {
Box::pin(std::future::ready(Ok(())))
}
}

/// A builder interface for [TestApp]
///
/// It allows configuring some parameters for the app service.
Expand Down Expand Up @@ -106,15 +120,11 @@ impl TestAppBuilder {
};

// Setup tracing
let sub = tracing_subscriber::fmt()
.pretty()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing_subscriber::filter::LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
.finish();
let sub = create_tracing_subscriber(
WebserviceMode.into(),
OpentelemetryConfig.into(),
NoopSpanExporter,
);
let tracing_guard = tracing::subscriber::set_default(sub);

// Config valkey
Expand Down Expand Up @@ -180,6 +190,9 @@ impl TestAppBuilder {
}
}

pub(crate) struct WebserviceMode;
pub(crate) struct OpentelemetryConfig;

/// Wraps an underlying, fully configured, actix service
///
/// It also holds a reference to the database connection pool and the core client,
Expand Down

0 comments on commit 5c49d18

Please sign in to comment.