Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: Update otel to 0.18.0 #2303

Merged
merged 4 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Expand Up @@ -155,11 +155,15 @@ jobs:
toolchain: 1.49.0
- subcrate: tracing-subscriber
toolchain: 1.49.0
- subcrate: tracing-opentelemetry
toolchain: 1.49.0
include:
- subcrate: tracing-appender
toolchain: 1.53.0
- subcrate: tracing-subscriber
toolchain: 1.50.0
- subcrate: tracing-opentelemetry
toolchain: 1.56.0
steps:
- uses: actions/checkout@v3
- name: install Rust nightly
Expand Down
4 changes: 2 additions & 2 deletions examples/Cargo.toml
Expand Up @@ -52,8 +52,8 @@ inferno = "0.11.6"
tempfile = "3.3.0"

# opentelemetry example
opentelemetry = { version = "0.17.0", default-features = false, features = ["trace"] }
opentelemetry-jaeger = "0.16.0"
opentelemetry = { version = "0.18.0", default-features = false, features = ["trace"] }
opentelemetry-jaeger = "0.17.0"

# fmt examples
snafu = "0.6.10"
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/opentelemetry.rs
Expand Up @@ -19,7 +19,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Install an otel pipeline with a simple span processor that exports data one at a time when
// spans end. See the `install_batch` option on each exporter's pipeline builder to see how to
// export in batches.
let tracer = opentelemetry_jaeger::new_pipeline()
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("report_example")
.install_simple()?;
let opentelemetry = tracing_opentelemetry::subscriber().with_tracer(tracer);
Expand Down
12 changes: 4 additions & 8 deletions tracing-opentelemetry/Cargo.toml
Expand Up @@ -17,28 +17,24 @@ categories = [
keywords = ["tracing", "opentelemetry", "jaeger", "zipkin", "async"]
license = "MIT"
edition = "2018"
rust-version = "1.46.0"
rust-version = "1.56.0"

[features]
default = ["tracing-log", "metrics"]
# Enables support for exporting OpenTelemetry metrics
metrics = ["opentelemetry/metrics"]

[dependencies]
opentelemetry = { version = "0.17.0", default-features = false, features = ["trace"] }
opentelemetry = { version = "0.18.0", default-features = false, features = ["trace"] }
tracing = { path = "../tracing", version = "0.2", default-features = false, features = ["std"] }
tracing-core = { path = "../tracing-core", version = "0.2" }
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", default-features = false, features = ["registry", "std"] }
tracing-log = { path = "../tracing-log", version = "0.2", default-features = false, optional = true }
once_cell = "1.13.0"

# Fix minimal-versions; opentelemetry specifies async-trait = "0.1" which breaks
async-trait = "0.1.20"
jtescher marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
async-trait = "0.1.56"
jtescher marked this conversation as resolved.
Show resolved Hide resolved
criterion = { version = "0.3.6", default_features = false }
opentelemetry-jaeger = "0.16.0"
opentelemetry-jaeger = "0.17.0"
futures-util = { version = "0.3.21", default-features = false }
tokio = { version = "1.20.0", features = ["full"] }
tokio-stream = "0.1.9"
Expand All @@ -52,4 +48,4 @@ harness = false

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
4 changes: 2 additions & 2 deletions tracing-opentelemetry/README.md
Expand Up @@ -50,7 +50,7 @@ The crate provides the following types:
[`tracing`]: https://crates.io/crates/tracing
[OpenTelemetry]: https://opentelemetry.io/

*Compiler support: [requires `rustc` 1.49+][msrv]*
*Compiler support: [requires `rustc` 1.56+][msrv]*

[msrv]: #supported-rust-versions

Expand Down Expand Up @@ -110,7 +110,7 @@ $ firefox http://localhost:16686/
## Supported Rust Versions

Tracing Opentelemetry is built against the latest stable release. The minimum
supported version is 1.46. The current Tracing version is not guaranteed to
supported version is 1.56. The current Tracing version is not guaranteed to
build on Rust versions earlier than the minimum supported version.

Tracing follows the same compiler support policies as the rest of the Tokio
Expand Down
4 changes: 2 additions & 2 deletions tracing-opentelemetry/src/lib.rs
Expand Up @@ -9,7 +9,7 @@
//! [OpenTelemetry]: https://opentelemetry.io
//! [`tracing`]: https://github.com/tokio-rs/tracing
//!
//! *Compiler support: [requires `rustc` 1.49+][msrv]*
//! *Compiler support: [requires `rustc` 1.56+][msrv]*
//!
//! [msrv]: #supported-rust-versions
//!
Expand Down Expand Up @@ -86,7 +86,7 @@
//! ## Supported Rust Versions
//!
//! Tracing is built against the latest stable release. The minimum supported
//! version is 1.49. The current Tracing version is not guaranteed to build on
//! version is 1.56. The current Tracing version is not guaranteed to build on
//! Rust versions earlier than the minimum supported version.
//!
//! Tracing follows the same compiler support policies as the rest of the Tokio
Expand Down
93 changes: 53 additions & 40 deletions tracing-opentelemetry/src/metrics.rs
Expand Up @@ -3,8 +3,9 @@ use tracing::{field::Visit, Collect};
use tracing_core::Field;

use opentelemetry::{
metrics::{Counter, Meter, MeterProvider, UpDownCounter, ValueRecorder},
sdk::metrics::PushController,
metrics::{Counter, Histogram, Meter, MeterProvider, UpDownCounter},
sdk::metrics::controllers::BasicController,
Context as OtelContext,
};
use tracing_subscriber::{registry::LookupSpan, subscribe::Context, Subscribe};

Expand All @@ -13,7 +14,7 @@ const INSTRUMENTATION_LIBRARY_NAME: &str = "tracing/tracing-opentelemetry";

const METRIC_PREFIX_MONOTONIC_COUNTER: &str = "monotonic_counter.";
const METRIC_PREFIX_COUNTER: &str = "counter.";
const METRIC_PREFIX_VALUE: &str = "value.";
const METRIC_PREFIX_HISTOGRAM: &str = "histogram.";
const I64_MAX: u64 = i64::MAX as u64;

#[derive(Default)]
Expand All @@ -22,9 +23,9 @@ pub(crate) struct Instruments {
f64_counter: MetricsMap<Counter<f64>>,
i64_up_down_counter: MetricsMap<UpDownCounter<i64>>,
f64_up_down_counter: MetricsMap<UpDownCounter<f64>>,
u64_value_recorder: MetricsMap<ValueRecorder<u64>>,
i64_value_recorder: MetricsMap<ValueRecorder<i64>>,
f64_value_recorder: MetricsMap<ValueRecorder<f64>>,
u64_histogram: MetricsMap<Histogram<u64>>,
i64_histogram: MetricsMap<Histogram<i64>>,
f64_histogram: MetricsMap<Histogram<f64>>,
}

type MetricsMap<T> = RwLock<HashMap<&'static str, T>>;
Expand All @@ -35,14 +36,15 @@ pub(crate) enum InstrumentType {
CounterF64(f64),
UpDownCounterI64(i64),
UpDownCounterF64(f64),
ValueRecorderU64(u64),
ValueRecorderI64(i64),
ValueRecorderF64(f64),
HistogramU64(u64),
HistogramI64(i64),
HistogramF64(f64),
}

impl Instruments {
pub(crate) fn update_metric(
&self,
cx: &OtelContext,
meter: &Meter,
instrument_type: InstrumentType,
metric_name: &'static str,
Expand Down Expand Up @@ -76,55 +78,55 @@ impl Instruments {
&self.u64_counter,
metric_name,
|| meter.u64_counter(metric_name).init(),
|ctr| ctr.add(value, &[]),
|ctr| ctr.add(cx, value, &[]),
);
}
InstrumentType::CounterF64(value) => {
update_or_insert(
&self.f64_counter,
metric_name,
|| meter.f64_counter(metric_name).init(),
|ctr| ctr.add(value, &[]),
|ctr| ctr.add(cx, value, &[]),
);
}
InstrumentType::UpDownCounterI64(value) => {
update_or_insert(
&self.i64_up_down_counter,
metric_name,
|| meter.i64_up_down_counter(metric_name).init(),
|ctr| ctr.add(value, &[]),
|ctr| ctr.add(cx, value, &[]),
);
}
InstrumentType::UpDownCounterF64(value) => {
update_or_insert(
&self.f64_up_down_counter,
metric_name,
|| meter.f64_up_down_counter(metric_name).init(),
|ctr| ctr.add(value, &[]),
|ctr| ctr.add(cx, value, &[]),
);
}
InstrumentType::ValueRecorderU64(value) => {
InstrumentType::HistogramU64(value) => {
update_or_insert(
&self.u64_value_recorder,
&self.u64_histogram,
metric_name,
|| meter.u64_value_recorder(metric_name).init(),
|rec| rec.record(value, &[]),
|| meter.u64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
);
}
InstrumentType::ValueRecorderI64(value) => {
InstrumentType::HistogramI64(value) => {
update_or_insert(
&self.i64_value_recorder,
&self.i64_histogram,
metric_name,
|| meter.i64_value_recorder(metric_name).init(),
|rec| rec.record(value, &[]),
|| meter.i64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
);
}
InstrumentType::ValueRecorderF64(value) => {
InstrumentType::HistogramF64(value) => {
update_or_insert(
&self.f64_value_recorder,
&self.f64_histogram,
metric_name,
|| meter.f64_value_recorder(metric_name).init(),
|rec| rec.record(value, &[]),
|| meter.f64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
);
}
};
Expand All @@ -142,15 +144,18 @@ impl<'a> Visit for MetricVisitor<'a> {
}

fn record_u64(&mut self, field: &Field, value: u64) {
let cx = OtelContext::current();
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::CounterU64(value),
metric_name,
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
if value <= I64_MAX {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::UpDownCounterI64(value as i64),
metric_name,
Expand All @@ -163,54 +168,63 @@ impl<'a> Visit for MetricVisitor<'a> {
value
);
}
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::ValueRecorderU64(value),
InstrumentType::HistogramU64(value),
metric_name,
);
}
}

fn record_f64(&mut self, field: &Field, value: f64) {
let cx = OtelContext::current();
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::CounterF64(value),
metric_name,
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::UpDownCounterF64(value),
metric_name,
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::ValueRecorderF64(value),
InstrumentType::HistogramF64(value),
metric_name,
);
}
}

