| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package scraperhelper // import "go.opentelemetry.io/collector/scraper/scraperhelper" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.uber.org/multierr" |
| 12 |
) |
| 13 |
|
| 14 |
var errNonPositiveInterval = errors.New("requires positive value") |
| 15 |
|
| 16 |
// ControllerConfig defines common settings for a scraper controller |
| 17 |
// configuration. Scraper controller receivers can embed this struct, instead |
| 18 |
// of receiver.Settings, and extend it with more fields if needed. |
| 19 |
type ControllerConfig struct { |
| 20 |
// CollectionInterval sets how frequently the scraper |
| 21 |
// should be called and used as the context timeout |
| 22 |
// to ensure that scrapers don't exceed the interval. |
| 23 |
CollectionInterval time.Duration `mapstructure:"collection_interval"` |
| 24 |
// InitialDelay sets the initial start delay for the scraper, |
| 25 |
// any non positive value is assumed to be immediately. |
| 26 |
InitialDelay time.Duration `mapstructure:"initial_delay"` |
| 27 |
// Timeout is an optional value used to set scraper's context deadline. |
| 28 |
Timeout time.Duration `mapstructure:"timeout"` |
| 29 |
// prevent unkeyed literal initialization |
| 30 |
_ struct{} |
| 31 |
} |
| 32 |
|
| 33 |
// NewDefaultControllerConfig returns default scraper controller |
| 34 |
// settings with a collection interval of one minute. |
| 35 |
func NewDefaultControllerConfig() ControllerConfig { |
| 36 |
return ControllerConfig{ |
| 37 |
CollectionInterval: time.Minute, |
| 38 |
InitialDelay: time.Second, |
| 39 |
Timeout: 0, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (set *ControllerConfig) Validate() (errs error) { |
| 44 |
if set.CollectionInterval <= 0 { |
| 45 |
errs = multierr.Append(errs, fmt.Errorf(`"collection_interval": %w`, errNonPositiveInterval)) |
| 46 |
} |
| 47 |
if set.Timeout < 0 { |
| 48 |
errs = multierr.Append(errs, fmt.Errorf(`"timeout": %w`, errNonPositiveInterval)) |
| 49 |
} |
| 50 |
return errs |
| 51 |
} |