vector/api/schema/events/
log.rsuse std::borrow::Cow;
use async_graphql::Object;
use chrono::{DateTime, Utc};
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 Log {
output: TapOutput,
event: event::LogEvent,
}
impl Log {
pub const fn new(output: TapOutput, event: event::LogEvent) -> Self {
Self { output, event }
}
pub fn get_message(&self) -> Option<Cow<'_, str>> {
Some(self.event.get(event_path!("message"))?.to_string_lossy())
}
pub fn get_timestamp(&self) -> Option<&DateTime<Utc>> {
self.event.get(event_path!("timestamp"))?.as_timestamp()
}
}
#[Object]
impl Log {
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 message(&self) -> Option<String> {
self.get_message().map(Into::into)
}
async fn timestamp(&self) -> Option<&DateTime<Utc>> {
self.get_timestamp()
}
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_value(self.event.value())
.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 trace event field failed. Please report.")
})
}
}