use std::{fmt, sync::Arc};
pub const MAX_TRACE_CORRELATION_ID_BYTES: usize = 256;
macro_rules! string_id {
($name:ident) => {
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $name(Arc<str>);
impl $name {
pub fn new(value: impl Into<Arc<str>>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for $name {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for $name {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
};
}
string_id!(ApplicationId);
string_id!(ModuleId);
string_id!(ServiceId);
string_id!(OperationId);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct TraceId(u128);
impl TraceId {
pub const fn from_u128(value: u128) -> Self {
Self(value)
}
pub const fn as_u128(self) -> u128 {
self.0
}
}
impl fmt::Display for TraceId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{:032x}", self.0)
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TraceCorrelationId(Arc<str>);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TraceCorrelationIdError {
Empty,
TooLong,
ControlCharacter,
}
impl TraceCorrelationId {
pub fn new(value: impl Into<Arc<str>>) -> Result<Self, TraceCorrelationIdError> {
let value = value.into();
if value.is_empty() {
return Err(TraceCorrelationIdError::Empty);
}
if value.len() > MAX_TRACE_CORRELATION_ID_BYTES {
return Err(TraceCorrelationIdError::TooLong);
}
if value.chars().any(char::is_control) {
return Err(TraceCorrelationIdError::ControlCharacter);
}
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for TraceCorrelationId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SpanId(u64);
impl SpanId {
pub const fn from_u64(value: u64) -> Self {
Self(value)
}
pub const fn as_u64(self) -> u64 {
self.0
}
}
impl fmt::Display for SpanId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{:016x}", self.0)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CallContext {
application: ApplicationId,
module: ModuleId,
service: ServiceId,
operation: OperationId,
trace_id: TraceId,
trace_correlation_id: TraceCorrelationId,
span_id: SpanId,
}
impl CallContext {
pub fn new(
application: ApplicationId,
module: ModuleId,
service: ServiceId,
operation: OperationId,
trace_id: TraceId,
span_id: SpanId,
) -> Self {
let trace_correlation_id = TraceCorrelationId(Arc::from(trace_id.to_string()));
Self {
application,
module,
service,
operation,
trace_id,
trace_correlation_id,
span_id,
}
}
pub fn with_trace_correlation_id(mut self, trace_correlation_id: TraceCorrelationId) -> Self {
self.trace_correlation_id = trace_correlation_id;
self
}
pub fn application(&self) -> &ApplicationId {
&self.application
}
pub fn module(&self) -> &ModuleId {
&self.module
}
pub fn service(&self) -> &ServiceId {
&self.service
}
pub fn operation(&self) -> &OperationId {
&self.operation
}
pub const fn trace_id(&self) -> TraceId {
self.trace_id
}
pub fn trace_correlation_id(&self) -> &TraceCorrelationId {
&self.trace_correlation_id
}
pub const fn span_id(&self) -> SpanId {
self.span_id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identifiers_have_stable_display_forms() {
assert_eq!(TraceId::from_u128(42).to_string().len(), 32);
assert_eq!(SpanId::from_u64(42).to_string().len(), 16);
assert_eq!(ServiceId::from("orders").to_string(), "orders");
}
#[test]
fn opaque_trace_correlation_id_is_bounded_and_rejects_controls() {
assert_eq!(
TraceCorrelationId::new("trace-1").unwrap().as_str(),
"trace-1"
);
assert_eq!(
TraceCorrelationId::new(""),
Err(TraceCorrelationIdError::Empty)
);
assert_eq!(
TraceCorrelationId::new("x".repeat(MAX_TRACE_CORRELATION_ID_BYTES + 1)),
Err(TraceCorrelationIdError::TooLong)
);
assert_eq!(
TraceCorrelationId::new("trace\n1"),
Err(TraceCorrelationIdError::ControlCharacter)
);
}
}