vector/api/schema/events/
trace.rs

1use async_graphql::Object;
2use vector_lib::{encode_logfmt, event, tap::topology::TapOutput};
3use vrl::event_path;
4
5use super::EventEncodingType;
6
7#[derive(Debug, Clone)]
8pub struct Trace {
9    output: TapOutput,
10    event: event::TraceEvent,
11}
12
13impl Trace {
14    pub const fn new(output: TapOutput, event: event::TraceEvent) -> Self {
15        Self { output, event }
16    }
17}
18
19#[Object]
20/// Trace event with fields for querying trace data
21impl Trace {
22    /// Id of the component associated with the trace event
23    async fn component_id(&self) -> &str {
24        self.output.output_id.component.id()
25    }
26
27    /// Type of component associated with the trace event
28    async fn component_type(&self) -> &str {
29        self.output.component_type.as_ref()
30    }
31
32    /// Kind of component associated with the trace event
33    async fn component_kind(&self) -> &str {
34        self.output.component_kind
35    }
36
37    /// Trace event as an encoded string format
38    async fn string(&self, encoding: EventEncodingType) -> String {
39        match encoding {
40            EventEncodingType::Json => serde_json::to_string(&self.event)
41                .expect("JSON serialization of log event failed. Please report."),
42            EventEncodingType::Yaml => serde_yaml::to_string(&self.event)
43                .expect("YAML serialization of log event failed. Please report."),
44            EventEncodingType::Logfmt => encode_logfmt::encode_map(self.event.as_map())
45                .expect("logfmt serialization of log event failed. Please report."),
46        }
47    }
48
49    /// Get JSON field data on the trace event, by field name
50    async fn json(&self, field: String) -> Option<String> {
51        self.event.get(event_path!(field.as_str())).map(|field| {
52            serde_json::to_string(field)
53                .expect("JSON serialization of log event field failed. Please report.")
54        })
55    }
56}