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: home <[email protected]>
  • Loading branch information
home committed Dec 18, 2024
1 parent 5af6621 commit c361ea8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
18 changes: 18 additions & 0 deletions editoast/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 editoast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ opentelemetry-otlp = { version = "0.27.0", default-features = false, features =
"trace",
] }
opentelemetry-semantic-conventions.workspace = true
opentelemetry-stdout = { version = "0.27.0" }
opentelemetry_sdk = { version = "0.27.1", features = ["rt-tokio", "trace"] }
ordered-float = { version = "4.5.0", features = ["serde"] }
osm_to_railjson = { path = "./osm_to_railjson" }
Expand Down
30 changes: 20 additions & 10 deletions editoast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use client::Commands;
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;
Expand Down Expand Up @@ -66,12 +67,16 @@ 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) {
pub fn init_tracing<T: SpanExporter + 'static>(
mode: EditoastMode,
telemetry_config: &client::TelemetryConfig,
exporter: T,
) {
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 +94,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 +116,12 @@ fn init_tracing(mode: EditoastMode, telemetry_config: &client::TelemetryConfig)
Some(layer)
}
};
tracing_subscriber::registry()

let _ = tracing_subscriber::registry()
.with(telemetry_layer)
.with(env_filter_layer)
.with(fmt_layer)
.init();
.try_init();
}

impl EditoastMode {
Expand Down Expand Up @@ -151,7 +152,16 @@ 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");
init_tracing(
EditoastMode::from_client(&client),
&client.telemetry_config,
exporter,
);

let pg_config = client.postgres_config;
let db_pool =
Expand Down
23 changes: 9 additions & 14 deletions editoast/src/views/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use tower_http::trace::TraceLayer;
use url::Url;

use crate::{
client::{TelemetryConfig, TelemetryKind},
core::{mocking::MockingClient, CoreClient},
generated_data::speed_limit_tags_config::SpeedLimitTagIds,
infra_cache::InfraCache,
init_tracing,
map::MapLayers,
valkey_utils::ValkeyConfig,
AppState, ValkeyClient,
AppState, EditoastMode, ValkeyClient,
};
use axum_test::TestRequest;
use axum_test::TestServer;
Expand Down Expand Up @@ -106,16 +108,12 @@ 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 tracing_guard = tracing::subscriber::set_default(sub);
let exporter = opentelemetry_stdout::SpanExporter::default();
let telemety_config = TelemetryConfig {
telemetry_kind: TelemetryKind::Opentelemetry,
..Default::default()
};
init_tracing(EditoastMode::Webservice, &telemety_config, exporter);

// Config valkey
let valkey = ValkeyClient::new(config.valkey_config.clone())
Expand Down Expand Up @@ -175,7 +173,6 @@ impl TestAppBuilder {
server,
db_pool: db_pool_v2,
core_client,
tracing_guard,
}
}
}
Expand All @@ -188,8 +185,6 @@ pub(crate) struct TestApp {
server: TestServer,
db_pool: Arc<DbConnectionPoolV2>,
core_client: Arc<CoreClient>,
#[allow(unused)] // included here to extend its lifetime, not meant to be used in any way
tracing_guard: tracing::subscriber::DefaultGuard,
}

impl TestApp {
Expand Down

0 comments on commit c361ea8

Please sign in to comment.