refactor(obs): drop unused features and trim verbose comments in lib/obs and config obs section
zhenyi committed
Modified lib/config/src/obs.rs lib/config/src/obs.rs +12 −33 View lib/config/src/obs.rs
@@ -4,48 +4,28 @@
44
55use crate::duration::deserialize_std_duration;
66
7/// OpenObserve (OTLP) observability settings.
8///
9/// When `enabled`, the `obs` crate exports traces, logs and metrics over
10/// OTLP/HTTP to the configured endpoint. Each signal is exported to
11/// `{endpoint}/{traces|logs|metrics}` (OpenObserve's OTLP HTTP layout).
127#[derive(Debug, Clone, Serialize, Deserialize)]
138#[serde(default)]
149pub struct ObsSettings {
15 /// Master switch; when false the apps keep console-only logging.
16 pub enabled: bool,
17 /// Resource attribute `service.name`. Empty means the app supplies its own
18 /// fallback (each binary knows its own name).
19 pub service_name: String,
20 /// Resource attribute `service.version`.
21 pub service_version: String,
22 /// OpenObserve `stream-name` header for logs/traces. Empty falls back to
23 /// `service_name`; when both are empty OpenObserve routes to `default`.
24 pub stream_name: String,
25 /// OTLP/HTTP base endpoint. OpenObserve exposes it at
26 /// `http://host:5080/api/{org}/v1` (each signal is appended by the exporter).
27 pub endpoint: String,
28 /// Bearer token (OpenObserve stream key). Mutually exclusive with user/password.
29 pub token: Option<String>,
30 /// Basic-auth user (OpenObserve organization user, usually an email).
31 pub user: Option<String>,
32 /// Basic-auth password (OpenObserve API key).
33 pub password: Option<String>,
10 pub enabled: bool,
11 pub service_name: String,
12 pub service_version: String,
13 pub stream_name: String,
14 pub endpoint: String,
15 pub token: Option<String>,
16 pub user: Option<String>,
17 pub password: Option<String>,
3418 pub enable_logs: bool,
3519 pub enable_metrics: bool,
3620 pub enable_traces: bool,
37 /// Trace sampling ratio in `[0.0, 1.0]`; applied with a parent-based sampler.
38 pub traces_sample_ratio: f64,
21 pub traces_sample_ratio: f64,
3922 pub batch_size: usize,
4023 #[serde(default, deserialize_with = "deserialize_std_duration")]
4124 pub batch_interval: Duration,
4225 #[serde(default, deserialize_with = "deserialize_std_duration")]
4326 pub timeout: Duration,
44 /// Keep the local console `fmt` layer even while exporting remotely.
45 pub console_log: bool,
46 /// EnvFilter directive for the console layer (e.g. `info,webapi=debug`).
47 /// Empty means `info`; each app may refine this with its own defaults.
48 pub log_level: String,
27 pub console_log: bool,
28 pub log_level: String,
4929}
5030
5131impl Default for ObsSettings {
@@ -73,8 +53,7 @@
7353}
7454
7555impl ObsSettings {
76 /// Fill in the per-app defaults (service name and log filter) when unset.
77 pub fn with_app_defaults(
56 pub fn with_app_defaults(
7857 mut self,
7958 service_name: impl Into<String>,
8059 log_level: impl Into<String>,
Modified lib/obs/examples/smoke.rs lib/obs/examples/smoke.rs +2 −15 View lib/obs/examples/smoke.rs
@@ -1,12 +1,3 @@
1//! Smoke test for the OTLP exporters: emits one span, one log line and one
2//! counter, then flushes via the guard.
3//!
4//! Run against a local OpenObserve:
5//!
6//! ```text
7//! cargo run -p obs --example smoke
8//! ```
9
101use std::time::Duration;
112
123use config::obs::ObsSettings;
@@ -14,7 +5,6 @@
145
156#[tokio::main]
167async fn main() -> Result<(), Box<dyn std::error::Error>> {
17 // Point at a local OpenObserve by default; override for a custom receiver.
188 let endpoint = std::env::var("SMOKE_ENDPOINT")
199 .unwrap_or_else(|_| "http://127.0.0.1:5080/api/default/v1".into());
2010 let token = std::env::var("SMOKE_TOKEN").ok();
@@ -36,17 +26,14 @@
3626
3727 let guard = init(&settings)?;
3828
39 // Trace: a span around a log line.
4029 let span = tracing::info_span!("smoke_operation", step = "one");
4130 {
4231 let _enter = span.enter();
43 // Log: routed to console and OTLP logs.
4432 tracing::info!(message = "smoke log line", key = "value");
45 // Metric: through the `metrics` facade.
4633 metrics::counter!("smoke_counter_total", "signal" => "metrics").increment(1);
4734 tracing::info!("smoke span emitted");
48 } // exit the span context
49 drop(span); // close the span so it is exported
35 }
36 drop(span);
5037
5138 guard.flush();
5239 drop(guard);
Modified lib/obs/guard.rs lib/obs/guard.rs +1 −6 View lib/obs/guard.rs
@@ -2,10 +2,6 @@
22use opentelemetry_sdk::metrics::SdkMeterProvider;
33use opentelemetry_sdk::trace::SdkTracerProvider;
44
5/// Holds the OpenTelemetry providers for the process lifetime.
6///
7/// Dropping the guard flushes trailing telemetry (batch processors / periodic
8/// reader), so apps should keep it alive until after the server shuts down.
95#[derive(Default)]
106pub struct ObservabilityGuard {
117 tracer: Option<SdkTracerProvider>,
@@ -26,8 +22,7 @@
2622 self.meter = Some(provider);
2723 }
2824
29 /// Force-flush all exporters (call before a planned shutdown).
30 pub fn flush(&self) {
25 pub fn flush(&self) {
3126 if let Some(provider) = &self.tracer {
3227 let _ = provider.force_flush();
3328 }
Modified lib/obs/init.rs lib/obs/init.rs +0 −7 View lib/obs/init.rs
@@ -8,13 +8,6 @@
88use crate::guard::ObservabilityGuard;
99use crate::{logs, metrics, otlp, traces};
1010
11/// Initialize the tracing subscriber and the OpenTelemetry exporters.
12///
13/// Console logging is always set up (unless `console_log` is off); the OTLP
14/// layers for traces, logs and metrics are added only when
15/// `obs.enabled` and the per-signal switches are on. Every provider is kept in
16/// the returned [`ObservabilityGuard`], which flushes trailing telemetry on
17/// drop.
1811pub fn init(settings: &ObsSettings) -> Result<ObservabilityGuard, ObsError> {
1912 let mut guard = ObservabilityGuard::default();
2013
Modified lib/obs/lib.rs lib/obs/lib.rs +0 −7 View lib/obs/lib.rs
@@ -1,10 +1,3 @@
1//! Observability bootstrap: traces, logs and metrics exported to OpenObserve
2//! over OTLP/HTTP.
3//!
4//! Apps call [`init`] once with the [`config::obs::ObsSettings`] section and
5//! hold the returned [`ObservabilityGuard`] for the process lifetime; the guard
6//! flushes trailing telemetry on drop.
7
81pub mod error;
92pub mod guard;
103pub mod init;
Modified lib/obs/logs.rs lib/obs/logs.rs +0 −3 View lib/obs/logs.rs
@@ -6,9 +6,6 @@
66use crate::error::ObsError;
77use crate::otlp;
88
9/// Build the logger provider with a batch exporter to `{endpoint}/logs`.
10/// The provider is consumed directly by the tracing appender layer; there is no
11/// global logger provider in opentelemetry 0.31.
129pub(crate) fn build(
1310 resource: Resource,
1411 settings: &ObsSettings,
Modified lib/obs/metrics.rs lib/obs/metrics.rs +0 −4 View lib/obs/metrics.rs
@@ -8,10 +8,6 @@
88use crate::error::ObsError;
99use crate::otlp;
1010
11/// Install the `metrics` facade recorder backed by an OTLP periodic reader to
12/// `{endpoint}/metrics`. Returns the meter provider for shutdown.
13///
14/// The periodic reader runs on its own thread (no tokio runtime dependency).
1511pub(crate) fn install(
1612 resource: Resource,
1713 settings: &ObsSettings,
Modified lib/obs/otlp.rs lib/obs/otlp.rs +0 −8 View lib/obs/otlp.rs
@@ -5,13 +5,10 @@
55use opentelemetry::KeyValue;
66use opentelemetry_sdk::Resource;
77
8/// Append the OTLP signal path (`traces` / `logs` / `metrics`) to the configured
9/// base endpoint. The base is expected to end in `.../otlp/v1`.
108pub(crate) fn endpoint_for(base: &str, signal: &str) -> String {
119 format!("{}/{}", base.trim_end_matches('/'), signal)
1210}
1311
14/// Build the `Authorization` header: `Bearer <token>` or `Basic base64(user:password)`.
1512fn authorization(settings: &ObsSettings) -> Option<(String, String)> {
1613 if let Some(token) = &settings.token {
1714 Some(("Authorization".to_string(), format!("Bearer {token}")))
@@ -24,7 +21,6 @@
2421 }
2522}
2623
27/// Header map attached to every OTLP export request.
2824pub(crate) fn headers(settings: &ObsSettings) -> HashMap<String, String> {
2925 let mut headers = HashMap::new();
3026 if let Some((name, value)) = authorization(settings) {
@@ -33,9 +29,6 @@
3329 headers
3430}
3531
36/// Headers for the logs/traces exporters. Adds the OpenObserve `stream-name`
37/// header (falling back to `service.name`), which decides the destination
38/// stream; without it OpenObserve routes every signal into `default`.
3932pub(crate) fn signal_headers(settings: &ObsSettings) -> HashMap<String, String> {
4033 let mut headers = headers(settings);
4134 let stream = if !settings.stream_name.is_empty() {
@@ -51,7 +44,6 @@
5144 headers
5245}
5346
54/// Resource attributes (`service.name`, `service.version`) shared by all signals.
5547pub(crate) fn resource(settings: &ObsSettings) -> Resource {
5648 let mut builder = Resource::builder();
5749 if !settings.service_name.is_empty() {
Modified lib/obs/traces.rs lib/obs/traces.rs +0 −2 View lib/obs/traces.rs
@@ -9,8 +9,6 @@
99use crate::error::ObsError;
1010use crate::otlp;
1111
12/// Build the tracer provider with a batch exporter to `{endpoint}/traces` and
13/// install it as the global provider. Returns the provider for shutdown.
1412pub(crate) fn build(
1513 resource: Resource,
1614 settings: &ObsSettings,