Skip to content

Commit

Permalink
⬆️♻️ Update open telemetry dependencies (#282)
Browse files Browse the repository at this point in the history
* ⬆️♻️ Update open telemetry dependencies

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>

* ♻️ Use common resource function

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>

* ♻️ Add period reader interval

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>

* ♻️ Trace timeout

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>

---------

Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
  • Loading branch information
evaline-ju authored Jan 21, 2025
1 parent 61388da commit cf5dd83
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 81 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ hyper = { version = "1.5.2", features = ["http1", "http2", "server"] }
hyper-rustls = { version = "0.27.5", features = ["ring"]}
hyper-timeout = "0.5.2"
hyper-util = { version = "0.1.10", features = ["server-auto", "server-graceful", "tokio"] }
opentelemetry = { version = "0.24.0", features = ["trace", "metrics"] }
opentelemetry-http = { version = "0.13.0", features = ["reqwest"] }
opentelemetry-otlp = { version = "0.17.0", features = ["http-proto"] }
opentelemetry_sdk = { version = "0.24.1", features = ["rt-tokio", "metrics"] }
opentelemetry = { version = "0.27.1", features = ["metrics", "trace"] }
opentelemetry-http = { version = "0.27.0", features = ["reqwest"] }
opentelemetry-otlp = { version = "0.27.0", features = ["grpc-tonic", "http-proto"] }
opentelemetry_sdk = { version = "0.27.1", features = ["rt-tokio", "metrics"] }
pin-project-lite = "0.2.16"
prost = "0.13.4"
reqwest = { version = "0.12.12", features = ["blocking", "rustls-tls", "json"] }
Expand All @@ -52,7 +52,7 @@ tonic = { version = "0.12.3", features = ["tls", "tls-roots", "tls-webpki-roots"
tower = { version = "0.5.2", features = ["timeout"] }
tower-http = { version = "0.6.2", features = ["trace"] }
tracing = "0.1.41"
tracing-opentelemetry = "0.25.0"
tracing-opentelemetry = "0.28.0"
tracing-subscriber = { version = "0.3.19", features = ["json", "env-filter"] }
url = "2.5.4"

Expand Down
116 changes: 55 additions & 61 deletions src/utils/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@ use std::time::Duration;
use axum::{extract::Request, http::HeaderMap, response::Response};
use opentelemetry::{
global,
metrics::MetricsError,
trace::{TraceContextExt, TraceError, TraceId, TracerProvider},
KeyValue,
};
use opentelemetry_http::{HeaderExtractor, HeaderInjector};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_otlp::{MetricExporter, SpanExporter, WithExportConfig, WithHttpConfig};
use opentelemetry_sdk::{
metrics::{
reader::{DefaultAggregationSelector, DefaultTemporalitySelector},
SdkMeterProvider,
},
metrics::{MetricError, PeriodicReader, SdkMeterProvider},
propagation::TraceContextPropagator,
runtime,
trace::{Config, Sampler},
trace::Sampler,
Resource,
};
use tracing::{error, info, info_span, Span};
Expand All @@ -48,16 +44,14 @@ pub enum TracingError {
#[error("Error from tracing provider: {0}")]
TraceError(#[from] TraceError),
#[error("Error from metrics provider: {0}")]
MetricsError(#[from] MetricsError),
MetricError(#[from] MetricError),
}

fn service_config(tracing_config: TracingConfig) -> Config {
Config::default()
.with_resource(Resource::new(vec![KeyValue::new(
"service.name",
tracing_config.service_name,
)]))
.with_sampler(Sampler::AlwaysOn)
fn resource(tracing_config: TracingConfig) -> Resource {
Resource::new(vec![KeyValue::new(
"service.name",
tracing_config.service_name,
)])
}

/// Initializes an OpenTelemetry tracer provider with an OTLP export pipeline based on the
Expand All @@ -66,31 +60,34 @@ fn init_tracer_provider(
tracing_config: TracingConfig,
) -> Result<Option<opentelemetry_sdk::trace::TracerProvider>, TracingError> {
if let Some((protocol, endpoint)) = tracing_config.clone().traces {
let timeout = Duration::from_secs(3);
let exporter = match protocol {
OtlpProtocol::Grpc => SpanExporter::builder()
.with_tonic()
.with_endpoint(endpoint)
.with_timeout(timeout)
.build()?,
OtlpProtocol::Http => SpanExporter::builder()
.with_http()
.with_http_client(reqwest::Client::new())
.with_endpoint(endpoint)
.with_timeout(timeout)
.build()?,
};
Ok(Some(
match protocol {
OtlpProtocol::Grpc => opentelemetry_otlp::new_pipeline().tracing().with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(endpoint)
.with_timeout(Duration::from_secs(3)),
),
OtlpProtocol::Http => opentelemetry_otlp::new_pipeline().tracing().with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_http_client(reqwest::Client::new())
.with_endpoint(endpoint)
.with_timeout(Duration::from_secs(3)),
),
}
.with_trace_config(service_config(tracing_config))
.install_batch(runtime::Tokio)?,
opentelemetry_sdk::trace::TracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.with_resource(resource(tracing_config))
.with_sampler(Sampler::AlwaysOn)
.build(),
))
} else if !tracing_config.quiet {
// We still need a tracing provider as long as we are logging in order to enable any
// trace-sensitive logs, such as any mentions of a request's trace_id.
Ok(Some(
opentelemetry_sdk::trace::TracerProvider::builder()
.with_config(service_config(tracing_config))
.with_resource(resource(tracing_config))
.with_sampler(Sampler::AlwaysOn)
.build(),
))
} else {
Expand All @@ -103,34 +100,31 @@ fn init_tracer_provider(
fn init_meter_provider(
tracing_config: TracingConfig,
) -> Result<Option<SdkMeterProvider>, TracingError> {
if let Some((protocol, endpoint)) = tracing_config.metrics {
if let Some((protocol, endpoint)) = tracing_config.clone().metrics {
// Note: DefaultAggregationSelector removed from OpenTelemetry SDK as of 0.26.0
// as custom aggregation should be available in Views. Cumulative temporality is default.
let timeout = Duration::from_secs(10);
let exporter = match protocol {
OtlpProtocol::Grpc => MetricExporter::builder()
.with_tonic()
.with_endpoint(endpoint)
.with_timeout(timeout)
.build()?,
OtlpProtocol::Http => MetricExporter::builder()
.with_http()
.with_http_client(reqwest::Client::new())
.with_endpoint(endpoint)
.with_timeout(timeout)
.build()?,
};
let reader = PeriodicReader::builder(exporter, runtime::Tokio)
.with_interval(Duration::from_secs(3))
.build();
Ok(Some(
match protocol {
OtlpProtocol::Grpc => opentelemetry_otlp::new_pipeline()
.metrics(runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(endpoint),
),
OtlpProtocol::Http => opentelemetry_otlp::new_pipeline()
.metrics(runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_http_client(reqwest::Client::new())
.with_endpoint(endpoint),
),
}
.with_resource(Resource::new(vec![KeyValue::new(
"service.name",
tracing_config.service_name,
)]))
.with_timeout(Duration::from_secs(10))
.with_period(Duration::from_secs(3))
.with_aggregation_selector(DefaultAggregationSelector::new())
.with_temporality_selector(DefaultTemporalitySelector::new())
.build()?,
SdkMeterProvider::builder()
.with_resource(resource(tracing_config))
.with_reader(reader)
.build(),
))
} else {
Ok(None)
Expand Down Expand Up @@ -227,7 +221,7 @@ pub fn init_tracing(
if let Some(meter_provider) = meter_provider {
meter_provider
.shutdown()
.map_err(TracingError::MetricsError)?;
.map_err(TracingError::MetricError)?;
}
Ok(())
})
Expand Down

0 comments on commit cf5dd83

Please sign in to comment.