From 00af7af4d6b09576d28de0b65f425e60f8cdc9aa Mon Sep 17 00:00:00 2001 From: Pavel Procopiuc Date: Mon, 2 Dec 2024 11:54:08 +0100 Subject: [PATCH] Reorganize otel to reduce code duplication --- Makefile.toml | 232 +++++++++----------------------------------- src/request/otel.rs | 109 +++++++-------------- 2 files changed, 80 insertions(+), 261 deletions(-) diff --git a/Makefile.toml b/Makefile.toml index fce27ea..8867c11 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -1,6 +1,17 @@ [config] skip_core_tasks = true +[env] +AVAILABLE_OTEL_FEATURES = [ + "tracing_opentelemetry_0_21", + "tracing_opentelemetry_0_22", + "tracing_opentelemetry_0_23", + "tracing_opentelemetry_0_24", + "tracing_opentelemetry_0_25", + "tracing_opentelemetry_0_26", + "tracing_opentelemetry_0_27", +] + [tasks.build] description = "Runs the rust compiler." category = "Build" @@ -21,13 +32,7 @@ args = ["fmt", "--", "--check"] dependencies = [ "test-base", "test-auth0", - "test-otel-0_21", - "test-otel-0_22", - "test-otel-0_23", - "test-otel-0_24", - "test-otel-0_25", - "test-otel-0_26", - "test-otel-0_27", + "test-all-otel-versions", ] [tasks.test-base] @@ -40,58 +45,23 @@ command = "cargo" args = ["test", "--features=auth0,gzip", "${@}"] dependencies = ["build"] -[tasks.test-otel-0_21] -command = "cargo" -args = [ - "test", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_21", -] - -[tasks.test-otel-0_22] -command = "cargo" -args = [ - "test", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_22", -] - -[tasks.test-otel-0_23] -command = "cargo" -args = [ - "test", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_23", -] - -[tasks.test-otel-0_24] -command = "cargo" -args = [ - "test", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_24", -] +[tasks.test-all-otel-versions] +script = """#!@duckscript -[tasks.test-otel-0_25] -command = "cargo" -args = [ - "test", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_25", -] +available_otel_features = split ${AVAILABLE_OTEL_FEATURES} ";" +for otel_feature in ${available_otel_features} + set_env otel_feature ${otel_feature} + cm_run_task "test-otel-version-from-env" +end +""" -[tasks.test-otel-0_26] +[tasks.test-otel-version-from-env] command = "cargo" args = [ "test", "--no-default-features", "--features", - "tracing_opentelemetry_0_26", + "${otel_feature}", ] [tasks.test-otel-0_27] @@ -107,13 +77,7 @@ args = [ dependencies = [ "clippy-base", "clippy-auth0", - "clippy-otel-0_21", - "clippy-otel-0_22", - "clippy-otel-0_23", - "clippy-otel-0_24", - "clippy-otel-0_25", - "clippy-otel-0_26", - "clippy-otel-0_27", + "clippy-all-otel-versions", ] [tasks.clippy-base] @@ -133,78 +97,23 @@ args = [ ] dependencies = ["build"] -[tasks.clippy-otel-0_21] -command = "cargo" -args = [ - "clippy", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_21", - "--all-targets", - "--", - "-D", - "warnings", -] +[tasks.clippy-all-otel-versions] +script = """#!@duckscript -[tasks.clippy-otel-0_22] -command = "cargo" -args = [ - "clippy", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_22", - "--all-targets", - "--", - "-D", - "warnings", -] +available_otel_features = split ${AVAILABLE_OTEL_FEATURES} ";" +for otel_feature in ${available_otel_features} + set_env otel_feature ${otel_feature} + cm_run_task "clippy-otel-version-from-env" +end +""" -[tasks.clippy-otel-0_23] +[tasks.clippy-otel-version-from-env] command = "cargo" args = [ "clippy", "--no-default-features", "--features", - "tracing_opentelemetry_0_23", - "--all-targets", - "--", - "-D", - "warnings", -] - -[tasks.clippy-otel-0_24] -command = "cargo" -args = [ - "clippy", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_24", - "--all-targets", - "--", - "-D", - "warnings", -] - -[tasks.clippy-otel-0_25] -command = "cargo" -args = [ - "clippy", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_25", - "--all-targets", - "--", - "-D", - "warnings", -] - -[tasks.clippy-otel-0_26] -command = "cargo" -args = [ - "clippy", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_26", + "${otel_feature}", "--all-targets", "--", "-D", @@ -226,80 +135,29 @@ args = [ [tasks.deny-check] dependencies = [ - "deny-check-otel-0_21", - "deny-check-otel-0_22", - "deny-check-otel-0_23", - "deny-check-otel-0_24", - "deny-check-otel-0_25", - "deny-check-otel-0_26", - "deny-check-otel-0_27", + "deny-check-all-otel-versions", ] -[tasks.deny-check-otel-0_21] -args = [ - "deny", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_21", - "check", -] -command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_21 feature" +[tasks.deny-check-all-otel-versions] +script = """#!@duckscript -[tasks.deny-check-otel-0_22] -args = [ - "deny", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_22", - "check", -] -command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_22 feature" +available_otel_features = split ${AVAILABLE_OTEL_FEATURES} ";" +for otel_feature in ${available_otel_features} + set_env otel_feature ${otel_feature} + cm_run_task "deny-check-otel-version-from-env" +end +""" -[tasks.deny-check-otel-0_23] -args = [ - "deny", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_23", - "check", -] +[tasks.deny-check-otel-version-from-env] command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_23 feature" - -[tasks.deny-check-otel-0_24] args = [ "deny", "--no-default-features", "--features", - "tracing_opentelemetry_0_24", + "${otel_feature}", "check", ] -command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_24 feature" - -[tasks.deny-check-otel-0_25] -args = [ - "deny", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_25", - "check", -] -command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_25 feature" - -[tasks.deny-check-otel-0_26] -args = [ - "deny", - "--no-default-features", - "--features", - "tracing_opentelemetry_0_26", - "check", -] -command = "cargo" -description = "Run cargo-deny with tracing_opentelemetry_0_26 feature" +description = "Run cargo-deny with ${otel_feature} feature" [tasks.deny-check-otel-0_27] args = [ diff --git a/src/request/otel.rs b/src/request/otel.rs index 2d647fa..0a1bd6d 100644 --- a/src/request/otel.rs +++ b/src/request/otel.rs @@ -1,97 +1,58 @@ #[cfg(feature = "tracing_opentelemetry_0_21")] -mod otel_0_21 { - pub use opentelemetry_0_21_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_21_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_22_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_21_pkg as opentelemetry; + pub use opentelemetry_sdk_0_21_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_22_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_22")] -mod otel_0_22 { - pub use opentelemetry_0_22_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_22_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_23_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_22_pkg as opentelemetry; + pub use opentelemetry_sdk_0_22_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_23_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_23")] -mod otel_0_23 { - pub use opentelemetry_0_23_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_23_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_24_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_23_pkg as opentelemetry; + pub use opentelemetry_sdk_0_23_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_24_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_24")] -mod otel_0_24 { - pub use opentelemetry_0_24_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_24_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_25_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_24_pkg as opentelemetry; + pub use opentelemetry_sdk_0_24_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_25_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_25")] -mod otel_0_25 { - pub use opentelemetry_0_25_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_25_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_26_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_25_pkg as opentelemetry; + pub use opentelemetry_sdk_0_25_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_26_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_26")] -mod otel_0_26 { - pub use opentelemetry_0_26_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_26_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_27_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_26_pkg as opentelemetry; + pub use opentelemetry_sdk_0_26_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_27_pkg as tracing_opentelemetry; } #[cfg(feature = "tracing_opentelemetry_0_27")] -mod otel_0_27 { - pub use opentelemetry_0_27_pkg::propagation::{Injector, TextMapPropagator}; - pub use opentelemetry_sdk_0_27_pkg::propagation::TraceContextPropagator; - pub use tracing_opentelemetry_0_28_pkg::OpenTelemetrySpanExt; - - pub fn inject_context(injector: &mut dyn Injector) { - TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); - } +mod otel_crates { + pub use opentelemetry_0_27_pkg as opentelemetry; + pub use opentelemetry_sdk_0_27_pkg as opentelemetry_sdk; + pub use tracing_opentelemetry_0_28_pkg as tracing_opentelemetry; } -#[cfg(feature = "tracing_opentelemetry_0_21")] -pub use otel_0_21::inject_context; +use otel_crates::*; -#[cfg(feature = "tracing_opentelemetry_0_22")] -pub use otel_0_22::inject_context; +use opentelemetry::propagation::{Injector, TextMapPropagator}; +use opentelemetry_sdk::propagation::TraceContextPropagator; +use tracing_opentelemetry::OpenTelemetrySpanExt; -#[cfg(feature = "tracing_opentelemetry_0_23")] -pub use otel_0_23::inject_context; - -#[cfg(feature = "tracing_opentelemetry_0_24")] -pub use otel_0_24::inject_context; - -#[cfg(feature = "tracing_opentelemetry_0_25")] -pub use otel_0_25::inject_context; - -#[cfg(feature = "tracing_opentelemetry_0_26")] -pub use otel_0_26::inject_context; - -#[cfg(feature = "tracing_opentelemetry_0_27")] -pub use otel_0_27::inject_context; +pub fn inject_context(injector: &mut dyn Injector) { + TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector); +}