From 8085d6d5d93cce23a6d9f8be42418e6388f5e0da Mon Sep 17 00:00:00 2001 From: Maxime Suret <11944422+msuret@users.noreply.github.com> Date: Sun, 17 Nov 2024 13:13:37 +0100 Subject: [PATCH] feat(deps): upgrade to opentelemetry 0.27 (#181) > I followed the migration guide described in https://github.com/open-telemetry/opentelemetry-rust/pull/2221 --- Cargo.toml | 18 +++++----- fake-opentelemetry-collector/src/lib.rs | 37 ++++++++++--------- init-tracing-opentelemetry/src/otlp.rs | 47 +++++++++++-------------- init-tracing-opentelemetry/src/stdio.rs | 14 ++++---- 4 files changed, 56 insertions(+), 60 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c956f4..e9d69d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,27 +27,27 @@ axum = { version = "0.7", default-features = false } http = "^1" hyper = "1" insta = { version = "1.40", features = ["yaml", "redactions"] } -opentelemetry = { version = "0.26", features = [ +opentelemetry = { version = "0.27", features = [ "trace", ], default-features = false } -opentelemetry_sdk = { version = "0.26", default-features = false, features = [ +opentelemetry_sdk = { version = "0.27", default-features = false, features = [ "rt-tokio", ] } opentelemetry-aws = { version = "0.14", default-features = false } -opentelemetry-jaeger-propagator = { version = "0.26", default-features = false } +opentelemetry-jaeger-propagator = { version = "0.27", default-features = false } opentelemetry-resource-detectors = { version = "0.5", default-features = false } -opentelemetry-semantic-conventions = { version = "0.26", default-features = false } -opentelemetry-zipkin = { version = "0.26", default-features = false } -opentelemetry-otlp = { version = "0.26", default-features = false } -opentelemetry-proto = { version = "0.26", default-features = false } -opentelemetry-stdout = { version = "0.26" } +opentelemetry-semantic-conventions = { version = "0.27", default-features = false } +opentelemetry-zipkin = { version = "0.27", default-features = false } +opentelemetry-otlp = { version = "0.27", default-features = false } +opentelemetry-proto = { version = "0.27", default-features = false } +opentelemetry-stdout = { version = "0.27" } rstest = "0.23" tokio = { version = "1", default-features = false } tokio-stream = { version = "0.1", default-features = false } tonic = { version = "0.12", default-features = false } #should be sync with opentelemetry-proto tower = { version = "0.5", default-features = false } tracing = "0.1" -tracing-opentelemetry = "0.27" +tracing-opentelemetry = "0.28" [profile.dev.package.insta] opt-level = 3 diff --git a/fake-opentelemetry-collector/src/lib.rs b/fake-opentelemetry-collector/src/lib.rs index ec1a395..fd2ed9c 100644 --- a/fake-opentelemetry-collector/src/lib.rs +++ b/fake-opentelemetry-collector/src/lib.rs @@ -12,7 +12,7 @@ use std::time::{Duration, Instant}; use futures::StreamExt; use opentelemetry::trace::TracerProvider; -use opentelemetry_otlp::WithExportConfig; +use opentelemetry_otlp::{LogExporter, SpanExporter, WithExportConfig}; use opentelemetry_proto::tonic::collector::logs::v1::logs_service_server::LogsServiceServer; use opentelemetry_proto::tonic::collector::trace::v1::trace_service_server::TraceServiceServer; use tokio::sync::mpsc; @@ -98,30 +98,33 @@ async fn recv_many(rx: &mut Receiver, at_least: usize, timeout: Duration) pub async fn setup_tracer(fake_server: &FakeCollectorServer) -> opentelemetry_sdk::trace::Tracer { // if the environment variable is set (in test or in caller), `with_endpoint` value is ignored std::env::remove_var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"); - opentelemetry_otlp::new_pipeline() - .tracing() - .with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_endpoint(fake_server.endpoint()), + + opentelemetry_sdk::trace::TracerProvider::builder() + .with_batch_exporter( + SpanExporter::builder() + .with_tonic() + .with_endpoint(fake_server.endpoint()) + .build() + .expect("failed to install tracer"), + opentelemetry_sdk::runtime::Tokio, ) - .install_batch(opentelemetry_sdk::runtime::Tokio) - .expect("failed to install tracer") + .build() .tracer("") } pub async fn setup_logger( fake_server: &FakeCollectorServer, ) -> opentelemetry_sdk::logs::LoggerProvider { - opentelemetry_otlp::new_pipeline() - .logging() - .with_exporter( - opentelemetry_otlp::new_exporter() - .tonic() - .with_endpoint(fake_server.endpoint()), + opentelemetry_sdk::logs::LoggerProvider::builder() + //Install simple so we don't have to wait for batching in tests + .with_simple_exporter( + LogExporter::builder() + .with_tonic() + .with_endpoint(fake_server.endpoint()) + .build() + .expect("failed to install logging"), ) - .install_simple() //Install simple so we don't have to wait for batching in tests - .expect("failed to install logging") + .build() } #[cfg(test)] diff --git a/init-tracing-opentelemetry/src/otlp.rs b/init-tracing-opentelemetry/src/otlp.rs index 87f7390..ccf1e5b 100644 --- a/init-tracing-opentelemetry/src/otlp.rs +++ b/init-tracing-opentelemetry/src/otlp.rs @@ -1,15 +1,13 @@ use std::{collections::HashMap, str::FromStr}; use opentelemetry::trace::TraceError; -use opentelemetry_otlp::SpanExporterBuilder; +use opentelemetry_otlp::{SpanExporter, WithHttpConfig}; use opentelemetry_sdk::{trace::Sampler, trace::TracerProvider, Resource}; #[cfg(feature = "tls")] -use tonic::transport::ClientTlsConfig; +use {opentelemetry_otlp::WithTonicConfig, tonic::transport::ClientTlsConfig}; #[must_use] -pub fn identity( - v: opentelemetry_otlp::OtlpTracePipeline, -) -> opentelemetry_otlp::OtlpTracePipeline { +pub fn identity(v: opentelemetry_sdk::trace::Builder) -> opentelemetry_sdk::trace::Builder { v } @@ -19,9 +17,7 @@ pub fn init_tracerprovider( transform: F, ) -> Result where - F: FnOnce( - opentelemetry_otlp::OtlpTracePipeline, - ) -> opentelemetry_otlp::OtlpTracePipeline, + F: FnOnce(opentelemetry_sdk::trace::Builder) -> opentelemetry_sdk::trace::Builder, { use opentelemetry_otlp::WithExportConfig; @@ -30,35 +26,34 @@ where infer_protocol_and_endpoint(maybe_protocol.as_deref(), maybe_endpoint.as_deref()); tracing::debug!(target: "otel::setup", OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = endpoint); tracing::debug!(target: "otel::setup", OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = protocol); - let exporter: SpanExporterBuilder = match protocol.as_str() { - "http/protobuf" => opentelemetry_otlp::new_exporter() - .http() + let exporter: SpanExporter = match protocol.as_str() { + "http/protobuf" => SpanExporter::builder() + .with_http() .with_endpoint(endpoint) .with_headers(read_headers_from_env()) - .into(), + .build()?, #[cfg(feature = "tls")] - "grpc/tls" => opentelemetry_otlp::new_exporter() - .tonic() + "grpc/tls" => SpanExporter::builder() + .with_tonic() .with_tls_config(ClientTlsConfig::new().with_native_roots()) .with_endpoint(endpoint) - .into(), - _ => opentelemetry_otlp::new_exporter() - .tonic() + .build()?, + _ => SpanExporter::builder() + .with_tonic() .with_endpoint(endpoint) - .into(), + .build()?, }; - let mut pipeline = opentelemetry_otlp::new_pipeline() - .tracing() - .with_exporter(exporter) - .with_trace_config( + let mut trace_provider: opentelemetry_sdk::trace::Builder = TracerProvider::builder() + .with_config( opentelemetry_sdk::trace::Config::default() .with_resource(resource) .with_sampler(read_sampler_from_env()), - ); - pipeline = transform(pipeline); - let provider = pipeline.install_batch(opentelemetry_sdk::runtime::Tokio)?; - Ok(provider) + ) + .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio); + + trace_provider = transform(trace_provider); + Ok(trace_provider.build()) } /// turn a string of "k1=v1,k2=v2,..." into an iterator of (key, value) tuples diff --git a/init-tracing-opentelemetry/src/stdio.rs b/init-tracing-opentelemetry/src/stdio.rs index fa39112..61bcea6 100644 --- a/init-tracing-opentelemetry/src/stdio.rs +++ b/init-tracing-opentelemetry/src/stdio.rs @@ -1,5 +1,5 @@ use opentelemetry::trace::{TraceError, TracerProvider as _}; -use opentelemetry::InstrumentationLibrary; +use opentelemetry::InstrumentationScope; use opentelemetry_sdk::trace as sdktrace; use opentelemetry_sdk::trace::BatchSpanProcessor; use opentelemetry_sdk::trace::TracerProvider; @@ -31,13 +31,11 @@ where ); provider_builder = transform(provider_builder); // tracer used in libraries/crates that optionally includes version and schema url - let library = std::sync::Arc::new( - InstrumentationLibrary::builder(env!("CARGO_PKG_NAME")) - .with_version(env!("CARGO_PKG_VERSION")) - .with_schema_url("https://opentelemetry.io/schema/1.0.0") - .build(), - ); - Ok(provider_builder.build().library_tracer(library)) + let scope = InstrumentationScope::builder(env!("CARGO_PKG_NAME")) + .with_version(env!("CARGO_PKG_VERSION")) + .with_schema_url("https://opentelemetry.io/schema/1.0.0") + .build(); + Ok(provider_builder.build().tracer_with_scope(scope)) } #[derive(Debug, Default)]