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