Skip to content

Commit

Permalink
fix: otel
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed May 25, 2024
1 parent 15c6dcd commit 8d0ad32
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 30 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 4 additions & 12 deletions viz-core/src/middleware/otel/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//!
//! [`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::{
metrics::{Histogram, Meter, Unit, UpDownCounter},
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};
Expand Down Expand Up @@ -155,17 +155,9 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec<KeyValue> {
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() {
Expand Down
19 changes: 5 additions & 14 deletions viz-core/src/middleware/otel/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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::{
Expand Down Expand Up @@ -166,17 +165,9 @@ fn build_attributes(req: &Request, http_route: &str) -> Vec<KeyValue> {
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() {
Expand Down
2 changes: 2 additions & 0 deletions viz-smol/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d0ad32

Please sign in to comment.