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

[chore] make update-otel #24633

Closed
wants to merge 7 commits into from
Closed
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
Prev Previous commit
Next Next commit
port chrony-related changes from #24640
Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Jul 27, 2023
commit f4415666ae3b7a48b6684539c314f62d1bfcd6d9
11 changes: 1 addition & 10 deletions receiver/chronyreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package chronyreceiver // import "github.com/open-telemetry/opentelemetry-collec

import (
"errors"
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand All @@ -26,8 +24,6 @@ type Config struct {
//
// The default value is unix:///var/run/chrony/chronyd.sock
Endpoint string `mapstructure:"endpoint"`
// Timeout controls the max time allowed to read data from chronyd
Timeout time.Duration `mapstructure:"timeout"`
}

var (
Expand All @@ -40,16 +36,11 @@ func newDefaultCongfig() component.Config {
return &Config{
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),

Endpoint: "unix:///var/run/chrony/chronyd.sock",
Timeout: 10 * time.Second,
Endpoint: "unix:///var/run/chrony/chronyd.sock",
}
}

func (c *Config) Validate() error {
if c.Timeout < 1 {
return fmt.Errorf("must have a positive timeout: %w", errInvalidValue)
}
_, _, err := chrony.SplitNetworkEndpoint(c.Endpoint)
return err
}
36 changes: 15 additions & 21 deletions receiver/chronyreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

assert.Equal(t, &Config{
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Endpoint: "udp://localhost:3030",
Timeout: 10 * time.Second,
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 60 * time.Second,
InitialDelay: time.Second,
Timeout: 10 * time.Second,
},
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Endpoint: "udp://localhost:3030",
}, cfg)
}

Expand All @@ -52,51 +55,42 @@ func TestValidate(t *testing.T) {
{
scenario: "Valid udp configuration",
conf: Config{
Endpoint: "udp://localhost:323",
Timeout: 10 * time.Second,
Endpoint: "udp://localhost:323",
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
},
err: nil,
},
{
scenario: "Invalid udp hostname",
conf: Config{
Endpoint: "udp://:323",
Timeout: 10 * time.Second,
},
err: chrony.ErrInvalidNetwork,
},
{
scenario: "Invalid udp port",
conf: Config{
Endpoint: "udp://localhost",
Timeout: 10 * time.Second,
Endpoint: "udp://localhost",
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
},
err: chrony.ErrInvalidNetwork,
},
{
scenario: "Valid unix path",
conf: Config{
Endpoint: fmt.Sprintf("unix://%s", t.TempDir()),
Timeout: 10 * time.Second,
Endpoint: fmt.Sprintf("unix://%s", t.TempDir()),
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
},
err: nil,
},
{
scenario: "Invalid unix path",
conf: Config{
Endpoint: "unix:///no/dir/to/socket",
Timeout: 10 * time.Second,
Endpoint: "unix:///no/dir/to/socket",
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
},
err: os.ErrNotExist,
},
{
scenario: "Invalid timeout set",
conf: Config{
Endpoint: "unix://no/dir/to/socket",
Timeout: 0,
},
err: errInvalidValue,
},
}

