Skip to content

Commit

Permalink
Add OTel HTTP support
Browse files Browse the repository at this point in the history
  • Loading branch information
yuandrew committed Dec 10, 2024
1 parent 4a2368d commit f6d0b8e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
12 changes: 12 additions & 0 deletions core-api/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub struct OtelCollectorOptions {
/// Overrides for histogram buckets. Units depend on the value of `use_seconds_for_durations`.
#[builder(default)]
pub histogram_bucket_overrides: HistogramBucketOverrides,
/// Protocol to use for communication with the collector
#[builder(default = "OtlpProtocl::Grpc")]
pub protocol: OtlpProtocl,
}

/// Options for exporting metrics to Prometheus
Expand Down Expand Up @@ -143,6 +146,15 @@ pub enum MetricTemporality {
Delta,
}

/// Options for configuring telemetry
#[derive(Debug, Clone, Copy)]
pub enum OtlpProtocl {
/// Use gRPC to communicate with the collector
Grpc,
/// Use HTTP to communicate with the collector
Http,
}

impl Default for TelemetryOptions {
fn default() -> Self {
TelemetryOptionsBuilder::default().build().unwrap()
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lru = "0.12"
mockall = "0.13"
opentelemetry = { workspace = true, features = ["metrics"], optional = true }
opentelemetry_sdk = { version = "0.26", features = ["rt-tokio", "metrics"], optional = true }
opentelemetry-otlp = { version = "0.26", features = ["tokio", "metrics", "tls"], optional = true }
opentelemetry-otlp = { version = "0.26", features = ["tokio", "metrics", "tls", "http-proto"], optional = true }
opentelemetry-prometheus = { git = "https://github.com/open-telemetry/opentelemetry-rust.git", rev = "e911383", optional = true }
parking_lot = { version = "0.12", features = ["send_guard"] }
pid = "4.0"
Expand Down
33 changes: 22 additions & 11 deletions core/src/telemetry/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use temporal_sdk_core_api::telemetry::{
CoreMeter, Counter, Gauge, GaugeF64, Histogram, HistogramDuration, HistogramF64,
MetricAttributes, MetricParameters, NewAttributes,
},
HistogramBucketOverrides, MetricTemporality, OtelCollectorOptions, PrometheusExporterOptions,
HistogramBucketOverrides, MetricTemporality, OtelCollectorOptions, OtlpProtocl,
PrometheusExporterOptions,
};
use tokio::task::AbortHandle;
use tonic::{metadata::MetadataMap, transport::ClientTlsConfig};
Expand Down Expand Up @@ -120,16 +121,26 @@ pub(super) fn augment_meter_provider_with_defaults(
pub fn build_otlp_metric_exporter(
opts: OtelCollectorOptions,
) -> Result<CoreOtelMeter, anyhow::Error> {
let mut exporter =
opentelemetry_otlp::TonicExporterBuilder::default().with_endpoint(opts.url.to_string());
if opts.url.scheme() == "https" || opts.url.scheme() == "grpcs" {
exporter = exporter.with_tls_config(ClientTlsConfig::new().with_native_roots());
}
let exporter = exporter
.with_metadata(MetadataMap::from_headers((&opts.headers).try_into()?))
.build_metrics_exporter(Box::new(metric_temporality_to_selector(
opts.metric_temporality,
)))?;
let exporter = match opts.protocol {
OtlpProtocl::Grpc => {
let mut exporter = opentelemetry_otlp::TonicExporterBuilder::default()
.with_endpoint(opts.url.to_string());
if opts.url.scheme() == "https" || opts.url.scheme() == "grpcs" {
exporter = exporter.with_tls_config(ClientTlsConfig::new().with_native_roots());
}
exporter
.with_metadata(MetadataMap::from_headers((&opts.headers).try_into()?))
.build_metrics_exporter(Box::new(metric_temporality_to_selector(
opts.metric_temporality,
)))?
}
OtlpProtocl::Http => opentelemetry_otlp::HttpExporterBuilder::default()
.with_endpoint(opts.url.to_string())
.with_headers(opts.headers)
.build_metrics_exporter(Box::new(metric_temporality_to_selector(
opts.metric_temporality,
)))?,
};
let reader = PeriodicReader::builder(exporter, runtime::Tokio)
.with_interval(opts.metric_periodicity)
.build();
Expand Down

0 comments on commit f6d0b8e

Please sign in to comment.