From 2df724680684ad0d2b405a0e71a68da6271d633d Mon Sep 17 00:00:00 2001 From: Jerry Leung Date: Mon, 29 Jan 2024 14:03:52 -0800 Subject: [PATCH] Updated --- .../config_test.go | 4 +-- .../extension.go | 31 ++++++++++++++----- .../extension_test.go | 10 +++--- .../solarwindsapmsettingsextension/factory.go | 2 +- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/collector/extension/solarwindsapmsettingsextension/config_test.go b/collector/extension/solarwindsapmsettingsextension/config_test.go index 2b87e14f01..c5d214996c 100644 --- a/collector/extension/solarwindsapmsettingsextension/config_test.go +++ b/collector/extension/solarwindsapmsettingsextension/config_test.go @@ -29,7 +29,7 @@ func TestLoadConfig(t *testing.T) { expected: &Config{ Endpoint: "0.0.0.0:1234", Key: "something", - Interval: time.Duration(10 * time.Second), + Interval: time.Duration(10) * time.Second, }, }, { @@ -37,7 +37,7 @@ func TestLoadConfig(t *testing.T) { expected: &Config{ Endpoint: "0.0.0.0:1234", Key: "something", - Interval: time.Duration(10 * time.Second), + Interval: time.Duration(10) * time.Second, }, }, } diff --git a/collector/extension/solarwindsapmsettingsextension/extension.go b/collector/extension/solarwindsapmsettingsextension/extension.go index ecf4c5bbd6..0bc126a7da 100644 --- a/collector/extension/solarwindsapmsettingsextension/extension.go +++ b/collector/extension/solarwindsapmsettingsextension/extension.go @@ -22,10 +22,11 @@ import ( ) const ( - RawOutputFile = "/tmp/solarwinds-apm-settings-raw" - JSONOutputFile = "/tmp/solarwinds-apm-settings.json" - MinimumInterval = time.Duration(5 * time.Second) - MaximumInterval = time.Duration(60 * time.Second) + RawOutputFile = "/tmp/solarwinds-apm-settings-raw" + JSONOutputFile = "/tmp/solarwinds-apm-settings.json" + GrpcContextDeadline = time.Duration(1) * time.Second + MinimumInterval = time.Duration(5) * time.Second + MaximumInterval = time.Duration(60) * time.Second ) type solarwindsapmSettingsExtension struct { @@ -134,7 +135,7 @@ func refresh(extension *solarwindsapmSettingsExtension) { if hostname, err := os.Hostname(); err != nil { extension.logger.Error("Unable to call os.Hostname() " + err.Error()) } else { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), GrpcContextDeadline) defer cancel() request := &collectorpb.SettingsRequest{ @@ -283,10 +284,8 @@ func (extension *solarwindsapmSettingsExtension) Start(ctx context.Context, _ co extension.conn, _ = grpc.Dial(extension.config.Endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))) extension.logger.Info("grpc.Dail to " + extension.config.Endpoint) extension.client = collectorpb.NewTraceCollectorClient(extension.conn) - // Perform refresh immediately - refresh(extension) go func() { - ticker := time.NewTicker(extension.config.Interval) + ticker := newTicker(extension.config.Interval) defer ticker.Stop() for { select { @@ -312,3 +311,19 @@ func (extension *solarwindsapmSettingsExtension) Shutdown(_ context.Context) err return nil } } + +// Start ticking immediately. +// Ref: https://stackoverflow.com/questions/32705582/how-to-get-time-tick-to-tick-immediately +func newTicker(repeat time.Duration) *time.Ticker { + ticker := time.NewTicker(repeat) + oc := ticker.C + nc := make(chan time.Time, 1) + go func() { + nc <- time.Now() + for tm := range oc { + nc <- tm + } + }() + ticker.C = nc + return ticker +} diff --git a/collector/extension/solarwindsapmsettingsextension/extension_test.go b/collector/extension/solarwindsapmsettingsextension/extension_test.go index a89c3b88b4..acae190e68 100644 --- a/collector/extension/solarwindsapmsettingsextension/extension_test.go +++ b/collector/extension/solarwindsapmsettingsextension/extension_test.go @@ -22,7 +22,7 @@ func TestCreateExtension(t *testing.T) { { name: "default", cfg: &Config{ - Interval: time.Duration(10 * time.Second), + Interval: time.Duration(10) * time.Second, }, }, { @@ -30,7 +30,7 @@ func TestCreateExtension(t *testing.T) { cfg: &Config{ Endpoint: "0.0.0.0:1234", Key: "something", - Interval: time.Duration(10 * time.Second), + Interval: time.Duration(10) * time.Second, }, }, } @@ -72,7 +72,7 @@ func TestValidateSolarwindsApmSettingsExtensionConfiguration(t *testing.T) { cfg: &Config{ Endpoint: "apm.collector.na-02.cloud.solarwinds.com:443", Key: "token:name", - Interval: time.Duration(10 * time.Second), + Interval: time.Duration(10) * time.Second, }, ok: true, message: []string{}, @@ -182,7 +182,7 @@ func TestValidateSolarwindsApmSettingsExtensionConfiguration(t *testing.T) { cfg: &Config{ Endpoint: "apm.collector.na-01.cloud.solarwinds.com:443", Key: "token:name", - Interval: time.Duration(4 * time.Second), + Interval: time.Duration(4) * time.Second, }, ok: true, message: []string{"Interval 4s is less than the minimum supported interval " + MinimumInterval.String() + ". use minimum interval " + MinimumInterval.String() + " instead"}, @@ -192,7 +192,7 @@ func TestValidateSolarwindsApmSettingsExtensionConfiguration(t *testing.T) { cfg: &Config{ Endpoint: "apm.collector.na-01.cloud.solarwinds.com:443", Key: "token:name", - Interval: time.Duration(61 * time.Second), + Interval: time.Duration(61) * time.Second, }, ok: true, message: []string{"Interval 1m1s is greater than the maximum supported interval " + MaximumInterval.String() + ". use maximum interval " + MaximumInterval.String() + " instead"}, diff --git a/collector/extension/solarwindsapmsettingsextension/factory.go b/collector/extension/solarwindsapmsettingsextension/factory.go index c4f795bdea..824eb3ca27 100644 --- a/collector/extension/solarwindsapmsettingsextension/factory.go +++ b/collector/extension/solarwindsapmsettingsextension/factory.go @@ -11,7 +11,7 @@ import ( ) const ( - DefaultInterval = time.Duration(10 * time.Second) + DefaultInterval = time.Duration(10) * time.Second ) func createDefaultConfig() component.Config {