From e745a2114e87adab6944aa2f9f5d2e7536c6ed54 Mon Sep 17 00:00:00 2001 From: Sunli Date: Fri, 19 Jul 2024 17:47:33 +0800 Subject: [PATCH 1/2] bump `opentelemetry` from `0.23` to `0.24` --- examples/poem/opentelemetry-jaeger/Cargo.toml | 12 +++---- .../poem/opentelemetry-jaeger/src/client.rs | 10 ++++-- .../poem/opentelemetry-jaeger/src/server1.rs | 15 +++++--- .../poem/opentelemetry-jaeger/src/server2.rs | 15 +++++--- poem/CHANGELOG.md | 4 +++ poem/src/middleware/opentelemetry_metrics.rs | 4 +-- poem/src/middleware/opentelemetry_tracing.rs | 35 ++++++------------- 7 files changed, 48 insertions(+), 47 deletions(-) diff --git a/examples/poem/opentelemetry-jaeger/Cargo.toml b/examples/poem/opentelemetry-jaeger/Cargo.toml index ebb564e595..87a4c13039 100644 --- a/examples/poem/opentelemetry-jaeger/Cargo.toml +++ b/examples/poem/opentelemetry-jaeger/Cargo.toml @@ -8,13 +8,11 @@ publish.workspace = true poem = { workspace = true, features = ["opentelemetry"] } tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tracing-subscriber.workspace = true -opentelemetry = { version = "0.23.0", features = ["metrics"] } -opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio"] } -opentelemetry-http = { version = "0.12.0" } -opentelemetry-otlp = { version = "0.16.0", features = [ - "trace", -] } -reqwest = "0.11" +opentelemetry = { version = "0.24.0", features = ["metrics"] } +opentelemetry_sdk = { version = "0.24.1", features = ["rt-tokio"] } +opentelemetry-http = { version = "0.13.0" } +opentelemetry-otlp = { version = "0.17.0", features = ["trace"] } +reqwest = "0.12" [[bin]] name = "example-opentelemetry-client" diff --git a/examples/poem/opentelemetry-jaeger/src/client.rs b/examples/poem/opentelemetry-jaeger/src/client.rs index 0223e8d930..ddde2c13e8 100644 --- a/examples/poem/opentelemetry-jaeger/src/client.rs +++ b/examples/poem/opentelemetry-jaeger/src/client.rs @@ -6,15 +6,19 @@ use opentelemetry::{ Context, KeyValue, }; use opentelemetry_http::HeaderInjector; -use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource}; +use opentelemetry_sdk::{ + propagation::TraceContextPropagator, + trace::{Config, TracerProvider}, + Resource, +}; use reqwest::{Client, Method, Url}; -fn init_tracer() -> Tracer { +fn init_tracer() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( - opentelemetry_sdk::trace::config() + Config::default() .with_resource(Resource::new(vec![KeyValue::new("service.name", "poem")])), ) .with_exporter(opentelemetry_otlp::new_exporter().tonic()) diff --git a/examples/poem/opentelemetry-jaeger/src/server1.rs b/examples/poem/opentelemetry-jaeger/src/server1.rs index c929cbca8f..35f390159c 100644 --- a/examples/poem/opentelemetry-jaeger/src/server1.rs +++ b/examples/poem/opentelemetry-jaeger/src/server1.rs @@ -2,11 +2,15 @@ use std::str::FromStr; use opentelemetry::{ global, - trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _}, + trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _, TracerProvider as _}, Context, KeyValue, }; use opentelemetry_http::HeaderInjector; -use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource}; +use opentelemetry_sdk::{ + propagation::TraceContextPropagator, + trace::{Config, Tracer, TracerProvider}, + Resource, +}; use poem::{ get, handler, listener::TcpListener, @@ -16,12 +20,12 @@ use poem::{ }; use reqwest::{Client, Url}; -fn init_tracer() -> Tracer { +fn init_tracer() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( - opentelemetry_sdk::trace::config().with_resource(Resource::new(vec![KeyValue::new( + Config::default().with_resource(Resource::new(vec![KeyValue::new( "service.name", "server1", )])), @@ -76,7 +80,8 @@ async fn main() -> Result<(), std::io::Error> { } tracing_subscriber::fmt::init(); - let tracer = init_tracer(); + let tracer_provider = init_tracer(); + let tracer = tracer_provider.tracer("server1"); let app = Route::new() .at("/api1", get(index)) diff --git a/examples/poem/opentelemetry-jaeger/src/server2.rs b/examples/poem/opentelemetry-jaeger/src/server2.rs index fe58590235..38ee118278 100644 --- a/examples/poem/opentelemetry-jaeger/src/server2.rs +++ b/examples/poem/opentelemetry-jaeger/src/server2.rs @@ -1,5 +1,9 @@ -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::{Config, TracerProvider}, + Resource, +}; use poem::{ get, handler, listener::TcpListener, @@ -7,12 +11,12 @@ use poem::{ EndpointExt, Route, Server, }; -fn init_tracer() -> Tracer { +fn init_tracer() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( - opentelemetry_sdk::trace::config().with_resource(Resource::new(vec![KeyValue::new( + Config::default().with_resource(Resource::new(vec![KeyValue::new( "service.name", "server2", )])), @@ -34,7 +38,8 @@ async fn main() -> Result<(), std::io::Error> { } tracing_subscriber::fmt::init(); - let tracer = init_tracer(); + let tracer_provider = init_tracer(); + let tracer = tracer_provider.tracer("server2"); let app = Route::new() .at("/api2", get(index)) diff --git a/poem/CHANGELOG.md b/poem/CHANGELOG.md index baa0a3813d..9563ac645a 100644 --- a/poem/CHANGELOG.md +++ b/poem/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# [3.0.3] 2024-07-20 + +- bump `opentelemetry` from `0.23` to `0.24` + # [3.0.2] 2024-07-19 - bump `opentelemetry` to `0.23` diff --git a/poem/src/middleware/opentelemetry_metrics.rs b/poem/src/middleware/opentelemetry_metrics.rs index d5bbc4635e..82da58d307 100644 --- a/poem/src/middleware/opentelemetry_metrics.rs +++ b/poem/src/middleware/opentelemetry_metrics.rs @@ -2,7 +2,7 @@ use std::time::Instant; use libopentelemetry::{ global, - metrics::{Counter, Histogram, Unit}, + metrics::{Counter, Histogram}, Key, KeyValue, }; use opentelemetry_semantic_conventions::trace; @@ -38,7 +38,7 @@ impl OpenTelemetryMetrics { .init(), duration: meter .f64_histogram("poem_request_duration_ms") - .with_unit(Unit::new("milliseconds")) + .with_unit("milliseconds") .with_description( "request duration histogram (in milliseconds, since start of service)", ) diff --git a/poem/src/middleware/opentelemetry_tracing.rs b/poem/src/middleware/opentelemetry_tracing.rs index 9a17c0ae19..04b996b86f 100644 --- a/poem/src/middleware/opentelemetry_tracing.rs +++ b/poem/src/middleware/opentelemetry_tracing.rs @@ -2,11 +2,11 @@ use std::sync::Arc; use libopentelemetry::{ global, - propagation::Extractor, trace::{FutureExt, Span, SpanKind, TraceContextExt, Tracer}, Context, Key, KeyValue, }; -use opentelemetry_semantic_conventions::{resource, trace}; +use opentelemetry_http::HeaderExtractor; +use opentelemetry_semantic_conventions::{attribute, resource}; use crate::{ route::PathPattern, @@ -52,21 +52,6 @@ pub struct OpenTelemetryTracingEndpoint { inner: E, } -struct HeaderExtractor<'a>(&'a http::HeaderMap); - -impl<'a> Extractor for HeaderExtractor<'a> { - fn get(&self, key: &str) -> Option<&str> { - self.0.get(key).and_then(|value| value.to_str().ok()) - } - - fn keys(&self) -> Vec<&str> { - self.0 - .keys() - .map(|value| value.as_str()) - .collect::>() - } -} - impl Endpoint for OpenTelemetryTracingEndpoint where T: Tracer + Send + Sync, @@ -98,16 +83,16 @@ where )); attributes.push(KeyValue::new(resource::TELEMETRY_SDK_LANGUAGE, "rust")); attributes.push(KeyValue::new( - trace::HTTP_REQUEST_METHOD, + attribute::HTTP_REQUEST_METHOD, req.method().to_string(), )); attributes.push(KeyValue::new( - trace::URL_FULL, + attribute::URL_FULL, req.original_uri().to_string(), )); - attributes.push(KeyValue::new(trace::CLIENT_ADDRESS, remote_addr)); + attributes.push(KeyValue::new(attribute::CLIENT_ADDRESS, remote_addr)); attributes.push(KeyValue::new( - trace::NETWORK_PROTOCOL_VERSION, + attribute::NETWORK_PROTOCOL_VERSION, format!("{:?}", req.version()), )); @@ -135,14 +120,14 @@ where let resp = resp.into_response(); span.add_event("request.completed".to_string(), vec![]); span.set_attribute(KeyValue::new( - trace::HTTP_RESPONSE_STATUS_CODE, + attribute::HTTP_RESPONSE_STATUS_CODE, resp.status().as_u16() as i64, )); if let Some(content_length) = resp.headers().typed_get::() { span.set_attribute(KeyValue::new( - trace::HTTP_RESPONSE_BODY_SIZE, + attribute::HTTP_RESPONSE_BODY_SIZE, content_length.0 as i64, )); } @@ -150,12 +135,12 @@ where } Err(err) => { span.set_attribute(KeyValue::new( - trace::HTTP_RESPONSE_STATUS_CODE, + attribute::HTTP_RESPONSE_STATUS_CODE, err.status().as_u16() as i64, )); span.add_event( "request.error".to_string(), - vec![KeyValue::new(trace::EXCEPTION_MESSAGE, err.to_string())], + vec![KeyValue::new(attribute::EXCEPTION_MESSAGE, err.to_string())], ); Err(err) } From e731a73f8efa2a1f2c836706996627d678563feb Mon Sep 17 00:00:00 2001 From: Sunli Date: Fri, 19 Jul 2024 17:47:39 +0800 Subject: [PATCH 2/2] poem 3.0.3 --- poem/Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/poem/Cargo.toml b/poem/Cargo.toml index 540f3686ff..7f4a799e89 100644 --- a/poem/Cargo.toml +++ b/poem/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "poem" -version = "3.0.2" +version = "3.0.3" authors.workspace = true edition.workspace = true license.workspace = true @@ -128,11 +128,11 @@ libcookie = { package = "cookie", version = "0.18", features = [ "key-expansion", "secure", ], optional = true } -opentelemetry-http = { version = "0.12.0", optional = true } -opentelemetry-semantic-conventions = { version = "0.15.0", optional = true } +opentelemetry-http = { version = "0.13.0", optional = true } +opentelemetry-semantic-conventions = { version = "0.16.0", optional = true } opentelemetry-prometheus = { version = "0.16.0", optional = true } libprometheus = { package = "prometheus", version = "0.13.0", optional = true } -libopentelemetry = { package = "opentelemetry", version = "0.23.0", features = [ +libopentelemetry = { package = "opentelemetry", version = "0.24.0", features = [ "metrics", ], optional = true } libtempfile = { package = "tempfile", version = "3.2.0", optional = true }