From 7e3f8cf8a75f3fab38fe5123b357a74f2c9cfd48 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Wed, 2 Aug 2023 06:40:32 +0800 Subject: [PATCH] fix: clippy --- viz-core/src/middleware/otel/metrics.rs | 22 +++++++++++----------- viz-core/src/middleware/otel/tracing.rs | 16 ++++++++-------- viz-core/src/response.rs | 5 ++++- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/viz-core/src/middleware/otel/metrics.rs b/viz-core/src/middleware/otel/metrics.rs index 7f4685ed..91fe41a9 100644 --- a/viz-core/src/middleware/otel/metrics.rs +++ b/viz-core/src/middleware/otel/metrics.rs @@ -7,7 +7,7 @@ use std::{net::SocketAddr, time::SystemTime}; use http::uri::Scheme; use opentelemetry::{ metrics::{Histogram, Meter, Unit, UpDownCounter}, - Context, KeyValue, + KeyValue, }; use opentelemetry_semantic_conventions::trace::{ CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, @@ -15,8 +15,8 @@ use opentelemetry_semantic_conventions::trace::{ }; use crate::{ - async_trait, headers::UserAgent, Handler, IntoResponse, Request, RequestExt, Response, - ResponseExt, Result, Transform, + async_trait, Handler, IntoResponse, Request, RequestExt, Response, ResponseExt, Result, + Transform, }; const HTTP_SERVER_ACTIVE_REQUESTS: &str = "http.server.active_requests"; @@ -123,7 +123,7 @@ where attributes.push(HTTP_RESPONSE_STATUS_CODE.i64(i64::from(resp.status().as_u16()))); - self.response_size_size + self.response_size .record(resp.content_length().unwrap_or(0), &attributes); resp @@ -144,30 +144,30 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec { attributes.push(HTTP_ROUTE.string(http_route)); // - attributes.push(HTTP_REQUEST_METHOD.string(req.method())); - attributes.push(NETWORK_PROTOCOL_VERSION.string(req.version())); + attributes.push(HTTP_REQUEST_METHOD.string(req.method().to_string())); + attributes.push(NETWORK_PROTOCOL_VERSION.string(format!("{:?}", req.version()))); let remote_addr = req.remote_addr(); if let Some(remote_addr) = remote_addr { - attributes.push(CLIENT_ADDRESS.string(remote_addr)); + attributes.push(CLIENT_ADDRESS.string(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) + .map_or(true, |remoteip| &remoteip != realip) }) { - attributes.push(CLIENT_SOCKET_ADDRESS.string(realip)); + attributes.push(CLIENT_SOCKET_ADDRESS.string(realip.to_string())); } let uri = req.uri(); if let Some(host) = uri.host() { attributes.push(SERVER_ADDRESS.string(host)); } - if let Some(port) = uri.port_u16().filter(|port| port != 80 && port != 443) { + if let Some(port) = uri.port_u16().filter(|port| *port != 80 && *port != 443) { attributes.push(SERVER_PORT.i64(port as i64)); } - attributes.push(URL_SCHEME.string(uri.schema().unwrap_or(&Scheme::HTTP))); + attributes.push(URL_SCHEME.string(uri.scheme().unwrap_or(&Scheme::HTTP))); attributes } diff --git a/viz-core/src/middleware/otel/tracing.rs b/viz-core/src/middleware/otel/tracing.rs index 789c8401..113811d7 100644 --- a/viz-core/src/middleware/otel/tracing.rs +++ b/viz-core/src/middleware/otel/tracing.rs @@ -16,14 +16,14 @@ use opentelemetry::{ use opentelemetry_semantic_conventions::trace::{ CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, EXCEPTION_MESSAGE, HTTP_REQUEST_BODY_SIZE, HTTP_REQUEST_METHOD, HTTP_RESPONSE_CONTENT_LENGTH, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, - HTTP_STATUS_CODE, NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_PATH, URL_QUERY, - URL_SCHEME, USER_AGENT_ORIGINAL, + NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_PATH, URL_QUERY, URL_SCHEME, + USER_AGENT_ORIGINAL, }; use crate::{ async_trait, header::{HeaderMap, HeaderName}, - headers::{self, HeaderMapExt, UserAgent}, + headers::UserAgent, Handler, IntoResponse, Request, RequestExt, Response, ResponseExt, Result, Transform, }; @@ -160,7 +160,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap { // attributes.insert(HTTP_REQUEST_METHOD, req.method().to_string().into()); - attributes.insert(NETWORK_PROTOCOL_VERSION, req.version().to_string().into()); + attributes.insert(NETWORK_PROTOCOL_VERSION, format!("{:?}", req.version())); let remote_addr = req.remote_addr(); if let Some(remote_addr) = remote_addr { @@ -169,7 +169,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap { if let Some(realip) = req.realip().map(|value| value.0).filter(|realip| { remote_addr .map(SocketAddr::ip) - .map_or(true, |remoteip| remoteip != realip) + .map_or(true, |remoteip| &remoteip != realip) }) { attributes.insert(CLIENT_SOCKET_ADDRESS, realip.to_string().into()); } @@ -178,7 +178,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap { if let Some(host) = uri.host() { attributes.insert(SERVER_ADDRESS, host.to_string().into()); } - if let Some(port) = uri.port_u16().filter(|port| port != 80 && port != 443) { + if let Some(port) = uri.port_u16().filter(|port| *port != 80 && *port != 443) { attributes.insert(SERVER_PORT, port.into()); } @@ -193,10 +193,10 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap { attributes.insert( URL_SCHEME, - uri.schema().unwrap_or(&Scheme::HTTP).to_string().into(), + uri.scheme().unwrap_or(&Scheme::HTTP).to_string().into(), ); - if let Some(content_length) = req.content_length().filter(|len| len > 0) { + if let Some(content_length) = req.content_length().filter(|len| *len > 0) { attributes.insert(HTTP_REQUEST_BODY_SIZE, content_length.into()); } diff --git a/viz-core/src/response.rs b/viz-core/src/response.rs index 35265c10..892dd994 100644 --- a/viz-core/src/response.rs +++ b/viz-core/src/response.rs @@ -140,7 +140,10 @@ pub trait ResponseExt: Sized { impl ResponseExt for Response { fn content_length(&self) -> Option { - self.header(header::CONTENT_LENGTH) + self.headers() + .get(header::CONTENT_LENGTH) + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.parse().ok()) } fn ok(&self) -> bool {