difftreelog
feat otel exporter ported to base fleet
in: trunk
6 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -642,6 +642,16 @@
]
[[package]]
+name = "core-foundation"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -974,6 +984,8 @@
"nom 8.0.0",
"openssh",
"opentelemetry",
+ "opentelemetry-appender-tracing",
+ "opentelemetry-exporter-env",
"opentelemetry_sdk",
"peg",
"regex",
@@ -1239,8 +1251,10 @@
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
+ "js-sys",
"libc",
"wasi",
+ "wasm-bindgen",
]
[[package]]
@@ -1250,9 +1264,11 @@
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
"cfg-if",
+ "js-sys",
"libc",
"r-efi 5.3.0",
"wasip2",
+ "wasm-bindgen",
]
[[package]]
@@ -1450,6 +1466,23 @@
]
[[package]]
+name = "hyper-rustls"
+version = "0.27.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
+dependencies = [
+ "http",
+ "hyper",
+ "hyper-util",
+ "rustls",
+ "rustls-native-certs",
+ "rustls-pki-types",
+ "tokio",
+ "tokio-rustls",
+ "tower-service",
+]
+
+[[package]]
name = "hyper-timeout"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1905,6 +1938,12 @@
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
+name = "lru-slab"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
+
+[[package]]
name = "matchers"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2123,6 +2162,12 @@
]
[[package]]
+name = "openssl-probe"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
+
+[[package]]
name = "opentelemetry"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2137,6 +2182,18 @@
]
[[package]]
+name = "opentelemetry-appender-tracing"
+version = "0.31.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef6a1ac5ca3accf562b8c306fa8483c85f4390f768185ab775f242f7fe8fdcc2"
+dependencies = [
+ "opentelemetry",
+ "tracing",
+ "tracing-core",
+ "tracing-subscriber",
+]
+
+[[package]]
name = "opentelemetry-exporter-env"
version = "0.1.0"
dependencies = [
@@ -2551,6 +2608,61 @@
]
[[package]]
+name = "quinn"
+version = "0.11.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20"
+dependencies = [
+ "bytes",
+ "cfg_aliases",
+ "pin-project-lite",
+ "quinn-proto",
+ "quinn-udp",
+ "rustc-hash 2.1.1",
+ "rustls",
+ "socket2 0.5.10",
+ "thiserror 2.0.18",
+ "tokio",
+ "tracing",
+ "web-time",
+]
+
+[[package]]
+name = "quinn-proto"
+version = "0.11.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "434b42fec591c96ef50e21e886936e66d3cc3f737104fdb9b737c40ffb94c098"
+dependencies = [
+ "bytes",
+ "getrandom 0.3.4",
+ "lru-slab",
+ "rand 0.9.2",
+ "ring",
+ "rustc-hash 2.1.1",
+ "rustls",
+ "rustls-pki-types",
+ "slab",
+ "thiserror 2.0.18",
+ "tinyvec",
+ "tracing",
+ "web-time",
+]
+
+[[package]]
+name = "quinn-udp"
+version = "0.5.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd"
+dependencies = [
+ "cfg_aliases",
+ "libc",
+ "once_cell",
+ "socket2 0.5.10",
+ "tracing",
+ "windows-sys 0.52.0",
+]
+
+[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2729,16 +2841,22 @@
"http-body",
"http-body-util",
"hyper",
+ "hyper-rustls",
"hyper-util",
"js-sys",
"log",
"percent-encoding",
"pin-project-lite",
+ "quinn",
+ "rustls",
+ "rustls-native-certs",
+ "rustls-pki-types",
"serde",
"serde_json",
"serde_urlencoded",
"sync_wrapper",
"tokio",
+ "tokio-rustls",
"tower 0.5.3",
"tower-http 0.6.8",
"tower-service",
@@ -2898,6 +3016,18 @@
]
[[package]]
+name = "rustls-native-certs"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "612460d5f7bea540c490b2b6395d8e34a953e52b491accd6c86c8164c5932a63"
+dependencies = [
+ "openssl-probe",
+ "rustls-pki-types",
+ "schannel",
+ "security-framework",
+]
+
+[[package]]
name = "rustls-pemfile"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2912,6 +3042,7 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd"
dependencies = [
+ "web-time",
"zeroize",
]
@@ -2957,6 +3088,15 @@
]
[[package]]
+name = "schannel"
+version = "0.1.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939"
+dependencies = [
+ "windows-sys 0.61.2",
+]
+
+[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2989,6 +3129,29 @@
]
[[package]]
+name = "security-framework"
+version = "3.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
+dependencies = [
+ "bitflags",
+ "core-foundation",
+ "core-foundation-sys",
+ "libc",
+ "security-framework-sys",
+]
+
+[[package]]
+name = "security-framework-sys"
+version = "2.17.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
+dependencies = [
+ "core-foundation-sys",
+ "libc",
+]
+
+[[package]]
name = "self_cell"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3455,6 +3618,21 @@
]
[[package]]
+name = "tinyvec"
+version = "1.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3e61e67053d25a4e82c844e8424039d9745781b3fc4f32b8d55ed50f5f667ef3"
+dependencies = [
+ "tinyvec_macros",
+]
+
+[[package]]
+name = "tinyvec_macros"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
+
+[[package]]
name = "tokio"
version = "1.50.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,6 +11,7 @@
fleet-shared = { path = "./crates/fleet-shared" }
nix-eval = { path = "./crates/nix-eval" }
nixlike = { path = "./crates/nixlike" }
+opentelemetry-exporter-env = { path = "./crates/opentelemetry-exporter-env" }
abort-on-drop = "0.2"
age = { version = "0.11", features = ["plugin", "ssh"] }
@@ -40,8 +41,9 @@
nix = { version = "0.31.2", features = ["fs", "user"] }
nom = "8.0.0"
opentelemetry = "0.31.0"
-opentelemetry-otlp = { version = "0.31.0", features = ["grpc-tonic", "gzip-tonic", "http-json"] }
+opentelemetry-otlp = { version = "0.31.0", features = ["grpc-tonic", "gzip-tonic", "http-json", "reqwest-rustls"] }
opentelemetry_sdk = "0.31.0"
+opentelemetry-appender-tracing = "0.31.1"
openssh = "0.11.5"
peg = "0.8.5"
pkg-config = "0.3.30"
cmds/fleet/Cargo.tomldiffbeforeafterboth--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -48,6 +48,8 @@
thiserror.workspace = true
tracing-indicatif = { workspace = true, optional = true }
tracing-opentelemetry.workspace = true
+opentelemetry-exporter-env.workspace = true
+opentelemetry-appender-tracing.workspace = true
[features]
default = ["indicatif"]
cmds/fleet/src/main.rsdiffbeforeafterboth--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -25,6 +25,12 @@
use nix_eval::{
gc_register_my_thread, gc_unregister_my_thread, init_libraries, init_tokio_for_nix,
};
+use opentelemetry::trace::TracerProvider;
+use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
+use opentelemetry_exporter_env::{
+ OtlpBaseSettings, OtlpLogsSettings, OtlpTracesSettings, ResolvedOtlpSettings,
+};
+use opentelemetry_sdk::{logs::SdkLoggerProvider, trace::SdkTracerProvider};
use tracing::{Instrument, error, info, info_span};
#[cfg(feature = "indicatif")]
use tracing_indicatif::IndicatifLayer;
@@ -96,6 +102,14 @@
fleet_opts: FleetOpts,
#[clap(subcommand)]
command: Opts,
+ #[clap(long, next_help_heading = "Telemetry", env = "OTEL_FLEET")]
+ otel: bool,
+ #[clap(flatten)]
+ otlp_base: OtlpBaseSettings,
+ #[clap(flatten)]
+ otel_logs: OtlpLogsSettings,
+ #[clap(flatten)]
+ otel_traces: OtlpTracesSettings,
}
async fn run_command(config: &Config, opts: FleetOpts, command: Opts) -> Result<()> {
@@ -115,7 +129,7 @@
Ok(())
}
-fn setup_logging() {
+fn setup_logging(opts: &RootOpts) -> Result<()> {
#[cfg(feature = "indicatif")]
let indicatif_layer = {
use std::time::Duration;
@@ -173,12 +187,35 @@
sub.with_filter(filter) // .without,
});
- if env::var_os("FLEET_OTEL").is_some() {}
-
- // #[cfg(feature = "indicatif")]
#[cfg(feature = "indicatif")]
let reg = reg.with(indicatif_layer);
- reg.init();
+
+ if opts.otel {
+ let traces = ResolvedOtlpSettings::traces(&opts.otlp_base, &opts.otel_traces)?;
+ let span_exporter = traces.span_exporter()?;
+ let logs = ResolvedOtlpSettings::logs(&opts.otlp_base, &opts.otel_logs)?;
+ let log_exporter = logs.log_exporter()?;
+
+ let span_provider = SdkTracerProvider::builder()
+ .with_batch_exporter(span_exporter)
+ .build();
+ let log_provider = SdkLoggerProvider::builder()
+ .with_batch_exporter(log_exporter)
+ .build();
+
+ let logger = OpenTelemetryTracingBridge::new(&log_provider);
+ let tracer = span_provider.tracer("fleet");
+
+ let reg = reg
+ .with(tracing_opentelemetry::layer().with_tracer(tracer))
+ .with(logger);
+
+ reg.init();
+ } else {
+ reg.init();
+ };
+
+ Ok(())
}
fn main() -> ExitCode {
@@ -188,7 +225,10 @@
return ExitCode::SUCCESS;
}
- setup_logging();
+ if let Err(e) = setup_logging(&opts) {
+ eprintln!("{e:#}");
+ return ExitCode::FAILURE;
+ }
init_libraries();
crates/fleet-base/src/opts.rsdiffbeforeafterboth--- a/crates/fleet-base/src/opts.rs
+++ b/crates/fleet-base/src/opts.rs
@@ -96,7 +96,7 @@
#[clap(long, default_value = env!("NIX_SYSTEM"))]
pub local_system: String,
- /// By default fleet continues on single derivation build failure
+ /// By default fleet continues on single derivation build failure;
/// this flag makes command fail immediately
///
/// Opposite of Nix's --keep-going
crates/opentelemetry-exporter-env/src/lib.rsdiffbeforeafterboth1use std::convert::Infallible;2use std::env::{self, VarError};3use std::ffi::OsString;4use std::num::ParseIntError;5use std::str::FromStr;6use std::time::Duration;78#[cfg(feature = "otlp")]9mod otlp;1011#[derive(thiserror::Error, Debug)]12pub enum Error {13 #[error("environment variable {env} contains invalid UTF-8: {value:?}")]14 InvalidUtf8 {15 env: &'static str,16 value: OsString,17 },18 #[error("environment variable {env}={value:?}: {error}")]19 EnvParse {20 env: &'static str,21 value: String,22 error: &'static str,23 },24 #[error("environment variable {env}={value:?}: {error}")]25 EnvParseInt {26 env: &'static str,27 value: String,28 error: ParseIntError,29 },30 #[cfg(feature = "otlp")]31 #[error("failed to build exporter: {0}")]32 Exporter(#[from] opentelemetry_otlp::ExporterBuildError),33}3435impl From<(&'static str, &'static str, String)> for Error {36 fn from((env, error, value): (&'static str, &'static str, String)) -> Self {37 Self::EnvParse { env, value, error }38 }39}40impl From<(&'static str, ParseIntError, String)> for Error {41 fn from((env, error, value): (&'static str, ParseIntError, String)) -> Self {42 Self::EnvParseInt { env, value, error }43 }44}45impl From<(&'static str, Infallible, String)> for Error {46 fn from(_v: (&'static str, Infallible, String)) -> Self {47 unreachable!()48 }49}5051fn load_env<T>(env: &'static str) -> Result<Option<T>, Error>52where53 T: FromStr,54 Error: From<(&'static str, <T as FromStr>::Err, String)>,55{56 match env::var(env) {57 Ok(v) => Ok(Some(T::from_str(&v).map_err(|err| (env, err, v))?)),58 Err(VarError::NotPresent) => Ok(None),59 Err(VarError::NotUnicode(value)) => Err(Error::InvalidUtf8 { env, value }),60 }61}6263macro_rules! impl_enum {64 (enum $id:ident {65 $(66 #[name = $value:literal]67 $var:ident,68 )*69 }) => {70 #[derive(Clone, Copy)]71 #[cfg_attr(feature = "clap", derive(clap::ValueEnum))]72 pub enum $id {73 $(74 #[cfg_attr(feature = "clap", value(name = $value))]75 $var,76 )*77 }78 impl FromStr for $id {79 type Err = &'static str;8081 fn from_str(s: &str) -> Result<Self, Self::Err> {82 Ok(match s {83 $(84 $value => Self::$var,85 )*86 _ => return Err("unsupported value")87 })88 }89 }90 };91}9293impl_enum! {94 enum ExporterKind {95 #[name = "otlp"]96 Otlp,97 #[name = "none"]98 None,99 }100}101102#[derive(Default)]103#[cfg_attr(feature = "clap", derive(clap::Parser))]104pub struct SignalExporterSettings {105 /// Traces exporter to be used.106 #[cfg_attr(feature = "clap", arg(long = "otel-traces-exporter", env = "OTEL_TRACES_EXPORTER", value_enum))]107 pub traces: Option<ExporterKind>,108 /// Metrics exporter to be used.109 #[cfg_attr(feature = "clap", arg(long = "otel-metrics-exporter", env = "OTEL_METRICS_EXPORTER", value_enum))]110 pub metrics: Option<ExporterKind>,111 /// Logs exporter to be used.112 #[cfg_attr(feature = "clap", arg(long = "otel-logs-exporter", env = "OTEL_LOGS_EXPORTER", value_enum))]113 pub logs: Option<ExporterKind>,114}115116impl SignalExporterSettings {117 pub fn from_env() -> Result<Self, Error> {118 Ok(Self {119 traces: load_env("OTEL_TRACES_EXPORTER")?,120 metrics: load_env("OTEL_METRICS_EXPORTER")?,121 logs: load_env("OTEL_LOGS_EXPORTER")?,122 })123 }124125 pub fn traces_enabled(&self) -> bool {126 !matches!(self.traces, Some(ExporterKind::None))127 }128 pub fn metrics_enabled(&self) -> bool {129 !matches!(self.metrics, Some(ExporterKind::None))130 }131 pub fn logs_enabled(&self) -> bool {132 !matches!(self.logs, Some(ExporterKind::None))133 }134}135136impl_enum! {137 enum Compression {138 #[name = "gzip"]139 Gzip,140 #[name = "zstd"]141 Zstd,142 }143}144#[cfg(feature = "otlp")]145impl From<Compression> for opentelemetry_otlp::Compression {146 fn from(value: Compression) -> Self {147 match value {148 Compression::Gzip => opentelemetry_otlp::Compression::Gzip,149 Compression::Zstd => opentelemetry_otlp::Compression::Zstd,150 }151 }152}153154impl_enum! {155 enum OtlpProtocol {156 #[name = "grpc"]157 Grpc,158 #[name = "http/protobuf"]159 HttpProtobuf,160 #[name = "http/json"]161 HttpJson,162 }163}164#[cfg(feature = "otlp")]165impl From<OtlpProtocol> for opentelemetry_otlp::Protocol {166 fn from(value: OtlpProtocol) -> Self {167 match value {168 OtlpProtocol::Grpc => opentelemetry_otlp::Protocol::Grpc,169 OtlpProtocol::HttpProtobuf => opentelemetry_otlp::Protocol::HttpBinary,170 OtlpProtocol::HttpJson => opentelemetry_otlp::Protocol::HttpJson,171 }172 }173}174175pub trait OtlpSignalSettings {176 fn compression(&self) -> Option<Compression>;177 fn endpoint(&self) -> Option<&str>;178 fn headers(&self) -> Option<&str>;179 fn protocol(&self) -> Option<OtlpProtocol>;180 fn timeout(&self) -> Option<u64>;181}182183macro_rules! impl_settings {184 (185 #[name($env_prefix:literal, $long_prefix:literal)]186 struct $id:ident {187 $(188 $(#[doc = $doc:literal])*189 #[name($env:literal, $long:literal)]190 $(#[arg($($tt:tt)*)])?191 $name:ident: $ty:ty,192 )*193 }) => {194 #[derive(Default)]195 #[cfg_attr(feature = "clap", derive(clap::Parser))]196 pub struct $id {197 $(198 $(#[doc = $doc])*199 #[cfg_attr(feature = "clap", arg(long = concat!("otel-exporter-otlp-", $long_prefix, $long), env = concat!("OTEL_EXPORTER_OTLP_", $env_prefix, $env) $(, $($tt)*)?))]200 pub $name: Option<$ty>,201 )*202 }203 impl $id {204 pub fn from_env() -> Result<Self, Error> {205 Ok(Self {206 $(207 $name: load_env(concat!("OTEL_EXPORTER_OTLP_", $env_prefix, $env))?,208 )*209 })210 }211 }212 impl OtlpSignalSettings for $id {213 fn compression(&self) -> Option<Compression> { self.compression }214 fn endpoint(&self) -> Option<&str> { self.endpoint.as_deref() }215 fn headers(&self) -> Option<&str> { self.headers.as_deref() }216 fn protocol(&self) -> Option<OtlpProtocol> { self.protocol }217 fn timeout(&self) -> Option<u64> { self.timeout }218 }219 }220}221222impl_settings! {223 #[name("", "")]224 struct OtlpBaseSettings {225 /// Specifies the OTLP transport compression to be used for all telemetry data.226 #[name("COMPRESSION", "compression")]227 #[arg(value_enum)]228 compression: Compression,229 /// A base endpoint URL for any signal type, with an optionally-specified port number. Helpful for when you're sending more than one signal to the same endpoint and want one environment variable to control the endpoint.230 #[name("ENDPOINT", "endpoint")]231 endpoint: String,232 /// A list of headers to apply to all outgoing data (traces, metrics, and logs).233 #[name("HEADERS", "headers")]234 headers: String,235 /// Specifies the OTLP transport protocol to be used for all telemetry data.236 #[name("PROTOCOL", "protocol")]237 #[arg(value_enum)]238 protocol: OtlpProtocol,239 /// The timeout value for all outgoing data (traces, metrics, and logs) in milliseconds.240 #[name("TIMEOUT", "timeout")]241 timeout: u64,242 }243}244impl_settings! {245 #[name("LOGS_", "logs-")]246 struct OtlpLogsSettings {247 /// Specifies the OTLP transport compression to be used for log data.248 #[name("COMPRESSION", "compression")]249 #[arg(value_enum)]250 compression: Compression,251 /// Endpoint URL for log data only, with an optionally-specified port number. Typically ends with `v1/logs` when using OTLP/HTTP.252 #[name("ENDPOINT", "endpoint")]253 endpoint: String,254 /// A list of headers to apply to all outgoing logs.255 #[name("HEADERS", "headers")]256 headers: String,257 /// Specifies the OTLP transport protocol to be used for log data.258 #[name("PROTOCOL", "protocol")]259 #[arg(value_enum)]260 protocol: OtlpProtocol,261 /// The timeout value for all outgoing logs in milliseconds.262 #[name("TIMEOUT", "timeout")]263 timeout: u64,264 }265}266impl_settings! {267 #[name("METRICS_", "metrics-")]268 struct OtlpMetricsSettings {269 /// Specifies the OTLP transport compression to be used for metrics data.270 #[name("COMPRESSION", "compression")]271 #[arg(value_enum)]272 compression: Compression,273 /// Endpoint URL for metric data only, with an optionally-specified port number. Typically ends with `v1/metrics` when using OTLP/HTTP.274 #[name("ENDPOINT", "endpoint")]275 endpoint: String,276 /// A list of headers to apply to all outgoing metrics.277 #[name("HEADERS", "headers")]278 headers: String,279 /// Specifies the OTLP transport protocol to be used for metrics data.280 #[name("PROTOCOL", "protocol")]281 #[arg(value_enum)]282 protocol: OtlpProtocol,283 /// The timeout value for all outgoing metrics in milliseconds.284 #[name("TIMEOUT", "timeout")]285 timeout: u64,286 }287}288impl_settings! {289 #[name("TRACES_", "traces-")]290 struct OtlpTracesSettings {291 /// Specifies the OTLP transport compression to be used for trace data.292 #[name("COMPRESSION", "compression")]293 #[arg(value_enum)]294 compression: Compression,295 /// Endpoint URL for trace data only, with an optionally-specified port number. Typically ends with `v1/traces` when using OTLP/HTTP.296 #[name("ENDPOINT", "endpoint")]297 endpoint: String,298 /// A list of headers to apply to all outgoing traces.299 #[name("HEADERS", "headers")]300 headers: String,301 /// Specifies the OTLP transport protocol to be used for trace data.302 #[name("PROTOCOL", "protocol")]303 #[arg(value_enum)]304 protocol: OtlpProtocol,305 /// The timeout value for all outgoing traces in milliseconds.306 #[name("TIMEOUT", "timeout")]307 timeout: u64,308 }309}310311pub struct ResolvedOtlpSettings {312 pub compression: Option<Compression>,313 pub endpoint: String,314 pub headers: Option<String>,315 pub protocol: OtlpProtocol,316 pub timeout: Duration,317}318319impl ResolvedOtlpSettings {320 const DEFAULT_TIMEOUT_MS: u64 = 10000;321 const DEFAULT_GRPC_ENDPOINT: &str = "http://localhost:4317";322 const DEFAULT_HTTP_ENDPOINT: &str = "http://localhost:4318";323324 pub fn traces(325 base: &impl OtlpSignalSettings,326 signal: &impl OtlpSignalSettings,327 ) -> Result<Self, Error> {328 Self::resolve(base, signal, "/v1/traces")329 }330331 pub fn metrics(332 base: &impl OtlpSignalSettings,333 signal: &impl OtlpSignalSettings,334 ) -> Result<Self, Error> {335 Self::resolve(base, signal, "/v1/metrics")336 }337338 pub fn logs(339 base: &impl OtlpSignalSettings,340 signal: &impl OtlpSignalSettings,341 ) -> Result<Self, Error> {342 Self::resolve(base, signal, "/v1/logs")343 }344345 fn resolve(346 base: &impl OtlpSignalSettings,347 signal: &impl OtlpSignalSettings,348 signal_path: &str,349 ) -> Result<Self, Error> {350 let protocol = signal351 .protocol()352 .or_else(|| base.protocol())353 .unwrap_or(OtlpProtocol::HttpProtobuf);354355 let endpoint = if let Some(ep) = signal.endpoint() {356 ep.to_owned()357 } else if let Some(ep) = base.endpoint() {358 match protocol {359 OtlpProtocol::Grpc => ep.to_owned(),360 _ => format!("{ep}{signal_path}"),361 }362 } else {363 match protocol {364 OtlpProtocol::Grpc => Self::DEFAULT_GRPC_ENDPOINT.to_owned(),365 _ => format!("{}{signal_path}", Self::DEFAULT_HTTP_ENDPOINT),366 }367 };368369 Ok(Self {370 compression: signal.compression().or_else(|| base.compression()),371 endpoint,372 headers: signal373 .headers()374 .or_else(|| base.headers())375 .map(str::to_owned),376 protocol,377 timeout: Duration::from_millis(378 signal379 .timeout()380 .or_else(|| base.timeout())381 .unwrap_or(Self::DEFAULT_TIMEOUT_MS),382 ),383 })384 }385}1use std::convert::Infallible;2use std::env::{self, VarError};3use std::ffi::OsString;4use std::num::ParseIntError;5use std::str::FromStr;6use std::time::Duration;78#[cfg(feature = "otlp")]9mod otlp;1011#[derive(thiserror::Error, Debug)]12pub enum Error {13 #[error("environment variable {env} contains invalid UTF-8: {value:?}")]14 InvalidUtf8 {15 env: &'static str,16 value: OsString,17 },18 #[error("environment variable {env}={value:?}: {error}")]19 EnvParse {20 env: &'static str,21 value: String,22 error: &'static str,23 },24 #[error("environment variable {env}={value:?}: {error}")]25 EnvParseInt {26 env: &'static str,27 value: String,28 error: ParseIntError,29 },30 #[cfg(feature = "otlp")]31 #[error("failed to build exporter: {0}")]32 Exporter(#[from] opentelemetry_otlp::ExporterBuildError),33}3435impl From<(&'static str, &'static str, String)> for Error {36 fn from((env, error, value): (&'static str, &'static str, String)) -> Self {37 Self::EnvParse { env, value, error }38 }39}40impl From<(&'static str, ParseIntError, String)> for Error {41 fn from((env, error, value): (&'static str, ParseIntError, String)) -> Self {42 Self::EnvParseInt { env, value, error }43 }44}45impl From<(&'static str, Infallible, String)> for Error {46 fn from(_v: (&'static str, Infallible, String)) -> Self {47 unreachable!()48 }49}5051fn load_env<T>(env: &'static str) -> Result<Option<T>, Error>52where53 T: FromStr,54 Error: From<(&'static str, <T as FromStr>::Err, String)>,55{56 match env::var(env) {57 Ok(v) => Ok(Some(T::from_str(&v).map_err(|err| (env, err, v))?)),58 Err(VarError::NotPresent) => Ok(None),59 Err(VarError::NotUnicode(value)) => Err(Error::InvalidUtf8 { env, value }),60 }61}6263macro_rules! impl_enum {64 (enum $id:ident {65 $(66 #[name = $value:literal]67 $var:ident,68 )*69 }) => {70 #[derive(Clone, Copy)]71 #[cfg_attr(feature = "clap", derive(clap::ValueEnum))]72 pub enum $id {73 $(74 #[cfg_attr(feature = "clap", value(name = $value))]75 $var,76 )*77 }78 impl FromStr for $id {79 type Err = &'static str;8081 fn from_str(s: &str) -> Result<Self, Self::Err> {82 Ok(match s {83 $(84 $value => Self::$var,85 )*86 _ => return Err("unsupported value")87 })88 }89 }90 };91}9293impl_enum! {94 enum ExporterKind {95 #[name = "otlp"]96 Otlp,97 #[name = "none"]98 None,99 }100}101102#[derive(Default)]103#[cfg_attr(feature = "clap", derive(clap::Parser))]104pub struct SignalExporterSettings {105 /// Traces exporter to be used.106 #[cfg_attr(feature = "clap", arg(long = "otel-traces-exporter", env = "OTEL_TRACES_EXPORTER", value_enum))]107 pub traces: Option<ExporterKind>,108 /// Metrics exporter to be used.109 #[cfg_attr(feature = "clap", arg(long = "otel-metrics-exporter", env = "OTEL_METRICS_EXPORTER", value_enum))]110 pub metrics: Option<ExporterKind>,111 /// Logs exporter to be used.112 #[cfg_attr(feature = "clap", arg(long = "otel-logs-exporter", env = "OTEL_LOGS_EXPORTER", value_enum))]113 pub logs: Option<ExporterKind>,114}115116impl SignalExporterSettings {117 pub fn from_env() -> Result<Self, Error> {118 Ok(Self {119 traces: load_env("OTEL_TRACES_EXPORTER")?,120 metrics: load_env("OTEL_METRICS_EXPORTER")?,121 logs: load_env("OTEL_LOGS_EXPORTER")?,122 })123 }124125 pub fn traces_enabled(&self) -> bool {126 !matches!(self.traces, Some(ExporterKind::None))127 }128 pub fn metrics_enabled(&self) -> bool {129 !matches!(self.metrics, Some(ExporterKind::None))130 }131 pub fn logs_enabled(&self) -> bool {132 !matches!(self.logs, Some(ExporterKind::None))133 }134}135136impl_enum! {137 enum Compression {138 #[name = "gzip"]139 Gzip,140 #[name = "zstd"]141 Zstd,142 }143}144#[cfg(feature = "otlp")]145impl From<Compression> for opentelemetry_otlp::Compression {146 fn from(value: Compression) -> Self {147 match value {148 Compression::Gzip => opentelemetry_otlp::Compression::Gzip,149 Compression::Zstd => opentelemetry_otlp::Compression::Zstd,150 }151 }152}153154impl_enum! {155 enum OtlpProtocol {156 #[name = "grpc"]157 Grpc,158 #[name = "http/protobuf"]159 HttpProtobuf,160 #[name = "http/json"]161 HttpJson,162 }163}164#[cfg(feature = "otlp")]165impl From<OtlpProtocol> for opentelemetry_otlp::Protocol {166 fn from(value: OtlpProtocol) -> Self {167 match value {168 OtlpProtocol::Grpc => opentelemetry_otlp::Protocol::Grpc,169 OtlpProtocol::HttpProtobuf => opentelemetry_otlp::Protocol::HttpBinary,170 OtlpProtocol::HttpJson => opentelemetry_otlp::Protocol::HttpJson,171 }172 }173}174175pub trait OtlpSignalSettings {176 fn compression(&self) -> Option<Compression>;177 fn endpoint(&self) -> Option<&str>;178 fn headers(&self) -> Option<&str>;179 fn protocol(&self) -> Option<OtlpProtocol>;180 fn timeout(&self) -> Option<u64>;181}182183macro_rules! impl_settings {184 (185 #[name($env_prefix:literal, $long_prefix:literal)]186 struct $id:ident {187 $(188 $(#[doc = $doc:literal])*189 #[name($env:literal, $long:literal)]190 $(#[arg($($tt:tt)*)])?191 $name:ident: $ty:ty,192 )*193 }) => {194 #[derive(Default)]195 #[cfg_attr(feature = "clap", derive(clap::Parser))]196 pub struct $id {197 $(198 $(#[doc = $doc])*199 #[cfg_attr(feature = "clap", arg(200 long = concat!("otel-exporter-otlp-", $long_prefix, $long),201 id = concat!("otel-exporter-otlp-", $long_prefix, $long),202 env = concat!("OTEL_EXPORTER_OTLP_", $env_prefix, $env)203 $(, $($tt)*)?)204 )]205 pub $name: Option<$ty>,206 )*207 }208 impl $id {209 pub fn from_env() -> Result<Self, Error> {210 Ok(Self {211 $(212 $name: load_env(concat!("OTEL_EXPORTER_OTLP_", $env_prefix, $env))?,213 )*214 })215 }216 }217 impl OtlpSignalSettings for $id {218 fn compression(&self) -> Option<Compression> { self.compression }219 fn endpoint(&self) -> Option<&str> { self.endpoint.as_deref() }220 fn headers(&self) -> Option<&str> { self.headers.as_deref() }221 fn protocol(&self) -> Option<OtlpProtocol> { self.protocol }222 fn timeout(&self) -> Option<u64> { self.timeout }223 }224 }225}226227impl_settings! {228 #[name("", "")]229 struct OtlpBaseSettings {230 /// Specifies the OTLP transport compression to be used for all telemetry data.231 #[name("COMPRESSION", "compression")]232 #[arg(value_enum)]233 compression: Compression,234 /// A base endpoint URL for any signal type, with an optionally-specified port number. Helpful for when you're sending more than one signal to the same endpoint and want one environment variable to control the endpoint.235 #[name("ENDPOINT", "endpoint")]236 endpoint: String,237 /// A list of headers to apply to all outgoing data (traces, metrics, and logs).238 #[name("HEADERS", "headers")]239 headers: String,240 /// Specifies the OTLP transport protocol to be used for all telemetry data.241 #[name("PROTOCOL", "protocol")]242 #[arg(value_enum)]243 protocol: OtlpProtocol,244 /// The timeout value for all outgoing data (traces, metrics, and logs) in milliseconds.245 #[name("TIMEOUT", "timeout")]246 timeout: u64,247 }248}249impl_settings! {250 #[name("LOGS_", "logs-")]251 struct OtlpLogsSettings {252 /// Specifies the OTLP transport compression to be used for log data.253 #[name("COMPRESSION", "compression")]254 #[arg(value_enum)]255 compression: Compression,256 /// Endpoint URL for log data only, with an optionally-specified port number. Typically ends with `v1/logs` when using OTLP/HTTP.257 #[name("ENDPOINT", "endpoint")]258 endpoint: String,259 /// A list of headers to apply to all outgoing logs.260 #[name("HEADERS", "headers")]261 headers: String,262 /// Specifies the OTLP transport protocol to be used for log data.263 #[name("PROTOCOL", "protocol")]264 #[arg(value_enum)]265 protocol: OtlpProtocol,266 /// The timeout value for all outgoing logs in milliseconds.267 #[name("TIMEOUT", "timeout")]268 timeout: u64,269 }270}271impl_settings! {272 #[name("METRICS_", "metrics-")]273 struct OtlpMetricsSettings {274 /// Specifies the OTLP transport compression to be used for metrics data.275 #[name("COMPRESSION", "compression")]276 #[arg(value_enum)]277 compression: Compression,278 /// Endpoint URL for metric data only, with an optionally-specified port number. Typically ends with `v1/metrics` when using OTLP/HTTP.279 #[name("ENDPOINT", "endpoint")]280 endpoint: String,281 /// A list of headers to apply to all outgoing metrics.282 #[name("HEADERS", "headers")]283 headers: String,284 /// Specifies the OTLP transport protocol to be used for metrics data.285 #[name("PROTOCOL", "protocol")]286 #[arg(value_enum)]287 protocol: OtlpProtocol,288 /// The timeout value for all outgoing metrics in milliseconds.289 #[name("TIMEOUT", "timeout")]290 timeout: u64,291 }292}293impl_settings! {294 #[name("TRACES_", "traces-")]295 struct OtlpTracesSettings {296 /// Specifies the OTLP transport compression to be used for trace data.297 #[name("COMPRESSION", "compression")]298 #[arg(value_enum)]299 compression: Compression,300 /// Endpoint URL for trace data only, with an optionally-specified port number. Typically ends with `v1/traces` when using OTLP/HTTP.301 #[name("ENDPOINT", "endpoint")]302 endpoint: String,303 /// A list of headers to apply to all outgoing traces.304 #[name("HEADERS", "headers")]305 headers: String,306 /// Specifies the OTLP transport protocol to be used for trace data.307 #[name("PROTOCOL", "protocol")]308 #[arg(value_enum)]309 protocol: OtlpProtocol,310 /// The timeout value for all outgoing traces in milliseconds.311 #[name("TIMEOUT", "timeout")]312 timeout: u64,313 }314}315316pub struct ResolvedOtlpSettings {317 pub compression: Option<Compression>,318 pub endpoint: String,319 pub headers: Option<String>,320 pub protocol: OtlpProtocol,321 pub timeout: Duration,322}323324impl ResolvedOtlpSettings {325 const DEFAULT_TIMEOUT_MS: u64 = 10000;326 const DEFAULT_GRPC_ENDPOINT: &str = "http://localhost:4317";327 const DEFAULT_HTTP_ENDPOINT: &str = "http://localhost:4318";328329 pub fn traces(330 base: &impl OtlpSignalSettings,331 signal: &impl OtlpSignalSettings,332 ) -> Result<Self, Error> {333 Self::resolve(base, signal, "/v1/traces")334 }335336 pub fn metrics(337 base: &impl OtlpSignalSettings,338 signal: &impl OtlpSignalSettings,339 ) -> Result<Self, Error> {340 Self::resolve(base, signal, "/v1/metrics")341 }342343 pub fn logs(344 base: &impl OtlpSignalSettings,345 signal: &impl OtlpSignalSettings,346 ) -> Result<Self, Error> {347 Self::resolve(base, signal, "/v1/logs")348 }349350 fn resolve(351 base: &impl OtlpSignalSettings,352 signal: &impl OtlpSignalSettings,353 signal_path: &str,354 ) -> Result<Self, Error> {355 let protocol = signal356 .protocol()357 .or_else(|| base.protocol())358 .unwrap_or(OtlpProtocol::HttpProtobuf);359360 let endpoint = if let Some(ep) = signal.endpoint() {361 ep.to_owned()362 } else if let Some(ep) = base.endpoint() {363 match protocol {364 OtlpProtocol::Grpc => ep.to_owned(),365 _ => format!("{ep}{signal_path}"),366 }367 } else {368 match protocol {369 OtlpProtocol::Grpc => Self::DEFAULT_GRPC_ENDPOINT.to_owned(),370 _ => format!("{}{signal_path}", Self::DEFAULT_HTTP_ENDPOINT),371 }372 };373374 Ok(Self {375 compression: signal.compression().or_else(|| base.compression()),376 endpoint,377 headers: signal378 .headers()379 .or_else(|| base.headers())380 .map(str::to_owned),381 protocol,382 timeout: Duration::from_millis(383 signal384 .timeout()385 .or_else(|| base.timeout())386 .unwrap_or(Self::DEFAULT_TIMEOUT_MS),387 ),388 })389 }390}