allframe_core/otel/
mod.rs1mod builder;
29mod testing;
30
31#[cfg(feature = "otel")]
33pub use allframe_macros::traced;
34
35pub use builder::{Observability, ObservabilityBuilder, ObservabilityError, ObservabilityGuard};
37
38pub use testing::{Histogram, MetricsRecorder, Span, SpanContext, SpanRecorder};
40
41use std::collections::HashMap;
45
46#[deprecated(since = "0.2.0", note = "Use tracing::Span::current() instead")]
48pub fn current_span_id() -> String {
49 "span-placeholder".to_string()
50}
51
52#[deprecated(since = "0.2.0", note = "Use tracing::Span::current() instead")]
54pub fn current_trace_id() -> String {
55 "trace-placeholder".to_string()
56}
57
58#[deprecated(since = "0.2.0", note = "Use tracing spans instead")]
60pub fn start_trace(_trace_id: &str) {
61 }
63
64#[deprecated(since = "0.2.0", note = "Use opentelemetry baggage API instead")]
66pub fn set_baggage(_key: &str, _value: &str) {
67 }
69
70#[deprecated(since = "0.2.0", note = "Use opentelemetry baggage API instead")]
72pub fn get_baggage(_key: &str) -> Option<String> {
73 None
74}
75
76#[deprecated(
78 since = "0.2.0",
79 note = "Use opentelemetry propagator API instead"
80)]
81pub fn inject_context(_context: &SpanContext) -> HashMap<String, String> {
82 let mut headers = HashMap::new();
83 headers.insert("traceparent".to_string(), "placeholder".to_string());
84 headers
85}
86
87#[deprecated(
89 since = "0.2.0",
90 note = "Use opentelemetry propagator API instead"
91)]
92pub fn extract_context(headers: &HashMap<String, String>) -> Option<SpanContext> {
93 headers.get("traceparent").map(|_| SpanContext {
94 trace_id: "extracted-trace".to_string(),
95 parent_span_id: "extracted-span".to_string(),
96 sampled: true,
97 })
98}
99
100#[derive(Debug, Clone, PartialEq)]
102#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
103pub enum ExporterType {
104 Stdout,
106 Jaeger {
108 endpoint: String,
110 },
111 Otlp {
113 endpoint: String,
115 },
116}
117
118#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
120#[allow(deprecated)]
121pub fn configure_exporter(_exporter: ExporterType) {
122 }
124
125#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
127pub fn configure_batch_export(_batch_size: usize, _flush_interval_ms: u64) {
128 }
130
131#[deprecated(since = "0.2.0", note = "This function will be removed")]
133pub fn get_export_count() -> usize {
134 0
135}
136
137#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
139pub fn configure_sampling(_rate: f64) {
140 }
142
143#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder::build() instead")]
145pub fn enable_tracing() {
146 }
148
149#[deprecated(since = "0.2.0", note = "Drop the ObservabilityGuard instead")]
151pub fn disable_tracing() {
152 }
154
155#[derive(Debug, Clone)]
157#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
158pub struct OtelConfig {
159 pub service_name: String,
161 pub exporter_type: String,
163 pub sampling_rate: f64,
165 pub batch_size: usize,
167}
168
169#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
171pub async fn configure_from_file(_path: &str) -> Result<(), String> {
172 Ok(())
173}
174
175#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
177#[allow(deprecated)]
178pub fn get_config() -> OtelConfig {
179 OtelConfig {
180 service_name: "allframe".to_string(),
181 exporter_type: "stdout".to_string(),
182 sampling_rate: 1.0,
183 batch_size: 512,
184 }
185}
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[test]
192 fn test_span_recorder() {
193 let recorder = SpanRecorder::new();
194
195 let span = Span {
196 span_id: "span-1".to_string(),
197 parent_span_id: None,
198 trace_id: "trace-1".to_string(),
199 name: "test".to_string(),
200 attributes: std::collections::HashMap::new(),
201 status: "ok".to_string(),
202 error_message: String::new(),
203 duration_ms: 100.0,
204 layer: String::new(),
205 };
206
207 recorder.record(span.clone());
208 let spans = recorder.spans();
209
210 assert_eq!(spans.len(), 1);
211 assert_eq!(spans[0].span_id, "span-1");
212 }
213
214 #[test]
215 fn test_histogram() {
216 let hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
217
218 assert_eq!(hist.count(), 5);
219 assert_eq!(hist.sum(), 15.0);
220 assert_eq!(hist.p50(), 3.0);
221 }
222
223 #[test]
224 fn test_builder_creation() {
225 let _builder = ObservabilityBuilder::new("test-service");
227 }
228}