2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-30 15:03:44 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
hw "github.com/jtolds/monkit-hw/v2"
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/spacemonkeygo/monkit/v3/environment"
|
|
|
|
"github.com/zeebo/admission/v2/admproto"
|
2019-01-15 15:02:54 +00:00
|
|
|
"go.uber.org/zap"
|
2018-06-27 09:02:49 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
2019-05-31 23:47:48 +01:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2018-05-30 15:03:44 +01:00
|
|
|
"storj.io/storj/pkg/telemetry"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/version"
|
2018-05-30 15:03:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-06-24 23:45:37 +01:00
|
|
|
metricInterval = flag.Duration("metrics.interval", telemetry.DefaultInterval, "how frequently to send up telemetry")
|
|
|
|
metricCollector = flag.String("metrics.addr", flagDefault("", "collectora.storj.io:9000"), "address to send telemetry to")
|
|
|
|
metricApp = flag.String("metrics.app", filepath.Base(os.Args[0]), "application name for telemetry identification")
|
|
|
|
metricAppSuffix = flag.String("metrics.app-suffix", flagDefault("-dev", "-release"), "application suffix")
|
|
|
|
metricInstancePrefix = flag.String("metrics.instance-prefix", "", "instance id prefix")
|
2018-05-30 15:03:44 +01:00
|
|
|
)
|
|
|
|
|
2019-06-24 23:45:37 +01:00
|
|
|
const (
|
|
|
|
maxInstanceLength = 52
|
|
|
|
)
|
|
|
|
|
|
|
|
func flagDefault(dev, release string) string {
|
|
|
|
if cfgstruct.DefaultsType() == "release" {
|
|
|
|
return release
|
|
|
|
}
|
|
|
|
return dev
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:02:54 +00:00
|
|
|
// InitMetrics initializes telemetry reporting. Makes a telemetry.Client and calls
|
|
|
|
// its Run() method in a goroutine.
|
2019-07-31 15:38:44 +01:00
|
|
|
func InitMetrics(ctx context.Context, log *zap.Logger, r *monkit.Registry, instanceID string) (err error) {
|
2019-11-08 20:40:39 +00:00
|
|
|
if r == nil {
|
|
|
|
r = monkit.Default
|
|
|
|
}
|
|
|
|
environment.Register(r)
|
|
|
|
hw.Register(r)
|
|
|
|
r.ScopeNamed("env").Chain(monkit.StatSourceFunc(version.Build.Stats))
|
|
|
|
|
2019-09-10 08:32:53 +01:00
|
|
|
log = log.Named("telemetry")
|
2018-05-30 15:03:44 +01:00
|
|
|
if *metricCollector == "" || *metricInterval == 0 {
|
2019-09-10 08:32:53 +01:00
|
|
|
log.Info("disabled")
|
|
|
|
return nil
|
2018-05-30 15:03:44 +01:00
|
|
|
}
|
2019-11-08 20:40:39 +00:00
|
|
|
|
2019-01-15 15:02:54 +00:00
|
|
|
if instanceID == "" {
|
|
|
|
instanceID = telemetry.DefaultInstanceID()
|
|
|
|
}
|
2019-06-24 23:45:37 +01:00
|
|
|
instanceID = *metricInstancePrefix + instanceID
|
|
|
|
if len(instanceID) > maxInstanceLength {
|
|
|
|
instanceID = instanceID[:maxInstanceLength]
|
|
|
|
}
|
2019-07-31 15:38:44 +01:00
|
|
|
c, err := telemetry.NewClient(log, *metricCollector, telemetry.ClientOpts{
|
2018-05-30 15:03:44 +01:00
|
|
|
Interval: *metricInterval,
|
|
|
|
Application: *metricApp + *metricAppSuffix,
|
|
|
|
Instance: instanceID,
|
|
|
|
Registry: r,
|
|
|
|
FloatEncoding: admproto.Float32Encoding,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go c.Run(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-15 15:02:54 +00:00
|
|
|
|
|
|
|
// InitMetricsWithCertPath initializes telemetry reporting, using the node ID
|
|
|
|
// corresponding to the given certificate as the telemetry instance ID.
|
2019-07-31 15:38:44 +01:00
|
|
|
func InitMetricsWithCertPath(ctx context.Context, log *zap.Logger, r *monkit.Registry, certPath string) error {
|
2019-01-15 15:02:54 +00:00
|
|
|
var metricsID string
|
|
|
|
nodeID, err := identity.NodeIDFromCertPath(certPath)
|
|
|
|
if err != nil {
|
2019-07-31 15:38:44 +01:00
|
|
|
log.Sugar().Errorf("Could not read identity for telemetry setup: %v", err)
|
2019-01-15 15:02:54 +00:00
|
|
|
metricsID = "" // InitMetrics() will fill in a default value
|
|
|
|
} else {
|
|
|
|
metricsID = nodeID.String()
|
|
|
|
}
|
2019-07-31 15:38:44 +01:00
|
|
|
return InitMetrics(ctx, log, r, metricsID)
|
2019-01-15 15:02:54 +00:00
|
|
|
}
|