clnrm_core/telemetry/
config.rs

1//! Telemetry configuration types for clnrm
2//!
3//! Provides comprehensive configuration for OpenTelemetry integration
4//! with support for multiple exporters and sampling strategies.
5
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9/// Main telemetry configuration
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct TelemetryConfig {
12    /// Whether telemetry is enabled
13    pub enabled: bool,
14    /// Service name for telemetry data
15    pub service_name: String,
16    /// Service version for telemetry data
17    pub service_version: String,
18    /// List of exporters to use
19    pub exporters: Vec<ExporterConfig>,
20    /// Sampling configuration
21    pub sampling: SamplingConfig,
22    /// Resource attributes to attach to all telemetry data
23    pub resource_attributes: HashMap<String, String>,
24}
25
26impl Default for TelemetryConfig {
27    fn default() -> Self {
28        Self {
29            enabled: false,
30            service_name: "clnrm".to_string(),
31            service_version: "1.0.0".to_string(),
32            exporters: vec![ExporterConfig::Stdout { pretty_print: true }],
33            sampling: SamplingConfig::default(),
34            resource_attributes: HashMap::new(),
35        }
36    }
37}
38
39/// Configuration for different telemetry exporters
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub enum ExporterConfig {
42    /// OTLP HTTP exporter
43    Otlp {
44        /// Endpoint URL (e.g., "http://localhost:4318")
45        endpoint: String,
46        /// Protocol to use
47        protocol: OtlpProtocol,
48        /// Custom headers to include
49        headers: HashMap<String, String>,
50    },
51    /// Jaeger exporter
52    Jaeger {
53        /// Jaeger collector endpoint
54        endpoint: String,
55        /// Agent host (optional)
56        agent_host: Option<String>,
57        /// Agent port (optional)
58        agent_port: Option<u16>,
59    },
60    /// Zipkin exporter
61    Zipkin {
62        /// Zipkin collector endpoint
63        endpoint: String,
64    },
65    /// Stdout exporter for development
66    Stdout {
67        /// Whether to use pretty printing
68        pretty_print: bool,
69    },
70}
71
72/// OTLP protocol options
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub enum OtlpProtocol {
75    /// HTTP with protobuf
76    HttpProto,
77    /// gRPC
78    Grpc,
79}
80
81impl Default for OtlpProtocol {
82    fn default() -> Self {
83        Self::HttpProto
84    }
85}
86
87/// Sampling configuration for traces
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct SamplingConfig {
90    /// Trace sampling ratio (0.0 to 1.0)
91    pub trace_sampling_ratio: f64,
92    /// Whether to use parent-based sampling
93    pub parent_based: bool,
94}
95
96impl Default for SamplingConfig {
97    fn default() -> Self {
98        Self {
99            trace_sampling_ratio: 1.0, // Sample all traces by default
100            parent_based: true,
101        }
102    }
103}
104
105/// Validation configuration for OpenTelemetry data
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct OtelValidationConfig {
108    /// Whether to validate spans
109    pub validate_spans: bool,
110    /// Whether to validate traces
111    pub validate_traces: bool,
112    /// Whether to validate exports
113    pub validate_exports: bool,
114    /// Whether to validate performance overhead
115    pub validate_performance: bool,
116}
117
118impl Default for OtelValidationConfig {
119    fn default() -> Self {
120        Self {
121            validate_spans: true,
122            validate_traces: true,
123            validate_exports: true,
124            validate_performance: true,
125        }
126    }
127}
128
129/// Span assertion for validation
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct SpanAssertion {
132    /// Name of the span to validate
133    pub name: String,
134    /// Whether this span is required
135    pub required: bool,
136    /// Expected attributes
137    pub attributes: HashMap<String, String>,
138    /// Minimum duration in milliseconds
139    pub min_duration_ms: Option<f64>,
140    /// Maximum duration in milliseconds
141    pub max_duration_ms: Option<f64>,
142}
143
144/// Trace assertion for validation
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct TraceAssertion {
147    /// Trace ID to validate (optional)
148    pub trace_id: Option<String>,
149    /// Expected spans in the trace
150    pub expected_spans: Vec<SpanAssertion>,
151    /// Parent-child relationships to validate
152    pub parent_child_relationships: Vec<ParentChildRelationship>,
153    /// Whether the trace should be complete
154    pub complete: bool,
155}
156
157/// Parent-child relationship assertion
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct ParentChildRelationship {
160    /// Name of the parent span
161    pub parent_span_name: String,
162    /// Name of the child span
163    pub child_span_name: String,
164}
165
166/// Result of span validation
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct SpanValidationResult {
169    /// Whether validation passed
170    pub passed: bool,
171    /// Name of the validated span
172    pub span_name: String,
173    /// Validation errors
174    pub errors: Vec<String>,
175    /// Actual attributes found
176    pub actual_attributes: HashMap<String, String>,
177    /// Actual duration in milliseconds
178    pub actual_duration_ms: Option<f64>,
179}
180
181/// Result of trace validation
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct TraceValidationResult {
184    /// Whether validation passed
185    pub passed: bool,
186    /// Trace ID that was validated
187    pub trace_id: Option<String>,
188    /// Expected number of spans
189    pub expected_span_count: usize,
190    /// Actual number of spans found
191    pub actual_span_count: usize,
192    /// Results for each span validation
193    pub span_results: Vec<SpanValidationResult>,
194    /// Validation errors
195    pub errors: Vec<String>,
196}