diff --git a/plugins/inputs/gnmi/handler.go b/plugins/inputs/gnmi/handler.go index 15447f81c..49d73b095 100644 --- a/plugins/inputs/gnmi/handler.go +++ b/plugins/inputs/gnmi/handler.go @@ -239,29 +239,26 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon // Group metrics for k, v := range fields { key := k - if len(aliasPath) < len(key) && len(aliasPath) != 0 { - // This may not be an exact prefix, due to naming style - // conversion on the key. - key = key[len(aliasPath)+1:] - } else if len(aliasPath) >= len(key) { - if h.canonicalFieldNames { - // Strip the origin is any for the field names - if parts := strings.SplitN(key, ":", 2); len(parts) == 2 { - key = parts[1] - } - } else { + if h.canonicalFieldNames { + // Strip the origin is any for the field names + if parts := strings.SplitN(key, ":", 2); len(parts) == 2 { + key = parts[1] + } + } else { + if len(aliasPath) < len(key) && len(aliasPath) != 0 { + // This may not be an exact prefix, due to naming style + // conversion on the key. + key = key[len(aliasPath)+1:] + } else if len(aliasPath) >= len(key) { // Otherwise use the last path element as the field key. key = path.Base(key) - - // If there are no elements skip the item; this would be an - // invalid message. - key = strings.TrimLeft(key, "/.") - if key == "" { - h.log.Errorf("invalid empty path: %q", k) - continue - } } } + key = strings.TrimLeft(key, "/.") + if key == "" { + h.log.Errorf("invalid empty path: %q", k) + continue + } grouper.Add(name, tags, timestamp, key, v) } } diff --git a/plugins/inputs/gnmi/testcases/canonical_field_names/expected.out b/plugins/inputs/gnmi/testcases/canonical_field_names/expected.out index ddf715d06..31a6219b4 100644 --- a/plugins/inputs/gnmi/testcases/canonical_field_names/expected.out +++ b/plugins/inputs/gnmi/testcases/canonical_field_names/expected.out @@ -1 +1 @@ -interfaces,name=eth42,path=openconfig-interfaces:/interfaces/interface,source=127.0.0.1 /interfaces/interface/descr="eth42",/interfaces/interface/config/id=42425u,/interfaces/interface/state/in_pkts=5678u,/interfaces/interface/state/out_pkts=5125u 1673608605875353770 \ No newline at end of file +interfaces,name=eth42,path=openconfig-interfaces:/interfaces/interface,source=127.0.0.1 interfaces/interface/descr="eth42",interfaces/interface/config/id=42425u,interfaces/interface/state/in_pkts=5678u,interfaces/interface/state/out_pkts=5125u 1673608605875353770 \ No newline at end of file diff --git a/plugins/inputs/gnmi/testcases/canonical_field_names/telegraf.conf b/plugins/inputs/gnmi/testcases/canonical_field_names/telegraf.conf index 7963e1e6e..b7376c1df 100644 --- a/plugins/inputs/gnmi/testcases/canonical_field_names/telegraf.conf +++ b/plugins/inputs/gnmi/testcases/canonical_field_names/telegraf.conf @@ -18,12 +18,6 @@ [[inputs.gnmi.subscription]] name = "interfaces" origin = "openconfig-interfaces" - path = "/interfaces/interface/state/in-pkts" - subscription_mode = "sample" - sample_interval = "10s" - [[inputs.gnmi.subscription]] - name = "interfaces" - origin = "openconfig-interfaces" - path = "/interfaces/interface/state/out-pkts" + path = "/interfaces/interface/state" subscription_mode = "sample" sample_interval = "10s"