Skip to content

Commit

Permalink
add compatibility for otel 0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowagain committed Nov 21, 2023
1 parent 217f35f commit a4bf949
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
21 changes: 13 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
54 changes: 37 additions & 17 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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
Expand All @@ -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",
]
Expand All @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions autometrics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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(
Expand 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) },
Expand Down
6 changes: 5 additions & 1 deletion autometrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 21 additions & 5 deletions autometrics/src/prometheus_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,10 +212,24 @@ fn initialize_prometheus_exporter() -> Result<GlobalPrometheus, ExporterInitiali

#[cfg(opentelemetry)]
{
use opentelemetry_0_20::global;
use opentelemetry_prometheus_0_13::exporter;
use opentelemetry_sdk_0_20::metrics::reader::AggregationSelector;
use opentelemetry_sdk_0_20::metrics::{Aggregation, InstrumentKind, MeterProvider};
#[cfg(feature = "opentelemetry-0-20")]
use opentelemetry_0_20 as opentelemetry;
#[cfg(feature = "opentelemetry-0-20")]
use opentelemetry_prometheus_0_13 as opentelemetry_prometheus;
#[cfg(feature = "opentelemetry-0-20")]
use opentelemetry_sdk_0_20 as opentelemetry_sdk;

#[cfg(feature = "opentelemetry-0-21")]
use opentelemetry_0_21 as opentelemetry;
#[cfg(feature = "opentelemetry-0-21")]
use opentelemetry_prometheus_0_14 as opentelemetry_prometheus;
#[cfg(feature = "opentelemetry-0-21")]
use opentelemetry_sdk_0_21 as opentelemetry_sdk;

use opentelemetry::global;
use opentelemetry_prometheus::exporter;
use opentelemetry_sdk::metrics::reader::AggregationSelector;
use opentelemetry_sdk::metrics::{Aggregation, InstrumentKind, MeterProvider};

/// A custom aggregation selector that uses the configured histogram buckets,
/// along with the other default aggregation settings.
Expand Down

0 comments on commit a4bf949

Please sign in to comment.