use crate::{trace::SpanContext, KeyValue};
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::time::SystemTime;
pub trait Span {
fn add_event<T>(&mut self, name: T, attributes: Vec<KeyValue>)
where
T: Into<Cow<'static, str>>,
{
self.add_event_with_timestamp(name, crate::time::now(), attributes)
}
fn record_exception(&mut self, err: &dyn Error) {
let attributes = vec![KeyValue::new("exception.message", err.to_string())];
self.add_event("exception".to_string(), attributes);
}
fn record_exception_with_stacktrace<T>(&mut self, err: &dyn Error, stacktrace: T)
where
T: Into<Cow<'static, str>>,
{
let attributes = vec![
KeyValue::new("exception.message", err.to_string()),
KeyValue::new("exception.stacktrace", stacktrace.into()),
];
self.add_event("exception".to_string(), attributes);
}
fn add_event_with_timestamp<T>(
&mut self,
name: T,
timestamp: SystemTime,
attributes: Vec<KeyValue>,
) where
T: Into<Cow<'static, str>>;
fn span_context(&self) -> &SpanContext;
fn is_recording(&self) -> bool;
fn set_attribute(&mut self, attribute: KeyValue);
fn set_status(&mut self, code: StatusCode, message: String);
fn update_name<T>(&mut self, new_name: T)
where
T: Into<Cow<'static, str>>;
fn end(&mut self) {
self.end_with_timestamp(crate::time::now());
}
fn end_with_timestamp(&mut self, timestamp: SystemTime);
}
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, PartialEq)]
pub enum SpanKind {
Client,
Server,
Producer,
Consumer,
Internal,
}
impl fmt::Display for SpanKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SpanKind::Client => write!(f, "client"),
SpanKind::Server => write!(f, "server"),
SpanKind::Producer => write!(f, "producer"),
SpanKind::Consumer => write!(f, "consumer"),
SpanKind::Internal => write!(f, "internal"),
}
}
}
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum StatusCode {
Unset,
Ok,
Error,
}
impl StatusCode {
pub fn as_str(&self) -> &'static str {
match self {
StatusCode::Unset => "",
StatusCode::Ok => "OK",
StatusCode::Error => "ERROR",
}
}
pub(crate) fn priority(&self) -> i32 {
match self {
StatusCode::Unset => 0,
StatusCode::Error => 1,
StatusCode::Ok => 2,
}
}
}