Skip to main content

vector_common/
lib.rs

1//! The Vector Core common library
2//!
3//! This library includes common functionality relied upon by vector-core
4//! and core-related crates (e.g. buffers).
5
6#![deny(warnings)]
7#![deny(clippy::all)]
8#![deny(clippy::pedantic)]
9#![deny(unreachable_pub)]
10#![deny(unused_allocation)]
11#![deny(unused_extern_crates)]
12#![deny(unused_assignments)]
13#![deny(unused_comparisons)]
14
15pub use vector_common_macros::NamedInternalEvent;
16
17#[cfg(feature = "btreemap")]
18pub use vrl::btreemap;
19
20#[cfg(feature = "byte_size_of")]
21pub mod byte_size_of;
22
23pub mod json_size;
24
25pub mod config;
26
27pub mod constants;
28
29#[cfg(feature = "conversion")]
30pub use vrl::compiler::TimeZone;
31
32#[cfg(feature = "encoding")]
33pub mod encode_logfmt {
34    pub use vrl::core::encode_logfmt::*;
35}
36
37pub mod conversion {
38    pub use vrl::compiler::conversion::*;
39}
40
41pub mod event_data_eq;
42pub use event_data_eq::EventDataEq;
43
44#[cfg(any(test, feature = "test"))]
45pub mod event_test_util;
46
47pub mod finalization;
48pub mod finalizer;
49pub use finalizer::EmptyStream;
50
51pub mod id;
52
53pub mod internal_event;
54
55pub mod request_metadata;
56
57pub mod shutdown;
58
59#[cfg(feature = "sensitive_string")]
60pub mod sensitive_string;
61
62pub mod atomic;
63pub mod compression;
64pub mod stats;
65pub mod trigger;
66
67#[macro_use]
68extern crate tracing;
69
70/// Typed wrapper around `metrics::counter!` that only accepts [`internal_event::CounterName`].
71#[macro_export]
72macro_rules! counter {
73    ($name:expr) => {{
74        let _name: $crate::internal_event::CounterName = $name;
75        #[allow(clippy::disallowed_macros)]
76        {
77            metrics::counter!(_name.as_str())
78        }
79    }};
80    ($name:expr, $($rest:tt)*) => {{
81        let _name: $crate::internal_event::CounterName = $name;
82        #[allow(clippy::disallowed_macros)]
83        {
84            metrics::counter!(_name.as_str(), $($rest)*)
85        }
86    }};
87}
88
89/// Typed wrapper around `metrics::histogram!` that only accepts [`internal_event::HistogramName`].
90#[macro_export]
91macro_rules! histogram {
92    ($name:expr) => {{
93        let _name: $crate::internal_event::HistogramName = $name;
94        #[allow(clippy::disallowed_macros)]
95        {
96            metrics::histogram!(_name.as_str())
97        }
98    }};
99    ($name:expr, $($rest:tt)*) => {{
100        let _name: $crate::internal_event::HistogramName = $name;
101        #[allow(clippy::disallowed_macros)]
102        {
103            metrics::histogram!(_name.as_str(), $($rest)*)
104        }
105    }};
106}
107
108/// Typed wrapper around `metrics::gauge!` that only accepts [`internal_event::GaugeName`].
109#[macro_export]
110macro_rules! gauge {
111    ($name:expr) => {{
112        let _name: $crate::internal_event::GaugeName = $name;
113        #[allow(clippy::disallowed_macros)]
114        {
115            metrics::gauge!(_name.as_str())
116        }
117    }};
118    ($name:expr, $($rest:tt)*) => {{
119        let _name: $crate::internal_event::GaugeName = $name;
120        #[allow(clippy::disallowed_macros)]
121        {
122            metrics::gauge!(_name.as_str(), $($rest)*)
123        }
124    }};
125}
126
127/// Vector's basic error type, dynamically dispatched and safe to send across
128/// threads.
129pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
130
131/// Vector's basic result type, defined in terms of [`Error`] and generic over
132/// `T`.
133pub type Result<T> = std::result::Result<T, Error>;
134
135/// Spawn a future on the current tokio runtime, propagating the current tracing span into the
136/// spawned task.  This ensures that any logs or internal metrics emitted by the task retain the
137/// component tags (`component_id`, `component_kind`, `component_type`) of the caller.
138///
139/// Prefer this over `tokio::spawn(future.in_current_span())` to keep call sites concise.
140#[track_caller]
141pub fn spawn_in_current_span<T>(
142    task: impl std::future::Future<Output = T> + Send + 'static,
143) -> tokio::task::JoinHandle<T>
144where
145    T: Send + 'static,
146{
147    use tracing::Instrument as _;
148    tokio::spawn(task.in_current_span())
149}