From 821865165a0429a14b58c49e4d77aae60c190c52 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:13:46 +0100 Subject: [PATCH] fix(inputs.gnmi): Handle canonical field-name correctly (#14953) --- plugins/inputs/gnmi/handler.go | 6 +- .../expected.out | 4 + .../responses.json | 182 ++++++++++++++++++ .../telegraf.conf | 15 ++ 4 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/expected.out create mode 100644 plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/responses.json create mode 100644 plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/telegraf.conf diff --git a/plugins/inputs/gnmi/handler.go b/plugins/inputs/gnmi/handler.go index be2140f3c..8b048785b 100644 --- a/plugins/inputs/gnmi/handler.go +++ b/plugins/inputs/gnmi/handler.go @@ -236,9 +236,9 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon var key string if h.canonicalFieldNames { // Strip the origin is any for the field names - if parts := strings.SplitN(strings.ReplaceAll(field.path.String(), "-", "_"), ":", 2); len(parts) == 2 { - key = parts[1] - } + field.path.origin = "" + key = field.path.String() + key = strings.ReplaceAll(key, "-", "_") } else { // If the alias is a subpath of the field path and the alias is // shorter than the full path to avoid an empty key, then strip the diff --git a/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/expected.out b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/expected.out new file mode 100644 index 000000000..5added8a1 --- /dev/null +++ b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/expected.out @@ -0,0 +1,4 @@ +gnmi_sys_memory,path=/system/memory/state,source=127.0.0.1 /system/memory/state/reserved=6359478272u,/system/memory/state/used=3479629824u 1709737743568119333 +gnmi_sys_memory,source=127.0.0.1 /system/memory/state/used=3479527424u 1709737753565697718 +gnmi_sys_cpu,index=ALL,source=127.0.0.1 /system/cpus/cpu/state/hardware_interrupt/min_time=1709805333568034887u 1709805333566280930 +gnmi_sys_cpu,index=ALL,path=/system/cpus/cpu/state,source=127.0.0.1 /system/cpus/cpu/state/hardware_interrupt/min_time=1709805343567684412u,/system/cpus/cpu/state/idle/avg=89u,/system/cpus/cpu/state/idle/instant=90u 1709805343565718902 diff --git a/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/responses.json b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/responses.json new file mode 100644 index 000000000..18526d1b2 --- /dev/null +++ b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/responses.json @@ -0,0 +1,182 @@ +[ + { + "update": { + "timestamp": "1709737743568119333", + "prefix": { + "elem": [ + { + "name": "system" + }, + { + "name": "memory" + }, + { + "name": "state" + } + ] + }, + "update": [ + { + "path": { + "elem": [ + { + "name": "reserved" + } + ] + }, + "val": { + "uintVal": "6359478272" + } + }, + { + "path": { + "elem": [ + { + "name": "used" + } + ] + }, + "val": { + "uintVal": "3479629824" + } + } + ] + } + }, + { + "update": { + "timestamp": "1709737753565697718", + "update": [ + { + "path": { + "elem": [ + { + "name": "system" + }, + { + "name": "memory" + }, + { + "name": "state" + }, + { + "name": "used" + } + ] + }, + "val": { + "uintVal": "3479527424" + } + } + ] + } + }, + { + "update": { + "timestamp": "1709805333566280930", + "update": [ + { + "path": { + "elem": [ + { + "name": "system" + }, + { + "name": "cpus" + }, + { + "name": "cpu", + "key": { + "index": "ALL" + } + }, + { + "name": "state" + }, + { + "name": "hardware-interrupt" + }, + { + "name": "min-time" + } + ] + }, + "val": { + "uintVal": "1709805333568034887" + } + } + ] + } + }, + { + "update": { + "timestamp": "1709805343565718902", + "prefix": { + "elem": [ + { + "name": "system" + }, + { + "name": "cpus" + }, + { + "name": "cpu", + "key": { + "index": "ALL" + } + }, + { + "name": "state" + } + ] + }, + "update": [ + { + "path": { + "elem": [ + { + "name": "hardware-interrupt" + }, + { + "name": "min-time" + } + ] + }, + "val": { + "uintVal": "1709805343567684412" + } + }, + { + "path": { + "elem": [ + { + "name": "idle" + }, + { + "name": "avg" + } + ] + }, + "val": { + "uintVal": "89" + } + }, + { + "path": { + "elem": [ + { + "name": "idle" + }, + { + "name": "instant" + } + ] + }, + "val": { + "uintVal": "90" + } + } + ] + } + } +] \ No newline at end of file diff --git a/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/telegraf.conf b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/telegraf.conf new file mode 100644 index 000000000..720d4fa2d --- /dev/null +++ b/plugins/inputs/gnmi/testcases/issue_14946_canonical_field_names/telegraf.conf @@ -0,0 +1,15 @@ +[[inputs.gnmi]] + addresses = ["dummy"] + canonical_field_names = true + + [[inputs.gnmi.subscription]] + name = "gnmi_sys_cpu" + path = "/system/cpus/cpu/state" + subscription_mode = "sample" + sample_interval = "10s" + + [[inputs.gnmi.subscription]] + name = "gnmi_sys_memory" + path = "/system/memory/state" + subscription_mode = "sample" + sample_interval = "10s"