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    byte_size_of::ByteSizeOf, internal_event::TaggedEventsSent, json_size::JsonSize,
8    request_metadata::GetEventCountTags, EventDataEq,
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<LogEvent> for TraceEvent {
122    fn from(log: LogEvent) -> Self {
123        Self(log)
124    }
125}
126
127impl From<ObjectMap> for TraceEvent {
128    fn from(map: ObjectMap) -> Self {
129        Self(map.into())
130    }
131}
132
133impl ByteSizeOf for TraceEvent {
134    fn allocated_bytes(&self) -> usize {
135        self.0.allocated_bytes()
136    }
137}
138
139impl EstimatedJsonEncodedSizeOf for TraceEvent {
140    fn estimated_json_encoded_size_of(&self) -> JsonSize {
141        self.0.estimated_json_encoded_size_of()
142    }
143}
144
145impl EventCount for TraceEvent {
146    fn event_count(&self) -> usize {
147        1
148    }
149}
150
151impl EventDataEq for TraceEvent {
152    fn event_data_eq(&self, other: &Self) -> bool {
153        self.0.event_data_eq(&other.0)
154    }
155}
156
157impl Finalizable for TraceEvent {
158    fn take_finalizers(&mut self) -> EventFinalizers {
159        self.0.take_finalizers()
160    }
161}
162
163impl AsRef<LogEvent> for TraceEvent {
164    fn as_ref(&self) -> &LogEvent {
165        &self.0
166    }
167}
168
169impl AsMut<LogEvent> for TraceEvent {
170    fn as_mut(&mut self) -> &mut LogEvent {
171        &mut self.0
172    }
173}
174
175impl GetEventCountTags for TraceEvent {
176    fn get_tags(&self) -> TaggedEventsSent {
177        self.0.get_tags()
178    }
179}