-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(new transform): Add new incremental_to_absolute transform and change to MetricSet to an LRU cache with optional capacity policies #23374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pront
merged 47 commits into
vectordotdev:master
from
GreyLilac09:greylilac09/add-incremental-to-absolute
Aug 27, 2025
Merged
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
5616c4e
add incremental_to_absolute
GreyLilac09 9c6e6dc
fix docs
GreyLilac09 239f7f3
fix tpos
GreyLilac09 284d9db
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 0d6c9f1
use sync
GreyLilac09 9d55415
fix extra aggregate
GreyLilac09 8dedf1c
remove interval_ms
GreyLilac09 0ee9cb1
address comments
GreyLilac09 3cc9d8b
Merge branch 'master' into greylilac09/add-incremental-to-absolute
thomasqueirozb b1a18aa
use lru cache
GreyLilac09 0761542
use max_bytes or max_events
GreyLilac09 7c6fa3e
add debug print
GreyLilac09 bbe41f4
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 5596b9c
Merge branch 'greylilac09/add-incremental-to-absolute' of github.com:…
GreyLilac09 07db954
remove debug print
GreyLilac09 a1ce197
use original cache impl
GreyLilac09 51d959a
restore original formatting
GreyLilac09 00c2388
use lrucache
GreyLilac09 c8f73d1
make some optimizations and fix clippy
GreyLilac09 f832a4e
fix incr_to_absolute and absolute_to_incr
GreyLilac09 7c692d5
fix
GreyLilac09 72f86a0
add overhead
GreyLilac09 3cc49f1
simplify free_items
GreyLilac09 33680bd
Update lib/vector-core/src/event/metric/data.rs
GreyLilac09 9a94c3f
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 72df8b0
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 024ae54
Make whitespace consistent
thomasqueirozb 00b3714
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 dbf0297
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 7a9eea7
fix
GreyLilac09 a62402c
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 7ca83cb
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 1640d9d
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 9510c02
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 ff2ccd4
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 ca1d3a3
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 0e620a5
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 76595bc
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 8676451
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 8db2a10
fmt
GreyLilac09 f596006
add more examples
GreyLilac09 c8c743c
exclude cache overhead
GreyLilac09 0d9e9a2
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 59814cb
fix fmt
GreyLilac09 c1f7988
Merge remote-tracking branch 'origin/master' into greylilac09/add-inc…
pront 1c07bee
add .
pront 69ab4bc
Merge branch 'master' into greylilac09/add-incremental-to-absolute
GreyLilac09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Add a new `incremental_to_absolute` transform which converts incremental metrics to absolute metrics. This is useful for | ||
| use cases when sending metrics to a sink is lossy or you want to get a historical record of metrics, in which case | ||
| incremental metrics may be inaccurate since any gaps in metrics sent will result in an inaccurate reading of the ending | ||
| value. | ||
|
|
||
| authors: GreyLilac09 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| use std::{collections::HashMap, future::ready, pin::Pin, time::Duration}; | ||
|
|
||
| use futures::{Stream, StreamExt}; | ||
| use vector_lib::config::LogNamespace; | ||
| use vector_lib::configurable::configurable_component; | ||
|
|
||
| use crate::sinks::util::buffer::metrics::{MetricSet, TtlPolicy}; | ||
| use crate::{ | ||
| config::{DataType, Input, OutputId, TransformConfig, TransformContext, TransformOutput}, | ||
| event::Event, | ||
| schema, | ||
| transforms::{TaskTransform, Transform}, | ||
| }; | ||
|
|
||
| /// Configuration for the `incremental_to_absolute` transform. | ||
| #[configurable_component(transform( | ||
| "incremental_to_absolute", | ||
| "Convert incremental metrics to absolute." | ||
| ))] | ||
| #[derive(Clone, Debug, Default)] | ||
| #[serde(deny_unknown_fields)] | ||
| pub struct IncrementalToAbsoluteConfig { | ||
| /// The amount of time, in seconds, that incremental metrics will persist in the internal | ||
| /// metrics cache after having not been updated before they expire and are removed. | ||
| /// Once removed, incremental counters are reset to 0. | ||
| #[serde(skip_serializing_if = "crate::serde::is_default")] | ||
pront marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[configurable(metadata(docs::common = false, docs::required = true, docs::examples = "120"))] | ||
| pub expire_metrics_secs: u64, | ||
| } | ||
|
|
||
| impl_generate_config_from_default!(IncrementalToAbsoluteConfig); | ||
|
|
||
| #[async_trait::async_trait] | ||
| #[typetag::serde(name = "incremental_to_absolute")] | ||
| impl TransformConfig for IncrementalToAbsoluteConfig { | ||
| async fn build(&self, _context: &TransformContext) -> crate::Result<Transform> { | ||
| IncrementalToAbsolute::new(self).map(Transform::event_task) | ||
| } | ||
|
|
||
| fn input(&self) -> Input { | ||
| Input::metric() | ||
| } | ||
|
|
||
| fn outputs( | ||
| &self, | ||
| _: vector_lib::enrichment::TableRegistry, | ||
| _: &[(OutputId, schema::Definition)], | ||
| _: LogNamespace, | ||
| ) -> Vec<TransformOutput> { | ||
| vec![TransformOutput::new(DataType::Metric, HashMap::new())] | ||
| } | ||
| } | ||
| #[derive(Debug)] | ||
| pub struct IncrementalToAbsolute { | ||
| data: MetricSet, | ||
| } | ||
|
|
||
| impl IncrementalToAbsolute { | ||
| pub fn new(config: &IncrementalToAbsoluteConfig) -> crate::Result<Self> { | ||
| Ok(Self { | ||
| data: MetricSet::with_ttl_policy(TtlPolicy::new(Duration::from_secs( | ||
| config.expire_metrics_secs, | ||
| ))), | ||
| }) | ||
| } | ||
| pub fn transform_one(&mut self, event: Event) -> Option<Event> { | ||
| if let Some(metric) = self.data.make_absolute(event.as_metric().clone()) { | ||
pront marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Create a new Event from the metric | ||
| Some(Event::Metric(metric)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl TaskTransform<Event> for IncrementalToAbsolute { | ||
| fn transform( | ||
| self: Box<Self>, | ||
| task: Pin<Box<dyn Stream<Item = Event> + Send>>, | ||
| ) -> Pin<Box<dyn Stream<Item = Event> + Send>> | ||
| where | ||
| Self: 'static, | ||
| { | ||
| let mut inner = self; | ||
| Box::pin(task.filter_map(move |v| ready(inner.transform_one(v)))) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use futures_util::SinkExt; | ||
| use similar_asserts::assert_eq; | ||
| use std::sync::Arc; | ||
| use vector_lib::config::ComponentKey; | ||
|
|
||
| use super::*; | ||
| use crate::event::{ | ||
| metric::{MetricKind, MetricValue}, | ||
| Metric, | ||
| }; | ||
|
|
||
| fn make_metric(name: &'static str, kind: MetricKind, value: MetricValue) -> Event { | ||
| let mut event = Event::Metric(Metric::new(name, kind, value)) | ||
| .with_source_id(Arc::new(ComponentKey::from("in"))) | ||
| .with_upstream_id(Arc::new(OutputId::from("transform"))); | ||
|
|
||
| event.metadata_mut().set_source_type("unit_test_stream"); | ||
|
|
||
| event | ||
| } | ||
|
|
||
| async fn assert_metric_eq( | ||
| tx: &mut futures::channel::mpsc::Sender<Event>, | ||
| mut out_stream: impl Stream<Item = Event> + Unpin, | ||
| metric: Event, | ||
| expected_metric: Event, | ||
| ) { | ||
| tx.send(metric).await.unwrap(); | ||
| if let Some(out_event) = out_stream.next().await { | ||
| let result = out_event; | ||
| assert_eq!(result, expected_metric); | ||
| } else { | ||
| panic!("Unexpectedly received None in output stream"); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_incremental_to_absolute() { | ||
| let config = toml::from_str::<IncrementalToAbsoluteConfig>( | ||
| r#" | ||
| expire_metrics_secs = 9999 | ||
| "#, | ||
| ) | ||
| .unwrap(); | ||
| let incremental_to_absolute = IncrementalToAbsolute::new(&config) | ||
| .map(Transform::event_task) | ||
| .unwrap(); | ||
| let incremental_to_absolute = incremental_to_absolute.into_task(); | ||
| let (mut tx, rx) = futures::channel::mpsc::channel(10); | ||
| let mut out_stream = incremental_to_absolute.transform_events(Box::pin(rx)); | ||
|
|
||
| let inc_counter_1 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Incremental, | ||
| MetricValue::Counter { value: 10.0 }, | ||
| ); | ||
| let expected_inc_counter_1 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Absolute, | ||
| MetricValue::Counter { value: 10.0 }, | ||
| ); | ||
| assert_metric_eq( | ||
| &mut tx, | ||
| &mut out_stream, | ||
| inc_counter_1, | ||
| expected_inc_counter_1, | ||
| ) | ||
| .await; | ||
|
|
||
| let inc_counter_2 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Incremental, | ||
| MetricValue::Counter { value: 10.0 }, | ||
| ); | ||
| let expected_inc_counter_2 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Absolute, | ||
| MetricValue::Counter { value: 20.0 }, | ||
| ); | ||
| assert_metric_eq( | ||
| &mut tx, | ||
| &mut out_stream, | ||
| inc_counter_2, | ||
| expected_inc_counter_2, | ||
| ) | ||
| .await; | ||
|
|
||
| let inc_counter_3 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Incremental, | ||
| MetricValue::Counter { value: 10.0 }, | ||
| ); | ||
| let expected_inc_counter_3 = make_metric( | ||
| "incremental_counter", | ||
| MetricKind::Absolute, | ||
| MetricValue::Counter { value: 30.0 }, | ||
| ); | ||
| assert_metric_eq( | ||
| &mut tx, | ||
| &mut out_stream, | ||
| inc_counter_3, | ||
| expected_inc_counter_3, | ||
| ) | ||
| .await; | ||
|
|
||
| // Absolute counters and gauges are emitted unchanged | ||
| let gauge = make_metric( | ||
| "gauge", | ||
| MetricKind::Absolute, | ||
| MetricValue::Gauge { value: 42.0 }, | ||
| ); | ||
| let expected_gauge = gauge.clone(); | ||
| assert_metric_eq(&mut tx, &mut out_stream, gauge, expected_gauge).await; | ||
|
|
||
| let absolute_counter = make_metric( | ||
| "absolute_counter", | ||
| MetricKind::Absolute, | ||
| MetricValue::Counter { value: 42.0 }, | ||
| ); | ||
| let absolute_counter_expected = absolute_counter.clone(); | ||
| assert_metric_eq( | ||
| &mut tx, | ||
| &mut out_stream, | ||
| absolute_counter, | ||
| absolute_counter_expected, | ||
| ) | ||
| .await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
website/cue/reference/components/transforms/generated/incremental_to_absolute.cue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package metadata | ||
|
|
||
| generated: components: transforms: incremental_to_absolute: configuration: expire_metrics_secs: { | ||
| common: false | ||
| description: """ | ||
| The amount of time, in seconds, that incremental metrics will persist in the internal | ||
| metrics cache after having not been updated before they expire and are removed. | ||
| Once removed, incremental counters are reset to 0. | ||
| """ | ||
| required: true | ||
| type: uint: examples: [ | ||
| "120", | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.