Skip to content

Commit

Permalink
fix: clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Aug 1, 2023
1 parent c7b0660 commit 7e3f8cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
22 changes: 11 additions & 11 deletions viz-core/src/middleware/otel/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ 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,
HTTP_ROUTE, NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_SCHEME,
};

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";
Expand Down Expand Up @@ -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
Expand All @@ -144,30 +144,30 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec<KeyValue> {
attributes.push(HTTP_ROUTE.string(http_route));

// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#common-attributes>
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
}
16 changes: 8 additions & 8 deletions viz-core/src/middleware/otel/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -160,7 +160,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap<Key, Value> {

// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#common-attributes>
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 {
Expand All @@ -169,7 +169,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap<Key, Value> {
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());
}
Expand All @@ -178,7 +178,7 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap<Key, Value> {
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());
}

Expand All @@ -193,10 +193,10 @@ fn build_attributes(req: &Request, http_route: &str) -> OrderMap<Key, Value> {

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());
}

Expand Down
5 changes: 4 additions & 1 deletion viz-core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ pub trait ResponseExt: Sized {

impl ResponseExt for Response {
fn content_length(&self) -> Option<u64> {
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 {
Expand Down

0 comments on commit 7e3f8cf

Please sign in to comment.