Skip to main content

packc/
telemetry.rs

1use anyhow::Result;
2use greentic_config_types::{TelemetryConfig, TelemetryExporterKind};
3pub use greentic_telemetry::with_task_local;
4use greentic_telemetry::{
5    TelemetryConfig as ServiceTelemetryConfig, TelemetryCtx,
6    export::{ExportConfig, ExportMode, Sampling},
7    init_telemetry_auto, init_telemetry_from_config, set_current_telemetry_ctx,
8};
9use greentic_types::TenantCtx;
10
11/// Install the default Greentic telemetry stack for the given service.
12pub fn install(service_name: &str) -> Result<()> {
13    init_telemetry_auto(ServiceTelemetryConfig {
14        service_name: service_name.to_string(),
15    })
16}
17
18/// Install telemetry honoring greentic-config telemetry settings.
19pub fn install_with_config(service_name: &str, cfg: &TelemetryConfig) -> Result<()> {
20    if !cfg.enabled || matches!(cfg.exporter, TelemetryExporterKind::None) {
21        return Ok(());
22    }
23
24    let export = match cfg.exporter {
25        TelemetryExporterKind::Otlp => export_config(ExportMode::OtlpGrpc, cfg),
26        TelemetryExporterKind::Stdout => export_config(ExportMode::JsonStdout, cfg),
27        TelemetryExporterKind::Gcp => export_config(ExportMode::GcpCloudTrace, cfg),
28        TelemetryExporterKind::Azure => export_config(ExportMode::AzureAppInsights, cfg),
29        TelemetryExporterKind::Aws => export_config(ExportMode::AwsXRay, cfg),
30        TelemetryExporterKind::None => unreachable!("handled above"),
31    };
32
33    init_telemetry_from_config(
34        ServiceTelemetryConfig {
35            service_name: service_name.to_string(),
36        },
37        export,
38    )
39}
40
41fn export_config(mode: ExportMode, cfg: &TelemetryConfig) -> ExportConfig {
42    let mut export = ExportConfig::default();
43    export.mode = mode;
44    export.endpoint = cfg.endpoint.clone();
45    export.sampling = Sampling::TraceIdRatio(cfg.sampling as f64);
46    export.compression = None;
47    export
48}
49
50/// Map the provided tenant context into the task-local telemetry slot.
51pub fn set_current_tenant_ctx(ctx: &TenantCtx) {
52    let mut telemetry = TelemetryCtx::new(ctx.tenant_id.as_ref());
53
54    if let Some(session) = ctx.session_id() {
55        telemetry = telemetry.with_session(session);
56    }
57    if let Some(flow) = ctx.flow_id() {
58        telemetry = telemetry.with_flow(flow);
59    }
60    if let Some(node) = ctx.node_id() {
61        telemetry = telemetry.with_node(node);
62    }
63    if let Some(provider) = ctx.provider_id() {
64        telemetry = telemetry.with_provider(provider);
65    }
66
67    set_current_telemetry_ctx(telemetry);
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use greentic_config_types::TelemetryConfig;
74    use greentic_types::{EnvId, TenantCtx, TenantId};
75    use std::str::FromStr;
76
77    #[test]
78    fn install_with_config_is_noop_when_disabled() {
79        let cfg = TelemetryConfig {
80            enabled: false,
81            exporter: TelemetryExporterKind::Otlp,
82            endpoint: Some("http://localhost:4317".to_string()),
83            sampling: 1.0,
84        };
85
86        install_with_config("packc-test", &cfg).expect("disabled config should be a no-op");
87    }
88
89    #[test]
90    fn install_with_config_is_noop_for_none_exporter() {
91        let cfg = TelemetryConfig {
92            enabled: true,
93            exporter: TelemetryExporterKind::None,
94            endpoint: None,
95            sampling: 0.25,
96        };
97
98        install_with_config("packc-test", &cfg).expect("none exporter should be a no-op");
99    }
100
101    #[test]
102    fn set_current_tenant_ctx_accepts_full_context() {
103        let tenant = TenantId::from_str("tenant-a").expect("tenant");
104        let env = EnvId::from_str("dev").expect("env");
105        let ctx = TenantCtx::new(env, tenant)
106            .with_session("sess-123")
107            .with_flow("flow.main")
108            .with_node("node-1")
109            .with_provider("provider-1");
110
111        set_current_tenant_ctx(&ctx);
112    }
113}