From 57322703301b25bd8c3a125cdc96afcd5680ab34 Mon Sep 17 00:00:00 2001 From: Dane Strandboge <136023093+DStrand1@users.noreply.github.com> Date: Fri, 9 Feb 2024 13:05:17 -0600 Subject: [PATCH] test(processors.regex): Add unit-test for tracking metrics (#14739) --- plugins/processors/regex/regex_test.go | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/plugins/processors/regex/regex_test.go b/plugins/processors/regex/regex_test.go index ea0f919f0..f556be964 100644 --- a/plugins/processors/regex/regex_test.go +++ b/plugins/processors/regex/regex_test.go @@ -990,3 +990,67 @@ func TestAnyFieldConversion(t *testing.T) { require.Equal(t, "access_log", processed[0].Name(), "Should not change name") } } + +func TestTrackedMetricNotLost(t *testing.T) { + regex := Regex{ + Tags: []converter{ + { + Key: "resp_code", + Pattern: "^(\\d)\\d\\d$", + Replacement: "${1}xx", + ResultKey: "resp_code_group", + }, + { + Key: "resp_code_group", + Pattern: "2xx", + Replacement: "OK", + ResultKey: "resp_code_text", + }, + }, + Fields: []converter{ + { + Key: "request", + Pattern: "^/api(?P/[\\w/]+)\\S*", + Replacement: "${method}", + ResultKey: "method", + }, + { + Key: "request", + Pattern: ".*category=(\\w+).*", + Replacement: "${1}", + ResultKey: "search_category", + }, + }, + Log: testutil.Logger{}, + } + require.NoError(t, regex.Init()) + + m := newM2().Copy() + var delivered bool + notify := func(di telegraf.DeliveryInfo) { + delivered = true + } + m, _ = metric.WithTracking(m, notify) + processed := regex.Apply(m) + processed[0].Accept() + + expectedFields := map[string]interface{}{ + "request": "/api/search/?category=plugins&q=regex&sort=asc", + "method": "/search/", + "search_category": "plugins", + "ignore_number": int64(200), + "ignore_bool": true, + } + expectedTags := map[string]string{ + "verb": "GET", + "resp_code": "200", + "resp_code_group": "2xx", + "resp_code_text": "OK", + } + + require.Equal(t, expectedFields, processed[0].Fields()) + require.Equal(t, expectedTags, processed[0].Tags()) + require.Eventually(t, func() bool { + return delivered + }, time.Second, 100*time.Millisecond, "metric not delivered") +}