Skip to content

Commit

Permalink
fix: add mutex to gnmi lookup map (#11006)
Browse files Browse the repository at this point in the history
  • Loading branch information
bewing committed Apr 20, 2022
1 parent 1898077 commit 63b278f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion plugins/inputs/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type GNMI struct {
cancel context.CancelFunc
wg sync.WaitGroup
// Lookup/device+name/key/value
lookup map[string]map[string]map[string]interface{}
lookup map[string]map[string]map[string]interface{}
lookupMutex sync.RWMutex

Log telegraf.Logger
}
Expand Down Expand Up @@ -88,7 +89,9 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error {
var request *gnmiLib.SubscribeRequest
c.acc = acc
ctx, c.cancel = context.WithCancel(context.Background())
c.lookupMutex.Lock()
c.lookup = make(map[string]map[string]map[string]interface{})
c.lookupMutex.Unlock()

// Validate configuration
if request, err = c.newSubscribeRequest(); err != nil {
Expand Down Expand Up @@ -142,7 +145,9 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error {

if subscription.TagOnly {
// Create the top-level lookup for this tag
c.lookupMutex.Lock()
c.lookup[name] = make(map[string]map[string]interface{})
c.lookupMutex.Unlock()
}
}
for alias, encodingPath := range c.Aliases {
Expand Down Expand Up @@ -310,26 +315,32 @@ func (c *GNMI) handleSubscribeResponseUpdate(address string, response *gnmiLib.S

// Update tag lookups and discard rest of update
subscriptionKey := tags["source"] + "/" + tags["name"]
c.lookupMutex.RLock()
if _, ok := c.lookup[name]; ok {
c.lookupMutex.RUnlock()
// We are subscribed to this, so add the fields to the lookup-table
c.lookupMutex.Lock()
if _, ok := c.lookup[name][subscriptionKey]; !ok {
c.lookup[name][subscriptionKey] = make(map[string]interface{})
}
for k, v := range fields {
c.lookup[name][subscriptionKey][path.Base(k)] = v
}
c.lookupMutex.Unlock()
// Do not process the data further as we only subscribed here for the lookup table
continue
}

// Apply lookups if present
c.lookupMutex.RLock()
for subscriptionName, values := range c.lookup {
if annotations, ok := values[subscriptionKey]; ok {
for k, v := range annotations {
tags[subscriptionName+"/"+k] = v.(string)
}
}
}
c.lookupMutex.RUnlock()

// Group metrics
for k, v := range fields {
Expand Down

0 comments on commit 63b278f

Please sign in to comment.