From 1fc4d4db54c82a86d529667c22ed469ee22037b0 Mon Sep 17 00:00:00 2001 From: "eric.cy.huang" Date: Sun, 1 Dec 2024 01:50:47 +0800 Subject: [PATCH 01/26] Try tag expansion --- src/transforms/log_to_metric.rs | 103 ++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 10 deletions(-) diff --git a/src/transforms/log_to_metric.rs b/src/transforms/log_to_metric.rs index 1e3d966d2c43a..9518863a5f637 100644 --- a/src/transforms/log_to_metric.rs +++ b/src/transforms/log_to_metric.rs @@ -1,8 +1,9 @@ use std::sync::Arc; -use std::{collections::HashMap, num::ParseFloatError}; +use std::{collections::HashMap, num::ParseFloatError, sync::LazyLock}; use chrono::Utc; use indexmap::IndexMap; +use regex::Regex; use vector_lib::configurable::configurable_component; use vector_lib::event::LogEvent; use vector_lib::{ @@ -110,7 +111,7 @@ pub struct MetricConfig { /// Tags to apply to the metric. #[configurable(metadata(docs::additional_props_description = "A metric tag."))] - pub tags: Option>, + pub tags: Option>, #[configurable(derived)] #[serde(flatten)] @@ -271,25 +272,48 @@ fn render_template(template: &Template, event: &Event) -> Result>, + tags: &Option>, event: &Event, ) -> Result, TransformError> { + let mut static_labels: HashMap = HashMap::new(); + let mut dynamic_labels: HashMap = HashMap::new(); Ok(match tags { None => None, Some(tags) => { let mut result = MetricTags::default(); for (name, config) in tags { match config { - TagConfig::Plain(template) => { - render_tag_into(event, name, template, &mut result)? - } + TagConfig::Plain(template) => render_tag_into( + event, + name, + template, + &mut result, + &mut static_labels, + &mut dynamic_labels, + )?, TagConfig::Multi(vec) => { for template in vec { - render_tag_into(event, name, template, &mut result)?; + render_tag_into( + event, + name, + template, + &mut result, + &mut static_labels, + &mut dynamic_labels, + )?; } } } } + for (k, v) in static_labels { + if let Some(discarded_v) = dynamic_labels.insert(k.clone(), v.clone()) { + warn!( + "Static label overrides dynamic label. \ + key: {}, value: {}, discarded value: {}", + k, v, discarded_v + ); + }; + } result.as_option() } }) @@ -297,19 +321,24 @@ fn render_tags( fn render_tag_into( event: &Event, - name: &str, + name: &Template, template: &Option