-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[scraper helper] improve ease of use of scraper controller settings (#…
…7951) - Reduce the complexity required in order to override values within components - Address feedback from #7703 (comment)
- Loading branch information
1 parent
11effd3
commit 38ba4d5
Showing
5 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. otlpreceiver) | ||
component: scraperhelper | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Adding optional timeout field to scrapers | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [7951] | ||
|
||
# (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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package scraperhelper // import "go.opentelemetry.io/collector/receiver/scraperhelper" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"go.uber.org/multierr" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
var ( | ||
errNonPositiveInterval = errors.New("requires positive value") | ||
) | ||
|
||
// ScraperControllerSettings defines common settings for a scraper controller | ||
// configuration. Scraper controller receivers can embed this struct, instead | ||
// of receiver.Settings, and extend it with more fields if needed. | ||
type ScraperControllerSettings struct { | ||
// CollectionInterval sets the how frequently the scraper | ||
// should be called and used as the context timeout | ||
// to ensure that scrapers don't exceed the interval. | ||
CollectionInterval time.Duration `mapstructure:"collection_interval"` | ||
// InitialDelay sets the initial start delay for the scraper, | ||
// any non positive value is assumed to be immediately. | ||
InitialDelay time.Duration `mapstructure:"initial_delay"` | ||
// Timeout is an optional value used to set scraper's context deadline. | ||
Timeout time.Duration `mapstructure:"timeout"` | ||
} | ||
|
||
// NewDefaultScraperControllerSettings returns default scraper controller | ||
// settings with a collection interval of one minute. | ||
func NewDefaultScraperControllerSettings(component.Type) ScraperControllerSettings { | ||
return ScraperControllerSettings{ | ||
CollectionInterval: time.Minute, | ||
InitialDelay: time.Second, | ||
Timeout: 0, | ||
} | ||
} | ||
|
||
func (set *ScraperControllerSettings) Validate() (errs error) { | ||
if set.CollectionInterval <= 0 { | ||
errs = multierr.Append(errs, fmt.Errorf(`"collection_interval": %w`, errNonPositiveInterval)) | ||
} | ||
if set.Timeout < 0 { | ||
errs = multierr.Append(errs, fmt.Errorf(`"timeout": %w`, errNonPositiveInterval)) | ||
} | ||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package scraperhelper | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestScrapeControllerSettings(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
set ScraperControllerSettings | ||
errVal string | ||
}{ | ||
{ | ||
name: "default configuration", | ||
set: NewDefaultScraperControllerSettings(""), | ||
errVal: "", | ||
}, | ||
{ | ||
name: "zero value configuration", | ||
set: ScraperControllerSettings{}, | ||
errVal: `"collection_interval": requires positive value`, | ||
}, | ||
{ | ||
name: "invalid timeout", | ||
set: ScraperControllerSettings{ | ||
CollectionInterval: time.Minute, | ||
Timeout: -1 * time.Minute, | ||
}, | ||
errVal: `"timeout": requires positive value`, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := tc.set.Validate() | ||
if tc.errVal == "" { | ||
assert.NoError(t, err, "Must not error") | ||
return | ||
} | ||
assert.EqualError(t, err, tc.errVal, "Must match the expected error") | ||
}) | ||
} | ||
} |