ipfs-cluster/observations/config.go
Hector Sanjuan 4ea830f74e Feat: ipfs-cluster-follow
This adds a new cluster command: ipfs-cluster-follow.

This command allows initializing and running follower peers as configured by a
remote-source configuration. The command can list configured peers
and obtain information for each of them.

Peers are launched with the rest API listening on a local unix socket. The
command can be run to list the items in the cluster pinset using this
endpoint. Alternatively, if no socket is present, the peer will be assumed to
be offline and the pin list will be directly read from the datastore.

Cluster peers launched with this command (and their configurations) are
compatible with ipfs-cluster-ctl and ipfs-cluster-service. We purposely do not
support most configuration options here. Using ipfs-cluster-ctl or launching
the peers using ipfs-cluster-service is always an option when the usecase
deviates from that supported by ipfs-cluster-follow.

Examples:

$ ipfs-cluster-follow -> list configured peers
$ ipfs-cluster-follow --help
$ ipfs-cluster-follow <clusterName> init <url>
$ ipfs-cluster-follow <clusterName> info
$ ipfs-cluster-follow <clusterName> run
$ ipfs-cluster-follow <clusterName> list
2019-12-07 15:38:59 +01:00

260 lines
6.7 KiB
Go

package observations
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/kelseyhightower/envconfig"
ma "github.com/multiformats/go-multiaddr"
"github.com/ipfs/ipfs-cluster/config"
)
const metricsConfigKey = "metrics"
const tracingConfigKey = "tracing"
const metricsEnvConfigKey = "cluster_metrics"
const tracingEnvConfigKey = "cluster_tracing"
// Default values for this Config.
const (
DefaultEnableStats = false
DefaultPrometheusEndpoint = "/ip4/127.0.0.1/tcp/8888"
DefaultReportingInterval = 2 * time.Second
DefaultEnableTracing = false
DefaultJaegerAgentEndpoint = "/ip4/0.0.0.0/udp/6831"
DefaultSamplingProb = 0.3
DefaultServiceName = "cluster-daemon"
)
// MetricsConfig configures metrics collection.
type MetricsConfig struct {
config.Saver
EnableStats bool
PrometheusEndpoint ma.Multiaddr
ReportingInterval time.Duration
}
type jsonMetricsConfig struct {
EnableStats bool `json:"enable_stats"`
PrometheusEndpoint string `json:"prometheus_endpoint"`
ReportingInterval string `json:"reporting_interval"`
}
// ConfigKey provides a human-friendly identifier for this type of Config.
func (cfg *MetricsConfig) ConfigKey() string {
return metricsConfigKey
}
// Default sets the fields of this Config to sensible values.
func (cfg *MetricsConfig) Default() error {
cfg.EnableStats = DefaultEnableStats
endpointAddr, _ := ma.NewMultiaddr(DefaultPrometheusEndpoint)
cfg.PrometheusEndpoint = endpointAddr
cfg.ReportingInterval = DefaultReportingInterval
return nil
}
// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *MetricsConfig) ApplyEnvVars() error {
jcfg := cfg.toJSONConfig()
err := envconfig.Process(metricsEnvConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
}
// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *MetricsConfig) Validate() error {
if cfg.EnableStats {
if cfg.PrometheusEndpoint == nil {
return errors.New("metrics.prometheus_endpoint is undefined")
}
if cfg.ReportingInterval < 0 {
return errors.New("metrics.reporting_interval is invalid")
}
}
return nil
}
// LoadJSON sets the fields of this Config to the values defined by the JSON
// representation of it, as generated by ToJSON.
func (cfg *MetricsConfig) LoadJSON(raw []byte) error {
jcfg := &jsonMetricsConfig{}
err := json.Unmarshal(raw, jcfg)
if err != nil {
logger.Error("Error unmarshaling observations config")
return err
}
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *MetricsConfig) applyJSONConfig(jcfg *jsonMetricsConfig) error {
err := cfg.loadMetricsOptions(jcfg)
if err != nil {
return err
}
return cfg.Validate()
}
func (cfg *MetricsConfig) loadMetricsOptions(jcfg *jsonMetricsConfig) error {
cfg.EnableStats = jcfg.EnableStats
endpointAddr, err := ma.NewMultiaddr(jcfg.PrometheusEndpoint)
if err != nil {
return fmt.Errorf("loadMetricsOptions: PrometheusEndpoint multiaddr: %v", err)
}
cfg.PrometheusEndpoint = endpointAddr
return config.ParseDurations(
metricsConfigKey,
&config.DurationOpt{
Duration: jcfg.ReportingInterval,
Dst: &cfg.ReportingInterval,
Name: "metrics.reporting_interval",
},
)
}
// ToJSON generates a human-friendly JSON representation of this Config.
func (cfg *MetricsConfig) ToJSON() ([]byte, error) {
jcfg := cfg.toJSONConfig()
return config.DefaultJSONMarshal(jcfg)
}
func (cfg *MetricsConfig) toJSONConfig() *jsonMetricsConfig {
return &jsonMetricsConfig{
EnableStats: cfg.EnableStats,
PrometheusEndpoint: cfg.PrometheusEndpoint.String(),
ReportingInterval: cfg.ReportingInterval.String(),
}
}
// TracingConfig configures tracing.
type TracingConfig struct {
config.Saver
EnableTracing bool
JaegerAgentEndpoint ma.Multiaddr
SamplingProb float64
ServiceName string
ClusterID string
ClusterPeername string
}
type jsonTracingConfig struct {
EnableTracing bool `json:"enable_tracing"`
JaegerAgentEndpoint string `json:"jaeger_agent_endpoint"`
SamplingProb float64 `json:"sampling_prob"`
ServiceName string `json:"service_name"`
}
// ConfigKey provides a human-friendly identifier for this type of Config.
func (cfg *TracingConfig) ConfigKey() string {
return tracingConfigKey
}
// Default sets the fields of this Config to sensible values.
func (cfg *TracingConfig) Default() error {
cfg.EnableTracing = DefaultEnableTracing
agentAddr, _ := ma.NewMultiaddr(DefaultJaegerAgentEndpoint)
cfg.JaegerAgentEndpoint = agentAddr
cfg.SamplingProb = DefaultSamplingProb
cfg.ServiceName = DefaultServiceName
return nil
}
// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *TracingConfig) ApplyEnvVars() error {
jcfg := cfg.toJSONConfig()
err := envconfig.Process(tracingEnvConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
}
// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *TracingConfig) Validate() error {
if cfg.EnableTracing {
if cfg.JaegerAgentEndpoint == nil {
return errors.New("tracing.jaeger_agent_endpoint is undefined")
}
if cfg.SamplingProb < 0 {
return errors.New("tracing.sampling_prob is invalid")
}
}
return nil
}
// LoadJSON sets the fields of this Config to the values defined by the JSON
// representation of it, as generated by ToJSON.
func (cfg *TracingConfig) LoadJSON(raw []byte) error {
jcfg := &jsonTracingConfig{}
err := json.Unmarshal(raw, jcfg)
if err != nil {
logger.Error("Error unmarshaling observations config")
return err
}
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *TracingConfig) applyJSONConfig(jcfg *jsonTracingConfig) error {
err := cfg.loadTracingOptions(jcfg)
if err != nil {
return err
}
return cfg.Validate()
}
func (cfg *TracingConfig) loadTracingOptions(jcfg *jsonTracingConfig) error {
cfg.EnableTracing = jcfg.EnableTracing
agentAddr, err := ma.NewMultiaddr(jcfg.JaegerAgentEndpoint)
if err != nil {
return fmt.Errorf("loadTracingOptions: JaegerAgentEndpoint multiaddr: %v", err)
}
cfg.JaegerAgentEndpoint = agentAddr
cfg.SamplingProb = jcfg.SamplingProb
cfg.ServiceName = jcfg.ServiceName
return nil
}
// ToJSON generates a human-friendly JSON representation of this Config.
func (cfg *TracingConfig) ToJSON() ([]byte, error) {
jcfg := cfg.toJSONConfig()
return config.DefaultJSONMarshal(jcfg)
}
func (cfg *TracingConfig) toJSONConfig() *jsonTracingConfig {
return &jsonTracingConfig{
EnableTracing: cfg.EnableTracing,
JaegerAgentEndpoint: cfg.JaegerAgentEndpoint.String(),
SamplingProb: cfg.SamplingProb,
ServiceName: cfg.ServiceName,
}
}