use std::fmt;
pub fn init_tracer() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[derive(Clone, Debug)]
pub struct SpanAttribute {
pub key: String,
pub value: String,
}
#[derive(Debug)]
pub struct SpanBuilder {
name: String,
attributes: Vec<SpanAttribute>,
}
impl SpanBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
attributes: Vec::new(),
}
}
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.push(SpanAttribute {
key: key.into(),
value: value.into(),
});
self
}
pub fn build(self) -> Span {
Span {
name: self.name,
attributes: self.attributes,
status: SpanStatus::Ok,
}
}
}
#[derive(Clone, Debug)]
pub enum SpanStatus {
Ok,
Error {
message: String,
code: String,
},
}
#[derive(Clone, Debug)]
pub struct Span {
pub name: String,
pub attributes: Vec<SpanAttribute>,
pub status: SpanStatus,
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Span({})", self.name)
}
}
pub fn create_span(name: impl Into<String>) -> SpanBuilder {
SpanBuilder::new(name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_span_builder() {
let span = SpanBuilder::new("test_operation")
.with_attribute("operation_type", "query")
.with_attribute("user_id", "user-123")
.build();
assert_eq!(span.name, "test_operation");
assert_eq!(span.attributes.len(), 2);
}
}