vector/api/schema/events/
log.rs1use std::borrow::Cow;
2
3use async_graphql::Object;
4use chrono::{DateTime, Utc};
5use vector_lib::{encode_logfmt, event, tap::topology::TapOutput};
6use vrl::event_path;
7
8use super::EventEncodingType;
9
10#[derive(Debug, Clone)]
11pub struct Log {
12 output: TapOutput,
13 event: event::LogEvent,
14}
15
16impl Log {
17 pub const fn new(output: TapOutput, event: event::LogEvent) -> Self {
18 Self { output, event }
19 }
20
21 pub fn get_message(&self) -> Option<Cow<'_, str>> {
22 Some(self.event.get(event_path!("message"))?.to_string_lossy())
23 }
24
25 pub fn get_timestamp(&self) -> Option<&DateTime<Utc>> {
26 self.event.get(event_path!("timestamp"))?.as_timestamp()
27 }
28}
29
30#[Object]
31impl Log {
33 async fn component_id(&self) -> &str {
35 self.output.output_id.component.id()
36 }
37
38 async fn component_type(&self) -> &str {
40 self.output.component_type.as_ref()
41 }
42
43 async fn component_kind(&self) -> &str {
45 self.output.component_kind
46 }
47
48 async fn message(&self) -> Option<String> {
50 self.get_message().map(Into::into)
51 }
52
53 async fn timestamp(&self) -> Option<&DateTime<Utc>> {
55 self.get_timestamp()
56 }
57
58 async fn string(&self, encoding: EventEncodingType) -> String {
60 match encoding {
61 EventEncodingType::Json => serde_json::to_string(&self.event)
62 .expect("JSON serialization of log event failed. Please report."),
63 EventEncodingType::Yaml => serde_yaml::to_string(&self.event)
64 .expect("YAML serialization of log event failed. Please report."),
65 EventEncodingType::Logfmt => encode_logfmt::encode_value(self.event.value())
66 .expect("logfmt serialization of log event failed. Please report."),
67 }
68 }
69
70 async fn json(&self, field: String) -> Option<String> {
72 self.event.get(event_path!(field.as_str())).map(|field| {
73 serde_json::to_string(field)
74 .expect("JSON serialization of trace event field failed. Please report.")
75 })
76 }
77}