Skip to content

Commit

Permalink
Add internal logging to HTTP libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Dec 18, 2024
1 parent 0605341 commit 467dc66
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
with:
components: rustfmt
- uses: arduino/setup-protoc@v3
- name: Run integration tests using docker compose
- name: Run integration tests
run: ./scripts/integration_tests.sh
2 changes: 1 addition & 1 deletion opentelemetry-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## vNext

- Bump msrv to 1.75.0.

- Add "internal-logs" feature flag (enabled by default), and emit internal logs.

## 0.27.0

Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ edition = "2021"
rust-version = "1.75.0"

[features]
default = ["internal-logs"]
hyper = ["dep:http-body-util", "dep:hyper", "dep:hyper-util", "dep:tokio"]
reqwest-rustls = ["reqwest", "reqwest/rustls-tls-native-roots"]
reqwest-rustls-webpki-roots = ["reqwest", "reqwest/rustls-tls-webpki-roots"]
internal-logs = ["tracing", "opentelemetry/internal-logs"]

[dependencies]
async-trait = { workspace = true }
Expand All @@ -24,3 +26,4 @@ hyper-util = { workspace = true, features = ["client-legacy", "http1", "http2"],
opentelemetry = { version = "0.27", path = "../opentelemetry", features = ["trace"] }
reqwest = { workspace = true, features = ["blocking"], optional = true }
tokio = { workspace = true, features = ["time"], optional = true }
tracing = {workspace = true, optional = true}
5 changes: 5 additions & 0 deletions opentelemetry-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ pub trait HttpClient: Debug + Send + Sync {

#[cfg(feature = "reqwest")]
mod reqwest {
use opentelemetry::otel_debug;

use super::{async_trait, Bytes, HttpClient, HttpError, Request, Response};

#[async_trait]
impl HttpClient for reqwest::Client {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
otel_debug!(name: "ReqwestClient.Send");
let request = request.try_into()?;
let mut response = self.execute(request).await?.error_for_status()?;
let headers = std::mem::take(response.headers_mut());
Expand All @@ -87,6 +90,7 @@ mod reqwest {
#[async_trait]
impl HttpClient for reqwest::blocking::Client {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
otel_debug!(name: "ReqwestBlockingClient.Send");
let request = request.try_into()?;
let mut response = self.execute(request)?.error_for_status()?;
let headers = std::mem::take(response.headers_mut());
Expand Down Expand Up @@ -156,6 +160,7 @@ pub mod hyper {
#[async_trait]
impl HttpClient for HyperClient {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
otel_debug!(name: "HyperClient.Send");
let (parts, body) = request.into_parts();
let mut request = Request::from_parts(parts, Body(Full::from(body)));
if let Some(ref authorization) = self.authorization {
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ trace = ["opentelemetry/trace", "opentelemetry_sdk/trace", "opentelemetry-proto/
metrics = ["opentelemetry/metrics", "opentelemetry_sdk/metrics", "opentelemetry-proto/metrics"]
logs = ["opentelemetry/logs", "opentelemetry_sdk/logs", "opentelemetry-proto/logs"]
populate-logs-event-name = ["opentelemetry-proto/populate-logs-event-name"]
internal-logs = ["tracing"]
internal-logs = ["tracing", "opentelemetry/internal-logs"]

# add ons
serialize = ["serde", "serde_json"]
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ experimental_metrics_periodicreader_with_async_runtime = ["opentelemetry_sdk/exp
once_cell = { workspace = true }
opentelemetry = { path = "../../../opentelemetry" }
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "experimental_metrics_periodicreader_with_async_runtime"]}
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "logs"] , default-features = false}
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "logs", "internal-logs"] , default-features = false}
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}

tokio = { workspace = true, features = ["full"] }
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-otlp/src/exporter/http/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use http::{header::CONTENT_TYPE, Method};
use opentelemetry::otel_debug;
use opentelemetry_sdk::export::logs::{LogBatch, LogExporter};
use opentelemetry_sdk::logs::{LogError, LogResult};

Expand Down Expand Up @@ -32,6 +33,7 @@ impl LogExporter for OtlpHttpClient {
}

let request_uri = request.uri().to_string();
otel_debug!(name: "HttpLogsClient.CallingExport");
let response = client.send(request).await?;

if !response.status().is_success() {
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-otlp/src/exporter/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use http::{header::CONTENT_TYPE, Method};
use opentelemetry::otel_debug;
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::metrics::{MetricError, MetricResult};

Expand Down Expand Up @@ -33,6 +34,7 @@ impl MetricsClient for OtlpHttpClient {
request.headers_mut().insert(k.clone(), v.clone());
}

otel_debug!(name: "HttpMetricsClient.CallingExport");
client
.send(request)
.await
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-otlp/src/exporter/http/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use futures_core::future::BoxFuture;
use http::{header::CONTENT_TYPE, Method};
use opentelemetry::trace::TraceError;
use opentelemetry::{otel_debug, trace::TraceError};
use opentelemetry_sdk::export::trace::{ExportResult, SpanData, SpanExporter};

use super::OtlpHttpClient;
Expand Down Expand Up @@ -47,6 +47,7 @@ impl SpanExporter for OtlpHttpClient {

Box::pin(async move {
let request_uri = request.uri().to_string();
otel_debug!(name: "HttpTracesClient.CallingExport");
let response = client.send(request).await?;

if !response.status().is_success() {
Expand Down

0 comments on commit 467dc66

Please sign in to comment.