use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use serde_json::Value;
use crate::core::{poll_until, Error, PollOptions, Result};
use crate::logging::{
AttrValue, Attributes, BatchOptions, DropPolicy, LogClient, LogEntry, LogInput, LogQueryClient,
LogRecord, SearchOptions, Severity,
};
use crate::testing::runner::{CategoryHandler, Scenario, ScenarioSession, StepOutput, Vars};
pub struct LoggingTarget {
pub client: Arc<dyn LogClient>,
pub query: Option<Arc<dyn LogQueryClient>>,
}
type Factory = Arc<
dyn Fn(BatchOptions) -> Pin<Box<dyn Future<Output = Result<LoggingTarget>> + Send>>
+ Send
+ Sync,
>;
#[derive(Clone)]
pub struct LoggingHandler {
factory: Factory,
}
impl std::fmt::Debug for LoggingHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LoggingHandler").finish_non_exhaustive()
}
}
impl LoggingHandler {
pub fn new<F, Fut>(factory: F) -> Self
where
F: Fn(BatchOptions) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<LoggingTarget>> + Send + 'static,
{
LoggingHandler {
factory: Arc::new(move |opts| Box::pin(factory(opts))),
}
}
}
pub fn batch_options_from_json(base: BatchOptions, json: Option<&Value>) -> Result<BatchOptions> {
let Some(obj) = json.and_then(Value::as_object) else {
return Ok(base);
};
let mut o = base;
let num = |v: &Value, k: &str| {
v.as_u64().ok_or_else(|| {
Error::config(format!("batchOptions.{k} must be a non-negative integer"))
})
};
for (k, v) in obj {
match k.as_str() {
"maxQueueSize" => o.max_queue_size = num(v, k)? as usize,
"maxBatchSize" => o.max_batch_size = num(v, k)? as usize,
"maxBatchBytes" => o.max_batch_bytes = num(v, k)? as usize,
"flushIntervalMs" => o.flush_interval = Duration::from_millis(num(v, k)?),
"exportTimeoutMs" => o.export_timeout = Duration::from_millis(num(v, k)?),
"retryBackoffMs" => o.retry_backoff = Duration::from_millis(num(v, k)?),
"maxExportRetries" => {
o.max_export_retries = u32::try_from(num(v, k)?)
.map_err(|_| Error::config("batchOptions.maxExportRetries is too large"))?
}
"gzip" => {
o.gzip = v
.as_bool()
.ok_or_else(|| Error::config("batchOptions.gzip must be a boolean"))?
}
"dropPolicy" => {
o.drop_policy = match v.as_str() {
Some("drop_newest") => DropPolicy::DropNewest,
Some("drop_oldest") => DropPolicy::DropOldest,
Some("block") => DropPolicy::Block,
_ => return Err(Error::config("batchOptions.dropPolicy is invalid")),
}
}
other => return Err(Error::config(format!("unknown batch option {other:?}"))),
}
}
Ok(o)
}
#[async_trait]
impl CategoryHandler for LoggingHandler {
fn category(&self) -> &str {
"logging"
}
async fn open(&self, scenario: &Scenario, _vars: &Vars) -> Result<Box<dyn ScenarioSession>> {
let opts =
batch_options_from_json(BatchOptions::default(), scenario.batch_options.as_ref())?;
let target = (self.factory)(opts).await?;
Ok(Box::new(LoggingSession { target }))
}
}
struct LoggingSession {
target: LoggingTarget,
}
fn str_arg<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
args.get(key).and_then(Value::as_str)
}
fn attributes(v: Option<&Value>, i: Option<usize>) -> Attributes {
let Some(Value::Object(map)) = v else {
return Attributes::new();
};
map.iter()
.map(|(k, v)| (k.clone(), index_value(v, i)))
.collect()
}
fn index_value(v: &AttrValue, i: Option<usize>) -> AttrValue {
match (v, i) {
(Value::String(s), Some(i)) => Value::String(s.replace("#{i}", &i.to_string())),
_ => v.clone(),
}
}
fn input(args: &Value, i: Option<usize>) -> Result<LogInput> {
let message = str_arg(args, "message")
.ok_or_else(|| Error::validation("logging op needs a \"message\""))?;
let message = match i {
Some(i) => message.replace("#{i}", &i.to_string()),
None => message.to_string(),
};
let mut input = LogInput::new(message).attributes(attributes(args.get("attributes"), i));
if let Some(s) = str_arg(args, "severity") {
input = input.severity(s.parse::<Severity>()?);
}
if let Some(t) = str_arg(args, "traceId") {
input.trace_id = Some(t.to_string());
}
if let Some(s) = str_arg(args, "spanId") {
input.span_id = Some(s.to_string());
}
Ok(input)
}
impl LoggingSession {
fn query(&self) -> Result<&Arc<dyn LogQueryClient>> {
self.target
.query
.as_ref()
.ok_or_else(|| Error::unsupported("logging target has no query client"))
}
async fn search_until(&self, args: &Value) -> Result<StepOutput> {
let query = self.query()?;
let text = str_arg(args, "query").unwrap_or_default().to_string();
let min = args.get("minCount").and_then(Value::as_u64).unwrap_or(1) as usize;
let timeout = args
.get("timeoutMs")
.and_then(Value::as_u64)
.unwrap_or(30_000);
let poll = PollOptions {
timeout: Duration::from_millis(timeout),
interval: Duration::from_millis(500),
};
let records = poll_until("logging.searchUntil", poll, || async {
let found = collect(query.as_ref(), &text, min).await?;
Ok((found.len() >= min).then_some(found))
})
.await?;
let value = serde_json::to_value(&records)?;
let display = format!("{records:?} {value}");
Ok(StepOutput::json(value).with_display(display))
}
}
async fn collect(query: &dyn LogQueryClient, text: &str, min: usize) -> Result<Vec<LogRecord>> {
let mut out = Vec::new();
let mut cursor: Option<String> = None;
loop {
let mut opts = SearchOptions::default().limit(min.clamp(100, 1000));
opts.cursor = cursor.take();
let page = query.search(text, opts).await?;
out.extend(page.records);
match page.next_cursor {
Some(c) if out.len() < min => cursor = Some(c),
_ => return Ok(out),
}
}
}
#[async_trait]
impl ScenarioSession for LoggingSession {
async fn run(&mut self, op: &str, args: &Value, _vars: &Vars) -> Result<StepOutput> {
let client = &self.target.client;
match op {
"send" => {
client.send(LogEntry::Input(input(args, None)?));
Ok(StepOutput::none())
}
"sendBatch" => {
let count = args.get("count").and_then(Value::as_u64).unwrap_or(1) as usize;
let entries = (0..count)
.map(|i| input(args, Some(i)).map(LogEntry::Input))
.collect::<Result<Vec<_>>>()?;
client.send_batch(entries);
Ok(StepOutput::none())
}
"flush" => {
let timeout = args
.get("timeoutMs")
.and_then(Value::as_u64)
.map(Duration::from_millis);
client.flush(timeout).await?;
Ok(StepOutput::none())
}
"stats" => Ok(StepOutput::json(serde_json::to_value(client.stats())?)),
"searchUntil" => self.search_until(args).await,
other => Err(Error::unsupported(format!("unknown logging op {other:?}"))),
}
}
async fn close(&mut self) {
let _ = self.target.client.close().await;
}
}