Skip to content

Commit

Permalink
[receiver/snmpreceiver] make timeout configureable
Browse files Browse the repository at this point in the history
The timeout of snmp requests was hard coded to 5 seconds.
This commit adds timeout to the configuration of this receiver.
The timeout can now be set using the timeout key at the higest level in the configuration.
The default is left at 5 seconds.
  • Loading branch information
technimad-splunk committed Aug 23, 2023
1 parent 653ab06 commit 297448d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .chloggen/snmpreceiver-add-timeout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: snmpreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Timeout for SNMP requests can now be configured.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25885]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
1 change: 1 addition & 0 deletions receiver/snmpreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This receiver supports SNMP versions:
These configuration options are for connecting to a SNMP host.

- `collection_interval`: (default = `10s`): This receiver collects metrics on an interval. This value must be a string readable by Golang's [time.ParseDuration](https://pkg.go.dev/time#ParseDuration). Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
- `timeout`: (default: `5s`): Timeout for each SNMP request. This value must be a string readable by Golang's [time.ParseDuration](https://pkg.go.dev/time#ParseDuration). Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
- `endpoint` (default: `udp://localhost:161`): SNMP endpoint to connect to in the form of `[udp|tcp][://]{host}[:{port}]`
- If no scheme is supplied, a default of `udp` is assumed
- If no port is supplied, a default of `161` is assumed
Expand Down
3 changes: 1 addition & 2 deletions receiver/snmpreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"strconv"
"strings"
"time"

"github.com/gosnmp/gosnmp"
"go.opentelemetry.io/collector/receiver/scrapererror"
Expand Down Expand Up @@ -62,7 +61,7 @@ var _ client = (*snmpClient)(nil)
func newClient(cfg *Config, logger *zap.Logger) (client, error) {
// Create goSNMP client
goSNMP := newGoSNMPWrapper()
goSNMP.SetTimeout(5 * time.Second)
goSNMP.SetTimeout(cfg.Timeout)

// Set goSNMP version based on config
switch cfg.Version {
Expand Down
5 changes: 5 additions & 0 deletions receiver/snmpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// Config Defaults
const (
defaultCollectionInterval = 10 * time.Second // In seconds
defaultTimeout = 5 * time.Second // In seconds
defaultEndpoint = "udp://localhost:161"
defaultVersion = "v2c"
defaultCommunity = "public"
Expand Down Expand Up @@ -76,6 +77,10 @@ type Config struct {
// If no port is given, 161 is assumed.
Endpoint string `mapstructure:"endpoint"`

// Timeout for each SNMP request.
// Default: 5 seconds
Timeout time.Duration `mapstructure:"timeout"`

// Version is the version of SNMP to use for this connection.
// Valid options: v1, v2c, v3.
// Default: v2c
Expand Down
3 changes: 2 additions & 1 deletion receiver/snmpreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func createDefaultConfig() component.Config {
CollectionInterval: defaultCollectionInterval,
},
Endpoint: defaultEndpoint,
Timeout: defaultTimeout,
Version: defaultVersion,
Community: defaultCommunity,
SecurityLevel: defaultSecurityLevel,
Expand Down Expand Up @@ -68,7 +69,7 @@ func createMetricsReceiver(
return scraperhelper.NewScraperControllerReceiver(&snmpConfig.ScraperControllerSettings, params, consumer, scraperhelper.AddScraper(scraper))
}

// addMissingConfigDefaults adds any missing comfig parameters that have defaults
// addMissingConfigDefaults adds any missing config parameters that have defaults
func addMissingConfigDefaults(cfg *Config) error {
// Add the schema prefix to the endpoint if it doesn't contain one
if !strings.Contains(cfg.Endpoint, "://") {
Expand Down
1 change: 1 addition & 0 deletions receiver/snmpreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestNewFactory(t *testing.T) {
CollectionInterval: defaultCollectionInterval,
},
Endpoint: defaultEndpoint,
Timeout: defaultTimeout,
Version: defaultVersion,
Community: defaultCommunity,
SecurityLevel: "no_auth_no_priv",
Expand Down

0 comments on commit 297448d

Please sign in to comment.