Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/23717_vector_test_color.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Disable ANSI color for `vector test` when running non-interactively. Honor `--color {auto|always|never}` and `VECTOR_COLOR`; VRL diagnostics no longer include ANSI sequences when color is disabled.

authors: VanjaRo
5 changes: 4 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ impl Application {
opts.root.internal_log_rate_limit,
);

// Set global color preference for downstream modules
crate::set_global_color(color);

// Can only log this after initializing the logging subsystem
if opts.root.openssl_no_probe {
debug!(
Expand Down Expand Up @@ -511,7 +514,7 @@ pub fn build_runtime(threads: Option<usize>, thread_name: &str) -> Result<Runtim
.unwrap_or_else(|_| panic!("double thread initialization"));
rt_builder.worker_threads(threads);

debug!(messaged = "Building runtime.", worker_threads = threads);
debug!(message = "Building runtime.", worker_threads = threads);
Ok(rt_builder.build().expect("Unable to create async runtime"))
}

Expand Down
14 changes: 12 additions & 2 deletions src/common/http/server_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,25 @@ impl HttpServerAuthConfig {
warnings,
config: _,
} = compile_vrl(source, &functions, &state, config).map_err(|diagnostics| {
Formatter::new(source, diagnostics).colored().to_string()
let fmt = Formatter::new(source, diagnostics);
if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
}
})?;

if !program.final_type_info().result.is_boolean() {
return Err("VRL conditions must return a boolean.".into());
}

if !warnings.is_empty() {
let warnings = Formatter::new(source, warnings).colored().to_string();
let fmt = Formatter::new(source, warnings);
let warnings = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
warn!(message = "VRL compilation warning.", %warnings);
}

Expand Down
38 changes: 26 additions & 12 deletions src/conditions/vrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,25 @@ impl ConditionalConfig for VrlConfig {
warnings,
config: _,
} = compile_vrl(&self.source, &functions, &state, config).map_err(|diagnostics| {
Formatter::new(&self.source, diagnostics)
.colored()
.to_string()
let fmt = Formatter::new(&self.source, diagnostics);
if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
}
})?;

if !program.final_type_info().result.is_boolean() {
return Err("VRL conditions must return a boolean.".into());
}

if !warnings.is_empty() {
let warnings = Formatter::new(&self.source, warnings).colored().to_string();
let fmt = Formatter::new(&self.source, warnings);
let warnings = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
warn!(message = "VRL compilation warning.", %warnings);
}

Expand Down Expand Up @@ -136,25 +144,31 @@ impl Conditional for Vrl {

let value_result = result.map_err(|err| match err {
Terminate::Abort(err) => {
let err = Formatter::new(
let fmt = Formatter::new(
&self.source,
vrl::diagnostic::Diagnostic::from(
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
),
)
.colored()
.to_string();
);
let err = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
format!("source execution aborted: {err}")
}
Terminate::Error(err) => {
let err = Formatter::new(
let fmt = Formatter::new(
&self.source,
vrl::diagnostic::Diagnostic::from(
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
),
)
.colored()
.to_string();
);
let err = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
format!("source execution failed: {err}")
}
});
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub use source_sender::SourceSender;
pub use vector_lib::{Error, Result, event, metrics, schema, shutdown, tcp, tls};

static APP_NAME_SLUG: std::sync::OnceLock<String> = std::sync::OnceLock::new();
static USE_COLOR: std::sync::OnceLock<bool> = std::sync::OnceLock::new();

/// The name used to identify this Vector application.
///
Expand All @@ -151,6 +152,18 @@ pub fn get_slugified_app_name() -> String {
.clone()
}

/// Sets the global color preference for diagnostics and CLI output.
/// This should be called once during application startup.
pub fn set_global_color(enabled: bool) {
let _ = USE_COLOR.set(enabled);
}

/// Returns true if color output is globally enabled.
/// Defaults to false if not set.
pub fn use_color() -> bool {
*USE_COLOR.get_or_init(|| false)
}

/// The current version of Vector in simplified format.
/// `<version-number>-nightly`.
pub fn vector_version() -> impl std::fmt::Display {
Expand Down
18 changes: 12 additions & 6 deletions src/sources/http_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,23 @@ impl Query {
match compile_vrl(param.value(), functions, &state, config) {
Ok(compilation_result) => {
if !compilation_result.warnings.is_empty() {
let warnings = Formatter::new(param.value(), compilation_result.warnings)
.colored()
.to_string();
let fmt = Formatter::new(param.value(), compilation_result.warnings);
let warnings = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
warn!(message = "VRL compilation warnings.", %warnings, internal_log_rate_limit = true);
}
Some(compilation_result.program)
}
Err(diagnostics) => {
let error = Formatter::new(param.value(), diagnostics)
.colored()
.to_string();
let fmt = Formatter::new(param.value(), diagnostics);
let error = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
warn!(message = "VRL compilation failed.", %error, internal_log_rate_limit = true);
None
}
Expand Down
17 changes: 15 additions & 2 deletions src/transforms/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,24 @@ impl RemapConfig {
config.set_custom(MeaningList::default());

let res = compile_vrl(&source, &functions, &state, config)
.map_err(|diagnostics| Formatter::new(&source, diagnostics).colored().to_string())
.map_err(|diagnostics| {
let fmt = Formatter::new(&source, diagnostics);
if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
}
})
.map(|result| {
let fmt = Formatter::new(&source, result.warnings);
let warnings = if crate::use_color() {
fmt.colored().to_string()
} else {
fmt.to_string()
};
(
result.program,
Formatter::new(&source, result.warnings).to_string(),
warnings,
result.config.get_custom::<MeaningList>().unwrap().clone(),
)
});
Expand Down
Loading