Skip to content

Commit

Permalink
chore: Upgrade otel/tracing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed Nov 17, 2024
1 parent 4e91186 commit c81047b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ dotenvy = "0.15.5"
# Tracing
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }
opentelemetry-semantic-conventions = "0.26.0"
opentelemetry = { version = "0.26.0", features = ["trace", "metrics", "logs"], optional = true }
opentelemetry_sdk = { version = "0.26.0", features = ["tokio", "rt-tokio", "metrics", "logs", "trace"], optional = true }
opentelemetry-otlp = { version = "0.26.0", features = ["metrics", "trace", "logs"], optional = true }
opentelemetry-semantic-conventions = "0.27.0"
opentelemetry = { version = "0.27.0", features = ["trace", "metrics", "logs"], optional = true }
opentelemetry_sdk = { version = "0.27.0", features = ["tokio", "rt-tokio", "metrics", "logs", "trace"], optional = true }
opentelemetry-otlp = { version = "0.27.0", features = ["metrics", "trace", "logs"], optional = true }
# Roadster technically doesn't need a direct dependency on `prost`, but we add one here to allow our
# `cargo minimal-versions check` check to pass -- `opentelemetry-proto` requires version `0.13.2` or higher
# in order to compile -- it fails to compile with `0.13.1` even though its dependencies don't specify `0.13.2`.
prost = { workspace = true, optional = true }
tracing-opentelemetry = { version = "0.27.0", features = ["metrics"], optional = true }
tracing-opentelemetry = { version = "0.28.0", features = ["metrics"], optional = true }

# HTTP APIs
# `axum-core` is not optional because we use the `FromRef` trait pretty extensively, even in parts of
Expand Down
6 changes: 3 additions & 3 deletions src/error/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum TracingInitError {

#[cfg(feature = "otel")]
#[error(transparent)]
OtelMetrics(#[from] opentelemetry::metrics::MetricsError),
OtelMetrics(#[from] opentelemetry_sdk::metrics::MetricError),

#[error(transparent)]
ParseLevel(#[from] tracing::metadata::ParseLevelError),
Expand All @@ -43,8 +43,8 @@ impl From<opentelemetry::trace::TraceError> for Error {
}

#[cfg(feature = "otel")]
impl From<opentelemetry::metrics::MetricsError> for Error {
fn from(value: opentelemetry::metrics::MetricsError) -> Self {
impl From<opentelemetry_sdk::metrics::MetricError> for Error {
fn from(value: opentelemetry_sdk::metrics::MetricError) -> Self {
Self::Tracing(TracingError::from(TracingInitError::from(value)))
}
}
Expand Down
58 changes: 31 additions & 27 deletions src/tracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ use opentelemetry::trace::TracerProvider;
#[cfg(feature = "otel")]
use opentelemetry_otlp::WithExportConfig;
#[cfg(feature = "otel")]
use opentelemetry_sdk::metrics::reader::DefaultTemporalitySelector;
use opentelemetry_otlp::{MetricExporter, SpanExporter};
#[cfg(feature = "otel")]
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
#[cfg(feature = "otel")]
use opentelemetry_sdk::propagation::TraceContextPropagator;
#[cfg(feature = "otel")]
use opentelemetry_sdk::runtime::Tokio;
#[cfg(feature = "otel")]
use opentelemetry_sdk::trace::Config;
#[cfg(feature = "otel")]
use opentelemetry_semantic_conventions::resource::{SERVICE_NAME, SERVICE_VERSION};
use std::str::FromStr;
use tracing::Level;
#[cfg(feature = "otel")]
use tracing_opentelemetry::MetricsLayer;
#[cfg(feature = "otel")]
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -109,42 +115,40 @@ pub fn init_tracing(
// Trace layer
#[cfg(feature = "otel")]
let oltp_traces_layer = if let Some(otlp_endpoint) = config.tracing.otlp_endpoint.as_ref() {
let otlp_tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(otlp_endpoint.to_string()),
)
.with_trace_config(
opentelemetry_sdk::trace::Config::default().with_resource(otel_resource.clone()),
)
.install_batch(Tokio)?
.tracer(service_name);
let exporter = SpanExporter::builder()
.with_tonic()
.with_endpoint(otlp_endpoint.to_string())
.build()?;
let provider = opentelemetry_sdk::trace::TracerProvider::builder()
.with_config(Config::default().with_resource(otel_resource.clone()))
.with_batch_exporter(exporter, Tokio)
.build();
opentelemetry::global::set_tracer_provider(provider.clone());
// Create a tracing layer with the configured tracer
Some(tracing_opentelemetry::layer().with_tracer(otlp_tracer))
Some(OpenTelemetryLayer::new(
provider.tracer("tracing-otel-subscriber"),
))
} else {
None
};

// Metric layer
#[cfg(feature = "otel")]
let otlp_metrics_layer = if let Some(otlp_endpoint) = config.tracing.otlp_endpoint.as_ref() {
let builder = opentelemetry_otlp::new_pipeline()
.metrics(Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(otlp_endpoint.clone()),
)
.with_resource(otel_resource)
.with_temporality_selector(DefaultTemporalitySelector::new());
let builder = if let Some(interval) = config.tracing.metrics_export_interval {
builder.with_period(interval)
let exporter = MetricExporter::builder()
.with_tonic()
.with_endpoint(otlp_endpoint.clone())
.build()?;
let reader = PeriodicReader::builder(exporter, Tokio);
let reader = if let Some(interval) = config.tracing.metrics_export_interval {
reader.with_interval(interval)
} else {
builder
reader
};
let provider = builder.build()?;
let provider = SdkMeterProvider::builder()
.with_reader(reader.build())
.with_resource(otel_resource.clone())
.build();
opentelemetry::global::set_meter_provider(provider.clone());
Some(MetricsLayer::new(provider))
} else {
Expand Down

0 comments on commit c81047b

Please sign in to comment.