From cf5dd832c65d3a2bf1fd0a1632aab3659dda8d65 Mon Sep 17 00:00:00 2001 From: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> Date: Tue, 21 Jan 2025 12:49:29 -0700 Subject: [PATCH] :arrow_up::recycle: Update open telemetry dependencies (#282) * :arrow_up::recycle: Update open telemetry dependencies Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> * :recycle: Use common resource function Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> * :recycle: Add period reader interval Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> * :recycle: Trace timeout Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> --------- Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com> --- Cargo.lock | 31 ++++++------ Cargo.toml | 10 ++-- src/utils/trace.rs | 116 +++++++++++++++++++++------------------------ 3 files changed, 76 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 035d278d..ec353421 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1660,23 +1660,23 @@ dependencies = [ [[package]] name = "opentelemetry" -version = "0.24.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c365a63eec4f55b7efeceb724f1336f26a9cf3427b70e59e2cd2a5b947fba96" +checksum = "ab70038c28ed37b97d8ed414b6429d343a8bbf44c9f79ec854f3a643029ba6d7" dependencies = [ "futures-core", "futures-sink", "js-sys", - "once_cell", "pin-project-lite", "thiserror 1.0.64", + "tracing", ] [[package]] name = "opentelemetry-http" -version = "0.13.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad31e9de44ee3538fb9d64fe3376c1362f406162434609e79aea2a41a0af78ab" +checksum = "10a8a7f5f6ba7c1b286c2fbca0454eaba116f63bbe69ed250b642d36fbb04d80" dependencies = [ "async-trait", "bytes", @@ -1687,9 +1687,9 @@ dependencies = [ [[package]] name = "opentelemetry-otlp" -version = "0.17.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b925a602ffb916fb7421276b86756027b37ee708f9dce2dbdcc51739f07e727" +checksum = "91cf61a1868dacc576bf2b2a1c3e9ab150af7272909e80085c3173384fe11f76" dependencies = [ "async-trait", "futures-core", @@ -1702,13 +1702,14 @@ dependencies = [ "thiserror 1.0.64", "tokio", "tonic", + "tracing", ] [[package]] name = "opentelemetry-proto" -version = "0.7.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30ee9f20bff9c984511a02f082dc8ede839e4a9bf15cc2487c8d6fea5ad850d9" +checksum = "a6e05acbfada5ec79023c85368af14abd0b307c015e9064d249b2a950ef459a6" dependencies = [ "opentelemetry", "opentelemetry_sdk", @@ -1718,16 +1719,15 @@ dependencies = [ [[package]] name = "opentelemetry_sdk" -version = "0.24.1" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692eac490ec80f24a17828d49b40b60f5aeaccdfe6a503f939713afd22bc28df" +checksum = "231e9d6ceef9b0b2546ddf52335785ce41252bc7474ee8ba05bfad277be13ab8" dependencies = [ "async-trait", "futures-channel", "futures-executor", "futures-util", "glob", - "once_cell", "opentelemetry", "percent-encoding", "rand", @@ -1735,6 +1735,7 @@ dependencies = [ "thiserror 1.0.64", "tokio", "tokio-stream", + "tracing", ] [[package]] @@ -2859,9 +2860,9 @@ dependencies = [ [[package]] name = "tracing-opentelemetry" -version = "0.25.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9784ed4da7d921bc8df6963f8c80a0e4ce34ba6ba76668acadd3edbd985ff3b" +checksum = "97a971f6058498b5c0f1affa23e7ea202057a7301dbff68e968b2d578bcbd053" dependencies = [ "js-sys", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 8238d50b..264ebf40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,10 +31,10 @@ hyper = { version = "1.5.2", features = ["http1", "http2", "server"] } hyper-rustls = { version = "0.27.5", features = ["ring"]} hyper-timeout = "0.5.2" hyper-util = { version = "0.1.10", features = ["server-auto", "server-graceful", "tokio"] } -opentelemetry = { version = "0.24.0", features = ["trace", "metrics"] } -opentelemetry-http = { version = "0.13.0", features = ["reqwest"] } -opentelemetry-otlp = { version = "0.17.0", features = ["http-proto"] } -opentelemetry_sdk = { version = "0.24.1", features = ["rt-tokio", "metrics"] } +opentelemetry = { version = "0.27.1", features = ["metrics", "trace"] } +opentelemetry-http = { version = "0.27.0", features = ["reqwest"] } +opentelemetry-otlp = { version = "0.27.0", features = ["grpc-tonic", "http-proto"] } +opentelemetry_sdk = { version = "0.27.1", features = ["rt-tokio", "metrics"] } pin-project-lite = "0.2.16" prost = "0.13.4" reqwest = { version = "0.12.12", features = ["blocking", "rustls-tls", "json"] } @@ -52,7 +52,7 @@ tonic = { version = "0.12.3", features = ["tls", "tls-roots", "tls-webpki-roots" tower = { version = "0.5.2", features = ["timeout"] } tower-http = { version = "0.6.2", features = ["trace"] } tracing = "0.1.41" -tracing-opentelemetry = "0.25.0" +tracing-opentelemetry = "0.28.0" tracing-subscriber = { version = "0.3.19", features = ["json", "env-filter"] } url = "2.5.4" diff --git a/src/utils/trace.rs b/src/utils/trace.rs index f38b5d98..6a6459c3 100644 --- a/src/utils/trace.rs +++ b/src/utils/trace.rs @@ -20,20 +20,16 @@ use std::time::Duration; use axum::{extract::Request, http::HeaderMap, response::Response}; use opentelemetry::{ global, - metrics::MetricsError, trace::{TraceContextExt, TraceError, TraceId, TracerProvider}, KeyValue, }; use opentelemetry_http::{HeaderExtractor, HeaderInjector}; -use opentelemetry_otlp::WithExportConfig; +use opentelemetry_otlp::{MetricExporter, SpanExporter, WithExportConfig, WithHttpConfig}; use opentelemetry_sdk::{ - metrics::{ - reader::{DefaultAggregationSelector, DefaultTemporalitySelector}, - SdkMeterProvider, - }, + metrics::{MetricError, PeriodicReader, SdkMeterProvider}, propagation::TraceContextPropagator, runtime, - trace::{Config, Sampler}, + trace::Sampler, Resource, }; use tracing::{error, info, info_span, Span}; @@ -48,16 +44,14 @@ pub enum TracingError { #[error("Error from tracing provider: {0}")] TraceError(#[from] TraceError), #[error("Error from metrics provider: {0}")] - MetricsError(#[from] MetricsError), + MetricError(#[from] MetricError), } -fn service_config(tracing_config: TracingConfig) -> Config { - Config::default() - .with_resource(Resource::new(vec![KeyValue::new( - "service.name", - tracing_config.service_name, - )])) - .with_sampler(Sampler::AlwaysOn) +fn resource(tracing_config: TracingConfig) -> Resource { + Resource::new(vec![KeyValue::new( + "service.name", + tracing_config.service_name, + )]) } /// Initializes an OpenTelemetry tracer provider with an OTLP export pipeline based on the @@ -66,31 +60,34 @@ fn init_tracer_provider( tracing_config: TracingConfig, ) -> Result, TracingError> { if let Some((protocol, endpoint)) = tracing_config.clone().traces { + let timeout = Duration::from_secs(3); + let exporter = match protocol { + OtlpProtocol::Grpc => SpanExporter::builder() + .with_tonic() + .with_endpoint(endpoint) + .with_timeout(timeout) + .build()?, + OtlpProtocol::Http => SpanExporter::builder() + .with_http() + .with_http_client(reqwest::Client::new()) + .with_endpoint(endpoint) + .with_timeout(timeout) + .build()?, + }; Ok(Some( - match protocol { - OtlpProtocol::Grpc => opentelemetry_otlp::new_pipeline().tracing().with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_endpoint(endpoint) - .with_timeout(Duration::from_secs(3)), - ), - OtlpProtocol::Http => opentelemetry_otlp::new_pipeline().tracing().with_exporter( - opentelemetry_otlp::new_exporter() - .http() - .with_http_client(reqwest::Client::new()) - .with_endpoint(endpoint) - .with_timeout(Duration::from_secs(3)), - ), - } - .with_trace_config(service_config(tracing_config)) - .install_batch(runtime::Tokio)?, + opentelemetry_sdk::trace::TracerProvider::builder() + .with_batch_exporter(exporter, runtime::Tokio) + .with_resource(resource(tracing_config)) + .with_sampler(Sampler::AlwaysOn) + .build(), )) } else if !tracing_config.quiet { // We still need a tracing provider as long as we are logging in order to enable any // trace-sensitive logs, such as any mentions of a request's trace_id. Ok(Some( opentelemetry_sdk::trace::TracerProvider::builder() - .with_config(service_config(tracing_config)) + .with_resource(resource(tracing_config)) + .with_sampler(Sampler::AlwaysOn) .build(), )) } else { @@ -103,34 +100,31 @@ fn init_tracer_provider( fn init_meter_provider( tracing_config: TracingConfig, ) -> Result, TracingError> { - if let Some((protocol, endpoint)) = tracing_config.metrics { + if let Some((protocol, endpoint)) = tracing_config.clone().metrics { + // Note: DefaultAggregationSelector removed from OpenTelemetry SDK as of 0.26.0 + // as custom aggregation should be available in Views. Cumulative temporality is default. + let timeout = Duration::from_secs(10); + let exporter = match protocol { + OtlpProtocol::Grpc => MetricExporter::builder() + .with_tonic() + .with_endpoint(endpoint) + .with_timeout(timeout) + .build()?, + OtlpProtocol::Http => MetricExporter::builder() + .with_http() + .with_http_client(reqwest::Client::new()) + .with_endpoint(endpoint) + .with_timeout(timeout) + .build()?, + }; + let reader = PeriodicReader::builder(exporter, runtime::Tokio) + .with_interval(Duration::from_secs(3)) + .build(); Ok(Some( - match protocol { - OtlpProtocol::Grpc => opentelemetry_otlp::new_pipeline() - .metrics(runtime::Tokio) - .with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_endpoint(endpoint), - ), - OtlpProtocol::Http => opentelemetry_otlp::new_pipeline() - .metrics(runtime::Tokio) - .with_exporter( - opentelemetry_otlp::new_exporter() - .http() - .with_http_client(reqwest::Client::new()) - .with_endpoint(endpoint), - ), - } - .with_resource(Resource::new(vec![KeyValue::new( - "service.name", - tracing_config.service_name, - )])) - .with_timeout(Duration::from_secs(10)) - .with_period(Duration::from_secs(3)) - .with_aggregation_selector(DefaultAggregationSelector::new()) - .with_temporality_selector(DefaultTemporalitySelector::new()) - .build()?, + SdkMeterProvider::builder() + .with_resource(resource(tracing_config)) + .with_reader(reader) + .build(), )) } else { Ok(None) @@ -227,7 +221,7 @@ pub fn init_tracing( if let Some(meter_provider) = meter_provider { meter_provider .shutdown() - .map_err(TracingError::MetricsError)?; + .map_err(TracingError::MetricError)?; } Ok(()) })