From 51dcf8dfd4b9be926fd45b584908e820df4c2b7a Mon Sep 17 00:00:00 2001 From: Jorge Hermo Date: Sun, 6 Oct 2024 03:32:05 +0200 Subject: [PATCH] fix(transforms): optional metric namespace in `log_to_metric` transform (#21429) * fix(transforms): optional namespace in log_to_metric transform * docs: add changelog entry * Update changelog.d/21423-optional-namespace-log-to-metric.fix.md --------- Co-authored-by: Jesse Szwedko --- ...23-optional-namespace-log-to-metric.fix.md | 4 ++ src/transforms/log_to_metric.rs | 63 ++++++++++++++++--- 2 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 changelog.d/21423-optional-namespace-log-to-metric.fix.md diff --git a/changelog.d/21423-optional-namespace-log-to-metric.fix.md b/changelog.d/21423-optional-namespace-log-to-metric.fix.md new file mode 100644 index 0000000000000..ba3e5e8df078d --- /dev/null +++ b/changelog.d/21423-optional-namespace-log-to-metric.fix.md @@ -0,0 +1,4 @@ +When using the `all_metrics: true` flag in `log_to_metric` transform, the `namespace` field is now optional and no longer required. If the `namespace` field is not provided, +the produced metric will not have a namespace at all. + +authors: jorgehermo9 diff --git a/src/transforms/log_to_metric.rs b/src/transforms/log_to_metric.rs index 55af676814870..3136c99a7ec79 100644 --- a/src/transforms/log_to_metric.rs +++ b/src/transforms/log_to_metric.rs @@ -795,12 +795,15 @@ fn to_metrics(event: &Event) -> Result { let value = value.ok_or(TransformError::MetricDetailsNotFound)?; - Ok( - Metric::new_with_metadata(name, kind, value, log.metadata().clone()) - .with_namespace(try_get_string_from_log(log, "namespace")?) - .with_tags(tags_result) - .with_timestamp(timestamp), - ) + let mut metric = Metric::new_with_metadata(name, kind, value, log.metadata().clone()) + .with_tags(tags_result) + .with_timestamp(timestamp); + + if let Ok(namespace) = try_get_string_from_log(log, "namespace") { + metric = metric.with_namespace(namespace); + } + + Ok(metric) } impl FunctionTransform for LogToMetric { @@ -1608,11 +1611,19 @@ mod tests { } // Metric Metadata Tests + // fn create_log_event(json_str: &str) -> Event { + create_log_event_with_namespace(json_str, Some("test_namespace")) + } + + fn create_log_event_with_namespace(json_str: &str, namespace: Option<&str>) -> Event { let mut log_value: Value = serde_json::from_str(json_str).expect("JSON was not well-formatted"); log_value.insert("timestamp", ts()); - log_value.insert("namespace", "test_namespace"); + + if let Some(namespace) = namespace { + log_value.insert("namespace", namespace); + } let mut metadata = EventMetadata::default(); metadata.set_source_id(Arc::new(ComponentKey::from("in"))); @@ -2003,4 +2014,42 @@ mod tests { .with_timestamp(Some(ts())) ); } + + #[tokio::test] + async fn transform_all_metrics_optional_namespace() { + let config = parse_yaml_config( + r#" + metrics: [] + all_metrics: true + "#, + ); + + let json_str = r#"{ + "counter": { + "value": 10.0 + }, + "kind": "incremental", + "name": "test.transform.counter", + "tags": { + "env": "test_env", + "host": "localhost" + } + }"#; + let log = create_log_event_with_namespace(json_str, None); + let metric = do_transform(config, log.clone()).await.unwrap(); + assert_eq!( + *metric.as_metric(), + Metric::new_with_metadata( + "test.transform.counter", + MetricKind::Incremental, + MetricValue::Counter { value: 10.0 }, + metric.metadata().clone(), + ) + .with_tags(Some(metric_tags!( + "env" => "test_env", + "host" => "localhost", + ))) + .with_timestamp(Some(ts())) + ); + } }