Mint OpenObserve OTLP Basic from ZO_ROOT_USER_*
Browse files at this commit9ce59bf445664bd658800e63652061e04bd3d7e9Parent: 2ba3280- feat
- mint OpenObserve OTLP Basic from ZO_ROOT_USER_*
4 changed files +62 -13
- .env.example +3 -1
- README.md +6 -5
- deploy/hezarfen_backend.env.example +4 -6
- src/telemetry.rs +49 -1
.env.examplemodified+3 -1
| @@ -92,7 +92,9 @@ RUST_LOG=info,hezarfen_backend=debug | |||
| 92 | # Wire transport: grpc (default, port 4317) or http/protobuf (port 4318). | 92 | # Wire transport: grpc (default, port 4317) or http/protobuf (port 4318). |
| 93 | # OTEL_EXPORTER_OTLP_PROTOCOL=grpc | 93 | # OTEL_EXPORTER_OTLP_PROTOCOL=grpc |
| 94 | 94 | ||
| 95 | -# Extra headers for the collector, e.g. an auth token, as key=value pairs. | 95 | +# Extra headers for the collector, as key=value pairs. Unset is fine when |
| 96 | +# exporting to the compose OpenObserve: the process mints Authorization=Basic | ||
| 97 | +# from ZO_ROOT_USER_EMAIL / ZO_ROOT_USER_PASSWORD. | ||
| 96 | # OTEL_EXPORTER_OTLP_HEADERS=authorization=Bearer%20token | 98 | # OTEL_EXPORTER_OTLP_HEADERS=authorization=Bearer%20token |
| 97 | 99 | ||
| 98 | # Service name on every exported signal. Defaults to hezarfen_backend. | 100 | # Service name on every exported signal. Defaults to hezarfen_backend. |
README.mdmodified+6 -5
| @@ -286,11 +286,12 @@ set on the service (`admin@hezarfen.local` / `Hezarfen_dev1!` for local dev — | |||
| 286 | OpenObserve rejects a weaker password at boot). Retention is per stream in | 286 | OpenObserve rejects a weaker password at boot). Retention is per stream in |
| 287 | days, defaulted globally to 90 by `ZO_COMPACT_DATA_RETENTION_DAYS`. Traces, | 287 | days, defaulted globally to 90 by `ZO_COMPACT_DATA_RETENTION_DAYS`. Traces, |
| 288 | metrics and logs start landing when the backend's environment carries | 288 | metrics and logs start landing when the backend's environment carries |
| 289 | -`OTEL_EXPORTER_OTLP_ENDPOINT` (`http://openobserve:5081`) and | 289 | +`OTEL_EXPORTER_OTLP_ENDPOINT` (`http://openobserve:5081`) — auth is minted |
| 290 | -`OTEL_EXPORTER_OTLP_HEADERS` (`Authorization=Basic <base64 email:password>` | 290 | +from the same `ZO_ROOT_USER_EMAIL` / `ZO_ROOT_USER_PASSWORD` the OpenObserve |
| 291 | -plus `organization=default,stream-name=default`) — those keys belong in the | 291 | +container already has, so there is no hand-rolled base64 header. Set |
| 292 | -env file (production: `hezarfen_backend.env`; locally: an optional file of | 292 | +`OTEL_EXPORTER_OTLP_HEADERS` only for a different collector. Those keys belong |
| 293 | -the same name beside `compose.yaml`), never in `compose.yaml` itself, where | 293 | +in the env file (production: `hezarfen_backend.env`; locally: an optional file |
| 294 | +of the same name beside `compose.yaml`), never in `compose.yaml` itself, where | ||
| 294 | an empty OTEL value would switch export on and fail the boot. The OTLP/HTTP | 295 | an empty OTEL value would switch export on and fail the boot. The OTLP/HTTP |
| 295 | variant (`http://openobserve:5080/api/default` with | 296 | variant (`http://openobserve:5080/api/default` with |
| 296 | `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf`) is the alternative. | 297 | `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf`) is the alternative. |
deploy/hezarfen_backend.env.examplemodified+4 -6
| @@ -50,14 +50,12 @@ AI_SHARED_TOKEN= | |||
| 50 | #AI_REQUEST_TIMEOUT_SECS=30 | 50 | #AI_REQUEST_TIMEOUT_SECS=30 |
| 51 | 51 | ||
| 52 | # ── Telemetry (OpenObserve over OTLP) ──────────────────────────────────────── | 52 | # ── Telemetry (OpenObserve over OTLP) ──────────────────────────────────────── |
| 53 | -# Leave commented to keep export off (stdout logs only). Uncomment after | 53 | +# Leave commented to keep export off (stdout logs only). Uncomment the |
| 54 | -# ZO_ROOT_USER_* below are set; Authorization is | 54 | +# endpoint after ZO_ROOT_USER_* below are set. The backend mints |
| 55 | -# base64("<ZO_ROOT_USER_EMAIL>:<ZO_ROOT_USER_PASSWORD>"): | 55 | +# Authorization=Basic from those two values — do not set |
| 56 | -# printf '%s:%s' 'admin@example.com' 'PASSWORD' | base64 | 56 | +# OTEL_EXPORTER_OTLP_HEADERS unless the collector is not this OpenObserve. |
| 57 | #OTEL_EXPORTER_OTLP_ENDPOINT=http://openobserve:5081 | 57 | #OTEL_EXPORTER_OTLP_ENDPOINT=http://openobserve:5081 |
| 58 | -#OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic <base64>,organization=default,stream-name=default | ||
| 59 | #OTEL_SERVICE_NAME=hezarfen_backend | 58 | #OTEL_SERVICE_NAME=hezarfen_backend |
| 60 | -# Names the deployment in every exported signal. | ||
| 61 | #DEPLOY_ENV=production | 59 | #DEPLOY_ENV=production |
| 62 | 60 | ||
| 63 | # ── OpenObserve root account (first boot) ──────────────────────────────────── | 61 | # ── OpenObserve root account (first boot) ──────────────────────────────────── |
src/telemetry.rsmodified+49 -1
| @@ -19,6 +19,7 @@ | |||
| 19 | 19 | ||
| 20 | use std::sync::{Arc, OnceLock}; | 20 | use std::sync::{Arc, OnceLock}; |
| 21 | 21 | ||
| 22 | +use base64::Engine; | ||
| 22 | use opentelemetry::metrics::{Counter, Gauge, Histogram, UpDownCounter}; | 23 | use opentelemetry::metrics::{Counter, Gauge, Histogram, UpDownCounter}; |
| 23 | use opentelemetry::{KeyValue, global}; | 24 | use opentelemetry::{KeyValue, global}; |
| 24 | use opentelemetry_sdk::Resource; | 25 | use opentelemetry_sdk::Resource; |
| @@ -340,6 +341,10 @@ pub fn init(cfg: &TelemetryConfig) -> Result<(TelemetryGuard, Metrics), Telemetr | |||
| 340 | publish(Metrics::new()), | 341 | publish(Metrics::new()), |
| 341 | )); | 342 | )); |
| 342 | }; | 343 | }; |
| 344 | + // SDK reads OTEL_EXPORTER_OTLP_HEADERS itself. If the operator left it | ||
| 345 | + // unset, mint HTTP Basic from the same ZO_ROOT_USER_* the OpenObserve | ||
| 346 | + // container already has — no hand-rolled base64 in the env file. | ||
| 347 | + fill_otlp_headers_from_zo(); | ||
| 343 | 348 | ||
| 344 | let resource = resource(); | 349 | let resource = resource(); |
| 345 | let protocol = Protocol::from_env()?; | 350 | let protocol = Protocol::from_env()?; |
| @@ -516,6 +521,39 @@ impl Protocol { | |||
| 516 | }) | 521 | }) |
| 517 | } | 522 | } |
| 518 | } | 523 | } |
| 524 | +/// When `OTEL_EXPORTER_OTLP_HEADERS` is unset, mint OpenObserve's gRPC Basic | ||
| 525 | +/// header from `ZO_ROOT_USER_EMAIL` / `ZO_ROOT_USER_PASSWORD`. An explicit | ||
| 526 | +/// headers value always wins (a different collector, a hand-set token). | ||
| 527 | +fn fill_otlp_headers_from_zo() { | ||
| 528 | + if env_present("OTEL_EXPORTER_OTLP_HEADERS") { | ||
| 529 | + return; | ||
| 530 | + } | ||
| 531 | + let Some(email) = env_nonempty("ZO_ROOT_USER_EMAIL") else { | ||
| 532 | + return; | ||
| 533 | + }; | ||
| 534 | + let Some(password) = env_nonempty("ZO_ROOT_USER_PASSWORD") else { | ||
| 535 | + return; | ||
| 536 | + }; | ||
| 537 | + let headers = openobserve_otlp_headers(&email, &password); | ||
| 538 | + // SAFETY: `init` runs once from `main` before any exporter is built. | ||
| 539 | + // The SDK reads this key on the next lines; no other thread has. | ||
| 540 | + unsafe { | ||
| 541 | + std::env::set_var("OTEL_EXPORTER_OTLP_HEADERS", headers); | ||
| 542 | + } | ||
| 543 | +} | ||
| 544 | + | ||
| 545 | +fn env_present(key: &str) -> bool { | ||
| 546 | + env_nonempty(key).is_some() | ||
| 547 | +} | ||
| 548 | + | ||
| 549 | +fn env_nonempty(key: &str) -> Option<String> { | ||
| 550 | + std::env::var(key).ok().filter(|v| !v.trim().is_empty()) | ||
| 551 | +} | ||
| 552 | + | ||
| 553 | +fn openobserve_otlp_headers(email: &str, password: &str) -> String { | ||
| 554 | + let token = base64::engine::general_purpose::STANDARD.encode(format!("{email}:{password}")); | ||
| 555 | + format!("Authorization=Basic {token},organization=default,stream-name=default") | ||
| 556 | +} | ||
| 519 | 557 | ||
| 520 | /// What every exported signal says it came from. `OTEL_SERVICE_NAME` wins when | 558 | /// What every exported signal says it came from. `OTEL_SERVICE_NAME` wins when |
| 521 | /// set (the SDK's own default detector reads it); `DEPLOY_ENV` names the | 559 | /// set (the SDK's own default detector reads it); `DEPLOY_ENV` names the |
| @@ -545,7 +583,7 @@ mod tests { | |||
| 545 | use opentelemetry::logs::{LogRecord as _, Logger as _, LoggerProvider as _}; | 583 | use opentelemetry::logs::{LogRecord as _, Logger as _, LoggerProvider as _}; |
| 546 | use opentelemetry_sdk::logs::{InMemoryLogExporter, SdkLoggerProvider}; | 584 | use opentelemetry_sdk::logs::{InMemoryLogExporter, SdkLoggerProvider}; |
| 547 | 585 | ||
| 548 | - use super::{LogFormat, StampTimestamp, TelemetryError}; | 586 | + use super::{LogFormat, StampTimestamp, TelemetryError, openobserve_otlp_headers}; |
| 549 | 587 | ||
| 550 | #[tokio::test] | 588 | #[tokio::test] |
| 551 | async fn log_format_parses_the_two_spellings_and_rejects_the_rest() { | 589 | async fn log_format_parses_the_two_spellings_and_rejects_the_rest() { |
| @@ -590,4 +628,14 @@ mod tests { | |||
| 590 | assert!(exported.timestamp().is_some()); | 628 | assert!(exported.timestamp().is_some()); |
| 591 | assert_eq!(exported.timestamp(), exported.observed_timestamp()); | 629 | assert_eq!(exported.timestamp(), exported.observed_timestamp()); |
| 592 | } | 630 | } |
| 631 | + | ||
| 632 | + #[test] | ||
| 633 | + fn openobserve_headers_are_basic_auth_of_email_and_password() { | ||
| 634 | + // The pair compose.yaml used as local-dev OpenObserve credentials; | ||
| 635 | + // the encoded value is what a collector would have accepted then. | ||
| 636 | + assert_eq!( | ||
| 637 | + openobserve_otlp_headers("admin@hezarfen.local", "Hezarfen_dev1!"), | ||
| 638 | + "Authorization=Basic YWRtaW5AaGV6YXJmZW4ubG9jYWw6SGV6YXJmZW5fZGV2MSE=,organization=default,stream-name=default" | ||
| 639 | + ); | ||
| 640 | + } | ||
| 593 | } | 641 | } |