From c81047b45b55d868fe265c12ac8bf2ebc84ab345 Mon Sep 17 00:00:00 2001
From: Spencer Ferris <3319370+spencewenski@users.noreply.github.com>
Date: Sat, 16 Nov 2024 23:55:16 -0800
Subject: [PATCH] chore: Upgrade otel/tracing dependencies

---
 Cargo.toml           | 10 ++++----
 src/error/tracing.rs |  6 ++---
 src/tracing/mod.rs   | 58 +++++++++++++++++++++++---------------------
 3 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 4cfe09ee..9314eccf 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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
diff --git a/src/error/tracing.rs b/src/error/tracing.rs
index 0b21745c..6eb94e38 100644
--- a/src/error/tracing.rs
+++ b/src/error/tracing.rs
@@ -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),
@@ -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)))
     }
 }
diff --git a/src/tracing/mod.rs b/src/tracing/mod.rs
index 7312527a..48667884 100644
--- a/src/tracing/mod.rs
+++ b/src/tracing/mod.rs
@@ -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;
@@ -109,20 +115,19 @@ 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
     };
@@ -130,21 +135,20 @@ pub fn init_tracing(
     // 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 {