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"
|
|
|
|
|
2018-11-09 13:32:35 +00:00
|
|
|
hw "github.com/jtolds/monkit-hw"
|
2018-05-30 15:03:44 +01:00
|
|
|
"github.com/zeebo/admission/admproto"
|
2019-01-15 15:02:54 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-22 15:48:23 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-05-30 15:03:44 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2/environment"
|
2018-06-27 09:02:49 +01:00
|
|
|
|
2019-04-10 07:38:26 +01:00
|
|
|
"storj.io/storj/internal/version"
|
2019-05-31 23:47:48 +01:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2019-01-15 15:02:54 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-05-30 15:03:44 +01:00
|
|
|
"storj.io/storj/pkg/telemetry"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-24 21:04:29 +00:00
|
|
|
metricInterval = flag.Duration("metrics.interval", telemetry.DefaultInterval, "how frequently to send up telemetry")
|
|
|
|
metricCollector = flag.String("metrics.addr", "collectora.storj.io:9000", "address to send telemetry to")
|
|
|
|
metricApp = flag.String("metrics.app", filepath.Base(os.Args[0]), "application name for telemetry identification")
|
2019-05-31 23:47:48 +01:00
|
|
|
metricAppSuffix = flag.String("metrics.app-suffix", "-"+cfgstruct.DefaultsType(), "application suffix")
|
2018-05-30 15:03:44 +01:00
|
|
|
)
|
|
|
|
|
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-02-01 18:21:00 +00:00
|
|
|
func InitMetrics(ctx context.Context, r *monkit.Registry, instanceID string) (err error) {
|
2018-05-30 15:03:44 +01:00
|
|
|
if *metricCollector == "" || *metricInterval == 0 {
|
|
|
|
return Error.New("telemetry disabled")
|
|
|
|
}
|
2019-01-15 15:02:54 +00:00
|
|
|
if r == nil {
|
|
|
|
r = monkit.Default
|
|
|
|
}
|
|
|
|
if instanceID == "" {
|
|
|
|
instanceID = telemetry.DefaultInstanceID()
|
|
|
|
}
|
2018-05-30 15:03:44 +01:00
|
|
|
c, err := telemetry.NewClient(*metricCollector, telemetry.ClientOpts{
|
|
|
|
Interval: *metricInterval,
|
|
|
|
Application: *metricApp + *metricAppSuffix,
|
|
|
|
Instance: instanceID,
|
|
|
|
Registry: r,
|
|
|
|
FloatEncoding: admproto.Float32Encoding,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
environment.Register(r)
|
2018-06-04 17:07:57 +01:00
|
|
|
hw.Register(r)
|
2019-04-10 07:38:26 +01:00
|
|
|
r.ScopeNamed("env").Chain("version", monkit.StatSourceFunc(version.Build.Stats))
|
2018-05-30 15:03:44 +01:00
|
|
|
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.
|
|
|
|
func InitMetricsWithCertPath(ctx context.Context, r *monkit.Registry, certPath string) error {
|
|
|
|
var metricsID string
|
|
|
|
nodeID, err := identity.NodeIDFromCertPath(certPath)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Errorf("Could not read identity for telemetry setup: %v", err)
|
|
|
|
metricsID = "" // InitMetrics() will fill in a default value
|
|
|
|
} else {
|
|
|
|
metricsID = nodeID.String()
|
|
|
|
}
|
|
|
|
return InitMetrics(ctx, r, metricsID)
|
|
|
|
}
|