Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/common/http/server_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ use vector_lib::{
use vrl::{
compiler::{CompilationResult, CompileConfig, Program, runtime::Runtime},
core::Value,
diagnostic::Formatter,
prelude::TypeState,
value::{KeyString, ObjectMap},
};

use crate::format_vrl_diagnostics;

use super::ErrorMessage;

/// Configuration of the authentication strategy for server mode sinks and sources.
Expand Down Expand Up @@ -159,16 +160,15 @@ impl HttpServerAuthConfig {
program,
warnings,
config: _,
} = compile_vrl(source, &functions, &state, config).map_err(|diagnostics| {
Formatter::new(source, diagnostics).colored().to_string()
})?;
} = compile_vrl(source, &functions, &state, config)
.map_err(|diagnostics| format_vrl_diagnostics(source, diagnostics))?;

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 warnings = format_vrl_diagnostics(source, warnings);
warn!(message = "VRL compilation warning.", %warnings);
}

Expand Down
23 changes: 8 additions & 15 deletions src/conditions/vrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use vrl::{
CompilationResult, CompileConfig, Program, TypeState, VrlRuntime,
runtime::{Runtime, RuntimeResult, Terminate},
},
diagnostic::Formatter,
value::Value,
};

use crate::{
conditions::{Condition, Conditional, ConditionalConfig},
config::LogNamespace,
event::{Event, TargetEvents, VrlTarget},
format_vrl_diagnostics,
internal_events::VrlConditionExecutionError,
};

Expand Down Expand Up @@ -64,18 +64,15 @@ impl ConditionalConfig for VrlConfig {
program,
warnings,
config: _,
} = compile_vrl(&self.source, &functions, &state, config).map_err(|diagnostics| {
Formatter::new(&self.source, diagnostics)
.colored()
.to_string()
})?;
} = compile_vrl(&self.source, &functions, &state, config)
.map_err(|diagnostics| format_vrl_diagnostics(&self.source, diagnostics))?;

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 warnings = format_vrl_diagnostics(&self.source, warnings);
warn!(message = "VRL compilation warning.", %warnings);
}

Expand Down Expand Up @@ -136,25 +133,21 @@ impl Conditional for Vrl {

let value_result = result.map_err(|err| match err {
Terminate::Abort(err) => {
let err = Formatter::new(
let err = format_vrl_diagnostics(
&self.source,
vrl::diagnostic::Diagnostic::from(
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
),
)
.colored()
.to_string();
);
format!("source execution aborted: {err}")
}
Terminate::Error(err) => {
let err = Formatter::new(
let err = format_vrl_diagnostics(
&self.source,
vrl::diagnostic::Diagnostic::from(
Box::new(err) as Box<dyn vrl::diagnostic::DiagnosticMessage>
),
)
.colored()
.to_string();
);
format!("source execution failed: {err}")
}
});
Expand Down
28 changes: 28 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,33 @@ 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) {
if let Err(e) = USE_COLOR.set(enabled) {
error!(message = "Failed to set global color.", %e);
}
}

/// 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)
}

/// Formats VRL diagnostics honoring the global color setting.
pub fn format_vrl_diagnostics(
source: &str,
diagnostics: impl Into<vrl::diagnostic::DiagnosticList>,
) -> String {
let formatter = vrl::diagnostic::Formatter::new(source, diagnostics);
if use_color() {
formatter.colored().to_string()
} else {
formatter.to_string()
}
}

/// The current version of Vector in simplified format.
/// `<version-number>-nightly`.
pub fn vector_version() -> impl std::fmt::Display {
Expand Down
11 changes: 4 additions & 7 deletions src/sources/http_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use vector_lib::{
};
use vrl::{
compiler::{CompileConfig, Function, Program, runtime::Runtime},
diagnostic::Formatter,
prelude::TypeState,
};

use crate::{
codecs::{Decoder, DecodingConfig},
config::{SourceConfig, SourceContext},
format_vrl_diagnostics,
http::{Auth, ParamType, ParameterValue, QueryParameterValue, QueryParameters},
serde::{default_decoding, default_framing_message_based},
sources,
Expand Down Expand Up @@ -248,17 +248,14 @@ 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 warnings =
format_vrl_diagnostics(param.value(), compilation_result.warnings);
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 error = format_vrl_diagnostics(param.value(), diagnostics);
warn!(message = "VRL compilation failed.", %error, internal_log_rate_limit = true);
None
}
Expand Down
7 changes: 4 additions & 3 deletions src/transforms/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use vrl::{
runtime::{Runtime, Terminate},
state::ExternalEnv,
},
diagnostic::{DiagnosticMessage, Formatter, Note},
diagnostic::{DiagnosticMessage, Note},
path,
path::ValuePath,
value::{Kind, Value},
Expand All @@ -37,6 +37,7 @@ use crate::{
TransformOutput, log_schema,
},
event::{Event, TargetEvents, VrlTarget},
format_vrl_diagnostics,
internal_events::{RemapMappingAbort, RemapMappingError},
schema,
transforms::{SyncTransform, Transform, TransformOutputsBuf},
Expand Down Expand Up @@ -228,11 +229,11 @@ 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| format_vrl_diagnostics(&source, diagnostics))
.map(|result| {
(
result.program,
Formatter::new(&source, result.warnings).to_string(),
format_vrl_diagnostics(&source, result.warnings),
result.config.get_custom::<MeaningList>().unwrap().clone(),
)
});
Expand Down
Loading