Skip to content

Commit

Permalink
update to opentelemetry 0.24 (#179)
Browse files Browse the repository at this point in the history
* update to opentelemetry 0.24

* replace `Unit` with the literal directly

* update feature flag

* update import

* update more otel stuff

* use the correct version in readme

* pr review

* update example also to new otel version

* cargo fmt
  • Loading branch information
mellowagain authored Jul 25, 2024
1 parent 9bc24e0 commit 12b0569
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ jobs:
- run: cargo clippy --features=metrics-0_21,prometheus-exporter
- run: cargo clippy --features=prometheus-0_13
- run: cargo clippy --features=prometheus-client-0_22
- run: cargo clippy --features=opentelemetry-0_21
- run: cargo clippy --features=opentelemetry-0_24

# 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_22,exemplars-tracing
- run: cargo test --features=prometheus-exporter,prometheus-client-0_22,exemplars-tracing-opentelemetry-0_22
- run: cargo test --features=prometheus-exporter,opentelemetry-0_21
- run: cargo test --features=prometheus-exporter,prometheus-client-0_22,exemplars-tracing-opentelemetry-0_25
- run: cargo test --features=prometheus-exporter,opentelemetry-0_24

# Build the crate using the other optional features
- run: cargo build --features=metrics-0_21,custom-objective-percentile,custom-objective-latency
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

-
### Breaking changes

- `opentelemetry` has been updated to v0.24 (#179)
New feature flags: `opentelemetry-0_24`, `exemplars-tracing-opentelemetry-0_25`
Removed feature flags: `opentelemetry-0_21`, `exemplars-tracing-opentelemetry-0_22`

**If you are using these metrics library separately in your application in addition
to Autometrics, ensure that you match the version that Autometrics uses.**

## [1.0.1](https://github.com/autometrics-dev/autometrics-rs/releases/tag/v1.0.1) - 2024-02-12

Expand Down
24 changes: 12 additions & 12 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ readme = "README.md"
[features]
# Metrics backends
metrics-0_21 = ["dep:metrics"]
opentelemetry-0_21 = ["opentelemetry/metrics", "dep:prometheus"]
opentelemetry-0_24 = ["opentelemetry/metrics", "dep:prometheus"]
prometheus-0_13 = ["dep:prometheus"]
prometheus-client-0_22 = ["dep:prometheus-client"]

# Deprecated feature flags
metrics = ["metrics-0_21"]
opentelemetry = ["opentelemetry-0_21"]
opentelemetry = ["opentelemetry-0_24"]
prometheus = ["prometheus-0_13"]
prometheus-client = ["prometheus-client-0_22"]
exemplars-tracing-opentelemetry = ["exemplars-tracing-opentelemetry-0_22"]
exemplars-tracing-opentelemetry = ["exemplars-tracing-opentelemetry-0_25"]

# Misc
prometheus-exporter = [
Expand Down Expand Up @@ -72,11 +72,11 @@ otel-push-exporter-async-std = [

# Exemplars
exemplars-tracing = ["tracing", "tracing-subscriber"]
exemplars-tracing-opentelemetry-0_22 = [
exemplars-tracing-opentelemetry-0_25 = [
"dep:opentelemetry",
"opentelemetry_sdk/trace",
"tracing",
"dep:tracing-opentelemetry-0-22",
"dep:tracing-opentelemetry",
]

# Custom objectives
Expand All @@ -91,19 +91,19 @@ spez = "0.1.2"
thiserror = "1"

# Used for opentelemetry feature
opentelemetry = { version = "0.21", default-features = false, optional = true }
opentelemetry = { version = "0.24", default-features = false, optional = true }

# Use for metrics feature
metrics = { version = "0.21", default-features = false, optional = true }

# Used for prometheus-exporter feature
http = { version = "1.0.0", optional = true }
metrics-exporter-prometheus = { version = "0.12", default-features = false, optional = true }
opentelemetry-prometheus = { version = "0.14", optional = true }
opentelemetry_sdk = { version = "0.21", default-features = false, features = [
opentelemetry-prometheus = { version = "0.17", optional = true }
opentelemetry_sdk = { version = "0.24.1", default-features = false, features = [
"metrics",
], optional = true }
opentelemetry-otlp = { version = "0.14.0", default-features = false, optional = true }
opentelemetry-otlp = { version = "0.17", default-features = false, optional = true }
prometheus = { version = "0.13", default-features = false, optional = true }

# Used for prometheus-client feature
Expand All @@ -116,15 +116,15 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
], optional = true }

# Used for exemplars-tracing-opentelemetry feature
tracing-opentelemetry-0-22 = { package = "tracing-opentelemetry", version = "0.22", default-features = false, optional = true }
tracing-opentelemetry = { version = "0.25", default-features = false, optional = true }

[dev-dependencies]
async-trait = "0.1.74"
axum = { version = "0.7.2", features = ["tokio"] }
criterion = "0.5"
http = "1.0.0"
opentelemetry = "0.21"
opentelemetry-stdout = { version = "0.2", features = ["trace"] }
opentelemetry = "0.24"
opentelemetry-stdout = { version = "0.5", features = ["trace"] }
prometheus-client = "0.22"
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
Expand Down
8 changes: 4 additions & 4 deletions autometrics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ 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_21` instead.");
println!("cargo:warning=The `opentelemetry` feature is deprecated and will be removed in the next version. Please use `opentelemetry-0_24` 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")]
println!("cargo:warning=The `prometheus-client` feature is deprecated and will be removed in the next version. Please use `prometheus-client-0_22` instead.");
#[cfg(feature = "exemplars-tracing-opentelemetry")]
println!("cargo:warning=The `exemplars-tracing-opentelemetry` feature is deprecated and will be removed in the next version. Please use `exemplars-tracing-opentelemetry-0_22` instead.");
println!("cargo:warning=The `exemplars-tracing-opentelemetry` feature is deprecated and will be removed in the next version. Please use `exemplars-tracing-opentelemetry-0_25` instead.");

cfg_aliases! {
// Backends
metrics: { any(feature = "metrics", feature = "metrics-0_21") },
opentelemetry: { any(feature = "opentelemetry", feature = "opentelemetry-0_21") },
opentelemetry: { any(feature = "opentelemetry", feature = "opentelemetry-0_24") },
prometheus: { any(feature = "prometheus", feature = "prometheus-0_13") },
prometheus_client_feature: { any(feature = "prometheus-client", feature = "prometheus-client-0_22") },
default_backend: { all(
Expand All @@ -32,7 +32,7 @@ pub fn main() {
// Exemplars
exemplars: { any(exemplars_tracing, exemplars_tracing_opentelemetry) },
exemplars_tracing: { feature = "exemplars-tracing" },
exemplars_tracing_opentelemetry: { any(feature = "exemplars-tracing-opentelemetry-0_22", feature = "exemplars-tracing-opentelemetry") },
exemplars_tracing_opentelemetry: { any(feature = "exemplars-tracing-opentelemetry-0_25", feature = "exemplars-tracing-opentelemetry") },

// Custom objectives
custom_objective_percentile: { feature = "custom-objective-percentile" },
Expand Down
4 changes: 2 additions & 2 deletions autometrics/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ If you require more customization than these offered feature flags, enable just

> If you are exporting metrics yourself rather than using the `prometheus-exporter`, you must ensure that you are using the exact same version of the metrics library as `autometrics` (and it must come from `crates.io` rather than git or another source). If not, the autometrics metrics will not appear in your exported metrics.
- `opentelemetry-0_21` - use the [opentelemetry](https://crates.io/crates/opentelemetry) crate for producing metrics.
- `opentelemetry-0_24` - use the [opentelemetry](https://crates.io/crates/opentelemetry) crate for producing metrics.
- `metrics-0_21` - use the [metrics](https://crates.io/crates/metrics) crate for producing metrics
- `prometheus-0_13` - use the [prometheus](https://crates.io/crates/prometheus) crate for producing metrics
- `prometheus-client-0_22` - use the official [prometheus-client](https://crates.io/crates/prometheus-client) crate for producing metrics
Expand All @@ -163,7 +163,7 @@ If you require more customization than these offered feature flags, enable just
See the [exemplars module docs](https://docs.rs/autometrics/latest/autometrics/exemplars/index.html) for details about these features. Currently only supported with the `prometheus-client` backend.

- `exemplars-tracing` - extract arbitrary fields from `tracing::Span`s
- `exemplars-tracing-opentelemetry-0_20` or `exemplars-tracing-opentelemetry-0_21` - extract the `trace_id` and `span_id` from the `opentelemetry::Context`, which is attached to `tracing::Span`s by the `tracing-opentelemetry` crate
- `exemplars-tracing-opentelemetry-0_25` - extract the `trace_id` and `span_id` from the `opentelemetry::Context`, which is attached to `tracing::Span`s by the `tracing-opentelemetry` crate

### Custom objective values

Expand Down
2 changes: 1 addition & 1 deletion autometrics/src/exemplars/tracing_opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::TraceLabels;
use opentelemetry::trace::TraceContextExt as _;
use std::iter::FromIterator;
use tracing::Span;
use tracing_opentelemetry_0_22::OpenTelemetrySpanExt;
use tracing_opentelemetry::OpenTelemetrySpanExt;

pub fn get_exemplar() -> Option<TraceLabels> {
// Get the OpenTelemetry Context from the tracing span
Expand Down
2 changes: 1 addition & 1 deletion autometrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod constants;
#[cfg(any(
feature = "exemplars-tracing",
feature = "exemplars-tracing-opentelemetry",
feature = "exemplars-tracing-opentelemetry-0_22",
feature = "exemplars-tracing-opentelemetry-0_25",
))]
pub mod exemplars;
mod labels;
Expand Down
8 changes: 4 additions & 4 deletions autometrics/src/otel_push_exporter.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use opentelemetry::metrics::MetricsError;
use opentelemetry_otlp::{ExportConfig, Protocol, WithExportConfig};
use opentelemetry_otlp::{OtlpMetricPipeline, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT};
use opentelemetry_sdk::metrics::MeterProvider;
use opentelemetry_sdk::metrics::SdkMeterProvider;
use std::ops::Deref;
use std::time::Duration;

/// Newtype struct holding a [`MeterProvider`] with a custom `Drop` implementation to automatically clean up itself
/// Newtype struct holding a [`SdkMeterProvider`] with a custom `Drop` implementation to automatically clean up itself
#[repr(transparent)]
#[must_use = "Assign this to a unused variable instead: `let _meter = ...` (NOT `let _ = ...`), as else it will be dropped immediately - which will cause it to be shut down"]
pub struct OtelMeterProvider(MeterProvider);
pub struct OtelMeterProvider(SdkMeterProvider);

impl Deref for OtelMeterProvider {
type Target = MeterProvider;
type Target = SdkMeterProvider;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down
10 changes: 7 additions & 3 deletions autometrics/src/prometheus_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use metrics_exporter_prometheus::{BuildError, PrometheusBuilder, PrometheusHandl
use once_cell::sync::OnceCell;
#[cfg(opentelemetry)]
use opentelemetry::metrics::MetricsError;
#[cfg(opentelemetry)]
use opentelemetry_sdk::metrics::SdkMeterProvider;
#[cfg(any(opentelemetry, prometheus))]
use prometheus::TextEncoder;
use thiserror::Error;
Expand Down Expand Up @@ -213,7 +215,7 @@ fn initialize_prometheus_exporter() -> Result<GlobalPrometheus, ExporterInitiali
use opentelemetry::global;
use opentelemetry_prometheus::exporter;
use opentelemetry_sdk::metrics::reader::AggregationSelector;
use opentelemetry_sdk::metrics::{Aggregation, InstrumentKind, MeterProvider};
use opentelemetry_sdk::metrics::{Aggregation, InstrumentKind};

/// A custom aggregation selector that uses the configured histogram buckets,
/// along with the other default aggregation settings.
Expand All @@ -228,7 +230,9 @@ fn initialize_prometheus_exporter() -> Result<GlobalPrometheus, ExporterInitiali
| InstrumentKind::UpDownCounter
| InstrumentKind::ObservableCounter
| InstrumentKind::ObservableUpDownCounter => Aggregation::Sum,
InstrumentKind::ObservableGauge => Aggregation::LastValue,
InstrumentKind::ObservableGauge | InstrumentKind::Gauge => {
Aggregation::LastValue
}
InstrumentKind::Histogram => Aggregation::ExplicitBucketHistogram {
boundaries: self.histogram_buckets.clone(),
record_min_max: false,
Expand All @@ -246,7 +250,7 @@ fn initialize_prometheus_exporter() -> Result<GlobalPrometheus, ExporterInitiali
.without_target_info()
.build()?;

let meter_provider = MeterProvider::builder().with_reader(exporter).build();
let meter_provider = SdkMeterProvider::builder().with_reader(exporter).build();

global::set_meter_provider(meter_provider);
}
Expand Down
2 changes: 1 addition & 1 deletion autometrics/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl AutometricsSettingsBuilder {
let (prometheus_client_registry, prometheus_client_metrics) =
crate::tracker::prometheus_client::initialize_registry(
self.prometheus_client_registry
.unwrap_or_else(|| <prometheus_client::registry::Registry>::default()),
.unwrap_or_else(<prometheus_client::registry::Registry>::default),
);

let repo_url = self
Expand Down
5 changes: 2 additions & 3 deletions autometrics/src/tracker/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::__private::FunctionDescription;
use crate::labels::{BuildInfoLabels, CounterLabels, GaugeLabels, HistogramLabels, Label};
use crate::{constants::*, tracker::TrackMetrics};
use once_cell::sync::Lazy;
use opentelemetry::metrics::{Counter, Histogram, Unit, UpDownCounter};
use opentelemetry::metrics::{Counter, Histogram, UpDownCounter};
use opentelemetry::{global, KeyValue};
use std::{sync::Once, time::Instant};

Expand All @@ -19,10 +19,9 @@ static HISTOGRAM: Lazy<Histogram<f64>> = Lazy::new(|| {
// Note that the unit needs to be written as "s" rather than "seconds"
// or it will not be included in the metric name
// https://github.com/open-telemetry/opentelemetry-rust/issues/1173
let unit = Unit::new("s");
global::meter(METER_NAME)
.f64_histogram(HISTOGRAM_NAME)
.with_unit(unit)
.with_unit("s")
.with_description(HISTOGRAM_DESCRIPTION)
.init()
});
Expand Down
4 changes: 2 additions & 2 deletions autometrics/tests/exemplars_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ fn tracing_opentelemetry_context() {
let tracer = provider.tracer("test");

// This adds the OpenTelemetry Context to every tracing Span
#[cfg(feature = "exemplars-tracing-opentelemetry-0_22")]
let otel_layer = tracing_opentelemetry_0_22::layer().with_tracer(tracer);
#[cfg(feature = "exemplars-tracing-opentelemetry-0_25")]
let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer);

let subscriber = Registry::default().with(otel_layer);

Expand Down
9 changes: 5 additions & 4 deletions examples/exemplars-tracing-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ edition = "2021"
autometrics = { path = "../../autometrics", features = [
"prometheus-client-0_22",
"prometheus-exporter",
"exemplars-tracing-opentelemetry-0_22",
"exemplars-tracing-opentelemetry-0_25",
] }
autometrics-example-util = { path = "../util" }
axum = { version = "0.7.2", features = ["json"] }
opentelemetry = "0.20"
opentelemetry-stdout = { version = "0.1", features = ["trace"] }
opentelemetry = "0.24"
opentelemetry_sdk = "0.24"
opentelemetry-stdout = { version = "0.5", features = ["trace"] }
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
tracing-opentelemetry = "0.21"
tracing-opentelemetry = "0.25"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
4 changes: 2 additions & 2 deletions examples/exemplars-tracing-opentelemetry/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use autometrics::{autometrics, prometheus_exporter};
use autometrics_example_util::run_prometheus;
use axum::{routing::get, Router, ServiceExt};
use opentelemetry::sdk::trace::TracerProvider;
use axum::{routing::get, Router};
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::trace::TracerProvider;
use opentelemetry_stdout::SpanExporter;
use std::error::Error;
use std::net::Ipv4Addr;
Expand Down
10 changes: 5 additions & 5 deletions examples/opentelemetry-push-custom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ publish = false
edition = "2021"

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_21"] }
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_24"] }
autometrics-example-util = { path = "../util" }
# Note that the version of the opentelemetry crate MUST match
# the version used by autometrics
opentelemetry = { version = "0.21", features = ["metrics"] }
opentelemetry_sdk = { version = "0.21.1", features = ["metrics", "rt-tokio"] }
opentelemetry-otlp = { version = "0.14", features = ["tonic", "metrics"] }
opentelemetry-semantic-conventions = { version = "0.13" }
opentelemetry = { version = "0.24", features = ["metrics"] }
opentelemetry_sdk = { version = "0.24", features = ["metrics", "rt-tokio"] }
opentelemetry-otlp = { version = "0.17", features = ["tonic", "metrics"] }
opentelemetry-semantic-conventions = { version = "0.16.0" }
tokio = { version = "1", features = ["full"] }
2 changes: 1 addition & 1 deletion examples/opentelemetry-push/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ publish = false
edition = "2021"

[dependencies]
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_21", "otel-push-exporter-http", "otel-push-exporter-tokio"] }
autometrics = { path = "../../autometrics", features = ["opentelemetry-0_24", "otel-push-exporter-http", "otel-push-exporter-tokio"] }
autometrics-example-util = { path = "../util" }
tokio = { version = "1", features = ["full"] }

0 comments on commit 12b0569

Please sign in to comment.