Skip to content

Commit

Permalink
[receiver/snmpreceiver] make timeout configureable (#26070)
Browse files Browse the repository at this point in the history
**Description:** 
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.

**Link to tracking Issue:** 
#25885

**Testing:** 
Updated `TestNewFactory` method to reflect new default config.
Ran receiver against devices demanding a high timeout. During testing
added extra logging statements to check if timeout setting was
propagated correctly to the underlying SNMP library and if it was
reflected in the SNMP commands executed. Tests succeeded. Removed extra
logging statements as we shouldn't create a log per datapoint received
in production.

**Documentation:** 
Added the new config option `timeout` to README.
  • Loading branch information
technimad-splunk authored Aug 28, 2023
1 parent c8c473f commit 164696c
Show file tree
Hide file tree
Showing 6 changed files with 33 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
1 change: 1 addition & 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
3 changes: 2 additions & 1 deletion receiver/snmpreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func createDefaultConfig() component.Config {
return &Config{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: defaultCollectionInterval,
Timeout: defaultTimeout,
},
Endpoint: defaultEndpoint,
Version: defaultVersion,
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 @@ -36,6 +36,7 @@ func TestNewFactory(t *testing.T) {
var expectedCfg component.Config = &Config{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: defaultCollectionInterval,
Timeout: defaultTimeout,
},
Endpoint: defaultEndpoint,
Version: defaultVersion,
Expand Down

0 comments on commit 164696c

Please sign in to comment.