vector/api/schema/events/
trace.rs

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