diff --git a/.chloggen/snmpreceiver-add-timeout.yaml b/.chloggen/snmpreceiver-add-timeout.yaml new file mode 100755 index 000000000000..c18256dc1f84 --- /dev/null +++ b/.chloggen/snmpreceiver-add-timeout.yaml @@ -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] diff --git a/receiver/snmpreceiver/README.md b/receiver/snmpreceiver/README.md index e1719a7d0707..61e72a030001 100644 --- a/receiver/snmpreceiver/README.md +++ b/receiver/snmpreceiver/README.md @@ -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 diff --git a/receiver/snmpreceiver/client.go b/receiver/snmpreceiver/client.go index e1c8ce7321c6..1b579999564d 100644 --- a/receiver/snmpreceiver/client.go +++ b/receiver/snmpreceiver/client.go @@ -8,7 +8,6 @@ import ( "net/url" "strconv" "strings" - "time" "github.com/gosnmp/gosnmp" "go.opentelemetry.io/collector/receiver/scrapererror" @@ -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 { diff --git a/receiver/snmpreceiver/config.go b/receiver/snmpreceiver/config.go index b01ab33874af..c740fab639d5 100644 --- a/receiver/snmpreceiver/config.go +++ b/receiver/snmpreceiver/config.go @@ -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" @@ -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 diff --git a/receiver/snmpreceiver/factory.go b/receiver/snmpreceiver/factory.go index 07b2198feb13..dff570ec12a1 100644 --- a/receiver/snmpreceiver/factory.go +++ b/receiver/snmpreceiver/factory.go @@ -35,6 +35,7 @@ func createDefaultConfig() component.Config { CollectionInterval: defaultCollectionInterval, }, Endpoint: defaultEndpoint, + Timeout: defaultTimeout, Version: defaultVersion, Community: defaultCommunity, SecurityLevel: defaultSecurityLevel, @@ -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, "://") { diff --git a/receiver/snmpreceiver/factory_test.go b/receiver/snmpreceiver/factory_test.go index 87734e9f30db..c5e65f104498 100644 --- a/receiver/snmpreceiver/factory_test.go +++ b/receiver/snmpreceiver/factory_test.go @@ -38,6 +38,7 @@ func TestNewFactory(t *testing.T) { CollectionInterval: defaultCollectionInterval, }, Endpoint: defaultEndpoint, + Timeout: defaultTimeout, Version: defaultVersion, Community: defaultCommunity, SecurityLevel: "no_auth_no_priv",