-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
utils.go
56 lines (49 loc) · 1.64 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package host // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/host"
import (
"context"
"hash/fnv"
"os"
"time"
)
const (
rootfs = "/rootfs" // the root directory "/" is mounted as "/rootfs" in container
hostProc = rootfs + "/proc" // "/rootfs/proc" in container refers to the host proc directory "/proc"
hostMounts = hostProc + "/mounts" // "/rootfs/proc/mounts" in container refers to "/proc/mounts" in the host
)
func hostJitter(max time.Duration) time.Duration {
hostName, err := os.Hostname()
if err != nil {
hostName = "Unknown"
}
hash := fnv.New64()
hash.Write([]byte(hostName))
// Right shift the uint64 hash by one to make sure the jitter duration is always positive
hostSleepJitter := time.Duration(int64(hash.Sum64()>>1)) % max
return hostSleepJitter
}
// execute the refresh() function periodically with the given refresh interval
// until shouldRefresh() return false or the context is canceled
func RefreshUntil(ctx context.Context, refresh func(context.Context), refreshInterval time.Duration,
shouldRefresh func() bool, maxJitterTime time.Duration) {
if maxJitterTime > 0 {
// add some sleep jitter to prevent a large number of receivers calling the ec2 api at the same time
time.Sleep(hostJitter(maxJitterTime))
}
// initial refresh
refresh(ctx)
refreshTicker := time.NewTicker(refreshInterval)
defer refreshTicker.Stop()
for {
select {
case <-refreshTicker.C:
if !shouldRefresh() {
return
}
refresh(ctx)
case <-ctx.Done():
return
}
}
}