From fe78ffc86e973d057867de479164341f1bfbb911 Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Mon, 29 Apr 2024 08:57:11 -0600 Subject: [PATCH 1/2] fix(inputs.gnmi): Ensure path contains elements, avoid panic fixes: #15257 --- plugins/inputs/gnmi/handler.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/gnmi/handler.go b/plugins/inputs/gnmi/handler.go index 988f76136739d..6d3e7c004b62d 100644 --- a/plugins/inputs/gnmi/handler.go +++ b/plugins/inputs/gnmi/handler.go @@ -258,8 +258,11 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon relative := field.path.segments[len(aliasInfo.segments):len(field.path.segments)] key = strings.Join(relative, "/") } else { - // Otherwise use the last path element as the field key. - key = field.path.segments[len(field.path.segments)-1] + // Otherwise use the last path element as the field key if it + // exists. + if len(field.path.segments) > 0 { + key = field.path.segments[len(field.path.segments)-1] + } } key = strings.ReplaceAll(key, "-", "_") } From 39f0cd00709994544321e6729186a52a0f3c34cc Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Mon, 29 Apr 2024 12:56:34 -0600 Subject: [PATCH 2/2] Skip parsing field if the path is empty --- plugins/inputs/gnmi/handler.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/inputs/gnmi/handler.go b/plugins/inputs/gnmi/handler.go index 6d3e7c004b62d..ff4012f3b6dde 100644 --- a/plugins/inputs/gnmi/handler.go +++ b/plugins/inputs/gnmi/handler.go @@ -212,6 +212,10 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon // Parse individual update message and create measurements for _, field := range valueFields { + if field.path.empty() { + continue + } + // Prepare tags from prefix fieldTags := field.path.Tags() tags := make(map[string]string, len(headerTags)+len(fieldTags))