Skip to content

Commit

Permalink
Cherry Pick #2071 into 0.27.0-rc.1 (#2086)
Browse files Browse the repository at this point in the history
* SNMP Exporter Scrape Configs (#2071)

* Pass along additional scrape parameters for each snmp_target in the v1 integration

* Pass along additional scrape parameters for each snmp_target in the v2 integration

* Driveby fix for #2017

* Clear the walk_params map on unmarshal to support the config reload case

* Update changelog for snmp integration changes

* update changelog

Co-authored-by: Ryan Geyer <[email protected]>
  • Loading branch information
captncraig and rgeyer authored Aug 29, 2022
1 parent 02a9c93 commit 914b2a3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ v0.27.0-rc.1 (2022-08-29)

- App agent receiver: add Event kind to payload (@domasx2)

### Bugfixes

- Fix snmp integration not passing module or walk_params parameters when scraping. (@rgeyer)

- Fix unmarshal errors (key "<walk_param name>" already set in map) for snmp integration config when walk_params is defined, and the config is reloaded. (@rgeyer)

v0.27.0-rc.0 (2022-08-24)
-------------------------

Expand Down
6 changes: 6 additions & 0 deletions pkg/integrations/snmp_exporter/snmp_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
for _, target := range i.sh.cfg.SnmpTargets {
queryParams := url.Values{}
queryParams.Add("target", target.Target)
if target.Module != "" {
queryParams.Add("module", target.Module)
}
if target.WalkParams != "" {
queryParams.Add("walk_params", target.WalkParams)
}
res = append(res, config.ScrapeConfig{
JobName: i.sh.cfg.Name() + "/" + target.Name,
MetricsPath: "/metrics",
Expand Down
18 changes: 16 additions & 2 deletions pkg/integrations/v2/snmp_exporter/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,26 @@ func (sh *snmpHandler) Targets(ep integrations.Endpoint) []*targetgroup.Group {
}

for _, t := range sh.cfg.SnmpTargets {
group.Targets = append(group.Targets, model.LabelSet{
labelSet := model.LabelSet{
model.AddressLabel: model.LabelValue(ep.Host),
model.MetricsPathLabel: model.LabelValue(path.Join(ep.Prefix, "metrics")),
"snmp_target": model.LabelValue(t.Target),
"__param_target": model.LabelValue(t.Target),
})
}

if t.Module != "" {
labelSet = labelSet.Merge(model.LabelSet{
"__param_module": model.LabelValue(t.Module),
})
}

if t.WalkParams != "" {
labelSet = labelSet.Merge(model.LabelSet{
"__param_walk_params": model.LabelValue(t.WalkParams),
})
}

group.Targets = append(group.Targets, labelSet)
}

return []*targetgroup.Group{group}
Expand Down
8 changes: 8 additions & 0 deletions pkg/integrations/v2/snmp_exporter/snmp_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (c *Config) ApplyDefaults(globals integrations_v2.Globals) error {

// Identifier returns a string that identifies the integration.
func (c *Config) Identifier(globals integrations_v2.Globals) (string, error) {
if c.Common.InstanceKey != nil {
return *c.Common.InstanceKey, nil
}
return c.Name(), nil
}

Expand Down Expand Up @@ -74,6 +77,11 @@ func (c *Config) NewIntegration(log log.Logger, globals integrations_v2.Globals)
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig

// This should technically be accomplished by assigning DefaultConfig, right?
// But in the reload case the existing values in this map are not purged and
// an unmarshal error is thrown stating that they key already exists in the map.
c.WalkParams = make(map[string]snmp_config.WalkParams)

type plain Config
return unmarshal((*plain)(c))
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/integrations/v2/snmp_exporter/snmp_exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package snmp_exporter

import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func TestSnmpConfig(t *testing.T) {
t.Run("reload unmarshals", func(t *testing.T) {
var config Config

strConfig := `---
walk_params:
keyone:
auth:
community: foo
`

// The first time should not return any errors.
require.NoError(t, yaml.UnmarshalStrict([]byte(strConfig), &config), "initial unmarshal")
require.Len(t, config.WalkParams, 1)

// A second time (executed on reload), the map will already have the specified key(s), but should still succeed
require.NoError(t, yaml.UnmarshalStrict([]byte(strConfig), &config), "reload unmarshal")
require.Len(t, config.WalkParams, 1)
})
}

0 comments on commit 914b2a3

Please sign in to comment.