use async_graphql::Object;
use vector_lib::encode_logfmt;
use vector_lib::event;
use vector_lib::tap::topology::TapOutput;
use vrl::event_path;
use super::EventEncodingType;
#[derive(Debug, Clone)]
pub struct Trace {
output: TapOutput,
event: event::TraceEvent,
}
impl Trace {
pub const fn new(output: TapOutput, event: event::TraceEvent) -> Self {
Self { output, event }
}
}
#[Object]
impl Trace {
async fn component_id(&self) -> &str {
self.output.output_id.component.id()
}
async fn component_type(&self) -> &str {
self.output.component_type.as_ref()
}
async fn component_kind(&self) -> &str {
self.output.component_kind
}
async fn string(&self, encoding: EventEncodingType) -> String {
match encoding {
EventEncodingType::Json => serde_json::to_string(&self.event)
.expect("JSON serialization of log event failed. Please report."),
EventEncodingType::Yaml => serde_yaml::to_string(&self.event)
.expect("YAML serialization of log event failed. Please report."),
EventEncodingType::Logfmt => encode_logfmt::encode_map(self.event.as_map())
.expect("logfmt serialization of log event failed. Please report."),
}
}
async fn json(&self, field: String) -> Option<String> {
self.event.get(event_path!(field.as_str())).map(|field| {
serde_json::to_string(field)
.expect("JSON serialization of log event field failed. Please report.")
})
}
}