Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Jul 19, 2024
2 parents 75a34ca + e731a73 commit ebe3c1e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 51 deletions.
12 changes: 5 additions & 7 deletions examples/poem/opentelemetry-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ publish.workspace = true
poem = { workspace = true, features = ["opentelemetry"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tracing-subscriber.workspace = true
opentelemetry = { version = "0.23.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio"] }
opentelemetry-http = { version = "0.12.0" }
opentelemetry-otlp = { version = "0.16.0", features = [
"trace",
] }
reqwest = "0.11"
opentelemetry = { version = "0.24.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.24.1", features = ["rt-tokio"] }
opentelemetry-http = { version = "0.13.0" }
opentelemetry-otlp = { version = "0.17.0", features = ["trace"] }
reqwest = "0.12"

[[bin]]
name = "example-opentelemetry-client"
Expand Down
10 changes: 7 additions & 3 deletions examples/poem/opentelemetry-jaeger/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ use opentelemetry::{
Context, KeyValue,
};
use opentelemetry_http::HeaderInjector;
use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource};
use opentelemetry_sdk::{
propagation::TraceContextPropagator,
trace::{Config, TracerProvider},
Resource,
};
use reqwest::{Client, Method, Url};

fn init_tracer() -> Tracer {
fn init_tracer() -> TracerProvider {
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry_sdk::trace::config()
Config::default()
.with_resource(Resource::new(vec![KeyValue::new("service.name", "poem")])),
)
.with_exporter(opentelemetry_otlp::new_exporter().tonic())
Expand Down
15 changes: 10 additions & 5 deletions examples/poem/opentelemetry-jaeger/src/server1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use std::str::FromStr;

use opentelemetry::{
global,
trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _},
trace::{FutureExt, SpanKind, TraceContextExt, Tracer as _, TracerProvider as _},
Context, KeyValue,
};
use opentelemetry_http::HeaderInjector;
use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource};
use opentelemetry_sdk::{
propagation::TraceContextPropagator,
trace::{Config, Tracer, TracerProvider},
Resource,
};
use poem::{
get, handler,
listener::TcpListener,
Expand All @@ -16,12 +20,12 @@ use poem::{
};
use reqwest::{Client, Url};

fn init_tracer() -> Tracer {
fn init_tracer() -> TracerProvider {
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry_sdk::trace::config().with_resource(Resource::new(vec![KeyValue::new(
Config::default().with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"server1",
)])),
Expand Down Expand Up @@ -76,7 +80,8 @@ async fn main() -> Result<(), std::io::Error> {
}
tracing_subscriber::fmt::init();

let tracer = init_tracer();
let tracer_provider = init_tracer();
let tracer = tracer_provider.tracer("server1");

let app = Route::new()
.at("/api1", get(index))
Expand Down
15 changes: 10 additions & 5 deletions examples/poem/opentelemetry-jaeger/src/server2.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource};
use opentelemetry::{global, trace::TracerProvider as _, KeyValue};
use opentelemetry_sdk::{
propagation::TraceContextPropagator,
trace::{Config, TracerProvider},
Resource,
};
use poem::{
get, handler,
listener::TcpListener,
middleware::{OpenTelemetryMetrics, OpenTelemetryTracing},
EndpointExt, Route, Server,
};

fn init_tracer() -> Tracer {
fn init_tracer() -> TracerProvider {
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry_sdk::trace::config().with_resource(Resource::new(vec![KeyValue::new(
Config::default().with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"server2",
)])),
Expand All @@ -34,7 +38,8 @@ async fn main() -> Result<(), std::io::Error> {
}
tracing_subscriber::fmt::init();

let tracer = init_tracer();
let tracer_provider = init_tracer();
let tracer = tracer_provider.tracer("server2");

let app = Route::new()
.at("/api2", get(index))
Expand Down
4 changes: 4 additions & 0 deletions poem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [3.0.3] 2024-07-20

- bump `opentelemetry` from `0.23` to `0.24`

# [3.0.2] 2024-07-19

- bump `opentelemetry` to `0.23`
Expand Down
8 changes: 4 additions & 4 deletions poem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poem"
version = "3.0.2"
version = "3.0.3"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down Expand Up @@ -128,11 +128,11 @@ libcookie = { package = "cookie", version = "0.18", features = [
"key-expansion",
"secure",
], optional = true }
opentelemetry-http = { version = "0.12.0", optional = true }
opentelemetry-semantic-conventions = { version = "0.15.0", optional = true }
opentelemetry-http = { version = "0.13.0", optional = true }
opentelemetry-semantic-conventions = { version = "0.16.0", optional = true }
opentelemetry-prometheus = { version = "0.16.0", optional = true }
libprometheus = { package = "prometheus", version = "0.13.0", optional = true }
libopentelemetry = { package = "opentelemetry", version = "0.23.0", features = [
libopentelemetry = { package = "opentelemetry", version = "0.24.0", features = [
"metrics",
], optional = true }
libtempfile = { package = "tempfile", version = "3.2.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions poem/src/middleware/opentelemetry_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Instant;

use libopentelemetry::{
global,
metrics::{Counter, Histogram, Unit},
metrics::{Counter, Histogram},
Key, KeyValue,
};
use opentelemetry_semantic_conventions::trace;
Expand Down Expand Up @@ -38,7 +38,7 @@ impl OpenTelemetryMetrics {
.init(),
duration: meter
.f64_histogram("poem_request_duration_ms")
.with_unit(Unit::new("milliseconds"))
.with_unit("milliseconds")
.with_description(
"request duration histogram (in milliseconds, since start of service)",
)
Expand Down
35 changes: 10 additions & 25 deletions poem/src/middleware/opentelemetry_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::sync::Arc;

use libopentelemetry::{
global,
propagation::Extractor,
trace::{FutureExt, Span, SpanKind, TraceContextExt, Tracer},
Context, Key, KeyValue,
};
use opentelemetry_semantic_conventions::{resource, trace};
use opentelemetry_http::HeaderExtractor;
use opentelemetry_semantic_conventions::{attribute, resource};

use crate::{
route::PathPattern,
Expand Down Expand Up @@ -52,21 +52,6 @@ pub struct OpenTelemetryTracingEndpoint<T, E> {
inner: E,
}

struct HeaderExtractor<'a>(&'a http::HeaderMap);

impl<'a> Extractor for HeaderExtractor<'a> {
fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).and_then(|value| value.to_str().ok())
}

fn keys(&self) -> Vec<&str> {
self.0
.keys()
.map(|value| value.as_str())
.collect::<Vec<_>>()
}
}

impl<T, E> Endpoint for OpenTelemetryTracingEndpoint<T, E>
where
T: Tracer + Send + Sync,
Expand Down Expand Up @@ -98,16 +83,16 @@ where
));
attributes.push(KeyValue::new(resource::TELEMETRY_SDK_LANGUAGE, "rust"));
attributes.push(KeyValue::new(
trace::HTTP_REQUEST_METHOD,
attribute::HTTP_REQUEST_METHOD,
req.method().to_string(),
));
attributes.push(KeyValue::new(
trace::URL_FULL,
attribute::URL_FULL,
req.original_uri().to_string(),
));
attributes.push(KeyValue::new(trace::CLIENT_ADDRESS, remote_addr));
attributes.push(KeyValue::new(attribute::CLIENT_ADDRESS, remote_addr));
attributes.push(KeyValue::new(
trace::NETWORK_PROTOCOL_VERSION,
attribute::NETWORK_PROTOCOL_VERSION,
format!("{:?}", req.version()),
));

Expand Down Expand Up @@ -135,27 +120,27 @@ where
let resp = resp.into_response();
span.add_event("request.completed".to_string(), vec![]);
span.set_attribute(KeyValue::new(
trace::HTTP_RESPONSE_STATUS_CODE,
attribute::HTTP_RESPONSE_STATUS_CODE,
resp.status().as_u16() as i64,
));
if let Some(content_length) =
resp.headers().typed_get::<headers::ContentLength>()
{
span.set_attribute(KeyValue::new(
trace::HTTP_RESPONSE_BODY_SIZE,
attribute::HTTP_RESPONSE_BODY_SIZE,
content_length.0 as i64,
));
}
Ok(resp)
}
Err(err) => {
span.set_attribute(KeyValue::new(
trace::HTTP_RESPONSE_STATUS_CODE,
attribute::HTTP_RESPONSE_STATUS_CODE,
err.status().as_u16() as i64,
));
span.add_event(
"request.error".to_string(),
vec![KeyValue::new(trace::EXCEPTION_MESSAGE, err.to_string())],
vec![KeyValue::new(attribute::EXCEPTION_MESSAGE, err.to_string())],
);
Err(err)
}
Expand Down

0 comments on commit ebe3c1e

Please sign in to comment.