Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to otel v0.23 #157

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ actix-http = { version = "3.0", default-features = false, features = ["compress-
actix-web = { version = "4.0", default-features = false, features = ["compress-zstd"] }
awc = { version = "3.0", optional = true, default-features = false, features = ["compress-zstd"] }
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
opentelemetry = { version = "0.22", default-features = false, features = ["trace"] }
opentelemetry-prometheus = { version = "0.15", optional = true }
opentelemetry-semantic-conventions = "0.14"
opentelemetry = { version = "0.23", default-features = false, features = ["trace"] }
opentelemetry-prometheus = { version = "0.16", optional = true }
opentelemetry-semantic-conventions = "0.15"
prometheus = { version = "0.13", default-features = false, optional = true }
serde = "1.0"

[dev-dependencies]
actix-web = { version = "4.0", features = ["macros"] }
actix-web-opentelemetry = { path = ".", features = ["metrics-prometheus", "sync-middleware", "awc"] }
opentelemetry_sdk = { version = "0.22", features = ["metrics", "rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.21", features = ["rt-tokio-current-thread"] }
opentelemetry-stdout = { version = "0.3", features = ["trace", "metrics"] }
opentelemetry_sdk = { version = "0.23", features = ["metrics", "rt-tokio-current-thread"] }
opentelemetry-otlp = "0.16"
opentelemetry-stdout = { version = "0.4", features = ["trace", "metrics"] }

[package.metadata.docs.rs]
all-features = true
Expand Down
25 changes: 20 additions & 5 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use actix_web_opentelemetry::ClientExt;
use opentelemetry::global;
use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use std::error::Error;
use std::io;
use opentelemetry_otlp::TonicExporterBuilder;
use opentelemetry_sdk::{Resource, trace};
use opentelemetry_sdk::runtime::TokioCurrentThread;

async fn execute_request(client: awc::Client) -> io::Result<String> {
let mut response = client
Expand All @@ -24,11 +27,23 @@ async fn execute_request(client: awc::Client) -> io::Result<String> {

#[actix_web::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Start a new jaeger trace pipeline
// Start a new OTLP trace pipeline
global::set_text_map_propagator(TraceContextPropagator::new());
let _tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("actix_client")
.install_simple()?;

let service_name_resource = Resource::new(vec![KeyValue::new(
"service.name",
"actix_client"
)]);

let _tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(TonicExporterBuilder::default())
.with_trace_config(
trace::Config::default()
.with_resource(service_name_resource)
)
.install_batch(TokioCurrentThread)
.expect("pipeline install error");

let client = awc::Client::new();
let response = execute_request(client).await?;
Expand Down
25 changes: 16 additions & 9 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use actix_web::{web, App, HttpRequest, HttpServer};
use actix_web_opentelemetry::{PrometheusMetricsHandler, RequestMetrics, RequestTracing};
use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::{
metrics::{Aggregation, Instrument, SdkMeterProvider, Stream},
propagation::TraceContextPropagator,
runtime::TokioCurrentThread,
Resource,
};
use opentelemetry_otlp::{TonicExporterBuilder, WithExportConfig};
use opentelemetry_sdk::{metrics::{Aggregation, Instrument, SdkMeterProvider, Stream}, propagation::TraceContextPropagator, runtime::TokioCurrentThread, Resource, trace};

async fn index(_req: HttpRequest, _path: actix_web::web::Path<String>) -> &'static str {
"Hello world!"
}

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Start a new jaeger trace pipeline
// Start a new OTLP trace pipeline
global::set_text_map_propagator(TraceContextPropagator::new());
let _tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("actix_server")

let service_name_resource = Resource::new(vec![KeyValue::new(
"service.name",
"actix_server"
)]);

let _tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(TonicExporterBuilder::default().with_endpoint("http://127.0.0.1:6565"))
.with_trace_config(
trace::Config::default()
.with_resource(service_name_resource)
)
.install_batch(TokioCurrentThread)
.expect("pipeline install error");

Expand Down
11 changes: 5 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ impl InstrumentedClientRequest {
F: FnOnce(ClientRequest) -> R,
R: Future<Output = AwcResult>,
{
let tracer = global::tracer_provider().versioned_tracer(
"actix-web-opentelemetry",
Some(env!("CARGO_PKG_VERSION")),
Some(opentelemetry_semantic_conventions::SCHEMA_URL),
None,
);
let tracer = global::tracer_provider().tracer_builder("actix-web-opentelemetry")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(opentelemetry_semantic_conventions::SCHEMA_URL)
.build();

// Client attributes
// https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#http-client
self.attrs.extend(
Expand Down
10 changes: 4 additions & 6 deletions src/middleware/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ where

fn new_transform(&self, service: S) -> Self::Future {
ok(RequestTracingMiddleware::new(
global::tracer_provider().versioned_tracer(
"actix-web-opentelemetry",
Some(env!("CARGO_PKG_VERSION")),
Some(opentelemetry_semantic_conventions::SCHEMA_URL),
None,
),
global::tracer_provider().tracer_builder("actix-web-opentelemetry")
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url(opentelemetry_semantic_conventions::SCHEMA_URL)
.build(),
service,
self.route_formatter.clone(),
))
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use actix_web::{
};
use opentelemetry::{KeyValue, Value};
use opentelemetry_semantic_conventions::trace::{
CLIENT_ADDRESS, CLIENT_SOCKET_ADDRESS, HTTP_REQUEST_BODY_SIZE, HTTP_REQUEST_METHOD, HTTP_ROUTE,
CLIENT_ADDRESS, NETWORK_PEER_ADDRESS, HTTP_REQUEST_BODY_SIZE, HTTP_REQUEST_METHOD, HTTP_ROUTE,
NETWORK_PROTOCOL_VERSION, SERVER_ADDRESS, SERVER_PORT, URL_PATH, URL_QUERY, URL_SCHEME,
USER_AGENT_ORIGINAL,
};
Expand Down Expand Up @@ -85,7 +85,7 @@ pub(super) fn trace_attributes_from_request(
if let Some(peer_addr) = req.peer_addr().map(|socket| socket.ip().to_string()) {
if Some(peer_addr.as_str()) != remote_addr {
// Client is going through a proxy
attributes.push(KeyValue::new(CLIENT_SOCKET_ADDRESS, peer_addr));
attributes.push(KeyValue::new(NETWORK_PEER_ADDRESS, peer_addr));
}
}
let mut host_parts = conn_info.host().split_terminator(':');
Expand Down
Loading