Skip to content

The platform for continuously improving AI agents.

3.4 KB
Raw Blame
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::usage::SpanUsage;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SpanRecord {
    pub span_id: String,
    pub trace_id: String,
    pub parent_span_id: Option<String>,
    pub name: String,
    pub kind: i32,
    pub start_time_ns: i64,
    pub end_time_ns: i64,
    pub duration_ns: i64,
    pub status_code: i32,
    pub status_message: String,
    pub service_name: String,
    pub resource_attrs: Value,
    pub scope_name: String,
    pub scope_version: String,
    pub attributes: Value,
    pub events: Value,
    pub links: Value,
    pub usage: SpanUsage,
    pub received_at_ms: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SpanEventRecord {
    pub trace_id: String,
    pub span_id: String,
    pub span_name: String,
    pub service_name: String,
    pub name: String,
    pub time_ns: i64,
    pub attributes: Value,
    pub received_at_ms: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetricPoint {
    pub metric_name: String,
    pub description: String,
    pub unit: String,
    pub metric_type: String,
    pub temporality: Option<String>,
    pub is_monotonic: Option<bool>,
    pub service_name: String,
    pub resource_attrs: Value,
    pub scope_name: String,
    pub scope_version: String,
    pub attributes: Value,
    pub start_time_ns: Option<i64>,
    pub time_ns: i64,
    pub int_value: Option<i64>,
    pub double_value: Option<f64>,
    pub count: Option<i64>,
    pub sum: Option<f64>,
    pub min: Option<f64>,
    pub max: Option<f64>,
    pub buckets: Option<Value>,
    pub received_at_ms: i64,
    pub trace_id: Option<String>,
    pub span_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LogEntry {
    pub time_ns: i64,
    pub observed_time_ns: i64,
    pub trace_id: Option<String>,
    pub span_id: Option<String>,
    pub severity_number: i32,
    pub severity_text: String,
    pub body: Value,
    pub service_name: String,
    pub resource_attrs: Value,
    pub scope_name: String,
    pub scope_version: String,
    pub attributes: Value,
    pub received_at_ms: i64,
}

/// `None` means no span in the set reported that value.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct UsageTotals {
    pub total_tokens: Option<i64>,
    pub total_cost: Option<f64>,
    pub cache_read_tokens: Option<i64>,
    pub model: Option<String>,
}

impl UsageTotals {
    /// `spans` must be ordered by start time so `model` is the earliest span's.
    pub fn sum<'a>(spans: impl IntoIterator<Item = &'a SpanRecord>) -> Self {
        let mut totals = Self::default();
        for span in spans {
            let u = &span.usage;
            if let Some(tokens) = u.total_tokens {
                totals.total_tokens = Some(totals.total_tokens.unwrap_or(0) + tokens);
            }
            if let Some(cost) = u.cost {
                totals.total_cost = Some(totals.total_cost.unwrap_or(0.0) + cost);
            }
            if let Some(cache) = u.cache_read_tokens {
                totals.cache_read_tokens = Some(totals.cache_read_tokens.unwrap_or(0) + cache);
            }
            if totals.model.is_none() {
                totals.model = u.model.clone();
            }
        }
        totals.total_cost = totals.total_cost.map(|c| (c * 1e6).round() / 1e6);
        totals
    }
}