Skip to content

Commit

Permalink
exemplars as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowagain committed Nov 27, 2023
1 parent f3a220e commit 1ada024
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- 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_21
- run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_22
- run: cargo test --features=prometheus-exporter,opentelemetry-0_21

# Build the crate using the other optional features
Expand Down
13 changes: 6 additions & 7 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ metrics = ["metrics-0_21"]
opentelemetry = ["opentelemetry-0_21"]
prometheus = ["prometheus-0_13"]
prometheus-client = ["prometheus-client-0_21"]
exemplars-tracing-opentelemetry = ["exemplars-tracing-opentelemetry-0_21"]
exemplars-tracing-opentelemetry = ["exemplars-tracing-opentelemetry-0_22"]

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

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

# Custom objectives
Expand All @@ -90,7 +90,6 @@ spez = "0.1.2"
thiserror = "1"

# Used for opentelemetry feature
opentelemetry_api = { version = "0.20", default-features = false, optional = true } # for compat with tracing-otel-0.21
opentelemetry = { version = "0.21", default-features = false, optional = true }

# Use for metrics feature
Expand All @@ -116,7 +115,7 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
], optional = true }

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

[dev-dependencies]
async-trait = "0.1.74"
Expand Down
4 changes: 2 additions & 2 deletions autometrics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn main() {
#[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_21` 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_20` or `exemplars-tracing-opentelemetry-0_21` 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_22` instead.");

cfg_aliases! {
// Backends
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_20", feature = "exemplars-tracing-opentelemetry-0_21", feature = "exemplars-tracing-opentelemetry") },
exemplars_tracing_opentelemetry: { any(feature = "exemplars-tracing-opentelemetry-0_22", feature = "exemplars-tracing-opentelemetry") },

// Custom objectives
custom_objective_percentile: { feature = "custom-objective-percentile" },
Expand Down
17 changes: 3 additions & 14 deletions autometrics/src/exemplars/tracing_opentelemetry.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
use super::TraceLabels;
use opentelemetry::trace::TraceContextExt as _;
use std::iter::FromIterator;
use opentelemetry_api::trace::TraceContextExt as _;
use tracing::Span;

#[cfg(all(
not(doc),
all(
feature = "exemplars-tracing-opentelemetry-0_20",
feature = "exemplars-tracing-opentelemetry-0_21"
)
))]
compile_error!("Only one of the `exemplars-tracing-opentelemetry-0_20` and `exemplars-tracing-opentelemetry-0_21` features can be enabled at a time");
use tracing_opentelemetry_0_22::OpenTelemetrySpanExt;

pub fn get_exemplar() -> Option<TraceLabels> {
// Get the OpenTelemetry Context from the tracing span
#[cfg(feature = "exemplars-tracing-opentelemetry-0_20")]
let context = tracing_opentelemetry_0_20::OpenTelemetrySpanExt::context(&Span::current());
#[cfg(feature = "exemplars-tracing-opentelemetry-0_21")]
let context = tracing_opentelemetry_0_21::OpenTelemetrySpanExt::context(&Span::current());
let context = OpenTelemetrySpanExt::context(&Span::current());

// Now get the OpenTelemetry "span" from the Context
// (it's confusing because the word "span" is used by both tracing and OpenTelemetry
Expand Down
3 changes: 1 addition & 2 deletions autometrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ mod constants;
#[cfg(any(
feature = "exemplars-tracing",
feature = "exemplars-tracing-opentelemetry",
feature = "exemplars-tracing-opentelemetry-0_20",
feature = "exemplars-tracing-opentelemetry-0_21",
feature = "exemplars-tracing-opentelemetry-0_22",
))]
pub mod exemplars;
mod labels;
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_21")]
let otel_layer = tracing_opentelemetry_0_21::layer().with_tracer(tracer);
#[cfg(feature = "exemplars-tracing-opentelemetry-0_22")]
let otel_layer = tracing_opentelemetry_0_22::layer().with_tracer(tracer);

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

Expand Down
2 changes: 1 addition & 1 deletion autometrics/tests/settings_custom_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn custom_prometheus_registry() {
#[cfg(opentelemetry)]
#[test]
fn custom_opentelemetry_registry() {
use opentelemetry_api::{global, KeyValue};
use opentelemetry::{global, KeyValue};
use prometheus::{Registry, TextEncoder};

// OpenTelemetry uses the `prometheus` crate under the hood
Expand Down
2 changes: 1 addition & 1 deletion examples/exemplars-tracing-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
autometrics = { path = "../../autometrics", features = [
"prometheus-client-0_21",
"prometheus-exporter",
"exemplars-tracing-opentelemetry-0_21",
"exemplars-tracing-opentelemetry-0_22",
] }
autometrics-example-util = { path = "../util" }
axum = { version = "0.6", features = ["json"] }
Expand Down

0 comments on commit 1ada024

Please sign in to comment.