From 8d0ad32c878a571e1c3e3b477e6bdba7c096d588 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Sun, 26 May 2024 03:39:56 +0800 Subject: [PATCH] fix: otel --- Cargo.toml | 8 ++++---- viz-core/src/middleware/otel/metrics.rs | 16 ++++------------ viz-core/src/middleware/otel/tracing.rs | 19 +++++-------------- viz-smol/src/server.rs | 2 ++ 4 files changed, 15 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71d41cc9..ccf6d12b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,12 +101,12 @@ tokio-tungstenite = "0.21" tokio-util = "0.7" # OpenTelemetry -opentelemetry = { version = "0.22", default-features = false } -opentelemetry_sdk = { version = "0.22", default-features = false } -opentelemetry-prometheus = { version = "0.15", features = [ +opentelemetry = { version = "0.23", default-features = false } +opentelemetry_sdk = { version = "0.23", default-features = false } +opentelemetry-prometheus = { version = "0.16", features = [ "prometheus-encoding", ] } -opentelemetry-semantic-conventions = { version = "0.14" } +opentelemetry-semantic-conventions = { version = "0.15" } prometheus = "0.13" # Tracing diff --git a/viz-core/src/middleware/otel/metrics.rs b/viz-core/src/middleware/otel/metrics.rs index 519ed2bc..28ed53fc 100644 --- a/viz-core/src/middleware/otel/metrics.rs +++ b/viz-core/src/middleware/otel/metrics.rs @@ -2,7 +2,7 @@ //! //! [`OpenTelemetry`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md -use std::{net::SocketAddr, time::SystemTime}; +use std::time::SystemTime; use http::uri::Scheme; use opentelemetry::{ @@ -10,8 +10,8 @@ use opentelemetry::{ KeyValue, }; use opentelemetry_semantic_conventions::trace::{ - CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, - HTTP_ROUTE, NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_SCHEME, + CLIENT_ADDRESS, HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, + NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_SCHEME, }; use crate::{Handler, IntoResponse, Request, RequestExt, Response, ResponseExt, Result, Transform}; @@ -155,17 +155,9 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec { format!("{:?}", req.version()), )); - let remote_addr = req.remote_addr(); - if let Some(remote_addr) = remote_addr { + if let Some(remote_addr) = req.remote_addr() { attributes.push(KeyValue::new(CLIENT_ADDRESS, remote_addr.to_string())); } - if let Some(realip) = req.realip().map(|value| value.0).filter(|realip| { - remote_addr - .map(SocketAddr::ip) - .map_or(true, |remoteip| &remoteip != realip) - }) { - attributes.push(KeyValue::new(CLIENT_SOCKET_ADDRESS, realip.to_string())); - } let uri = req.uri(); if let Some(host) = uri.host() { diff --git a/viz-core/src/middleware/otel/tracing.rs b/viz-core/src/middleware/otel/tracing.rs index 72058516..7d3fbe70 100644 --- a/viz-core/src/middleware/otel/tracing.rs +++ b/viz-core/src/middleware/otel/tracing.rs @@ -2,7 +2,7 @@ //! //! [`OpenTelemetry`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md -use std::{net::SocketAddr, sync::Arc}; +use std::sync::Arc; use http::{uri::Scheme, HeaderValue}; use opentelemetry::{ @@ -12,10 +12,9 @@ use opentelemetry::{ Context, KeyValue, }; use opentelemetry_semantic_conventions::trace::{ - CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, EXCEPTION_MESSAGE, HTTP_REQUEST_BODY_SIZE, - HTTP_REQUEST_METHOD, HTTP_RESPONSE_BODY_SIZE, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, - NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_PATH, URL_QUERY, URL_SCHEME, - USER_AGENT_ORIGINAL, + CLIENT_ADDRESS, EXCEPTION_MESSAGE, HTTP_REQUEST_BODY_SIZE, HTTP_REQUEST_METHOD, + HTTP_RESPONSE_BODY_SIZE, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, NETWORK_PROTOCOL_VERSION, + SERVER_ADDRESS, SERVER_PORT, URL_PATH, URL_QUERY, URL_SCHEME, USER_AGENT_ORIGINAL, }; use crate::{ @@ -166,17 +165,9 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec { format!("{:?}", req.version()), )); - let remote_addr = req.remote_addr(); - if let Some(remote_addr) = remote_addr { + if let Some(remote_addr) = req.remote_addr() { attributes.push(KeyValue::new(CLIENT_ADDRESS, remote_addr.to_string())); } - if let Some(realip) = req.realip().map(|value| value.0).filter(|realip| { - remote_addr - .map(SocketAddr::ip) - .map_or(true, |remoteip| &remoteip != realip) - }) { - attributes.push(KeyValue::new(CLIENT_SOCKET_ADDRESS, realip.to_string())); - } let uri = req.uri(); if let Some(host) = uri.host() { diff --git a/viz-smol/src/server.rs b/viz-smol/src/server.rs index f73a5bea..b97265f5 100644 --- a/viz-smol/src/server.rs +++ b/viz-smol/src/server.rs @@ -56,7 +56,9 @@ where let executor = executor.clone(); async move { let mut builder = Builder::new(SmolExecutor::new(AsRefExecutor(executor.borrow()))); + #[cfg(feature = "http1")] builder.http1().timer(SmolTimer::new()); + #[cfg(feature = "http2")] builder.http2().timer(SmolTimer::new()); if let Err(err) = builder.serve_connection_with_upgrades(io, responder).await {