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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// SPDX-FileCopyrightText: Copyright (C) 2026 provide.io llc
// SPDX-License-Identifier: Apache-2.0
// SPDX-Comment: Part of provide-telemetry.
//

use std::collections::HashMap;
use std::env;

use percent_encoding::percent_decode_str;
use serde::{Deserialize, Serialize};

use crate::errors::ConfigurationError;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct RuntimeOverrides {
    pub sampling: Option<SamplingConfig>,
    pub backpressure: Option<BackpressureConfig>,
    pub exporter: Option<ExporterPolicyConfig>,
    pub security: Option<SecurityConfig>,
    pub slo: Option<SLOConfig>,
    pub pii_max_depth: Option<usize>,
    pub strict_schema: Option<bool>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct LoggingConfig {
    pub level: String,
    pub fmt: String,
    pub otlp_headers: HashMap<String, String>,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "INFO".to_string(),
            fmt: "console".to_string(),
            otlp_headers: HashMap::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TracingConfig {
    pub enabled: bool,
    pub otlp_headers: HashMap<String, String>,
}

impl Default for TracingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            otlp_headers: HashMap::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MetricsConfig {
    pub enabled: bool,
    pub otlp_headers: HashMap<String, String>,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            otlp_headers: HashMap::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct SchemaConfig {
    pub required_keys: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SamplingConfig {
    pub logs_rate: f64,
    pub traces_rate: f64,
    pub metrics_rate: f64,
}

impl Default for SamplingConfig {
    fn default() -> Self {
        Self {
            logs_rate: 1.0,
            traces_rate: 1.0,
            metrics_rate: 1.0,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct BackpressureConfig {
    pub logs_maxsize: usize,
    pub traces_maxsize: usize,
    pub metrics_maxsize: usize,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ExporterPolicyConfig {
    pub logs_retries: usize,
    pub traces_retries: usize,
    pub metrics_retries: usize,
    pub logs_backoff_seconds: f64,
    pub traces_backoff_seconds: f64,
    pub metrics_backoff_seconds: f64,
    pub logs_timeout_seconds: f64,
    pub traces_timeout_seconds: f64,
    pub metrics_timeout_seconds: f64,
    pub logs_fail_open: bool,
    pub traces_fail_open: bool,
    pub metrics_fail_open: bool,
}

impl Default for ExporterPolicyConfig {
    fn default() -> Self {
        Self {
            logs_retries: 0,
            traces_retries: 0,
            metrics_retries: 0,
            logs_backoff_seconds: 0.0,
            traces_backoff_seconds: 0.0,
            metrics_backoff_seconds: 0.0,
            logs_timeout_seconds: 10.0,
            traces_timeout_seconds: 10.0,
            metrics_timeout_seconds: 10.0,
            logs_fail_open: true,
            traces_fail_open: true,
            metrics_fail_open: true,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SLOConfig {
    pub enable_red_metrics: bool,
    pub enable_use_metrics: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecurityConfig {
    pub max_attr_value_length: usize,
    pub max_attr_count: usize,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            max_attr_value_length: 1024,
            max_attr_count: 64,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TelemetryConfig {
    pub service_name: String,
    pub environment: String,
    pub version: String,
    pub strict_schema: bool,
    pub pii_max_depth: usize,
    pub logging: LoggingConfig,
    pub tracing: TracingConfig,
    pub metrics: MetricsConfig,
    pub event_schema: SchemaConfig,
    pub sampling: SamplingConfig,
    pub backpressure: BackpressureConfig,
    pub exporter: ExporterPolicyConfig,
    pub slo: SLOConfig,
    pub security: SecurityConfig,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            service_name: "provide-service".to_string(),
            environment: "dev".to_string(),
            version: "0.0.0".to_string(),
            strict_schema: false,
            pii_max_depth: 8,
            logging: LoggingConfig::default(),
            tracing: TracingConfig::default(),
            metrics: MetricsConfig::default(),
            event_schema: SchemaConfig::default(),
            sampling: SamplingConfig::default(),
            backpressure: BackpressureConfig::default(),
            exporter: ExporterPolicyConfig::default(),
            slo: SLOConfig::default(),
            security: SecurityConfig::default(),
        }
    }
}

impl TelemetryConfig {
    pub fn from_env() -> Result<Self, ConfigurationError> {
        let env_map = env::vars().collect::<HashMap<_, _>>();
        Self::from_map(&env_map)
    }

    pub fn from_map(env: &HashMap<String, String>) -> Result<Self, ConfigurationError> {
        let shared_headers = parse_otlp_headers(env_value(env, &["OTEL_EXPORTER_OTLP_HEADERS"]))?
            .unwrap_or_default();

        Ok(Self {
            service_name: env_value(env, &["PROVIDE_TELEMETRY_SERVICE_NAME"])
                .unwrap_or("provide-service")
                .to_string(),
            environment: env_value(env, &["PROVIDE_TELEMETRY_ENV", "PROVIDE_ENV"])
                .unwrap_or("dev")
                .to_string(),
            version: env_value(env, &["PROVIDE_TELEMETRY_VERSION", "PROVIDE_VERSION"])
                .unwrap_or("0.0.0")
                .to_string(),
            strict_schema: parse_bool(
                env_value(env, &["PROVIDE_TELEMETRY_STRICT_SCHEMA"]),
                false,
                "PROVIDE_TELEMETRY_STRICT_SCHEMA",
            )?,
            pii_max_depth: parse_usize(
                env_value(env, &["PROVIDE_LOG_PII_MAX_DEPTH"]),
                8,
                "PROVIDE_LOG_PII_MAX_DEPTH",
            )?,
            logging: LoggingConfig {
                level: env_value(env, &["PROVIDE_LOG_LEVEL"])
                    .unwrap_or("INFO")
                    .to_string(),
                fmt: env_value(env, &["PROVIDE_LOG_FORMAT"])
                    .unwrap_or("console")
                    .to_string(),
                otlp_headers: parse_otlp_headers(env_value(
                    env,
                    &["OTEL_EXPORTER_OTLP_LOGS_HEADERS"],
                ))?
                .unwrap_or_else(|| shared_headers.clone()),
            },
            tracing: TracingConfig {
                enabled: parse_bool(
                    env_value(env, &["PROVIDE_TRACE_ENABLED"]),
                    true,
                    "PROVIDE_TRACE_ENABLED",
                )?,
                otlp_headers: parse_otlp_headers(env_value(
                    env,
                    &["OTEL_EXPORTER_OTLP_TRACES_HEADERS"],
                ))?
                .unwrap_or_else(|| shared_headers.clone()),
            },
            metrics: MetricsConfig {
                enabled: parse_bool(
                    env_value(env, &["PROVIDE_METRICS_ENABLED"]),
                    true,
                    "PROVIDE_METRICS_ENABLED",
                )?,
                otlp_headers: parse_otlp_headers(env_value(
                    env,
                    &["OTEL_EXPORTER_OTLP_METRICS_HEADERS"],
                ))?
                .unwrap_or(shared_headers),
            },
            event_schema: SchemaConfig::default(),
            sampling: SamplingConfig {
                logs_rate: parse_rate(
                    env_value(env, &["PROVIDE_SAMPLING_LOGS_RATE"]),
                    1.0,
                    "PROVIDE_SAMPLING_LOGS_RATE",
                )?,
                traces_rate: parse_rate(
                    env_value(env, &["PROVIDE_SAMPLING_TRACES_RATE"]),
                    1.0,
                    "PROVIDE_SAMPLING_TRACES_RATE",
                )?,
                metrics_rate: parse_rate(
                    env_value(env, &["PROVIDE_SAMPLING_METRICS_RATE"]),
                    1.0,
                    "PROVIDE_SAMPLING_METRICS_RATE",
                )?,
            },
            backpressure: BackpressureConfig {
                logs_maxsize: parse_usize(
                    env_value(env, &["PROVIDE_BACKPRESSURE_LOGS_MAXSIZE"]),
                    0,
                    "PROVIDE_BACKPRESSURE_LOGS_MAXSIZE",
                )?,
                traces_maxsize: parse_usize(
                    env_value(env, &["PROVIDE_BACKPRESSURE_TRACES_MAXSIZE"]),
                    0,
                    "PROVIDE_BACKPRESSURE_TRACES_MAXSIZE",
                )?,
                metrics_maxsize: parse_usize(
                    env_value(env, &["PROVIDE_BACKPRESSURE_METRICS_MAXSIZE"]),
                    0,
                    "PROVIDE_BACKPRESSURE_METRICS_MAXSIZE",
                )?,
            },
            exporter: ExporterPolicyConfig {
                logs_retries: parse_usize(
                    env_value(env, &["PROVIDE_EXPORTER_LOGS_RETRIES"]),
                    0,
                    "PROVIDE_EXPORTER_LOGS_RETRIES",
                )?,
                traces_retries: parse_usize(
                    env_value(env, &["PROVIDE_EXPORTER_TRACES_RETRIES"]),
                    0,
                    "PROVIDE_EXPORTER_TRACES_RETRIES",
                )?,
                metrics_retries: parse_usize(
                    env_value(env, &["PROVIDE_EXPORTER_METRICS_RETRIES"]),
                    0,
                    "PROVIDE_EXPORTER_METRICS_RETRIES",
                )?,
                logs_backoff_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_LOGS_BACKOFF_SECONDS"]),
                    0.0,
                    "PROVIDE_EXPORTER_LOGS_BACKOFF_SECONDS",
                )?,
                traces_backoff_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_TRACES_BACKOFF_SECONDS"]),
                    0.0,
                    "PROVIDE_EXPORTER_TRACES_BACKOFF_SECONDS",
                )?,
                metrics_backoff_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_METRICS_BACKOFF_SECONDS"]),
                    0.0,
                    "PROVIDE_EXPORTER_METRICS_BACKOFF_SECONDS",
                )?,
                logs_timeout_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_LOGS_TIMEOUT_SECONDS"]),
                    10.0,
                    "PROVIDE_EXPORTER_LOGS_TIMEOUT_SECONDS",
                )?,
                traces_timeout_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_TRACES_TIMEOUT_SECONDS"]),
                    10.0,
                    "PROVIDE_EXPORTER_TRACES_TIMEOUT_SECONDS",
                )?,
                metrics_timeout_seconds: parse_non_negative_float(
                    env_value(env, &["PROVIDE_EXPORTER_METRICS_TIMEOUT_SECONDS"]),
                    10.0,
                    "PROVIDE_EXPORTER_METRICS_TIMEOUT_SECONDS",
                )?,
                logs_fail_open: parse_bool(
                    env_value(env, &["PROVIDE_EXPORTER_LOGS_FAIL_OPEN"]),
                    true,
                    "PROVIDE_EXPORTER_LOGS_FAIL_OPEN",
                )?,
                traces_fail_open: parse_bool(
                    env_value(env, &["PROVIDE_EXPORTER_TRACES_FAIL_OPEN"]),
                    true,
                    "PROVIDE_EXPORTER_TRACES_FAIL_OPEN",
                )?,
                metrics_fail_open: parse_bool(
                    env_value(env, &["PROVIDE_EXPORTER_METRICS_FAIL_OPEN"]),
                    true,
                    "PROVIDE_EXPORTER_METRICS_FAIL_OPEN",
                )?,
            },
            slo: SLOConfig {
                enable_red_metrics: parse_bool(
                    env_value(env, &["PROVIDE_SLO_ENABLE_RED_METRICS"]),
                    false,
                    "PROVIDE_SLO_ENABLE_RED_METRICS",
                )?,
                enable_use_metrics: parse_bool(
                    env_value(env, &["PROVIDE_SLO_ENABLE_USE_METRICS"]),
                    false,
                    "PROVIDE_SLO_ENABLE_USE_METRICS",
                )?,
            },
            security: SecurityConfig {
                max_attr_value_length: parse_usize(
                    env_value(env, &["PROVIDE_SECURITY_MAX_ATTR_VALUE_LENGTH"]),
                    1024,
                    "PROVIDE_SECURITY_MAX_ATTR_VALUE_LENGTH",
                )?,
                max_attr_count: parse_usize(
                    env_value(env, &["PROVIDE_SECURITY_MAX_ATTR_COUNT"]),
                    64,
                    "PROVIDE_SECURITY_MAX_ATTR_COUNT",
                )?,
            },
        })
    }
}

pub fn redact_config(cfg: &TelemetryConfig) -> TelemetryConfig {
    fn mask(headers: &HashMap<String, String>) -> HashMap<String, String> {
        headers
            .keys()
            .map(|k| (k.clone(), "***REDACTED***".to_string()))
            .collect()
    }
    let mut out = cfg.clone();
    if !out.logging.otlp_headers.is_empty() {
        out.logging.otlp_headers = mask(&cfg.logging.otlp_headers);
    }
    if !out.tracing.otlp_headers.is_empty() {
        out.tracing.otlp_headers = mask(&cfg.tracing.otlp_headers);
    }
    if !out.metrics.otlp_headers.is_empty() {
        out.metrics.otlp_headers = mask(&cfg.metrics.otlp_headers);
    }
    out
}

fn env_value<'a>(env: &'a HashMap<String, String>, keys: &[&str]) -> Option<&'a str> {
    keys.iter()
        .find_map(|key| env.get(*key).map(String::as_str))
}

fn parse_bool(raw: Option<&str>, default: bool, field: &str) -> Result<bool, ConfigurationError> {
    match raw.map(str::trim) {
        None | Some("") => Ok(default),
        Some(value)
            if matches!(
                value.to_ascii_lowercase().as_str(),
                "1" | "true" | "yes" | "on"
            ) =>
        {
            Ok(true)
        }
        Some(value)
            if matches!(
                value.to_ascii_lowercase().as_str(),
                "0" | "false" | "no" | "off"
            ) =>
        {
            Ok(false)
        }
        Some(value) => Err(ConfigurationError::new(format!(
            "invalid boolean for {field}: {value:?} (expected one of: 1,true,yes,on,0,false,no,off)"
        ))),
    }
}

fn parse_usize(
    raw: Option<&str>,
    default: usize,
    field: &str,
) -> Result<usize, ConfigurationError> {
    match raw.map(str::trim) {
        None | Some("") => Ok(default),
        Some(value) => value.parse::<usize>().map_err(|_| {
            ConfigurationError::new(format!("invalid integer for {field}: {value:?}"))
        }),
    }
}

fn parse_non_negative_float(
    raw: Option<&str>,
    default: f64,
    field: &str,
) -> Result<f64, ConfigurationError> {
    match raw.map(str::trim) {
        None | Some("") => Ok(default),
        Some(value) => {
            let parsed = value.parse::<f64>().map_err(|_| {
                ConfigurationError::new(format!("invalid float for {field}: {value:?}"))
            })?;
            if !parsed.is_finite() || parsed < 0.0 {
                return Err(ConfigurationError::new(format!(
                    "{field} must be >= 0, got {parsed}"
                )));
            }
            Ok(parsed)
        }
    }
}

fn parse_rate(raw: Option<&str>, default: f64, field: &str) -> Result<f64, ConfigurationError> {
    let parsed = parse_non_negative_float(raw, default, field)?;
    if !(0.0..=1.0).contains(&parsed) {
        return Err(ConfigurationError::new(format!(
            "{field} must be in [0, 1], got {parsed}"
        )));
    }
    Ok(parsed)
}

fn parse_otlp_headers(
    raw: Option<&str>,
) -> Result<Option<HashMap<String, String>>, ConfigurationError> {
    let Some(raw) = raw else {
        return Ok(None);
    };
    if raw.trim().is_empty() {
        return Ok(Some(HashMap::new()));
    }

    let mut headers = HashMap::new();
    for pair in raw.split(',') {
        let Some((key, value)) = pair.split_once('=') else {
            continue;
        };
        let Ok(key) = decode_header_component(key.trim()) else {
            continue;
        };
        if key.is_empty() {
            continue;
        }
        let Ok(value) = decode_header_component(value.trim()) else {
            continue;
        };
        headers.insert(key, value);
    }
    Ok(Some(headers))
}

fn decode_header_component(raw: &str) -> Result<String, ConfigurationError> {
    if has_invalid_percent_encoding(raw) {
        return Err(ConfigurationError::new(format!(
            "invalid OTLP header encoding: {raw:?}"
        )));
    }
    Ok(percent_decode_str(raw).decode_utf8_lossy().into_owned())
}

fn has_invalid_percent_encoding(raw: &str) -> bool {
    let bytes = raw.as_bytes();
    let mut idx = 0;
    while idx < bytes.len() {
        if bytes[idx] == b'%' {
            if idx + 2 >= bytes.len()
                || !bytes[idx + 1].is_ascii_hexdigit()
                || !bytes[idx + 2].is_ascii_hexdigit()
            {
                return true;
            }
            idx += 3;
            continue;
        }
        idx += 1;
    }
    false
}