posemesh_compute_node/
telemetry.rs

1use crate::config::LogFormat;
2use std::sync::Once;
3use tracing_subscriber::{fmt, prelude::*, EnvFilter};
4
5static INIT: Once = Once::new();
6
7/// Initialize global tracing subscriber with the given log format.
8/// Safe to call multiple times; only the first call installs a subscriber.
9pub fn init_with_format(fmt_mode: LogFormat) -> anyhow::Result<()> {
10    INIT.call_once(|| {
11        let env_filter =
12            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
13        let fmt_layer = match fmt_mode {
14            LogFormat::Json => fmt::layer().json().boxed(),
15            LogFormat::Text => fmt::layer().boxed(),
16        };
17
18        tracing_subscriber::registry()
19            .with(env_filter)
20            .with(fmt_layer)
21            .init();
22    });
23    Ok(())
24}
25
26/// Initialize tracing using `LOG_FORMAT` env var ("json" or "text", default json).
27pub fn init_from_env() -> anyhow::Result<()> {
28    let mode = match std::env::var("LOG_FORMAT").ok().as_deref() {
29        Some("text") => LogFormat::Text,
30        _ => LogFormat::Json,
31    };
32    init_with_format(mode)
33}
34
35/// Create a span for a task with common fields as per the spec.
36pub fn task_span(
37    task_id: uuid::Uuid,
38    job_id: uuid::Uuid,
39    capability: &str,
40    domain_id: uuid::Uuid,
41) -> tracing::Span {
42    tracing::info_span!(
43        "task",
44        task_id = %task_id,
45        job_id = %job_id,
46        capability = %capability,
47        domain_id = %domain_id
48    )
49}
50
51#[cfg(feature = "metrics")]
52pub mod metrics {
53    /// Metric names as per ยง10 Telemetry.
54    pub const DMS_POLL_LATENCY_MS: &str = "dms.poll.latency_ms";
55    pub const DMS_ACTIVE_TASK: &str = "dms.active_task";
56    pub const RUNNER_RUN_LATENCY_MS: &str = "runner.run.latency_ms";
57    pub const TOKEN_ROTATE_COUNT: &str = "token.rotate.count";
58    pub const STORAGE_BYTES_UPLOADED: &str = "storage.bytes.uploaded";
59    pub const STORAGE_BYTES_DOWNLOADED: &str = "storage.bytes.downloaded";
60
61    /// Increment a counter (no-op placeholder; exporter not wired in this refactor).
62    pub fn incr(_name: &str, _by: u64) {}
63
64    /// Record a gauge value (no-op placeholder).
65    pub fn gauge(_name: &str, _value: u64) {}
66}