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

update opentelemetry to 0.20.0 #36

Merged
merged 7 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default = ["tracing-log", "metrics"]
metrics = ["opentelemetry/metrics"]

[dependencies]
opentelemetry = { version = "0.19.0", default-features = false, features = ["trace"] }
opentelemetry = { version = "0.20.0", default-features = false, features = ["trace"] }
tracing = { version = "0.1.35", default-features = false, features = ["std"] }
tracing-core = "0.1.28"
tracing-subscriber = { version = "0.3.0", default-features = false, features = ["registry", "std"] }
Expand All @@ -39,7 +39,8 @@ thiserror = { version = "1.0.31", optional = true }
[dev-dependencies]
async-trait = "0.1.56"
criterion = { version = "0.4.0", default-features = false, features = ["html_reports"] }
opentelemetry-jaeger = "0.18.0"
opentelemetry-jaeger = "0.19.0"
opentelemetry-stdout = { version = "0.1.0", features = ["trace"] }
pprof = { version = "0.11.1", features = ["flamegraph", "criterion"] }
futures-util = { version = "0.3", default-features = false }
tokio = { version = "1", features = ["full"] }
Expand Down
4 changes: 2 additions & 2 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,10 +1160,10 @@ mod tests {
fn trace_id_from_existing_context() {
let tracer = TestTracer(Arc::new(Mutex::new(None)));
let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone()));
let trace_id = otel::TraceId::from(42u128.to_be_bytes());
let trace_id = otel::TraceId::from(42u128);
ymgyt marked this conversation as resolved.
Show resolved Hide resolved
let existing_cx = OtelContext::current_with_span(TestSpan(otel::SpanContext::new(
trace_id,
otel::SpanId::from(1u64.to_be_bytes()),
otel::SpanId::from(1u64),
TraceFlags::default(),
false,
Default::default(),
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@
//! ## Examples
//!
//! ```
//! use opentelemetry::sdk::export::trace::stdout;
//! use opentelemetry::sdk::trace::TracerProvider;
//! use opentelemetry::trace::{Tracer, TracerProvider as _};
//! use tracing::{error, span};
//! use tracing_subscriber::layer::SubscriberExt;
//! use tracing_subscriber::Registry;
//!
//! // Create a new OpenTelemetry pipeline
//! let tracer = stdout::new_pipeline().install_simple();
//! // Create a new OpenTelemetry trace pipeline that prints to stdout
//! let provider = TracerProvider::builder()
//! .with_simple_exporter(opentelemetry_stdout::SpanExporter::default())
//! .build();
//! let tracer = provider.tracer("readme_example");
ymgyt marked this conversation as resolved.
Show resolved Hide resolved
//!
//! // Create a tracing layer with the configured tracer
//! let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
Expand Down
56 changes: 23 additions & 33 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::{collections::HashMap, fmt, sync::RwLock};
use tracing::{field::Visit, Subscriber};
use tracing_core::Field;

use opentelemetry::{
metrics::{Counter, Histogram, Meter, MeterProvider, UpDownCounter},
sdk::metrics::controllers::BasicController,
Context as OtelContext,
};
use opentelemetry::metrics::{Counter, Histogram, Meter, MeterProvider, UpDownCounter};
use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -44,7 +40,6 @@ pub(crate) enum InstrumentType {
impl Instruments {
pub(crate) fn update_metric(
&self,
cx: &OtelContext,
meter: &Meter,
instrument_type: InstrumentType,
metric_name: &'static str,
Expand Down Expand Up @@ -78,55 +73,55 @@ impl Instruments {
&self.u64_counter,
metric_name,
|| meter.u64_counter(metric_name).init(),
|ctr| ctr.add(cx, value, &[]),
|ctr| ctr.add(value, &[]),
);
}
InstrumentType::CounterF64(value) => {
update_or_insert(
&self.f64_counter,
metric_name,
|| meter.f64_counter(metric_name).init(),
|ctr| ctr.add(cx, value, &[]),
|ctr| ctr.add(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(cx, value, &[]),
|ctr| ctr.add(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(cx, value, &[]),
|ctr| ctr.add(value, &[]),
);
}
InstrumentType::HistogramU64(value) => {
update_or_insert(
&self.u64_histogram,
metric_name,
|| meter.u64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
|rec| rec.record(value, &[]),
);
}
InstrumentType::HistogramI64(value) => {
update_or_insert(
&self.i64_histogram,
metric_name,
|| meter.i64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
|rec| rec.record(value, &[]),
);
}
InstrumentType::HistogramF64(value) => {
update_or_insert(
&self.f64_histogram,
metric_name,
|| meter.f64_histogram(metric_name).init(),
|rec| rec.record(cx, value, &[]),
|rec| rec.record(value, &[]),
);
}
};
Expand All @@ -144,18 +139,15 @@ 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 @@ -170,7 +162,6 @@ impl<'a> Visit for MetricVisitor<'a> {
}
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::HistogramU64(value),
metric_name,
Expand All @@ -179,24 +170,20 @@ impl<'a> Visit for MetricVisitor<'a> {
}

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_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::HistogramF64(value),
metric_name,
Expand All @@ -205,24 +192,20 @@ impl<'a> Visit for MetricVisitor<'a> {
}

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_HISTOGRAM) {
self.instruments.update_metric(
&cx,
self.meter,
InstrumentType::HistogramI64(value),
metric_name,
Expand All @@ -246,14 +229,14 @@ impl<'a> Visit for MetricVisitor<'a> {
/// use tracing_opentelemetry::MetricsLayer;
/// use tracing_subscriber::layer::SubscriberExt;
/// use tracing_subscriber::Registry;
/// # use opentelemetry::sdk::metrics::controllers::BasicController;
/// # use opentelemetry::sdk::metrics::MeterProvider;
///
/// // Constructing a BasicController is out-of-scope for the docs here, but there
/// // Constructing a MeterProvider 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/d4b9befea04bcc7fc19319a6ebf5b5070131c486/examples/basic-otlp/src/main.rs#L35-L52
/// # let controller: BasicController = unimplemented!();
/// // https://github.com/open-telemetry/opentelemetry-rust/blob/dfeac078ff7853e7dc814778524b93470dfa5c9c/examples/metrics-basic/src/main.rs#L7
/// # let meter_provider: MeterProvider = unimplemented!();
///
/// let opentelemetry_metrics = MetricsLayer::new(controller);
/// let opentelemetry_metrics = MetricsLayer::new(meter_provider);
/// let subscriber = Registry::default().with(opentelemetry_metrics);
/// tracing::subscriber::set_global_default(subscriber).unwrap();
/// ```
Expand Down Expand Up @@ -343,9 +326,16 @@ pub struct MetricsLayer {

impl MetricsLayer {
/// Create a new instance of MetricsLayer.
pub fn new(controller: BasicController) -> Self {
let meter =
controller.versioned_meter(INSTRUMENTATION_LIBRARY_NAME, Some(CARGO_PKG_VERSION), None);
pub fn new<M>(meter_provider: M) -> Self
where
M: MeterProvider,
ymgyt marked this conversation as resolved.
Show resolved Hide resolved
{
let meter = meter_provider.versioned_meter(
INSTRUMENTATION_LIBRARY_NAME,
Some(CARGO_PKG_VERSION),
None::<&'static str>,
ymgyt marked this conversation as resolved.
Show resolved Hide resolved
None,
);
MetricsLayer {
meter,
instruments: Default::default(),
Expand Down
7 changes: 3 additions & 4 deletions src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl PreSampledTracer for Tracer {
builder.span_kind.as_ref().unwrap_or(&SpanKind::Internal),
builder.attributes.as_ref().unwrap_or(&OrderMap::default()),
builder.links.as_deref().unwrap_or(&[]),
self.instrumentation_library(),
));

process_sampling_result(
Expand Down Expand Up @@ -169,7 +168,7 @@ mod tests {
let provider = TracerProvider::default();
let tracer = provider.tracer("test");
let mut builder = SpanBuilder::from_name("empty".to_string());
builder.span_id = Some(SpanId::from(1u64.to_be_bytes()));
builder.span_id = Some(SpanId::from(1u64));
builder.trace_id = None;
let parent_cx = OtelContext::new();
let cx = tracer.sampled_context(&mut OtelData { builder, parent_cx });
Expand Down Expand Up @@ -224,8 +223,8 @@ mod tests {

fn span_context(trace_flags: TraceFlags, is_remote: bool) -> SpanContext {
SpanContext::new(
TraceId::from(1u128.to_be_bytes()),
SpanId::from(1u64.to_be_bytes()),
TraceId::from(1u128),
SpanId::from(1u64),
trace_flags,
is_remote,
Default::default(),
Expand Down
Loading