use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use opentelemetry::trace::{Span as _, SpanKind, Status, TraceContextExt, Tracer as _};
use opentelemetry::{Context, KeyValue};
use opentelemetry_sdk::trace::SdkTracer;
use serde_json::Value;
use crate::core::{Error, Result};
use crate::testing::runner::{CategoryHandler, Scenario, ScenarioSession, StepOutput, Vars};
use crate::tracing::{otel_value, TraceQueryClient, TracingHandle, WaitForTraceOptions};
const LIST_SERVICES_INTERVAL: Duration = Duration::from_millis(500);
pub struct TracingTarget {
pub handle: TracingHandle,
pub query: Option<Arc<dyn TraceQueryClient>>,
}
type Factory =
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = Result<TracingTarget>> + Send>> + Send + Sync>;
#[derive(Clone)]
pub struct TracingHandler {
factory: Factory,
}
impl std::fmt::Debug for TracingHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TracingHandler").finish_non_exhaustive()
}
}
impl TracingHandler {
pub fn new<F, Fut>(factory: F) -> Self
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<TracingTarget>> + Send + 'static,
{
TracingHandler {
factory: Arc::new(move || Box::pin(factory())),
}
}
}
#[async_trait]
impl CategoryHandler for TracingHandler {
fn category(&self) -> &str {
"tracing"
}
async fn open(&self, _scenario: &Scenario, _vars: &Vars) -> Result<Box<dyn ScenarioSession>> {
let target = (self.factory)().await?;
Ok(Box::new(TracingSession { target }))
}
}
struct TracingSession {
target: TracingTarget,
}
fn kind_of(s: Option<&str>) -> SpanKind {
match s.map(str::to_ascii_lowercase).as_deref() {
Some("server") => SpanKind::Server,
Some("client") => SpanKind::Client,
Some("producer") => SpanKind::Producer,
Some("consumer") => SpanKind::Consumer,
_ => SpanKind::Internal,
}
}
fn create_span(tracer: &SdkTracer, node: &Value, parent: &Context) -> Result<Context> {
let name = node
.get("name")
.and_then(Value::as_str)
.ok_or_else(|| Error::validation("span op needs a \"name\""))?
.to_string();
let attributes: Vec<KeyValue> = node
.get("attributes")
.and_then(Value::as_object)
.map(|m| {
m.iter()
.map(|(k, v)| KeyValue::new(k.clone(), otel_value(v)))
.collect()
})
.unwrap_or_default();
let mut span = tracer
.span_builder(name)
.with_kind(kind_of(node.get("kind").and_then(Value::as_str)))
.with_attributes(attributes)
.start_with_context(tracer, parent);
for event in node
.get("events")
.and_then(Value::as_array)
.into_iter()
.flatten()
{
if let Some(e) = event.as_str() {
span.add_event(e.to_string(), Vec::new());
}
}
if let Some(msg) = node.get("error").and_then(Value::as_str) {
span.add_event(
"exception",
vec![KeyValue::new("exception.message", msg.to_string())],
);
span.set_status(Status::error(msg.to_string()));
}
let cx = parent.with_span(span);
for child in node
.get("children")
.and_then(Value::as_array)
.into_iter()
.flatten()
{
create_span(tracer, child, &cx)?;
}
cx.span().end();
Ok(cx)
}
impl TracingSession {
fn query(&self) -> Result<&Arc<dyn TraceQueryClient>> {
self.target
.query
.as_ref()
.ok_or_else(|| Error::unsupported("tracing target has no query client"))
}
}
#[async_trait]
impl ScenarioSession for TracingSession {
async fn run(&mut self, op: &str, args: &Value, vars: &Vars) -> Result<StepOutput> {
let handle = &self.target.handle;
match op {
"span" => {
let tracer = handle.tracer("grow-conformance", None);
let cx = create_span(&tracer, args, &Context::new())?;
let trace_id = cx.span().span_context().trace_id().to_string();
Ok(StepOutput::json(Value::String(trace_id)))
}
"flush" => {
handle.force_flush(None).await?;
Ok(StepOutput::none())
}
"getTraceUntil" => {
let id = args
.get("traceId")
.and_then(Value::as_str)
.ok_or_else(|| Error::validation("getTraceUntil needs a \"traceId\""))?;
let opts = WaitForTraceOptions::default()
.min_spans(args.get("minSpans").and_then(Value::as_u64).unwrap_or(1) as usize)
.timeout(Duration::from_millis(
args.get("timeoutMs")
.and_then(Value::as_u64)
.unwrap_or(30_000),
))
.interval(Duration::from_millis(500));
let trace = self.query()?.wait_for_trace(id, opts).await?;
let count = trace.spans.len();
Ok(StepOutput::json(serde_json::to_value(&trace)?).with_count(count))
}
"listServices" => {
let query = self.query()?;
let wanted = vars.get("serviceName").map(str::to_owned);
let timeout = Duration::from_millis(
args.get("timeoutMs")
.and_then(Value::as_u64)
.unwrap_or(60_000),
);
let deadline = tokio::time::Instant::now() + timeout;
loop {
let services = query.list_services().await?;
let found = wanted
.as_deref()
.is_none_or(|w| services.iter().any(|s| s == w));
if found || tokio::time::Instant::now() >= deadline {
return Ok(StepOutput::json(serde_json::to_value(services)?));
}
tokio::time::sleep(LIST_SERVICES_INTERVAL).await;
}
}
other => Err(Error::unsupported(format!("unknown tracing op {other:?}"))),
}
}
async fn close(&mut self) {
let _ = self.target.handle.force_flush(None).await;
}
}