From accbb3e0678c1e91e0db8b03ac783b7d230c1a6d Mon Sep 17 00:00:00 2001 From: Miikka Koskinen Date: Wed, 27 Mar 2024 12:24:01 +0200 Subject: [PATCH] update opentelemetry to v0.22.x (#784) * update opentelemetry to v0.22.x * update examples to opentelemetry to v0.22.x * update CHANGELOG.md --- examples/poem/opentelemetry-jaeger/Cargo.toml | 8 +-- poem/CHANGELOG.md | 1 + poem/Cargo.toml | 8 +-- poem/src/middleware/opentelemetry_metrics.rs | 28 +++++++--- poem/src/middleware/opentelemetry_tracing.rs | 56 ++++++++++++------- 5 files changed, 66 insertions(+), 35 deletions(-) diff --git a/examples/poem/opentelemetry-jaeger/Cargo.toml b/examples/poem/opentelemetry-jaeger/Cargo.toml index 5e69c09a8c..b2a79ec3b8 100644 --- a/examples/poem/opentelemetry-jaeger/Cargo.toml +++ b/examples/poem/opentelemetry-jaeger/Cargo.toml @@ -8,10 +8,10 @@ publish.workspace = true poem = { workspace = true, features = ["opentelemetry"] } tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tracing-subscriber.workspace = true -opentelemetry = { version = "0.21.0", features = ["metrics"] } -opentelemetry_sdk = { version = "0.21.0", features = ["rt-tokio"] } -opentelemetry-http = { version = "0.10.0" } -opentelemetry-otlp = { version = "0.14.0", features = [ +opentelemetry = { version = "0.22.0", features = ["metrics"] } +opentelemetry_sdk = { version = "0.22.0", features = ["rt-tokio"] } +opentelemetry-http = { version = "0.11.0" } +opentelemetry-otlp = { version = "0.15.0", features = [ "trace", ] } reqwest = "0.11" diff --git a/poem/CHANGELOG.md b/poem/CHANGELOG.md index 971ac96d0e..2dd2a2dd23 100644 --- a/poem/CHANGELOG.md +++ b/poem/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - bump `nix` to 0.28 - bump `redis` to 0.25 - bump `reqwest` to 0.12 +- bump `opentelemetry` to 0.22 [#784](https://github.com/poem-web/poem/pull/784) # [2.0.1] 2024-03-04 diff --git a/poem/Cargo.toml b/poem/Cargo.toml index 3868fed3ea..2a4f7b1b31 100644 --- a/poem/Cargo.toml +++ b/poem/Cargo.toml @@ -128,11 +128,11 @@ libcookie = { package = "cookie", version = "0.18", features = [ "key-expansion", "secure", ], optional = true } -opentelemetry-http = { version = "0.10.0", optional = true } -opentelemetry-semantic-conventions = { version = "0.13.0", optional = true } -opentelemetry-prometheus = { version = "0.14.0", optional = true } +opentelemetry-http = { version = "0.11.0", optional = true } +opentelemetry-semantic-conventions = { version = "0.14.0", optional = true } +opentelemetry-prometheus = { version = "0.15.0", optional = true } libprometheus = { package = "prometheus", version = "0.13.0", optional = true } -libopentelemetry = { package = "opentelemetry", version = "0.21.0", features = [ +libopentelemetry = { package = "opentelemetry", version = "0.22.0", features = [ "metrics", ], optional = true } libtempfile = { package = "tempfile", version = "3.2.0", optional = true } diff --git a/poem/src/middleware/opentelemetry_metrics.rs b/poem/src/middleware/opentelemetry_metrics.rs index 02d2cb7d17..d5bbc4635e 100644 --- a/poem/src/middleware/opentelemetry_metrics.rs +++ b/poem/src/middleware/opentelemetry_metrics.rs @@ -3,7 +3,7 @@ use std::time::Instant; use libopentelemetry::{ global, metrics::{Counter, Histogram, Unit}, - Key, + Key, KeyValue, }; use opentelemetry_semantic_conventions::trace; @@ -74,8 +74,14 @@ impl Endpoint for OpenTelemetryMetricsEndpoint { async fn call(&self, req: Request) -> Result { let mut labels = Vec::with_capacity(3); - labels.push(trace::HTTP_REQUEST_METHOD.string(req.method().to_string())); - labels.push(trace::URL_FULL.string(req.original_uri().to_string())); + labels.push(KeyValue::new( + trace::HTTP_REQUEST_METHOD, + req.method().to_string(), + )); + labels.push(KeyValue::new( + trace::URL_FULL, + req.original_uri().to_string(), + )); let s = Instant::now(); let res = self.inner.call(req).await.map(IntoResponse::into_response); @@ -85,20 +91,26 @@ impl Endpoint for OpenTelemetryMetricsEndpoint { Ok(resp) => { if let Some(path_pattern) = resp.data::() { const HTTP_PATH_PATTERN: Key = Key::from_static_str("http.path_pattern"); - labels.push(HTTP_PATH_PATTERN.string(path_pattern.0.to_string())); + labels.push(KeyValue::new(HTTP_PATH_PATTERN, path_pattern.0.to_string())); } - labels.push(trace::HTTP_RESPONSE_STATUS_CODE.i64(resp.status().as_u16() as i64)); + labels.push(KeyValue::new( + trace::HTTP_RESPONSE_STATUS_CODE, + resp.status().as_u16() as i64, + )); } Err(err) => { if let Some(path_pattern) = err.data::() { const HTTP_PATH_PATTERN: Key = Key::from_static_str("http.path_pattern"); - labels.push(HTTP_PATH_PATTERN.string(path_pattern.0.to_string())); + labels.push(KeyValue::new(HTTP_PATH_PATTERN, path_pattern.0.to_string())); } - labels.push(trace::HTTP_RESPONSE_STATUS_CODE.i64(err.status().as_u16() as i64)); + labels.push(KeyValue::new( + trace::HTTP_RESPONSE_STATUS_CODE, + err.status().as_u16() as i64, + )); self.error_count.add(1, &labels); - labels.push(trace::EXCEPTION_MESSAGE.string(err.to_string())); + labels.push(KeyValue::new(trace::EXCEPTION_MESSAGE, err.to_string())); } } diff --git a/poem/src/middleware/opentelemetry_tracing.rs b/poem/src/middleware/opentelemetry_tracing.rs index a171358851..9a17c0ae19 100644 --- a/poem/src/middleware/opentelemetry_tracing.rs +++ b/poem/src/middleware/opentelemetry_tracing.rs @@ -4,7 +4,7 @@ use libopentelemetry::{ global, propagation::Extractor, trace::{FutureExt, Span, SpanKind, TraceContextExt, Tracer}, - Context, Key, + Context, Key, KeyValue, }; use opentelemetry_semantic_conventions::{resource, trace}; @@ -88,17 +88,32 @@ where }); let mut attributes = Vec::new(); - attributes.push(resource::TELEMETRY_SDK_NAME.string(env!("CARGO_CRATE_NAME"))); - attributes.push(resource::TELEMETRY_SDK_VERSION.string(env!("CARGO_PKG_VERSION"))); - attributes.push(resource::TELEMETRY_SDK_LANGUAGE.string("rust")); - attributes.push(trace::HTTP_REQUEST_METHOD.string(req.method().to_string())); - attributes.push(trace::URL_FULL.string(req.original_uri().to_string())); - attributes.push(trace::CLIENT_ADDRESS.string(remote_addr)); - attributes.push(trace::NETWORK_PROTOCOL_VERSION.string(format!("{:?}", req.version()))); + attributes.push(KeyValue::new( + resource::TELEMETRY_SDK_NAME, + env!("CARGO_CRATE_NAME"), + )); + attributes.push(KeyValue::new( + resource::TELEMETRY_SDK_VERSION, + env!("CARGO_PKG_VERSION"), + )); + attributes.push(KeyValue::new(resource::TELEMETRY_SDK_LANGUAGE, "rust")); + attributes.push(KeyValue::new( + trace::HTTP_REQUEST_METHOD, + req.method().to_string(), + )); + attributes.push(KeyValue::new( + trace::URL_FULL, + req.original_uri().to_string(), + )); + attributes.push(KeyValue::new(trace::CLIENT_ADDRESS, remote_addr)); + attributes.push(KeyValue::new( + trace::NETWORK_PROTOCOL_VERSION, + format!("{:?}", req.version()), + )); if let Some(path_pattern) = req.data::() { const HTTP_PATH_PATTERN: Key = Key::from_static_str("http.path_pattern"); - attributes.push(HTTP_PATH_PATTERN.string(path_pattern.0.to_string())); + attributes.push(KeyValue::new(HTTP_PATH_PATTERN, path_pattern.0.to_string())); } let mut span = self @@ -119,25 +134,28 @@ where Ok(resp) => { let resp = resp.into_response(); span.add_event("request.completed".to_string(), vec![]); - span.set_attribute( - trace::HTTP_RESPONSE_STATUS_CODE.i64(resp.status().as_u16() as i64), - ); + span.set_attribute(KeyValue::new( + trace::HTTP_RESPONSE_STATUS_CODE, + resp.status().as_u16() as i64, + )); if let Some(content_length) = resp.headers().typed_get::() { - span.set_attribute( - trace::HTTP_RESPONSE_BODY_SIZE.i64(content_length.0 as i64), - ); + span.set_attribute(KeyValue::new( + trace::HTTP_RESPONSE_BODY_SIZE, + content_length.0 as i64, + )); } Ok(resp) } Err(err) => { - span.set_attribute( - trace::HTTP_RESPONSE_STATUS_CODE.i64(err.status().as_u16() as i64), - ); + span.set_attribute(KeyValue::new( + trace::HTTP_RESPONSE_STATUS_CODE, + err.status().as_u16() as i64, + )); span.add_event( "request.error".to_string(), - vec![trace::EXCEPTION_MESSAGE.string(err.to_string())], + vec![KeyValue::new(trace::EXCEPTION_MESSAGE, err.to_string())], ); Err(err) }