diff --git a/Cargo.toml b/Cargo.toml index 7d3b30b30..0e338078b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,10 +81,10 @@ native-tls = "0.2" nix = { version = "0.29", default-features = false } once_cell = "1" openssl = "0.10" -opentelemetry-http = { version = "0.12", default-features = false } -opentelemetry-semantic-conventions = { version = "0.15", default-features = false } +opentelemetry = { version = "0.24", default-features = false } +opentelemetry-http = { version = "0.13", default-features = false } opentelemetry-prometheus = { version = "0.16", default-features = false } -opentelemetry = { version = "0.23", default-features = false } +opentelemetry-semantic-conventions = { version = "0.16", default-features = false } parking_lot = "0.12" path-slash = "0.2" percent-encoding = "2" diff --git a/crates/otel/Cargo.toml b/crates/otel/Cargo.toml index ae81d7503..33caeaad1 100644 --- a/crates/otel/Cargo.toml +++ b/crates/otel/Cargo.toml @@ -25,7 +25,6 @@ opentelemetry-http = { workspace = true } opentelemetry-semantic-conventions = { workspace = true } opentelemetry = { workspace = true, features = ["metrics"] } salvo_core = { workspace = true, default-features = false } -headers03 = {version = "0.3", package = "headers"} [dev-dependencies] salvo_core = { workspace = true, features = ["test"] } diff --git a/crates/otel/src/metrics.rs b/crates/otel/src/metrics.rs index aa4c94bbd..54253765e 100644 --- a/crates/otel/src/metrics.rs +++ b/crates/otel/src/metrics.rs @@ -1,6 +1,6 @@ use std::time::Instant; -use opentelemetry::metrics::{Counter, Histogram, Unit}; +use opentelemetry::metrics::{Counter, Histogram}; use opentelemetry::{global, KeyValue}; use opentelemetry_semantic_conventions::trace; use salvo_core::http::ResBody; @@ -34,7 +34,7 @@ impl Metrics { .init(), duration: meter .f64_histogram("salvo_request_duration_ms") - .with_unit(Unit::new("milliseconds")) + .with_unit("milliseconds") .with_description("request duration histogram (in milliseconds, since start of service)") .init(), } diff --git a/crates/otel/src/tracing.rs b/crates/otel/src/tracing.rs index 5ba3bba22..0834d9f1e 100644 --- a/crates/otel/src/tracing.rs +++ b/crates/otel/src/tracing.rs @@ -1,9 +1,8 @@ -use headers03::{HeaderMap, HeaderName, HeaderValue}; use opentelemetry::trace::{FutureExt, Span, SpanKind, TraceContextExt, Tracer}; use opentelemetry::{global, Context, KeyValue}; use opentelemetry_http::HeaderExtractor; -use opentelemetry_semantic_conventions::{resource, trace}; -use salvo_core::http::headers::{self, HeaderMapExt}; +use opentelemetry_semantic_conventions::{attribute, resource, trace}; +use salvo_core::http::headers::{self, HeaderMap, HeaderMapExt, HeaderName, HeaderValue}; use salvo_core::prelude::*; /// Middleware for tracing with OpenTelemetry. @@ -74,7 +73,10 @@ where span.add_event(event.to_string(), vec![]); span.set_attribute(KeyValue::new(trace::HTTP_RESPONSE_STATUS_CODE, status.as_u16() as i64)); if let Some(content_length) = res.headers().typed_get::() { - span.set_attribute(KeyValue::new(trace::HTTP_RESPONSE_BODY_SIZE, content_length.0 as i64)); + span.set_attribute(KeyValue::new( + attribute::HTTP_RESPONSE_BODY_SIZE, + content_length.0 as i64, + )); } } .with_context(Context::current_with_span(span)) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 9b38db4e4..ab370169b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -34,17 +34,18 @@ sea-orm-migration = "0.12" eyre = "0.6" tera = "1.3" futures = "0.3" -opentelemetry = "0.23.0" -opentelemetry-http = "0.12.0" -opentelemetry-otlp = {version = "0.16.0" } +opentelemetry = "0.24.0" +opentelemetry-http = "0.13.0" +opentelemetry-otlp = {version = "0.17.0" } opentelemetry-prometheus = "0.16.0" -opentelemetry_sdk = "0.23.0" +opentelemetry_sdk = "0.24.0" tokio-stream = "0.1.14" async-stream = "0.3.5" futures-util = { version = "0.3", default-features = true } jsonwebtoken = "9" once_cell = "1" parking_lot = "0.12" +reqwest = "0.12" salvo = { path = "../crates/salvo", default-features = true } serde = "1" serde_json = "1" diff --git a/examples/otel-jaeger/Cargo.toml b/examples/otel-jaeger/Cargo.toml index 10db0dfad..dace85d79 100644 --- a/examples/otel-jaeger/Cargo.toml +++ b/examples/otel-jaeger/Cargo.toml @@ -22,7 +22,7 @@ tokio = { workspace = true, features = ["macros"] } tracing.workspace = true tracing-subscriber.workspace = true opentelemetry = { workspace = true, features = ["metrics"] } -reqwest = "0.11" +reqwest = { workspace = true } prometheus = "0.13" opentelemetry-prometheus.workspace = true opentelemetry-http.workspace = true diff --git a/examples/otel-jaeger/src/client.rs b/examples/otel-jaeger/src/client.rs index 712c9cbf2..fd4cde29d 100644 --- a/examples/otel-jaeger/src/client.rs +++ b/examples/otel-jaeger/src/client.rs @@ -7,12 +7,12 @@ use opentelemetry::{ }; use opentelemetry_http::HeaderInjector; use opentelemetry_otlp::WithExportConfig; -use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer}; +use opentelemetry_sdk::propagation::TraceContextPropagator; use reqwest::{Client, Method, Url}; -fn init_tracer() -> Tracer { +fn init_tracer() { global::set_text_map_propagator(TraceContextPropagator::new()); - opentelemetry_otlp::new_pipeline() + let provider = opentelemetry_otlp::new_pipeline() .tracing() .with_exporter( opentelemetry_otlp::new_exporter() @@ -20,12 +20,13 @@ fn init_tracer() -> Tracer { .with_endpoint("http://localhost:14268/api/traces"), ) .install_batch(opentelemetry_sdk::runtime::Tokio) - .unwrap() + .unwrap(); + let _ = global::set_tracer_provider(provider); } #[tokio::main] async fn main() { - let _tracer = init_tracer(); + init_tracer(); let client = Client::new(); let span = global::tracer("example-opentelemetry/client").start("request/server1"); let cx = Context::current_with_span(span); diff --git a/examples/otel-jaeger/src/server1.rs b/examples/otel-jaeger/src/server1.rs index 7d338ec9f..056b42fbb 100644 --- a/examples/otel-jaeger/src/server1.rs +++ b/examples/otel-jaeger/src/server1.rs @@ -1,10 +1,11 @@ use std::str::FromStr; use std::sync::Arc; -use opentelemetry::trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _}; +use opentelemetry::trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _, TracerProvider as _}; use opentelemetry::{global, KeyValue}; use opentelemetry_http::HeaderInjector; -use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource}; +use opentelemetry_sdk::trace::{Tracer, TracerProvider}; +use opentelemetry_sdk::{propagation::TraceContextPropagator, Resource}; use reqwest::{Client, Method, Url}; use salvo::otel::{Metrics, Tracing}; use salvo::prelude::*; @@ -12,12 +13,12 @@ use salvo::prelude::*; mod exporter; use exporter::Exporter; -fn init_tracer() -> Tracer { +fn init_tracer_provider() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( - opentelemetry_sdk::trace::config() + opentelemetry_sdk::trace::Config::default() .with_resource(Resource::new(vec![KeyValue::new("service.name", "server1")])), ) .with_exporter(opentelemetry_otlp::new_exporter().tonic()) @@ -66,7 +67,7 @@ async fn index(req: &mut Request, depot: &mut Depot) -> String { async fn main() { tracing_subscriber::fmt().init(); - let tracer = init_tracer(); + let tracer = init_tracer_provider().tracer("app"); let router = Router::new() .hoop(affix::inject(Arc::new(tracer.clone()))) .hoop(Metrics::new()) diff --git a/examples/otel-jaeger/src/server2.rs b/examples/otel-jaeger/src/server2.rs index 247720326..1ff3ba719 100644 --- a/examples/otel-jaeger/src/server2.rs +++ b/examples/otel-jaeger/src/server2.rs @@ -1,17 +1,17 @@ -use opentelemetry::{global, KeyValue}; -use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource}; +use opentelemetry::{global, trace::TracerProvider as _, KeyValue}; +use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::TracerProvider, Resource}; use salvo::otel::{Metrics, Tracing}; use salvo::prelude::*; mod exporter; use exporter::Exporter; -fn init_tracer() -> Tracer { +fn init_tracer_provider() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( - opentelemetry_sdk::trace::config() + opentelemetry_sdk::trace::Config::default() .with_resource(Resource::new(vec![KeyValue::new("service.name", "server2")])), ) .with_exporter(opentelemetry_otlp::new_exporter().tonic()) @@ -28,7 +28,7 @@ async fn index(req: &mut Request) -> String { async fn main() { tracing_subscriber::fmt().init(); - let tracer = init_tracer(); + let tracer = init_tracer_provider().tracer("app"); let router = Router::new() .hoop(Metrics::new()) .hoop(Tracing::new(tracer)) diff --git a/examples/proxy-simple/src/main.rs b/examples/proxy-simple/src/main.rs index 2d33f76b1..c42be423b 100644 --- a/examples/proxy-simple/src/main.rs +++ b/examples/proxy-simple/src/main.rs @@ -4,9 +4,9 @@ use salvo::prelude::*; async fn main() { tracing_subscriber::fmt().init(); -// In this example, if the requested URL begins with , the proxy goes to -// ; if the requested URL begins with , the proxy -// goes to . + // In this example, if the requested URL begins with , the proxy goes to + // ; if the requested URL begins with , the proxy + // goes to . let router = Router::new() .push( Router::new()