From 9ce59bf445664bd658800e63652061e04bd3d7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 14 Sep 2026 15:22:30 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20mint=20OpenObserve=20OTLP?= =?UTF-8?q?=20Basic=20from=20ZO=5FROOT=5FUSER=5F*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When OTEL_EXPORTER_OTLP_ENDPOINT is set and HEADERS is not, the process builds Authorization=Basic from ZO_ROOT_USER_EMAIL/PASSWORD. No hand base64 in the env file. --- .env.example | 4 ++- README.md | 11 ++++--- deploy/hezarfen_backend.env.example | 10 +++--- src/telemetry.rs | 50 ++++++++++++++++++++++++++++- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index cd4da29..fdffe42 100644 --- a/.env.example +++ b/.env.example @@ -92,7 +92,9 @@ RUST_LOG=info,hezarfen_backend=debug # Wire transport: grpc (default, port 4317) or http/protobuf (port 4318). # OTEL_EXPORTER_OTLP_PROTOCOL=grpc -# Extra headers for the collector, e.g. an auth token, as key=value pairs. +# Extra headers for the collector, as key=value pairs. Unset is fine when +# exporting to the compose OpenObserve: the process mints Authorization=Basic +# from ZO_ROOT_USER_EMAIL / ZO_ROOT_USER_PASSWORD. # OTEL_EXPORTER_OTLP_HEADERS=authorization=Bearer%20token # Service name on every exported signal. Defaults to hezarfen_backend. diff --git a/README.md b/README.md index 355b4e4..285a843 100644 --- a/README.md +++ b/README.md @@ -286,11 +286,12 @@ set on the service (`admin@hezarfen.local` / `Hezarfen_dev1!` for local dev — OpenObserve rejects a weaker password at boot). Retention is per stream in days, defaulted globally to 90 by `ZO_COMPACT_DATA_RETENTION_DAYS`. Traces, metrics and logs start landing when the backend's environment carries -`OTEL_EXPORTER_OTLP_ENDPOINT` (`http://openobserve:5081`) and -`OTEL_EXPORTER_OTLP_HEADERS` (`Authorization=Basic ` -plus `organization=default,stream-name=default`) — those keys belong in the -env file (production: `hezarfen_backend.env`; locally: an optional file of -the same name beside `compose.yaml`), never in `compose.yaml` itself, where +`OTEL_EXPORTER_OTLP_ENDPOINT` (`http://openobserve:5081`) — auth is minted +from the same `ZO_ROOT_USER_EMAIL` / `ZO_ROOT_USER_PASSWORD` the OpenObserve +container already has, so there is no hand-rolled base64 header. Set +`OTEL_EXPORTER_OTLP_HEADERS` only for a different collector. Those keys belong +in the env file (production: `hezarfen_backend.env`; locally: an optional file +of the same name beside `compose.yaml`), never in `compose.yaml` itself, where an empty OTEL value would switch export on and fail the boot. The OTLP/HTTP variant (`http://openobserve:5080/api/default` with `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf`) is the alternative. diff --git a/deploy/hezarfen_backend.env.example b/deploy/hezarfen_backend.env.example index b3d8953..1135e6b 100644 --- a/deploy/hezarfen_backend.env.example +++ b/deploy/hezarfen_backend.env.example @@ -50,14 +50,12 @@ AI_SHARED_TOKEN= #AI_REQUEST_TIMEOUT_SECS=30 # ── Telemetry (OpenObserve over OTLP) ──────────────────────────────────────── -# Leave commented to keep export off (stdout logs only). Uncomment after -# ZO_ROOT_USER_* below are set; Authorization is -# base64(":"): -# printf '%s:%s' 'admin@example.com' 'PASSWORD' | base64 +# Leave commented to keep export off (stdout logs only). Uncomment the +# endpoint after ZO_ROOT_USER_* below are set. The backend mints +# Authorization=Basic from those two values — do not set +# OTEL_EXPORTER_OTLP_HEADERS unless the collector is not this OpenObserve. #OTEL_EXPORTER_OTLP_ENDPOINT=http://openobserve:5081 -#OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic ,organization=default,stream-name=default #OTEL_SERVICE_NAME=hezarfen_backend -# Names the deployment in every exported signal. #DEPLOY_ENV=production # ── OpenObserve root account (first boot) ──────────────────────────────────── diff --git a/src/telemetry.rs b/src/telemetry.rs index d8fc0dc..da5c905 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -19,6 +19,7 @@ use std::sync::{Arc, OnceLock}; +use base64::Engine; use opentelemetry::metrics::{Counter, Gauge, Histogram, UpDownCounter}; use opentelemetry::{KeyValue, global}; use opentelemetry_sdk::Resource; @@ -340,6 +341,10 @@ pub fn init(cfg: &TelemetryConfig) -> Result<(TelemetryGuard, Metrics), Telemetr publish(Metrics::new()), )); }; + // SDK reads OTEL_EXPORTER_OTLP_HEADERS itself. If the operator left it + // unset, mint HTTP Basic from the same ZO_ROOT_USER_* the OpenObserve + // container already has — no hand-rolled base64 in the env file. + fill_otlp_headers_from_zo(); let resource = resource(); let protocol = Protocol::from_env()?; @@ -516,6 +521,39 @@ impl Protocol { }) } } +/// When `OTEL_EXPORTER_OTLP_HEADERS` is unset, mint OpenObserve's gRPC Basic +/// header from `ZO_ROOT_USER_EMAIL` / `ZO_ROOT_USER_PASSWORD`. An explicit +/// headers value always wins (a different collector, a hand-set token). +fn fill_otlp_headers_from_zo() { + if env_present("OTEL_EXPORTER_OTLP_HEADERS") { + return; + } + let Some(email) = env_nonempty("ZO_ROOT_USER_EMAIL") else { + return; + }; + let Some(password) = env_nonempty("ZO_ROOT_USER_PASSWORD") else { + return; + }; + let headers = openobserve_otlp_headers(&email, &password); + // SAFETY: `init` runs once from `main` before any exporter is built. + // The SDK reads this key on the next lines; no other thread has. + unsafe { + std::env::set_var("OTEL_EXPORTER_OTLP_HEADERS", headers); + } +} + +fn env_present(key: &str) -> bool { + env_nonempty(key).is_some() +} + +fn env_nonempty(key: &str) -> Option { + std::env::var(key).ok().filter(|v| !v.trim().is_empty()) +} + +fn openobserve_otlp_headers(email: &str, password: &str) -> String { + let token = base64::engine::general_purpose::STANDARD.encode(format!("{email}:{password}")); + format!("Authorization=Basic {token},organization=default,stream-name=default") +} /// What every exported signal says it came from. `OTEL_SERVICE_NAME` wins when /// set (the SDK's own default detector reads it); `DEPLOY_ENV` names the @@ -545,7 +583,7 @@ mod tests { use opentelemetry::logs::{LogRecord as _, Logger as _, LoggerProvider as _}; use opentelemetry_sdk::logs::{InMemoryLogExporter, SdkLoggerProvider}; - use super::{LogFormat, StampTimestamp, TelemetryError}; + use super::{LogFormat, StampTimestamp, TelemetryError, openobserve_otlp_headers}; #[tokio::test] async fn log_format_parses_the_two_spellings_and_rejects_the_rest() { @@ -590,4 +628,14 @@ mod tests { assert!(exported.timestamp().is_some()); assert_eq!(exported.timestamp(), exported.observed_timestamp()); } + + #[test] + fn openobserve_headers_are_basic_auth_of_email_and_password() { + // The pair compose.yaml used as local-dev OpenObserve credentials; + // the encoded value is what a collector would have accepted then. + assert_eq!( + openobserve_otlp_headers("admin@hezarfen.local", "Hezarfen_dev1!"), + "Authorization=Basic YWRtaW5AaGV6YXJmZW4ubG9jYWw6SGV6YXJmZW5fZGV2MSE=,organization=default,stream-name=default" + ); + } }