for _, tc := range tests {
Expand Down
1 change: 0 additions & 1 deletion receiver/chronyreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestCreatingMetricsReceiver(t *testing.T) {
},
MetricsBuilderConfig: mbc,
Endpoint: "udp://localhost:323",
Timeout: 10 * time.Second,
},
consumertest.NewNop(),
)
Expand Down
28 changes: 13 additions & 15 deletions receiver/chronyreceiver/internal/chrony/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ type client struct {

// New creates a client ready to use with chronyd
func New(addr string, timeout time.Duration, opts ...clientOption) (Client, error) {
if timeout < 1 {
return nil, errors.New("timeout must be positive")
}

network, endpoint, err := SplitNetworkEndpoint(addr)
if err != nil {
return nil, err
Expand All @@ -66,27 +62,22 @@ func New(addr string, timeout time.Duration, opts ...clientOption) (Client, erro
}

func (c *client) GetTrackingData(ctx context.Context) (*Tracking, error) {
clk := clock.FromContext(ctx)

ctx, cancel := clk.TimeoutContext(ctx, c.timeout)
ctx, cancel := c.getContext(ctx)
defer cancel()

sock, err := c.dialer(ctx, c.proto, c.addr)
if err != nil {
return nil, err
}

deadline, ok := ctx.Deadline()
if !ok {
return nil, errors.New("no deadline set")
}

if err = sock.SetDeadline(deadline); err != nil {
return nil, err
if deadline, ok := ctx.Deadline(); ok {
if err = sock.SetDeadline(deadline); err != nil {
return nil, err
}
}

packet := chrony.NewTrackingPacket()
packet.SetSequence(uint32(clk.Now().UnixNano()))
packet.SetSequence(uint32(clock.Now(ctx).UnixNano()))

if err := binary.Write(sock, binary.BigEndian, packet); err != nil {
return nil, multierr.Combine(err, sock.Close())
Expand All @@ -102,3 +93,10 @@ func (c *client) GetTrackingData(ctx context.Context) (*Tracking, error) {

return newTrackingData(data)
}

func (c *client) getContext(ctx context.Context) (context.Context, context.CancelFunc) {
if c.timeout == 0 {
return context.WithCancel(ctx)
}
return clock.TimeoutContext(ctx, c.timeout)
}
7 changes: 0 additions & 7 deletions receiver/dockerstatsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package dockerstatsreceiver // import "github.com/open-telemetry/opentelemetry-c
import (
"errors"
"fmt"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand All @@ -21,9 +20,6 @@ type Config struct {
// The URL of the docker server. Default is "unix:///var/run/docker.sock"
Endpoint string `mapstructure:"endpoint"`

// The maximum amount of time to wait for docker API responses. Default is 5s
Timeout time.Duration `mapstructure:"timeout"`

// A mapping of container label names to MetricDescriptor label keys.
// The corresponding container label value will become the DataPoint label value
// for the mapped name. E.g. `io.kubernetes.container.name: container_spec_name`
Expand Down Expand Up @@ -53,9 +49,6 @@ func (config Config) Validate() error {
if config.Endpoint == "" {
return errors.New("endpoint must be specified")
}
if config.CollectionInterval == 0 {
return errors.New("collection_interval must be a positive duration")
}
if config.DockerAPIVersion < minimalRequiredDockerAPIVersion {
return fmt.Errorf("api_version must be at least %v", minimalRequiredDockerAPIVersion)
}
Expand Down
7 changes: 2 additions & 5 deletions receiver/dockerstatsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func TestLoadConfig(t *testing.T) {
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 2 * time.Second,
InitialDelay: time.Second,
Timeout: 20 * time.Second,
},

Endpoint: "http://example.com/",
Timeout: 20 * time.Second,
DockerAPIVersion: 1.24,

ExcludedImages: []string{
Expand Down Expand Up @@ -92,10 +92,7 @@ func TestLoadConfig(t *testing.T) {

func TestValidateErrors(t *testing.T) {
cfg := &Config{}
assert.Equal(t, "endpoint must be specified", component.ValidateConfig(cfg).Error())

cfg = &Config{Endpoint: "someEndpoint"}
assert.Equal(t, "collection_interval must be a positive duration", component.ValidateConfig(cfg).Error())
assert.Equal(t, "endpoint must be specified; \"collection_interval\": requires positive value", component.ValidateConfig(cfg).Error())

cfg = &Config{ScraperControllerSettings: scraperhelper.ScraperControllerSettings{CollectionInterval: 1 * time.Second}, Endpoint: "someEndpoint", DockerAPIVersion: 1.21}
assert.Equal(t, "api_version must be at least 1.22", component.ValidateConfig(cfg).Error())
Expand Down
2 changes: 1 addition & 1 deletion receiver/dockerstatsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func NewFactory() rcvr.Factory {
func createDefaultConfig() component.Config {
scs := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
scs.CollectionInterval = 10 * time.Second
scs.Timeout = 5 * time.Second
return &Config{
ScraperControllerSettings: scs,
Endpoint: "unix:///var/run/docker.sock",
Timeout: 5 * time.Second,
DockerAPIVersion: defaultDockerAPIVersion,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
Expand Down
5 changes: 3 additions & 2 deletions receiver/expvarreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func TestLoadConfig(t *testing.T) {
expected: &Config{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 30 * time.Second,
InitialDelay: time.Second,
InitialDelay: 1 * time.Second,
Timeout: 5 * time.Second,
},
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "http://localhost:8000/custom/path",
Timeout: time.Second * 5,
Timeout: 5 * time.Second,
},
MetricsBuilderConfig: metricCfg,
},
Expand Down
16 changes: 6 additions & 10 deletions receiver/mongodbreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ func TestValidate(t *testing.T) {
}

cfg := &Config{
Username: tc.username,
Password: configopaque.String(tc.password),
Hosts: hosts,
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 1,
},
Username: tc.username,
Password: configopaque.String(tc.password),
Hosts: hosts,
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
}
err := component.ValidateConfig(cfg)
if tc.expected == nil {
Expand Down Expand Up @@ -141,10 +139,8 @@ func TestBadTLSConfigs(t *testing.T) {
Endpoint: "localhost:27017",
},
},
TLSClientSetting: tc.tlsConfig,
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 1,
},
TLSClientSetting: tc.tlsConfig,
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
}
err := component.ValidateConfig(cfg)
if tc.expectError {
Expand Down
5 changes: 5 additions & 0 deletions receiver/nsxtreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/receiver/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver/internal/metadata"
)
Expand All @@ -35,6 +36,7 @@ func TestMetricValidation(t *testing.T) {
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "wss://not-supported-websockets",
},
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
},
expectedError: errors.New("url scheme must be http or https"),
},
Expand All @@ -44,6 +46,7 @@ func TestMetricValidation(t *testing.T) {
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "\x00",
},
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
},
expectedError: errors.New("parse"),
},
Expand All @@ -54,6 +57,7 @@ func TestMetricValidation(t *testing.T) {
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "http://localhost",
},
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
},
expectedError: errors.New("username not provided"),
},
Expand All @@ -64,6 +68,7 @@ func TestMetricValidation(t *testing.T) {
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "http://localhost",
},
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(""),
},
expectedError: errors.New("password not provided"),
},
Expand Down
7 changes: 0 additions & 7 deletions receiver/podmanreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package podmanreceiver // import "github.com/open-telemetry/opentelemetry-collec

import (
"errors"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configopaque"
Expand All @@ -20,9 +19,6 @@ type Config struct {
// The URL of the podman server. Default is "unix:///run/podman/podman.sock"
Endpoint string `mapstructure:"endpoint"`

// The maximum amount of time to wait for Podman API responses. Default is 5s
Timeout time.Duration `mapstructure:"timeout"`

APIVersion string `mapstructure:"api_version"`
SSHKey string `mapstructure:"ssh_key"`
SSHPassphrase configopaque.String `mapstructure:"ssh_passphrase"`
Expand All @@ -32,8 +28,5 @@ func (config Config) Validate() error {
if config.Endpoint == "" {
return errors.New("config.Endpoint must be specified")
}
if config.CollectionInterval == 0 {
return errors.New("config.CollectionInterval must be specified")
}
return nil
}
4 changes: 2 additions & 2 deletions receiver/podmanreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func TestLoadConfig(t *testing.T) {
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 10 * time.Second,
InitialDelay: time.Second,
Timeout: 5 * time.Second,
},
APIVersion: defaultAPIVersion,
Endpoint: "unix:///run/podman/podman.sock",
Timeout: 5 * time.Second,
},
},
{
Expand All @@ -46,10 +46,10 @@ func TestLoadConfig(t *testing.T) {
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 2 * time.Second,
InitialDelay: time.Second,
Timeout: 20 * time.Second,
},
APIVersion: defaultAPIVersion,
Endpoint: "http://example.com/",
Timeout: 20 * time.Second,
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions receiver/podmanreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func NewFactory() rcvr.Factory {
func createDefaultConfig() *Config {
cfg := scraperhelper.NewDefaultScraperControllerSettings(metadata.Type)
cfg.CollectionInterval = 10 * time.Second

cfg.Timeout = 5 * time.Second

return &Config{
ScraperControllerSettings: cfg,
Endpoint: "unix:///run/podman/podman.sock",
Timeout: 5 * time.Second,
APIVersion: defaultAPIVersion,
}
}
Expand Down
Loading