Skip to content

Commit

Permalink
feat: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss committed Nov 7, 2023
1 parent 420b89b commit 5f45d3c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 1 addition & 3 deletions cmd/poller/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func New(c, n string, o *options.Options, p conf.Exporter, params *conf.Poller)
labels := params.Labels
if labels != nil {
for _, labelPtr := range *labels {
for key, value := range labelPtr {
abc.Metadata.SetGlobalLabel(key, value)
}
abc.Metadata.SetGlobalLabels(labelPtr)
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,8 @@ func (p *Poller) loadMetadata() {
labels := p.params.Labels
if labels != nil {
for _, labelPtr := range *labels {
for key, value := range labelPtr {
p.metadata.SetGlobalLabel(key, value)
p.status.SetGlobalLabel(key, value)
}
p.metadata.SetGlobalLabels(labelPtr)
p.status.SetGlobalLabels(labelPtr)
}
}
p.status.SetExportOptions(matrix.DefaultExportOptions())
Expand Down
27 changes: 26 additions & 1 deletion cmd/poller/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
package schedule

import (
"fmt"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"math/rand"
"os"
"sync"
"time"
)

Expand All @@ -46,6 +50,18 @@ type Task struct {
identifier string // optional additional information about schedule i.e. collector name
}

var (
mu sync.Mutex
r *rand.Rand
disableRandomSchedulingFlag string
)

func init() {
s := rand.NewSource(time.Now().UnixNano())
r = rand.New(s)

Check failure on line 61 in cmd/poller/schedule/schedule.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
disableRandomSchedulingFlag = os.Getenv("HARVEST_DISABLE_RANDOM_SCHEDULE")
}

// Start marks the task as started by updating timer
// Use this method if you are executing the task yourself and you need to register
// when task started. If the task has a pointer to the executing function, use
Expand Down Expand Up @@ -176,7 +192,16 @@ func (s *Schedule) NewTask(n string, i time.Duration, f func() (map[string]*matr
t := &Task{Name: n, interval: i, foo: f, identifier: identifier}
s.cachedInterval[n] = t.interval // remember normal interval of task
if runNow {
t.timer = time.Now().Add(-i) // set to run immediately
if n == "data" && disableRandomSchedulingFlag == "" {
mu.Lock() // Only allow one goroutine to generate a random number at a time
randomDelay := time.Duration(r.Int63n(int64(i)))
mu.Unlock()
fmt.Printf("schedule: %v \n", i.Seconds())
fmt.Println(randomDelay.Seconds())
t.timer = time.Now().Add(randomDelay)
} else {
t.timer = time.Now().Add(-i) // set to run immediately
}
} else {
t.timer = time.Now().Add(0) // run after interval has elapsed
}
Expand Down

0 comments on commit 5f45d3c

Please sign in to comment.