use std::time::SystemTime;
use opentelemetry::Context;
use opentelemetry::trace::{Span, SpanBuilder, Tracer};
use subms::{ObservationCtx, SubMsBenchSummary, SubMsObserver};
use crate::bridge::attributes_from_ctx;
pub const TRACING_SPAN_NAME: &str = "subms.stage.record";
pub struct TracingObserver<T: Tracer + Send + Sync> {
tracer: T,
}
impl<T> TracingObserver<T>
where
T: Tracer + Send + Sync,
T::Span: Span + Send + Sync + 'static,
{
pub fn new(tracer: T) -> Self {
Self { tracer }
}
}
impl<T> SubMsObserver for TracingObserver<T>
where
T: Tracer + Send + Sync,
T::Span: Span + Send + Sync + 'static,
{
fn on_record(&self, ctx: &ObservationCtx, ns: u64) {
let now = SystemTime::now();
let start = now
.checked_sub(std::time::Duration::from_nanos(ns))
.unwrap_or(now);
let parent_ctx = Context::current();
let attrs = attributes_from_ctx(ctx);
let mut span = SpanBuilder::from_name(TRACING_SPAN_NAME)
.with_start_time(start)
.with_attributes(attrs)
.start_with_context(&self.tracer, &parent_ctx);
span.end_with_timestamp(now);
}
fn on_summarize(&self, _summary: &SubMsBenchSummary) {
}
}