fn record_i64(&mut self, field: &Field, value: i64) {
let cx = OtelContext::current();
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::CounterU64(value as u64),
metric_name,
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::UpDownCounterI64(value),
metric_name,
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::ValueRecorderI64(value),
InstrumentType::HistogramI64(value),
metric_name,
);
}
Expand All @@ -232,14 +246,14 @@ impl<'a> Visit for MetricVisitor<'a> {
/// use tracing_opentelemetry::MetricsSubscriber;
/// use tracing_subscriber::subscribe::CollectExt;
/// use tracing_subscriber::Registry;
/// # use opentelemetry::sdk::metrics::PushController;
/// # use opentelemetry::sdk::metrics::controllers::BasicController;
///
/// // Constructing a PushController is out-of-scope for the docs here, but there
/// // Constructing a BasicController is out-of-scope for the docs here, but there
/// // are examples in the opentelemetry repository. See:
/// // https://github.com/open-telemetry/opentelemetry-rust/blob/c13a11e62a68eacd8c41a0742a0d097808e28fbd/examples/basic-otlp/src/main.rs#L39-L53
/// # let push_controller: PushController = unimplemented!();
/// // https://github.com/open-telemetry/opentelemetry-rust/blob/d4b9befea04bcc7fc19319a6ebf5b5070131c486/examples/basic-otlp/src/main.rs#L35-L52
/// # let controller: BasicController = unimplemented!();
///
/// let opentelemetry_metrics = MetricsSubscriber::new(push_controller);
/// let opentelemetry_metrics = MetricsSubscriber::new(controller);
/// let collector = Registry::default().with(opentelemetry_metrics);
/// tracing::collect::set_global_default(collector).unwrap();
/// ```
Expand Down Expand Up @@ -328,10 +342,9 @@ pub struct MetricsSubscriber {

impl MetricsSubscriber {
/// Create a new instance of MetricsSubscriber.
pub fn new(push_controller: PushController) -> Self {
let meter = push_controller
.provider()
.meter(INSTRUMENTATION_LIBRARY_NAME, Some(CARGO_PKG_VERSION));
pub fn new(controller: BasicController) -> Self {
let meter =
controller.versioned_meter(INSTRUMENTATION_LIBRARY_NAME, Some(CARGO_PKG_VERSION), None);
MetricsSubscriber {
meter,
instruments: Default::default(),
Expand Down