provide-telemetry 0.3.0

Cross-language telemetry helpers with privacy, resilience, and OTLP support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// SPDX-FileCopyrightText: Copyright (C) 2026 provide.io llc
// SPDX-License-Identifier: Apache-2.0
// SPDX-Comment: Part of provide-telemetry.
//
use std::sync::{Mutex, OnceLock};
use std::time::Duration;

use tokio::runtime::Builder;

use provide_telemetry::{
    get_circuit_state, get_health_snapshot, release, run_with_resilience, set_exporter_policy,
    set_queue_policy, set_sampling_policy, should_sample, try_acquire, ExporterPolicy, QueuePolicy,
    SamplingPolicy, Signal,
};

#[cfg(feature = "otel")]
#[path = "../examples/support/e2e_shared.rs"]
mod e2e_shared;

#[path = "../examples/support/basic_telemetry.rs"]
mod basic_telemetry;
#[cfg(feature = "governance")]
#[path = "../examples/support/data_governance.rs"]
mod data_governance;
#[path = "../examples/support/error_degradation.rs"]
mod error_degradation;
#[path = "../examples/support/error_sessions.rs"]
mod error_sessions;
#[path = "../examples/support/exporter_resilience.rs"]
mod exporter_resilience;
#[path = "../examples/support/full_hardening.rs"]
mod full_hardening;
#[path = "../examples/support/lazy_loading.rs"]
mod lazy_loading;
#[path = "../examples/support/performance_metrics.rs"]
mod performance_metrics;
#[path = "../examples/support/pii_cardinality.rs"]
mod pii_cardinality;
#[path = "../examples/support/runtime_reconfigure.rs"]
mod runtime_reconfigure;
#[path = "../examples/support/sampling_backpressure.rs"]
mod sampling_backpressure;
#[path = "../examples/support/security_hardening.rs"]
mod security_hardening;
#[path = "../examples/support/slo_health.rs"]
mod slo_health;
#[path = "../examples/support/w3c_propagation.rs"]
mod w3c_propagation;

static POLICY_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

fn policy_lock() -> &'static Mutex<()> {
    POLICY_LOCK.get_or_init(|| Mutex::new(()))
}

fn reset_policies() {
    provide_telemetry::sampling::_reset_sampling_for_tests();
    provide_telemetry::backpressure::_reset_backpressure_for_tests();
    provide_telemetry::resilience::_reset_resilience_for_tests();
    provide_telemetry::health::_reset_health_for_tests();
}

#[cfg(feature = "otel")]
fn restore_var(key: &str, previous: Option<String>) {
    match previous {
        Some(value) => std::env::set_var(key, value),
        None => std::env::remove_var(key),
    }
}

#[cfg(feature = "otel")]
#[test]
fn integration_test_e2e_tracer_provider_builds_with_http_exporter() {
    let endpoint_key = "OTEL_EXPORTER_OTLP_ENDPOINT";
    let headers_key = "OTEL_EXPORTER_OTLP_HEADERS";
    let previous_endpoint = std::env::var(endpoint_key).ok();
    let previous_headers = std::env::var(headers_key).ok();
    std::env::set_var(endpoint_key, "http://localhost:5080/api/default");
    std::env::set_var(headers_key, "Authorization=Basic%20test");

    let result = e2e_shared::init_tracer_provider("rust-e2e-test");

    restore_var(endpoint_key, previous_endpoint);
    restore_var(headers_key, previous_headers);

    assert!(
        result.is_ok(),
        "expected OTLP tracer provider to build for E2E helper, got {result:?}"
    );
}

#[test]
fn integration_test_basic_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = basic_telemetry::run_demo().expect("basic telemetry example should succeed");

    assert_eq!(summary.iterations, 3);
    assert_eq!(summary.logged_events, 6);
    assert_eq!(summary.counter_total, 3.0);
    assert_eq!(summary.gauge_value, 1.0);
    assert_eq!(summary.histogram_count, 3);
    assert_eq!(summary.histogram_total, 37.5);
    assert_eq!(summary.context_keys_after_clear, 0);
    assert_eq!(summary.unbound_key.as_deref(), Some("region"));
}

#[test]
fn integration_test_w3c_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = w3c_propagation::run_demo().expect("w3c propagation example should succeed");

    assert_eq!(
        summary.http_trace_id.as_deref(),
        Some("4bf92f3577b34da6a3ce929d0e0e4736") // pragma: allowlist secret
    );
    assert_eq!(summary.manual_trace_id_after_clear, None);
    assert_eq!(
        summary.nested_outer_restored.as_deref(),
        Some("1111111111111111ffffffffffffffff")
    );
    assert_eq!(summary.nested_after_clear, None);
}

