Skip to main content

vector/
lib.rs

1#![recursion_limit = "256"] // for async-stream
2#![deny(unreachable_pub)]
3#![deny(unused_extern_crates)]
4#![deny(unused_allocation)]
5#![deny(unused_assignments)]
6#![deny(unused_comparisons)]
7#![deny(warnings)]
8#![deny(missing_docs)]
9#![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::broken_intra_doc_links))]
10#![allow(async_fn_in_trait)]
11#![allow(clippy::approx_constant)]
12#![allow(clippy::float_cmp)]
13#![allow(clippy::match_wild_err_arm)]
14#![allow(clippy::new_ret_no_self)]
15#![allow(clippy::type_complexity)]
16#![allow(clippy::unit_arg)]
17#![deny(clippy::clone_on_ref_ptr)]
18#![deny(clippy::trivially_copy_pass_by_ref)]
19#![deny(clippy::disallowed_methods)] // [nursery] mark some functions as verboten
20#![deny(clippy::missing_const_for_fn)] // [nursery] valuable to the optimizer, but may produce false positives
21
22//! The main library to support building Vector.
23
24#[cfg(all(unix, feature = "sinks-socket"))]
25#[macro_use]
26extern crate cfg_if;
27#[macro_use]
28extern crate derivative;
29#[macro_use]
30extern crate tracing;
31#[macro_use]
32extern crate vector_lib;
33
34#[cfg(all(
35    target_os = "linux",
36    any(
37        feature = "antithesis-scenario-memory",
38        feature = "antithesis-scenario-disk"
39    )
40))]
41extern crate antithesis_instrumentation as _;
42
43pub use indoc::indoc;
44// re-export codecs for convenience
45pub use vector_lib::codecs;
46
47#[cfg(all(unix, feature = "tikv-jemallocator"))]
48#[global_allocator]
49static ALLOC: self::internal_telemetry::allocations::Allocator<tikv_jemallocator::Jemalloc> =
50    self::internal_telemetry::allocations::get_grouped_tracing_allocator(
51        tikv_jemallocator::Jemalloc,
52    );
53
54#[allow(unreachable_pub)]
55pub mod internal_telemetry;
56
57#[macro_use]
58#[allow(unreachable_pub)]
59pub mod config;
60pub mod cli;
61#[allow(unreachable_pub)]
62pub mod components;
63pub mod conditions;
64pub mod dns;
65#[cfg(feature = "docker")]
66pub mod docker;
67pub mod expiring_hash_map;
68pub mod generate;
69pub mod generate_schema;
70#[macro_use]
71#[allow(unreachable_pub)]
72pub mod internal_events;
73#[cfg(feature = "lapin")]
74pub mod amqp;
75#[cfg(feature = "api")]
76#[allow(unreachable_pub)]
77pub mod api;
78pub mod app;
79pub mod async_read;
80#[cfg(feature = "aws-config")]
81pub mod aws;
82pub mod common;
83pub mod completion;
84mod convert_config;
85pub mod cpu_time;
86pub mod encoding_transcode;
87pub mod enrichment_tables;
88pub mod extra_context;
89#[cfg(feature = "gcp")]
90pub mod gcp;
91pub(crate) mod graph;
92pub mod heartbeat;
93pub mod http;
94#[allow(unreachable_pub)]
95#[cfg(any(feature = "sources-kafka", feature = "sinks-kafka"))]
96pub mod kafka;
97#[allow(unreachable_pub)]
98pub mod kubernetes;
99pub mod line_agg;
100pub mod list;
101#[cfg(any(feature = "sources-nats", feature = "sinks-nats"))]
102pub mod nats;
103pub mod net;
104#[allow(unreachable_pub)]
105pub(crate) mod proto;
106pub mod providers;
107pub mod secrets;
108pub mod serde;
109#[cfg(windows)]
110pub mod service;
111pub mod signal;
112pub(crate) mod sink_ext;
113#[allow(unreachable_pub)]
114pub mod sinks;
115#[allow(unreachable_pub)]
116pub mod sources;
117#[cfg(feature = "api-client")]
118#[allow(unreachable_pub)]
119pub mod tap;
120pub mod template;
121pub mod test_util;
122#[cfg(feature = "top")]
123pub mod top;
124#[allow(unreachable_pub)]
125pub mod topology;
126pub mod trace;
127#[allow(unreachable_pub)]
128pub mod transforms;
129pub mod types;
130pub mod unit_test;
131pub(crate) mod utilization;
132pub mod validate;
133#[cfg(windows)]
134pub mod vector_windows;
135
136pub use vector_lib::{
137    Error, Result, event, metrics, schema, shutdown, source_sender::SourceSender, tcp, tls,
138};
139
140static APP_NAME_SLUG: std::sync::OnceLock<String> = std::sync::OnceLock::new();
141static USE_COLOR: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
142
143/// The name used to identify this Vector application.
144///
145/// This can be set at compile-time through the VECTOR_APP_NAME env variable.
146/// Defaults to "Vector".
147pub fn get_app_name() -> &'static str {
148    option_env!("VECTOR_APP_NAME").unwrap_or("Vector")
149}
150
151/// Returns a slugified version of the name used to identify this Vector application.
152///
153/// Defaults to "vector".
154pub fn get_slugified_app_name() -> String {
155    APP_NAME_SLUG
156        .get_or_init(|| get_app_name().to_lowercase().replace(' ', "-"))
157        .clone()
158}
159
160/// Sets the global color preference for diagnostics and CLI output.
161/// This should be called once during application startup.
162pub fn set_global_color(enabled: bool) {
163    if let Err(e) = USE_COLOR.set(enabled) {
164        error!(message = "Failed to set global color.", %e);
165    }
166}
167
168/// Returns true if color output is globally enabled.
169/// Defaults to false if not set.
170pub fn use_color() -> bool {
171    *USE_COLOR.get_or_init(|| false)
172}
173
174/// Formats VRL diagnostics honoring the global color setting.
175pub fn format_vrl_diagnostics(
176    source: &str,
177    diagnostics: impl Into<vrl::diagnostic::DiagnosticList>,
178) -> String {
179    let formatter = vrl::diagnostic::Formatter::new(source, diagnostics);
180    if use_color() {
181        formatter.colored().to_string()
182    } else {
183        formatter.to_string()
184    }
185}
186
187/// The current version of Vector in simplified format.
188/// `<version-number>-nightly`.
189pub fn vector_version() -> impl std::fmt::Display {
190    #[cfg(feature = "nightly")]
191    let pkg_version = format!("{}-nightly", built_info::PKG_VERSION);
192
193    #[cfg(not(feature = "nightly"))]
194    let pkg_version = match built_info::DEBUG {
195        // If any debug info is included, consider it a non-release build.
196        "1" | "2" | "true" => {
197            format!(
198                "{}-custom-{}",
199                built_info::PKG_VERSION,
200                built_info::GIT_SHORT_HASH
201            )
202        }
203        _ => built_info::PKG_VERSION.to_string(),
204    };
205
206    pkg_version
207}
208
209/// Returns a string containing full version information of the current build.
210pub fn get_version() -> String {
211    let pkg_version = vector_version();
212    let build_desc = built_info::VECTOR_BUILD_DESC;
213    let build_string = match build_desc {
214        Some(desc) => format!("{} {}", built_info::TARGET, desc),
215        None => built_info::TARGET.into(),
216    };
217
218    // We do not add 'debug' to the BUILD_DESC unless the caller has flagged on line
219    // or full debug symbols. See the Cargo Book profiling section for value meaning:
220    // https://doc.rust-lang.org/cargo/reference/profiles.html#debug
221    let build_string = match built_info::DEBUG {
222        "1" => format!("{build_string} debug=line"),
223        "2" | "true" => format!("{build_string} debug=full"),
224        _ => build_string,
225    };
226
227    format!("{pkg_version} ({build_string})")
228}
229
230/// Includes information about the current build.
231#[allow(warnings)]
232pub mod built_info {
233    include!(concat!(env!("OUT_DIR"), "/built.rs"));
234}
235
236/// Returns the host name of the current system.
237/// The hostname can be overridden by setting the VECTOR_HOSTNAME environment variable.
238pub fn get_hostname() -> std::io::Result<String> {
239    Ok(if let Ok(hostname) = std::env::var("VECTOR_HOSTNAME") {
240        hostname.to_string()
241    } else {
242        hostname::get()?.to_string_lossy().into_owned()
243    })
244}
245
246pub(crate) use vector_lib::spawn_in_current_span;
247
248/// Spawn a task with the given name. The name is only used if
249/// built with [`tokio_unstable`][tokio_unstable].
250///
251/// [tokio_unstable]: https://docs.rs/tokio/latest/tokio/#unstable-features
252#[track_caller]
253pub(crate) fn spawn_named<T>(
254    task: impl std::future::Future<Output = T> + Send + 'static,
255    _name: &str,
256) -> tokio::task::JoinHandle<T>
257where
258    T: Send + 'static,
259{
260    #[cfg(tokio_unstable)]
261    return tokio::task::Builder::new()
262        .name(_name)
263        .spawn(task)
264        .expect("tokio task should spawn");
265
266    #[cfg(not(tokio_unstable))]
267    tokio::spawn(task)
268}
269
270/// Returns an estimate of the number of recommended threads that Vector should spawn.
271pub fn num_threads() -> usize {
272    let count = match std::thread::available_parallelism() {
273        Ok(count) => count,
274        Err(error) => {
275            warn!(message = "Failed to determine available parallelism for thread count, defaulting to 1.", %error);
276            std::num::NonZeroUsize::new(1).unwrap()
277        }
278    };
279    usize::from(count)
280}