Skip to main content

synwire_core/observability/
span_kind.rs

1//! Observability span kind enumeration.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// The kind of observability span, corresponding to AI framework operations.
7///
8/// This enum is `#[non_exhaustive]` so that new span kinds can be added in
9/// future minor releases without breaking downstream code.
10///
11/// # Example
12///
13/// ```
14/// use synwire_core::observability::ObservabilitySpanKind;
15///
16/// let kind = ObservabilitySpanKind::Llm;
17/// assert_eq!(kind.as_str(), "llm");
18/// ```
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
20#[non_exhaustive]
21pub enum ObservabilitySpanKind {
22    /// A large language model invocation.
23    Llm,
24    /// A chain of operations.
25    Chain,
26    /// A tool invocation.
27    Tool,
28    /// An embedding operation.
29    Embedding,
30    /// A retriever query.
31    Retriever,
32    /// A graph execution step.
33    Graph,
34}
35
36impl ObservabilitySpanKind {
37    /// Returns the string representation of this span kind.
38    pub const fn as_str(&self) -> &'static str {
39        match self {
40            Self::Llm => "llm",
41            Self::Chain => "chain",
42            Self::Tool => "tool",
43            Self::Embedding => "embedding",
44            Self::Retriever => "retriever",
45            Self::Graph => "graph",
46        }
47    }
48}
49
50impl fmt::Display for ObservabilitySpanKind {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        f.write_str(self.as_str())
53    }
54}
55
56#[cfg(test)]
57#[allow(clippy::unwrap_used)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn span_kind_display() {
63        assert_eq!(ObservabilitySpanKind::Llm.to_string(), "llm");
64        assert_eq!(ObservabilitySpanKind::Chain.to_string(), "chain");
65        assert_eq!(ObservabilitySpanKind::Tool.to_string(), "tool");
66        assert_eq!(ObservabilitySpanKind::Embedding.to_string(), "embedding");
67        assert_eq!(ObservabilitySpanKind::Retriever.to_string(), "retriever");
68        assert_eq!(ObservabilitySpanKind::Graph.to_string(), "graph");
69    }
70
71    #[test]
72    fn span_kind_serialization_roundtrip() {
73        let kind = ObservabilitySpanKind::Llm;
74        let json = serde_json::to_string(&kind).unwrap();
75        let deserialized: ObservabilitySpanKind = serde_json::from_str(&json).unwrap();
76        assert_eq!(kind, deserialized);
77    }
78}