Skip to content

Commit

Permalink
fix(transforms): optional metric namespace in log_to_metric transfo…
Browse files Browse the repository at this point in the history
…rm (#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 <[email protected]>
  • Loading branch information
jorgehermo9 and jszwedko authored Oct 6, 2024
1 parent 3eecbe7 commit 51dcf8d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog.d/21423-optional-namespace-log-to-metric.fix.md
Original file line number Diff line number Diff line change
@@ -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
63 changes: 56 additions & 7 deletions src/transforms/log_to_metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,15 @@ fn to_metrics(event: &Event) -> Result<Metric, TransformError> {

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 {
Expand Down Expand Up @@ -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")));
Expand Down Expand Up @@ -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()))
);
}
}

0 comments on commit 51dcf8d

Please sign in to comment.