#[test]
fn integration_test_sampling_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = sampling_backpressure::run_demo().expect("sampling example should succeed");

    assert!(!summary.logs_routine_sampled);
    assert!(summary.logs_critical_sampled);
    assert!(summary.first_trace_ticket_acquired);
    assert!(!summary.second_trace_ticket_acquired);
    assert!(summary.third_trace_ticket_acquired);
    assert!(summary.dropped_traces >= 1);
}

#[test]
fn integration_test_runtime_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = runtime_reconfigure::run_demo().expect("runtime example should succeed");

    assert_eq!(summary.before_logs_rate, 1.0);
    assert_eq!(summary.after_update_logs_rate, 0.0);
    assert_eq!(summary.after_reconfigure_logs_rate, 1.0);
    assert_eq!(summary.after_reload_logs_rate, 1.0);
}

#[test]
fn integration_test_pii_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = pii_cardinality::run_demo().expect("pii/cardinality example should succeed");

    assert_eq!(summary.hashed_email_len, 12);
    assert!(summary.credit_card_removed);
    assert_eq!(summary.truncated_password.as_deref(), Some("hunt..."));
    assert_eq!(summary.cardinality_max_values, Some(1));
    assert_eq!(summary.cardinality_ttl_seconds, Some(1.0));
}

#[test]
fn integration_test_resilience_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = exporter_resilience::run_demo().expect("resilience example should succeed");

    assert!(summary.fail_open_result_is_none);
    assert!(summary.fail_closed_is_error);
    assert!(summary.timeout_result_is_none);
    assert_eq!(summary.metrics_circuit_state.as_str(), "open");
    assert!(summary.metrics_open_count >= 1);
}

#[test]
fn integration_test_slo_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = slo_health::run_demo().expect("slo example should succeed");

    assert_eq!(summary.classify_404.as_deref(), Some("client_error"));
    assert_eq!(summary.classify_503.as_deref(), Some("server_error"));
    assert_eq!(summary.classify_200.as_deref(), Some("ok"));
}

#[test]
fn integration_test_full_hardening_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = full_hardening::run_demo().expect("hardening example should succeed");

    assert_eq!(summary.pii_rules_active, 2);
    assert_eq!(summary.cardinality_limit_max, Some(3));
    assert_eq!(summary.queue_traces_maxsize, 2);
    assert_eq!(summary.metrics_circuit_state.as_str(), "open");
}

#[test]
fn integration_test_error_degradation_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = error_degradation::run_demo().expect("error/degradation example should succeed");

    assert!(summary.configuration_error_seen);
    assert!(summary.event_schema_error_seen);
    assert!(summary.telemetry_error_catchall_count >= 2);
}

#[test]
fn integration_test_performance_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = performance_metrics::run_demo().expect("performance example should succeed");

    assert!(summary.event_ns > 0.0);
    assert!(summary.counter_ns > 0.0);
    assert!(summary.should_sample_ns > 0.0);
}

#[test]
fn integration_test_lazy_loading_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = lazy_loading::run_demo().expect("lazy loading example should succeed");

    assert!(!summary.slo_loaded_before_classify);
    assert!(!summary.metrics_loaded_before_use);
    assert!(summary.slo_loaded_after_classify);
    assert!(summary.metrics_loaded_after_use);
}

#[test]
fn integration_test_error_sessions_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = error_sessions::run_demo().expect("error/session example should succeed");

    assert_eq!(summary.value_error_a, summary.value_error_b);
    assert_ne!(summary.value_error_a, summary.type_error);
    assert!(!summary.runtime_error_fingerprint.is_empty());
    assert_eq!(summary.session_before, None);
    assert_eq!(summary.session_after_bind.as_deref(), Some("sess-demo-42"));
    assert_eq!(summary.session_after_clear, None);
}

#[test]
fn integration_test_security_hardening_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = security_hardening::run_demo().expect("security example should succeed");

    assert!(summary.secret_redacted);
    assert!(summary.password_redacted);
    assert_eq!(summary.depth_preserved_leaf.as_deref(), Some("deep"));
}

#[cfg(feature = "governance")]
#[test]
fn integration_test_data_governance_example_summary_matches_demo_flow() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    let summary = data_governance::run_demo().expect("data governance example should succeed");

    assert!(summary.full_logs_debug_allowed);
    assert!(!summary.none_traces_allowed);
    assert_eq!(summary.redacted_ssn.as_deref(), Some("***"));
    assert_eq!(summary.hashed_card_len, Some(12));
    assert!(summary.diagnosis_dropped);
    assert!(summary.api_key_dropped);
    assert_eq!(summary.ssn_class.as_deref(), Some("PII"));
    assert_eq!(summary.card_class.as_deref(), Some("PCI"));
    assert_eq!(summary.receipt_action.as_deref(), Some("redact"));
    assert!(summary.receipt_hmac_prefix_len >= 8);
}

