From 5e0edd5a1c504e477b21fba5ee7e98146efb5a92 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Mon, 29 Dec 2025 22:47:16 +0000 Subject: [PATCH] feat: Allow configuring the OTLP protocol --- Cargo.lock | 2 ++ Cargo.toml | 2 +- conduwuit-example.toml | 5 +++++ src/core/config/mod.rs | 9 +++++++++ src/main/logging.rs | 28 ++++++++++++++++++++++++---- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 88817c5a..bd9de1db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3305,6 +3305,8 @@ dependencies = [ "prost", "reqwest", "thiserror 2.0.17", + "tokio", + "tonic", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 42a463da..a0e97452 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -426,7 +426,7 @@ features = ["rt-tokio"] [workspace.dependencies.opentelemetry-otlp] version = "0.31.0" -features = ["http", "trace", "logs", "metrics"] +features = ["http", "grpc-tonic", "trace", "logs", "metrics"] diff --git a/conduwuit-example.toml b/conduwuit-example.toml index fdc74ce9..2a36e921 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -608,6 +608,11 @@ # #otlp_filter = "info" +# Protocol to use for OTLP tracing export. Options are "http" or "grpc". +# The HTTP protocol uses port 4318 by default, while gRPC uses port 4317. +# +#otlp_protocol = "http" + # If the 'perf_measurements' compile-time feature is enabled, enables # collecting folded stack trace profile of tracing spans using # tracing_flame. The resulting profile can be visualized with inferno[1], diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index 20092ef1..e3a447bd 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -737,6 +737,13 @@ pub struct Config { #[serde(default = "default_otlp_filter", alias = "jaeger_filter")] pub otlp_filter: String, + /// Protocol to use for OTLP tracing export. Options are "http" or "grpc". + /// The HTTP protocol uses port 4318 by default, while gRPC uses port 4317. + /// + /// default: "http" + #[serde(default = "default_otlp_protocol")] + pub otlp_protocol: String, + /// If the 'perf_measurements' compile-time feature is enabled, enables /// collecting folded stack trace profile of tracing spans using /// tracing_flame. The resulting profile can be visualized with inferno[1], @@ -2419,6 +2426,8 @@ fn default_otlp_filter() -> String { .to_owned() } +fn default_otlp_protocol() -> String { "http".to_owned() } + fn default_tracing_flame_output_path() -> String { "./tracing.folded".to_owned() } fn default_trusted_servers() -> Vec { diff --git a/src/main/logging.rs b/src/main/logging.rs index 4f1d3e2b..0f76dbc2 100644 --- a/src/main/logging.rs +++ b/src/main/logging.rs @@ -9,6 +9,8 @@ use conduwuit_core::{ }; #[cfg(feature = "otlp_telemetry")] use opentelemetry::trace::TracerProvider; +#[cfg(feature = "otlp_telemetry")] +use opentelemetry_otlp::WithExportConfig; use tracing_subscriber::{EnvFilter, Layer, Registry, fmt, layer::SubscriberExt, reload}; #[cfg(feature = "perf_measurements")] @@ -80,10 +82,28 @@ pub(crate) fn init( opentelemetry_sdk::propagation::TraceContextPropagator::new(), ); - let exporter = opentelemetry_otlp::SpanExporter::builder() - .with_http() - .build() - .expect("Failed to create OTLP exporter"); + let exporter = match config.otlp_protocol.as_str() { + | "grpc" => opentelemetry_otlp::SpanExporter::builder() + .with_tonic() + .with_protocol(opentelemetry_otlp::Protocol::Grpc) + .build() + .expect("Failed to create OTLP gRPC exporter"), + | "http" => opentelemetry_otlp::SpanExporter::builder() + .with_http() + .build() + .expect("Failed to create OTLP HTTP exporter"), + | protocol => { + debug_warn!( + "Invalid OTLP protocol '{}', falling back to HTTP. Valid options are \ + 'http' or 'grpc'.", + protocol + ); + opentelemetry_otlp::SpanExporter::builder() + .with_http() + .build() + .expect("Failed to create OTLP HTTP exporter") + }, + }; let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder() .with_batch_exporter(exporter)