Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrytfleung committed Jan 29, 2024
1 parent 248d562 commit 2df7246
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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,
},
},
{
id: component.NewIDWithName(metadata.Type, "2"),
expected: &Config{
Endpoint: "0.0.0.0:1234",
Key: "something",
Interval: time.Duration(10 * time.Second),
Interval: time.Duration(10) * time.Second,
},
},
}
Expand Down
31 changes: 23 additions & 8 deletions collector/extension/solarwindsapmsettingsextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func TestCreateExtension(t *testing.T) {
{
name: "default",
cfg: &Config{
Interval: time.Duration(10 * time.Second),
Interval: time.Duration(10) * time.Second,
},
},
{
name: "anything",
cfg: &Config{
Endpoint: "0.0.0.0:1234",
Key: "something",
Interval: time.Duration(10 * time.Second),
Interval: time.Duration(10) * time.Second,
},
},
}
Expand Down Expand Up @@ -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{},
Expand Down Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
DefaultInterval = time.Duration(10 * time.Second)
DefaultInterval = time.Duration(10) * time.Second
)

func createDefaultConfig() component.Config {
Expand Down

0 comments on commit 2df7246

Please sign in to comment.