Skip to content

Commit f9037d6

Browse files
committed
chore: remove transform type coercion
Signed-off-by: Luke Steensen <[email protected]>
1 parent 0518176 commit f9037d6

File tree

7 files changed

+32
-70
lines changed

7 files changed

+32
-70
lines changed

lib/vector-core/src/transform/mod.rs

-42
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,6 @@ impl Transform {
4040
Transform::Function(Box::new(v))
4141
}
4242

43-
/// Mutably borrow the inner transform as a function transform.
44-
///
45-
/// # Panics
46-
///
47-
/// If the transform is not a [`FunctionTransform`] this will panic.
48-
pub fn as_function(&mut self) -> &mut Box<dyn FunctionTransform> {
49-
match self {
50-
Transform::Function(t) => t,
51-
_ => panic!(
52-
"Called `Transform::as_function` on something that was not a function variant."
53-
),
54-
}
55-
}
56-
57-
/// Transmute the inner transform into a function transform.
58-
///
59-
/// # Panics
60-
///
61-
/// If the transform is not a [`FunctionTransform`] this will panic.
62-
pub fn into_function(self) -> Box<dyn FunctionTransform> {
63-
match self {
64-
Transform::Function(t) => t,
65-
_ => panic!(
66-
"Called `Transform::into_function` on something that was not a function variant."
67-
),
68-
}
69-
}
70-
7143
/// Create a new synchronous transform.
7244
///
7345
/// This is a broader trait than the simple [`FunctionTransform`] in that it allows transforms
@@ -104,20 +76,6 @@ impl Transform {
10476
Transform::Task(Box::new(WrapEventTask(v)))
10577
}
10678

107-
/// Mutably borrow the inner transform as a task transform.
108-
///
109-
/// # Panics
110-
///
111-
/// If the transform is a [`FunctionTransform`] this will panic.
112-
pub fn as_task(&mut self) -> &mut Box<dyn TaskTransform<EventArray>> {
113-
match self {
114-
Transform::Task(t) => t,
115-
_ => {
116-
panic!("Called `Transform::as_task` on something that was not a task variant.")
117-
}
118-
}
119-
}
120-
12179
/// Transmute the inner transform into a task transform.
12280
///
12381
/// # Panics

src/sinks/humio/metrics.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ use indoc::indoc;
66
use lookup::lookup_v2::OptionalValuePath;
77
use vector_common::sensitive_string::SensitiveString;
88
use vector_config::configurable_component;
9-
use vector_core::{sink::StreamSink, transform::Transform};
9+
use vector_core::sink::StreamSink;
1010

