Skip to content

Commit

Permalink
update opentelemetry to v0.22.x (#784)
Browse files Browse the repository at this point in the history
* update opentelemetry to v0.22.x

* update examples to opentelemetry to v0.22.x

* update CHANGELOG.md
  • Loading branch information
miikka authored Mar 27, 2024
1 parent ed0b077 commit accbb3e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
8 changes: 4 additions & 4 deletions examples/poem/opentelemetry-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions poem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions poem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
28 changes: 20 additions & 8 deletions poem/src/middleware/opentelemetry_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Instant;
use libopentelemetry::{
global,
metrics::{Counter, Histogram, Unit},
Key,
Key, KeyValue,
};
use opentelemetry_semantic_conventions::trace;

Expand Down Expand Up @@ -74,8 +74,14 @@ impl<E: Endpoint> Endpoint for OpenTelemetryMetricsEndpoint<E> {

async fn call(&self, req: Request) -> Result<Self::Output> {
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);
Expand All @@ -85,20 +91,26 @@ impl<E: Endpoint> Endpoint for OpenTelemetryMetricsEndpoint<E> {
Ok(resp) => {
if let Some(path_pattern) = resp.data::<PathPattern>() {
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::<PathPattern>() {
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()));
}
}

Expand Down
56 changes: 37 additions & 19 deletions poem/src/middleware/opentelemetry_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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::<PathPattern>() {
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
Expand All @@ -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::<headers::ContentLength>()
{
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)
}
Expand Down

0 comments on commit accbb3e

Please sign in to comment.