Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/snmpreceiver] make timeout configureable #26070

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ScraperControllerSettings already has Timeout option for this purposes, let's reuse it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the Timeout setting to ScraperControllerSettings.


// 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