1111
use super::{
1212
host_key,
1313
logs::{HumioLogsConfig, HOST},
1414
};
1515
use crate::{
1616
config::{
17-
AcknowledgementsConfig, GenerateConfig, Input, SinkConfig, SinkContext, TransformConfig,
18-
TransformContext,
17+
AcknowledgementsConfig, GenerateConfig, Input, SinkConfig, SinkContext, TransformContext,
1918
},
2019
event::{Event, EventArray, EventContainer},
2120
sinks::{
@@ -25,7 +24,10 @@ use crate::{
2524
},
2625
template::Template,
2726
tls::TlsConfig,
28-
transforms::{metric_to_log::MetricToLogConfig, OutputBuffer},
27+
transforms::{
28+
metric_to_log::{MetricToLog, MetricToLogConfig},
29+
FunctionTransform, OutputBuffer,
30+
},
2931
};
3032

3133
/// Configuration for the `humio_metrics` sink.
@@ -153,9 +155,7 @@ impl SinkConfig for HumioMetricsConfig {
153155
async fn build(&self, cx: SinkContext) -> crate::Result<(VectorSink, Healthcheck)> {
154156
let transform = self
155157
.transform
156-
.clone()
157-
.build(&TransformContext::new_with_globals(cx.globals.clone()))
158-
.await?;
158+
.build(&TransformContext::new_with_globals(cx.globals.clone()));
159159

160160
let sink = HumioLogsConfig {
161161
token: self.token.clone(),
@@ -199,7 +199,7 @@ impl SinkConfig for HumioMetricsConfig {
199199

200200
pub struct HumioMetricsSink {
201201
inner: VectorSink,
202-
transform: Transform,
202+
transform: MetricToLog,
203203
}
204204

205205
#[async_trait]
@@ -210,7 +210,7 @@ impl StreamSink<EventArray> for HumioMetricsSink {
210210
.run(input.map(move |events| {
211211
let mut buf = OutputBuffer::with_capacity(events.len());
212212
for event in events.into_events() {
213-
transform.as_function().transform(&mut buf, event);
213+
transform.transform(&mut buf, event);
214214
}
215215
// Awkward but necessary for the `EventArray` type
216216
let events = buf.into_events().map(Event::into_log).collect::<Vec<_>>();

src/sources/kubernetes_logs/parser/cri.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ pub mod tests {
286286
fn test_parsing_valid_vector_namespace() {
287287
trace_init();
288288
test_util::test_parser(
289-
|| Transform::function(Cri::new(LogNamespace::Vector)),
289+
|| Cri::new(LogNamespace::Vector),
290290
|bytes| Event::Log(LogEvent::from(value!(bytes))),
291291
valid_cases(LogNamespace::Vector),
292292
);
@@ -296,7 +296,7 @@ pub mod tests {
296296
fn test_parsing_valid_legacy_namespace() {
297297
trace_init();
298298
test_util::test_parser(
299-
|| Transform::function(Cri::new(LogNamespace::Legacy)),
299+
|| Cri::new(LogNamespace::Legacy),
300300
|bytes| Event::Log(LogEvent::from(bytes)),
301301
valid_cases(LogNamespace::Legacy),
302302
);

src/sources/kubernetes_logs/parser/docker.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ pub mod tests {
316316

317317
test_util::test_parser(
318318
|| {
319-
Transform::function(Docker {
319+
Docker {
320320
log_namespace: LogNamespace::Vector,
321-
})
321+
}
322322
},
323323
|bytes| Event::Log(LogEvent::from(value!(bytes))),
324324
valid_cases(LogNamespace::Vector),
@@ -330,10 +330,8 @@ pub mod tests {
330330
trace_init();
331331

332332
test_util::test_parser(
333-
|| {
334-
Transform::function(Docker {
335-
log_namespace: LogNamespace::Legacy,
336-
})
333+
|| Docker {
334+
log_namespace: LogNamespace::Legacy,
337335
},
338336
|bytes| Event::Log(LogEvent::from(bytes)),
339337
valid_cases(LogNamespace::Legacy),

src/sources/kubernetes_logs/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ mod tests {
117117
fn test_parsing_valid_legacy_namespace() {
118118
trace_init();
119119
test_util::test_parser(
120-
|| Transform::function(Parser::new(LogNamespace::Legacy)),
120+
|| Parser::new(LogNamespace::Legacy),
121121
|bytes| Event::Log(LogEvent::from(bytes)),
122122
valid_cases(LogNamespace::Legacy),
123123
);

src/sources/kubernetes_logs/parser/test_util.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use vrl::value::{value, Value};
99
use crate::{
1010
event::{Event, LogEvent},
1111
sources::kubernetes_logs::Config,
12-
transforms::{OutputBuffer, Transform},
12+
transforms::{OutputBuffer, FunctionTransform},
1313
};
1414

1515
/// Build a log event for test purposes.
@@ -58,15 +58,15 @@ pub fn make_log_event(
5858
/// Shared logic for testing parsers.
5959
///
6060
/// Takes a parser builder and a list of test cases.
61-
pub fn test_parser<B, L, S>(builder: B, loader: L, cases: Vec<(S, Vec<Event>)>)
61+
pub fn test_parser<B, L, S, F>(builder: B, loader: L, cases: Vec<(S, Vec<Event>)>)
6262
where
63-
B: Fn() -> Transform,
63+
B: Fn() -> F,
64+
F: FunctionTransform,
6465
L: Fn(S) -> Event,
6566
{
6667
for (message, expected) in cases {
6768
let input = loader(message);
6869
let mut parser = (builder)();
69-
let parser = parser.as_function();
7070
let mut output = OutputBuffer::default();
7171
parser.transform(&mut output, input);
7272

src/transforms/metric_to_log.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ pub struct MetricToLogConfig {
6363
pub metric_tag_values: MetricTagValues,
6464
}
6565

66+
impl MetricToLogConfig {
67+
pub fn build(&self, context: &TransformContext) -> MetricToLog {
68+
MetricToLog::new(
69+
self.host_tag.as_deref(),
70+
self.timezone.unwrap_or_else(|| context.globals.timezone()),
71+
context.log_namespace(self.log_namespace),
72+
self.metric_tag_values,
73+
)
74+
}
75+
}
76+
6677
impl GenerateConfig for MetricToLogConfig {
6778
fn generate_config() -> toml::Value {
6879
toml::Value::try_from(Self {
@@ -79,12 +90,7 @@ impl GenerateConfig for MetricToLogConfig {
7990
#[typetag::serde(name = "metric_to_log")]
8091
impl TransformConfig for MetricToLogConfig {
8192
async fn build(&self, context: &TransformContext) -> crate::Result<Transform> {
82-
Ok(Transform::function(MetricToLog::new(
83-
self.host_tag.as_deref(),
84-
self.timezone.unwrap_or_else(|| context.globals.timezone()),
85-
context.log_namespace(self.log_namespace),
86-
self.metric_tag_values,
87-
)))
93+
Ok(Transform::function(self.build(context)))
8894
}
8995

9096
fn input(&self) -> Input {

0 commit comments

Comments
 (0)