From a4bf9493eb68ee2f5ecba41d6b435528237b0741 Mon Sep 17 00:00:00 2001 From: Mari Date: Tue, 21 Nov 2023 14:13:43 +0100 Subject: [PATCH] add compatibility for otel 0.21 --- .github/workflows/ci.yml | 21 ++++++---- autometrics/Cargo.toml | 54 ++++++++++++++++++-------- autometrics/build.rs | 6 +-- autometrics/src/lib.rs | 6 ++- autometrics/src/prometheus_exporter.rs | 26 ++++++++++--- 5 files changed, 79 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 71d3072..de5aaa2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,15 +23,21 @@ jobs: - run: cargo clippy --features=prometheus-0_13 - run: cargo clippy --features=prometheus-client-0_21 - run: cargo clippy --features=opentelemetry-0_20 + - run: cargo clippy --features=opentelemetry-0_21 # Run the tests with each of the different metrics libraries - - run: cargo test --features=prometheus-exporter - - run: cargo test --features=prometheus-exporter,metrics-0_21 - - run: cargo test --features=prometheus-exporter,prometheus-0_13 - - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing - - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_20 - - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_21 - - run: cargo test --features=prometheus-exporter,opentelemetry-0_20 + - run: cargo test --features=prometheus-exporter-otel-0_20 + - run: cargo test --features=prometheus-exporter-otel-0_21 + - run: cargo test --features=prometheus-exporter-otel-0_20,metrics-0_21 + - run: cargo test --features=prometheus-exporter-otel-0_21,metrics-0_21 + - run: cargo test --features=prometheus-exporter-otel-0_20,prometheus-0_13 + - run: cargo test --features=prometheus-exporter-otel-0_21,prometheus-0_13 + - run: cargo test --features=prometheus-exporter-otel-0_20,prometheus-client-0_21,exemplars-tracing + - run: cargo test --features=prometheus-exporter-otel-0_21,prometheus-client-0_21,exemplars-tracing + - run: cargo test --features=prometheus-exporter-otel-0_20,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_20 + - run: cargo test --features=prometheus-exporter-otel-0_20,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_21 + - run: cargo test --features=prometheus-exporter-otel-0_20,opentelemetry-0_20 + - run: cargo test --features=prometheus-exporter-otel-0_21,opentelemetry-0_21 # Build the crate using the other optional features - run: cargo build --features=metrics-0_21,custom-objective-percentile,custom-objective-latency @@ -45,6 +51,5 @@ jobs: - run: cargo build --package example-full-api - run: cargo build --package example-opentelemetry-push - # Make sure the docs can be built - run: cargo doc --all-features diff --git a/autometrics/Cargo.toml b/autometrics/Cargo.toml index 033dd2b..41c382f 100644 --- a/autometrics/Cargo.toml +++ b/autometrics/Cargo.toml @@ -15,7 +15,8 @@ readme = "README.md" [features] # Metrics backends metrics-0_21 = ["dep:metrics"] -opentelemetry-0_20 = ["opentelemetry-0-20/metrics", "dep:prometheus"] +opentelemetry-0_20 = ["dep:opentelemetry-0-20", "opentelemetry-0-20/metrics", "dep:prometheus"] +opentelemetry-0_21 = ["dep:opentelemetry-0-21", "opentelemetry-0-21/metrics", "dep:prometheus"] prometheus-0_13 = ["dep:prometheus"] prometheus-client-0_21 = ["dep:prometheus-client"] @@ -25,48 +26,59 @@ opentelemetry = ["opentelemetry-0_20"] prometheus = ["prometheus-0_13"] prometheus-client = ["prometheus-client-0_21"] exemplars-tracing-opentelemetry = ["exemplars-tracing-opentelemetry-0_20"] +prometheus-exporter = ["prometheus-exporter-otel-0_20"] # Misc -prometheus-exporter = [ +prometheus-exporter-otel-0_20 = [ + "prometheus-exporter-parent", + "dep:opentelemetry-prometheus-0-13", + "dep:opentelemetry-sdk-0-20", +] + +prometheus-exporter-otel-0_21 = [ + "prometheus-exporter-parent", + "dep:opentelemetry-prometheus-0-14", + "dep:opentelemetry-sdk-0-21", +] + +prometheus-exporter-parent = [ "http", "metrics-exporter-prometheus", - "opentelemetry-prometheus-0-13", - "opentelemetry-sdk-0-20", "dep:prometheus", "dep:prometheus-client", ] otel-push-exporter = [ - "opentelemetry-sdk-0-20", - "dep:opentelemetry-0-20", - "opentelemetry-otlp-0-20", - "opentelemetry-otlp-0-20/metrics", - "opentelemetry-otlp-0-20/tls-roots" + "dep:opentelemetry-sdk-0-21", + "dep:opentelemetry-0-21", + "dep:opentelemetry-otlp-0-21", + "opentelemetry-otlp-0-21/metrics", + "opentelemetry-otlp-0-21/tls-roots" ] otel-push-exporter-http = [ "otel-push-exporter", - "opentelemetry-otlp-0-20/http-proto" + "opentelemetry-otlp-0-21/http-proto" ] otel-push-exporter-grpc = [ "otel-push-exporter", - "opentelemetry-otlp-0-20/grpc-tonic" + "opentelemetry-otlp-0-21/grpc-tonic" ] otel-push-exporter-tokio = [ "otel-push-exporter", - "opentelemetry-sdk-0-20/rt-tokio" + "opentelemetry-sdk-0-21/rt-tokio" ] otel-push-exporter-tokio-current-thread = [ "otel-push-exporter", - "opentelemetry-sdk-0-20/rt-tokio-current-thread" + "opentelemetry-sdk-0-21/rt-tokio-current-thread" ] otel-push-exporter-async-std = [ "otel-push-exporter", - "opentelemetry-sdk-0-20/rt-async-std" + "opentelemetry-sdk-0-21/rt-async-std" ] # Exemplars @@ -77,7 +89,7 @@ exemplars-tracing-opentelemetry-0_20 = [ "dep:tracing-opentelemetry-0-20", ] exemplars-tracing-opentelemetry-0_21 = [ - "opentelemetry-0-20/trace", + "opentelemetry-0-20/trace", # yea for some reason they use otel 0.20 with tracing-opentelemetry 0.21. only 0.22 is updated w/ otel 0.21 "tracing", "dep:tracing-opentelemetry-0-21", ] @@ -94,10 +106,18 @@ spez = "0.1.2" thiserror = "1" # Used for opentelemetry feature +## The opentelemetry_api crate has been incorperated into `opentelemetry` as of v0.21 so the `package` directives are different opentelemetry-0-20 = { package = "opentelemetry_api", version = "0.20", default-features = false, optional = true } -opentelemetry-prometheus-0-13 = { package = "opentelemetry-prometheus", version = "0.13.0", optional = true } # otel 0.20 +opentelemetry-0-21 = { package = "opentelemetry", version = "0.21", default-features = false, optional = true } + +opentelemetry-prometheus-0-13 = { package = "opentelemetry-prometheus", version = "0.13", optional = true } # otel 0.20 +opentelemetry-prometheus-0-14 = { package = "opentelemetry-prometheus", version = "0.14", optional = true } # otel 0.21 + opentelemetry-sdk-0-20 = { package = "opentelemetry_sdk", version = "0.20", default-features = false, features = ["metrics"], optional = true } -opentelemetry-otlp-0-20 = { package = "opentelemetry-otlp", version = "0.13.0", default-features = false, optional = true } +opentelemetry-sdk-0-21 = { package = "opentelemetry_sdk", version = "0.21", default-features = false, features = ["metrics"], optional = true } + +opentelemetry-otlp-0-20 = { package = "opentelemetry-otlp", version = "0.13", default-features = false, optional = true } +opentelemetry-otlp-0-21 = { package = "opentelemetry-otlp", version = "0.14", default-features = false, optional = true } # Use for metrics feature metrics = { version = "0.21", default-features = false, optional = true } diff --git a/autometrics/build.rs b/autometrics/build.rs index 17ae3d0..ab5755c 100644 --- a/autometrics/build.rs +++ b/autometrics/build.rs @@ -6,7 +6,7 @@ pub fn main() { #[cfg(feature = "metrics")] println!("cargo:warning=The `metrics` feature is deprecated and will be removed in the next version. Please use `metrics-0_21` instead."); #[cfg(feature = "opentelemetry")] - println!("cargo:warning=The `opentelemetry` feature is deprecated and will be removed in the next version. Please use `opentelemetry-0_20` instead."); + println!("cargo:warning=The `opentelemetry` feature is deprecated and will be removed in the next version. Please use `opentelemetry-0_20` or `opentelemetry-0_21` instead."); #[cfg(feature = "prometheus")] println!("cargo:warning=The `prometheus` feature is deprecated and will be removed in the next version. Please use `prometheus-0_13` instead."); #[cfg(feature = "prometheus-client")] @@ -17,7 +17,7 @@ pub fn main() { cfg_aliases! { // Backends metrics: { any(feature = "metrics", feature = "metrics-0_21") }, - opentelemetry: { any(feature = "opentelemetry", feature = "opentelemetry-0_20") }, + opentelemetry: { any(feature = "opentelemetry", feature = "opentelemetry-0_20", feature="opentelemetry-0_21") }, prometheus: { any(feature = "prometheus", feature = "prometheus-0_13") }, prometheus_client_feature: { any(feature = "prometheus-client", feature = "prometheus-client-0_21") }, default_backend: { all( @@ -27,7 +27,7 @@ pub fn main() { prometheus_client: { any(prometheus_client_feature, default_backend) }, // Misc - prometheus_exporter: { feature = "prometheus-exporter" }, + prometheus_exporter: { any(feature = "prometheus-exporter", feature = "prometheus-exporter-otel-0_20", feature = "prometheus-exporter-otel-0_21") }, // Exemplars exemplars: { any(exemplars_tracing, exemplars_tracing_opentelemetry) }, diff --git a/autometrics/src/lib.rs b/autometrics/src/lib.rs index 12623af..c12eb1e 100644 --- a/autometrics/src/lib.rs +++ b/autometrics/src/lib.rs @@ -17,7 +17,11 @@ mod labels; pub mod objectives; #[cfg(feature = "otel-push-exporter")] pub mod otel_push_exporter; -#[cfg(feature = "prometheus-exporter")] +#[cfg(any( + feature = "prometheus-exporter", + feature = "prometheus-exporter-otel-0_20", + feature = "prometheus-exporter-otel-0_21" +))] pub mod prometheus_exporter; pub mod settings; mod task_local; diff --git a/autometrics/src/prometheus_exporter.rs b/autometrics/src/prometheus_exporter.rs index 4a813ec..6bc6920 100644 --- a/autometrics/src/prometheus_exporter.rs +++ b/autometrics/src/prometheus_exporter.rs @@ -27,8 +27,10 @@ use http::{header::CONTENT_TYPE, Response}; #[cfg(metrics)] use metrics_exporter_prometheus::{BuildError, PrometheusBuilder, PrometheusHandle}; use once_cell::sync::OnceCell; -#[cfg(opentelemetry)] +#[cfg(feature = "opentelemetry-0-20")] use opentelemetry_0_20::metrics::MetricsError; +#[cfg(feature = "opentelemetry-0-21")] +use opentelemetry_0_21::metrics::MetricsError; #[cfg(any(opentelemetry, prometheus))] use prometheus::TextEncoder; use thiserror::Error; @@ -210,10 +212,24 @@ fn initialize_prometheus_exporter() -> Result