mod builder;
mod testing;
use std::collections::HashMap;
#[cfg(feature = "otel")]
pub use allframe_macros::traced;
pub use builder::{Observability, ObservabilityBuilder, ObservabilityError, ObservabilityGuard};
pub use testing::{Histogram, MetricsRecorder, Span, SpanContext, SpanRecorder};
#[deprecated(since = "0.2.0", note = "Use tracing::Span::current() instead")]
pub fn current_span_id() -> String {
"span-placeholder".to_string()
}
#[deprecated(since = "0.2.0", note = "Use tracing::Span::current() instead")]
pub fn current_trace_id() -> String {
"trace-placeholder".to_string()
}
#[deprecated(since = "0.2.0", note = "Use tracing spans instead")]
pub fn start_trace(_trace_id: &str) {
}
#[deprecated(since = "0.2.0", note = "Use opentelemetry baggage API instead")]
pub fn set_baggage(_key: &str, _value: &str) {
}
#[deprecated(since = "0.2.0", note = "Use opentelemetry baggage API instead")]
pub fn get_baggage(_key: &str) -> Option<String> {
None
}
#[deprecated(since = "0.2.0", note = "Use opentelemetry propagator API instead")]
pub fn inject_context(_context: &SpanContext) -> HashMap<String, String> {
let mut headers = HashMap::new();
headers.insert("traceparent".to_string(), "placeholder".to_string());
headers
}
#[deprecated(since = "0.2.0", note = "Use opentelemetry propagator API instead")]
pub fn extract_context(headers: &HashMap<String, String>) -> Option<SpanContext> {
headers.get("traceparent").map(|_| SpanContext {
trace_id: "extracted-trace".to_string(),
parent_span_id: "extracted-span".to_string(),
sampled: true,
})
}
#[derive(Debug, Clone, PartialEq)]
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
pub enum ExporterType {
Stdout,
Jaeger {
endpoint: String,
},
Otlp {
endpoint: String,
},
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
#[allow(deprecated)]
pub fn configure_exporter(_exporter: ExporterType) {
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
pub fn configure_batch_export(_batch_size: usize, _flush_interval_ms: u64) {
}
#[deprecated(since = "0.2.0", note = "This function will be removed")]
pub fn get_export_count() -> usize {
0
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
pub fn configure_sampling(_rate: f64) {
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder::build() instead")]
pub fn enable_tracing() {
}
#[deprecated(since = "0.2.0", note = "Drop the ObservabilityGuard instead")]
pub fn disable_tracing() {
}
#[derive(Debug, Clone)]
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
pub struct OtelConfig {
pub service_name: String,
pub exporter_type: String,
pub sampling_rate: f64,
pub batch_size: usize,
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
pub async fn configure_from_file(_path: &str) -> Result<(), String> {
Ok(())
}
#[deprecated(since = "0.2.0", note = "Use ObservabilityBuilder instead")]
#[allow(deprecated)]
pub fn get_config() -> OtelConfig {
OtelConfig {
service_name: "allframe".to_string(),
exporter_type: "stdout".to_string(),
sampling_rate: 1.0,
batch_size: 512,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_span_recorder() {
let recorder = SpanRecorder::new();
let span = Span {
span_id: "span-1".to_string(),
parent_span_id: None,
trace_id: "trace-1".to_string(),
name: "test".to_string(),
attributes: std::collections::HashMap::new(),
status: "ok".to_string(),
error_message: String::new(),
duration_ms: 100.0,
layer: String::new(),
};
recorder.record(span.clone());
let spans = recorder.spans();
assert_eq!(spans.len(), 1);
assert_eq!(spans[0].span_id, "span-1");
}
#[test]
fn test_histogram() {
let hist = Histogram::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
assert_eq!(hist.count(), 5);
assert_eq!(hist.sum(), 15.0);
assert_eq!(hist.p50(), 3.0);
}
#[test]
fn test_builder_creation() {
let _builder = ObservabilityBuilder::new("test-service");
}
}