hyperi_rustlib/metrics/otel_types.rs
1// Project: hyperi-rustlib
2// File: src/metrics/otel_types.rs
3// Purpose: Configuration types for OTel metrics backend
4// Language: Rust
5//
6// License: BUSL-1.1
7// Copyright: (c) 2026 HYPERI PTY LIMITED
8
9//! Configuration types for the OpenTelemetry metrics backend.
10//!
11//! These types extend `MetricsConfig` with OTel-specific options
12//! when the `otel-metrics` feature is enabled.
13
14use std::collections::HashMap;
15
16use serde::{Deserialize, Serialize};
17
18/// OTLP transport protocol.
19#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
20#[serde(rename_all = "lowercase")]
21pub enum OtelProtocol {
22 /// gRPC (default, port 4317)
23 #[default]
24 Grpc,
25 /// HTTP/protobuf (port 4318)
26 Http,
27}
28
29impl OtelProtocol {
30 /// Default endpoint for this protocol.
31 #[must_use]
32 pub fn default_endpoint(self) -> &'static str {
33 match self {
34 Self::Grpc => "http://localhost:4317",
35 Self::Http => "http://localhost:4318",
36 }
37 }
38}
39
40/// OTel-specific configuration for the metrics backend.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(default)]
43pub struct OtelMetricsConfig {
44 /// OTLP endpoint (default: protocol-dependent).
45 ///
46 /// Override via `OTEL_EXPORTER_OTLP_ENDPOINT` env var.
47 pub endpoint: String,
48
49 /// OTLP transport protocol.
50 ///
51 /// Override via `OTEL_EXPORTER_OTLP_PROTOCOL` env var.
52 pub protocol: OtelProtocol,
53
54 /// Service name reported in OTel resource.
55 ///
56 /// Override via `OTEL_SERVICE_NAME` env var.
57 pub service_name: String,
58
59 /// Additional OTLP headers (e.g. API keys for HyperDX).
60 pub headers: HashMap<String, String>,
61
62 /// Additional resource attributes.
63 pub resource_attributes: HashMap<String, String>,
64
65 /// Metric export interval in seconds (default: 60).
66 ///
67 /// Override via `OTEL_METRIC_EXPORT_INTERVAL` env var (in milliseconds).
68 pub export_interval_secs: u64,
69}
70
71impl Default for OtelMetricsConfig {
72 fn default() -> Self {
73 Self {
74 endpoint: OtelProtocol::Grpc.default_endpoint().to_string(),
75 protocol: OtelProtocol::default(),
76 service_name: String::new(),
77 headers: HashMap::new(),
78 resource_attributes: HashMap::new(),
79 export_interval_secs: 60,
80 }
81 }
82}