vector_core/event/
trace.rs

1use std::fmt::Debug;
2
3use lookup::lookup_v2::TargetPath;
4use serde::{Deserialize, Serialize};
5use vector_buffers::EventCount;
6use vector_common::{
7    EventDataEq, byte_size_of::ByteSizeOf, internal_event::TaggedEventsSent, json_size::JsonSize,
8    request_metadata::GetEventCountTags,
9};
10use vrl::path::PathParseError;
11
12use super::{
13    BatchNotifier, EstimatedJsonEncodedSizeOf, EventFinalizer, EventFinalizers, EventMetadata,
14    Finalizable, LogEvent, ObjectMap, Value,
15};
16
17/// Traces are a newtype of `LogEvent`
18#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
19pub struct TraceEvent(LogEvent);
20
21impl TraceEvent {
22    /// Convert a `TraceEvent` into a tuple of its components
23    /// # Panics
24    ///
25    /// Panics if the fields of the `TraceEvent` are not a `Value::Map`.
26    pub fn into_parts(self) -> (ObjectMap, EventMetadata) {
27        let (value, metadata) = self.0.into_parts();
28        let map = value.into_object().expect("inner value must be a map");
29        (map, metadata)
30    }
31
32    pub fn from_parts(fields: ObjectMap, metadata: EventMetadata) -> Self {
33        Self(LogEvent::from_map(fields, metadata))
34    }
35
36    pub fn value(&self) -> &Value {
37        self.0.value()
38    }
39
40    pub fn value_mut(&mut self) -> &mut Value {
41        self.0.value_mut()
42    }
43
44    pub fn metadata(&self) -> &EventMetadata {
45        self.0.metadata()
46    }
47
48    pub fn metadata_mut(&mut self) -> &mut EventMetadata {
49        self.0.metadata_mut()
50    }
51
52    pub fn add_finalizer(&mut self, finalizer: EventFinalizer) {
53        self.0.add_finalizer(finalizer);
54    }
55
56    #[must_use]
57    pub fn with_batch_notifier(self, batch: &BatchNotifier) -> Self {
58        Self(self.0.with_batch_notifier(batch))
59    }
60
61    #[must_use]
62    pub fn with_batch_notifier_option(self, batch: &Option<BatchNotifier>) -> Self {
63        Self(self.0.with_batch_notifier_option(batch))
64    }
65
66    /// Convert a `TraceEvent` into an `ObjectMap` of it's fields
67    /// # Panics
68    ///
69    /// Panics if the fields of the `TraceEvent` are not a `Value::Map`.
70    pub fn as_map(&self) -> &ObjectMap {
71        self.0.as_map().expect("inner value must be a map")
72    }
73
74    /// Parse the specified `path` and if there are no parsing errors, attempt to get a reference to a value.
75    /// # Errors
76    /// Will return an error if path parsing failed.
77    pub fn parse_path_and_get_value(
78        &self,
79        path: impl AsRef<str>,
80    ) -> Result<Option<&Value>, PathParseError> {
81        self.0.parse_path_and_get_value(path)
82    }
83
84    #[allow(clippy::needless_pass_by_value)] // TargetPath is always a reference
85    pub fn get<'a>(&self, key: impl TargetPath<'a>) -> Option<&Value> {
86        self.0.get(key)
87    }
88
89    pub fn get_mut<'a>(&mut self, key: impl TargetPath<'a>) -> Option<&mut Value> {
90        self.0.get_mut(key)
91    }
92
93    pub fn contains<'a>(&self, key: impl TargetPath<'a>) -> bool {
94        self.0.contains(key)
95    }
96
97    pub fn insert<'a>(
98        &mut self,
99        key: impl TargetPath<'a>,
100        value: impl Into<Value> + Debug,
101    ) -> Option<Value> {
102        self.0.insert(key, value.into())
103    }
104
105    pub fn maybe_insert<'a, F: FnOnce() -> Value>(
106        &mut self,
107        path: Option<impl TargetPath<'a>>,
108        value_callback: F,
109    ) -> Option<Value> {
110        if let Some(path) = path {
111            return self.0.insert(path, value_callback());
112        }
113        None
114    }
115
116    pub fn remove<'a>(&mut self, key: impl TargetPath<'a>) -> Option<Value> {
117        self.0.remove(key)
118    }
119}
120
121impl From<Value> for TraceEvent {
122    fn from(value: Value) -> Self {
123        let log_event = LogEvent::from(value);
124        Self(log_event)
125    }
126}
127
128impl From<LogEvent> for TraceEvent {
129    fn from(log: LogEvent) -> Self {
130        Self(log)
131    }
132}
133
134impl From<ObjectMap> for TraceEvent {
135    fn from(map: ObjectMap) -> Self {
136        Self(map.into())
137    }
138}
139
140impl ByteSizeOf for TraceEvent {
141    fn allocated_bytes(&self) -> usize {
142        self.0.allocated_bytes()
143    }
144}
145
146impl EstimatedJsonEncodedSizeOf for TraceEvent {
147    fn estimated_json_encoded_size_of(&self) -> JsonSize {
148        self.0.estimated_json_encoded_size_of()
149    }
150}
151
152impl EventCount for TraceEvent {
153    fn event_count(&self) -> usize {
154        1
155    }
156}
157
158impl EventDataEq for TraceEvent {
159    fn event_data_eq(&self, other: &Self) -> bool {
160        self.0.event_data_eq(&other.0)
161    }
162}
163
164impl Finalizable for TraceEvent {
165    fn take_finalizers(&mut self) -> EventFinalizers {
166        self.0.take_finalizers()
167    }
168}
169
170impl AsRef<LogEvent> for TraceEvent {
171    fn as_ref(&self) -> &LogEvent {
172        &self.0
173    }
174}
175
176impl AsMut<LogEvent> for TraceEvent {
177    fn as_mut(&mut self) -> &mut LogEvent {
178        &mut self.0
179    }
180}
181
182impl GetEventCountTags for TraceEvent {
183    fn get_tags(&self) -> TaggedEventsSent {
184        self.0.get_tags()
185    }
186}