#[cfg(feature = "otel")]
fn reset_runtime() {
    use provide_telemetry::shutdown_telemetry;

    let _ = shutdown_telemetry();
    provide_telemetry::otel::_reset_otel_for_tests();
}

#[test]
fn integration_test_sampling_drop_increments_health() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    reset_policies();
    set_sampling_policy(
        Signal::Logs,
        SamplingPolicy {
            default_rate: 0.0,
            overrides: Default::default(),
        },
    )
    .expect("policy should set");

    let keep = should_sample(Signal::Logs, Some("event-name")).expect("sampling should work");
    assert!(!keep);

    let snapshot = get_health_snapshot();
    assert_eq!(snapshot.dropped_logs, 1);
}

#[test]
fn integration_test_bounded_queue_drop_increments_health() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    reset_policies();
    set_queue_policy(QueuePolicy {
        logs_maxsize: 1,
        traces_maxsize: 0,
        metrics_maxsize: 0,
    });

    let ticket = try_acquire(Signal::Logs).expect("first acquire should succeed");
    let dropped = try_acquire(Signal::Logs);
    assert!(dropped.is_none());

    let snapshot = get_health_snapshot();
    assert_eq!(snapshot.dropped_logs, 1);

    release(ticket);
    let reacquired = try_acquire(Signal::Logs);
    assert!(
        reacquired.is_some(),
        "release should restore queue capacity"
    );
    if let Some(ticket) = reacquired {
        release(ticket);
    }
}

#[test]
fn integration_test_circuit_breaker_trips_after_three_timeouts() {
    let _guard = policy_lock().lock().expect("policy lock poisoned");
    reset_policies();
    let runtime = Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("runtime");

    runtime.block_on(async {
        set_exporter_policy(
            Signal::Logs,
            ExporterPolicy {
                retries: 0,
                backoff_seconds: 0.0,
                timeout_seconds: 0.01,
                fail_open: true,
                allow_blocking_in_event_loop: false,
            },
        )
        .expect("policy should set");

        for _ in 0..3 {
            let result = run_with_resilience(Signal::Logs, || async {
                tokio::time::sleep(Duration::from_millis(25)).await;
                Ok::<_, provide_telemetry::TelemetryError>(())
            })
            .await
            .expect("timeout should be fail-open");
            assert!(result.is_none());
        }

        let state = get_circuit_state(Signal::Logs).expect("state should be available");
        assert_eq!(state.0, "open");

        let short_circuit = run_with_resilience(Signal::Logs, || async {
            Ok::<_, provide_telemetry::TelemetryError>(())
        })
        .await
        .expect("open circuit should fail open");
        assert!(short_circuit.is_none());
    });

    let snapshot = get_health_snapshot();
    assert!(snapshot.export_failures_logs >= 3);
    assert!(snapshot.circuit_open_count_logs >= 1);
    assert_eq!(snapshot.circuit_state_logs, "open");
}

#[cfg(feature = "otel")]
#[test]
fn integration_test_setup_registers_otel_providers() {
    use provide_telemetry::setup_telemetry;

    let _guard = policy_lock().lock().expect("policy lock poisoned");
    reset_runtime();

    std::env::set_var("PROVIDE_TELEMETRY_SERVICE_NAME", "rust-otel-test");
    let config = setup_telemetry().expect("setup should succeed");

    assert_eq!(config.service_name, "rust-otel-test");
    assert!(provide_telemetry::otel::otel_installed_for_tests());

    reset_runtime();
    std::env::remove_var("PROVIDE_TELEMETRY_SERVICE_NAME");
}

#[cfg(feature = "otel")]
#[test]
fn integration_test_reconfigure_rejects_provider_replacement_after_install() {
    use provide_telemetry::{get_runtime_config, reconfigure_telemetry, setup_telemetry};

    let _guard = policy_lock().lock().expect("policy lock poisoned");
    reset_runtime();

    std::env::set_var("PROVIDE_TELEMETRY_SERVICE_NAME", "rust-otel-test");
    setup_telemetry().expect("setup should succeed");

    let mut changed = get_runtime_config().expect("runtime config should exist");
    changed.service_name = "rust-otel-test-2".to_string();
    let err = reconfigure_telemetry(Some(changed)).expect_err("provider replacement should fail");
    assert!(err
        .to_string()
        .contains("OpenTelemetry providers already installed"));

    reset_runtime();
    std::env::remove_var("PROVIDE_TELEMETRY_SERVICE_NAME");
}