fix(inputs.gnmi): Ensure path contains elements to avoid panic (#15259)

This commit is contained in:
Joshua Powers 2024-05-02 12:48:22 -06:00 committed by GitHub
parent d3f0ba9368
commit d6671eb05e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 2 deletions

View File

@ -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))
@ -258,8 +262,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, "-", "_")
}