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