Skip to content

Commit

Permalink
Fix path handling issues in cisco_telemetry_gnmi (influxdata#6403)
Browse files Browse the repository at this point in the history
- Avoid crashing when a field has no value or one of deprecated type
- Emit measurement names correctly for replies with empty origin
- Skip paths with empty names instead of adding a '/'
  • Loading branch information
sbyx authored and danielnelson committed Sep 24, 2019
1 parent f18c861 commit e367c76
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions plugins/inputs/cisco_telemetry_gnmi/cisco_telemetry_gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net"
"path"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -102,21 +103,27 @@ func (c *CiscoTelemetryGNMI) Start(acc telegraf.Accumulator) error {
// Invert explicit alias list and prefill subscription names
c.aliases = make(map[string]string, len(c.Subscriptions)+len(c.Aliases))
for _, subscription := range c.Subscriptions {
var gnmiLongPath, gnmiShortPath *gnmi.Path

// Build the subscription path without keys
gnmiPath, err := parsePath(subscription.Origin, subscription.Path, "")
if err != nil {
if gnmiLongPath, err = parsePath(subscription.Origin, subscription.Path, ""); err != nil {
return err
}
if gnmiShortPath, err = parsePath("", subscription.Path, ""); err != nil {
return err
}

path, _ := c.handlePath(gnmiPath, nil, "")
longPath, _ := c.handlePath(gnmiLongPath, nil, "")
shortPath, _ := c.handlePath(gnmiShortPath, nil, "")
name := subscription.Name

// If the user didn't provide a measurement name, use last path element
if len(name) == 0 {
name = path[strings.LastIndexByte(path, '/')+1:]
name = path.Base(shortPath)
}
if len(name) > 0 {
c.aliases[path] = name
c.aliases[longPath] = name
c.aliases[shortPath] = name
}
}
for alias, path := range c.Aliases {
Expand Down Expand Up @@ -296,6 +303,12 @@ func (c *CiscoTelemetryGNMI) handleTelemetryField(update *gnmi.Update, tags map[
var value interface{}
var jsondata []byte

// Make sure a value is actually set
if update.Val == nil || update.Val.Value == nil {
log.Printf("I! [inputs.cisco_telemetry_gnmi]: Discarded empty or legacy type value with path: %s", path)
return aliasPath, nil
}

switch val := update.Val.Value.(type) {
case *gnmi.TypedValue_AsciiVal:
value = val.AsciiVal
Expand Down Expand Up @@ -347,8 +360,10 @@ func (c *CiscoTelemetryGNMI) handlePath(path *gnmi.Path, tags map[string]string,

// Parse generic keys from prefix
for _, elem := range path.Elem {
builder.WriteRune('/')
builder.WriteString(elem.Name)
if len(elem.Name) > 0 {
builder.WriteRune('/')
builder.WriteString(elem.Name)
}
name := builder.String()

if _, exists := c.aliases[name]; exists {
Expand Down

0 comments on commit e367c76

Please sign in to comment.