WS-G: apply review fixes F1-F8

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-15 18:43:21 -04:00
1 parent 2aeb9ae154
commit 1eb7ffce19
4 files changed
+219 -99

No files matched your search

+44 -31
View File
@@ -49,29 +49,20 @@ pub struct TelemetryGuard {
providers: Option<Providers>,
}
impl TelemetryGuard {
/// True when OTLP export is active.
pub fn is_enabled(&self) -> bool {
self.providers.is_some()
}
}
impl Drop for TelemetryGuard {
fn drop(&mut self) {
let Some(p) = self.providers.take() else {
return;
};
if let Err(e) = p.tracer.force_flush() {
impl Providers {
/// Blocking flush and shutdown, each provider bounded by `FLUSH_TIMEOUT`.
fn flush_and_shutdown(&self) {
if let Err(e) = self.tracer.force_flush() {
eprintln!("otel: trace flush failed: {e}");
}
let _ = p.tracer.shutdown_with_timeout(FLUSH_TIMEOUT);
if let Some(m) = &p.meter {
let _ = self.tracer.shutdown_with_timeout(FLUSH_TIMEOUT);
if let Some(m) = &self.meter {
if let Err(e) = m.force_flush() {
eprintln!("otel: metrics flush failed: {e}");
}
let _ = m.shutdown_with_timeout(FLUSH_TIMEOUT);
}
if let Some(l) = &p.logger {
if let Some(l) = &self.logger {
if let Err(e) = l.force_flush() {
eprintln!("otel: logs flush failed: {e}");
}
@@ -80,6 +71,40 @@ impl Drop for TelemetryGuard {
}
}
impl TelemetryGuard {
/// True when OTLP export is active.
pub fn is_enabled(&self) -> bool {
self.providers.is_some()
}
/// Flush and shut down all providers without blocking the async runtime.
/// Call this (and await it) on graceful shutdown, after the exporter task
/// handles from `spawn_exporters` have finished. `Drop` is only a
/// best-effort fallback.
pub async fn shutdown(mut self) {
if let Some(p) = self.providers.take() {
let _ = tokio::task::spawn_blocking(move || p.flush_and_shutdown()).await;
}
}
}
impl Drop for TelemetryGuard {
/// Best effort. Inside a tokio runtime the flush runs on a detached thread
/// (it may not finish before the process exits); outside one it runs inline.
fn drop(&mut self) {
let Some(p) = self.providers.take() else {
return;
};
if tokio::runtime::Handle::try_current().is_ok() {
let _ = std::thread::Builder::new()
.name("otel-flush".into())
.spawn(move || p.flush_and_shutdown());
} else {
p.flush_and_shutdown();
}
}
}
/// Install the tracing subscriber and, when `config.otel.enabled`, the OTLP
/// providers. Works (stderr logging only) when OTel is disabled.
pub fn init(config: &Config) -> Result<TelemetryGuard> {
@@ -359,21 +384,9 @@ mod tests {
..Default::default()
};
o.headers.insert("Authorization".into(), "Bearer x".into());
let results = [
build_span_exporter(&o).map(drop),
build_metric_exporter(&o).map(drop),
build_log_exporter(&o).map(drop),
];
for r in results {
match (protocol, r) {
(_, Ok(())) => {}
// Contract: HTTP needs the `reqwest-blocking-client` feature on
// opentelemetry-otlp (Cargo.toml, owned by P0).
(OtelProtocol::HttpProtobuf, Err(e))
if format!("{e:?}").contains("http_client") => {}
(_, Err(e)) => panic!("exporter build failed: {e:?}"),
}
}
build_span_exporter(&o).unwrap();
build_metric_exporter(&o).unwrap();
build_log_exporter(&o).unwrap();
}
}