2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-07 19:03:40 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
2018-05-30 15:03:44 +01:00
|
|
|
"log"
|
2018-05-07 19:03:40 +01:00
|
|
|
"math/rand"
|
2018-05-30 15:03:44 +01:00
|
|
|
"net"
|
2018-05-07 19:03:40 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-05-30 15:03:44 +01:00
|
|
|
const (
|
|
|
|
unknownInstanceID = "unknown"
|
|
|
|
)
|
|
|
|
|
2018-05-07 19:03:40 +01:00
|
|
|
func jitter(t time.Duration) time.Duration {
|
|
|
|
nanos := rand.NormFloat64()*float64(t/4) + float64(t)
|
|
|
|
if nanos <= 0 {
|
|
|
|
nanos = 1
|
|
|
|
}
|
|
|
|
return time.Duration(nanos)
|
|
|
|
}
|
2018-05-30 15:03:44 +01:00
|
|
|
|
|
|
|
// DefaultInstanceID will return the first non-nil mac address if possible,
|
|
|
|
// unknown otherwise.
|
|
|
|
func DefaultInstanceID() string {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to determine default instance id: %v", err)
|
|
|
|
return unknownInstanceID
|
|
|
|
}
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
if iface.HardwareAddr != nil {
|
|
|
|
return iface.HardwareAddr.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unknownInstanceID
|
|
|
|
}
|