ipfs-cluster/observations/setup.go

160 lines
4.4 KiB
Go
Raw Normal View History

package observations
import (
"context"
"expvar"
"net/http"
"net/http/pprof"
rpc "github.com/libp2p/go-libp2p-gorpc"
manet "github.com/multiformats/go-multiaddr/net"
2019-04-29 16:57:13 +00:00
"contrib.go.opencensus.io/exporter/jaeger"
"contrib.go.opencensus.io/exporter/prometheus"
ocgorpc "github.com/lanzafame/go-libp2p-ocgorpc"
prom "github.com/prometheus/client_golang/prometheus"
Dependency upgrades (#1378) * build(deps): bump github.com/multiformats/go-multiaddr-dns Bumps [github.com/multiformats/go-multiaddr-dns](https://github.com/multiformats/go-multiaddr-dns) from 0.2.0 to 0.3.1. - [Release notes](https://github.com/multiformats/go-multiaddr-dns/releases) - [Commits](https://github.com/multiformats/go-multiaddr-dns/compare/v0.2.0...v0.3.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/hashicorp/go-hclog from 0.15.0 to 0.16.0 Bumps [github.com/hashicorp/go-hclog](https://github.com/hashicorp/go-hclog) from 0.15.0 to 0.16.0. - [Release notes](https://github.com/hashicorp/go-hclog/releases) - [Commits](https://github.com/hashicorp/go-hclog/compare/v0.15.0...v0.16.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/ipfs/go-unixfs from 0.2.4 to 0.2.5 Bumps [github.com/ipfs/go-unixfs](https://github.com/ipfs/go-unixfs) from 0.2.4 to 0.2.5. - [Release notes](https://github.com/ipfs/go-unixfs/releases) - [Commits](https://github.com/ipfs/go-unixfs/compare/v0.2.4...v0.2.5) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/libp2p/go-libp2p-peerstore Bumps [github.com/libp2p/go-libp2p-peerstore](https://github.com/libp2p/go-libp2p-peerstore) from 0.2.6 to 0.2.7. - [Release notes](https://github.com/libp2p/go-libp2p-peerstore/releases) - [Commits](https://github.com/libp2p/go-libp2p-peerstore/compare/v0.2.6...v0.2.7) Signed-off-by: dependabot[bot] <support@github.com> * build(deps): bump go.uber.org/multierr from 1.6.0 to 1.7.0 Bumps [go.uber.org/multierr](https://github.com/uber-go/multierr) from 1.6.0 to 1.7.0. - [Release notes](https://github.com/uber-go/multierr/releases) - [Changelog](https://github.com/uber-go/multierr/blob/master/CHANGELOG.md) - [Commits](https://github.com/uber-go/multierr/compare/v1.6.0...v1.7.0) Signed-off-by: dependabot[bot] <support@github.com> * Chore: update deps * Update changelog * Update to go1.16. Downgrade unixfs. * go mod tidy * travis: use go install * golint no more * Update configuration for dependabot * Fix wrong dependabot config * dependabot * Revert update of go-unixfs * Dependency upgrades * Bump github.com/libp2p/go-libp2p-gorpc from 0.1.2 to 0.1.3 Bumps [github.com/libp2p/go-libp2p-gorpc](https://github.com/libp2p/go-libp2p-gorpc) from 0.1.2 to 0.1.3. - [Release notes](https://github.com/libp2p/go-libp2p-gorpc/releases) - [Commits](https://github.com/libp2p/go-libp2p-gorpc/compare/v0.1.2...v0.1.3) --- updated-dependencies: - dependency-name: github.com/libp2p/go-libp2p-gorpc dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Fix deprecated objects with prometheus Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-07-06 12:06:04 +00:00
"github.com/prometheus/client_golang/prometheus/collectors"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opencensus.io/zpages"
)
// SetupMetrics configures and starts stats tooling,
// if enabled.
func SetupMetrics(cfg *MetricsConfig) error {
if cfg.EnableStats {
logger.Infof("stats collection enabled on %s", cfg.PrometheusEndpoint)
return setupMetrics(cfg)
}
return nil
}
// JaegerTracer implements ipfscluster.Tracer.
type JaegerTracer struct {
jaeger *jaeger.Exporter
}
// SetClient no-op.
func (t *JaegerTracer) SetClient(*rpc.Client) {}
// Shutdown the tracer and flush any remaining traces.
func (t *JaegerTracer) Shutdown(context.Context) error {
// nil check for testing, where tracer may not be configured
if t != (*JaegerTracer)(nil) && t.jaeger != nil {
t.jaeger.Flush()
}
return nil
}
// SetupTracing configures and starts tracing tooling,
// if enabled.
func SetupTracing(cfg *TracingConfig) (*JaegerTracer, error) {
if !cfg.EnableTracing {
return nil, nil
}
logger.Info("tracing enabled...")
je, err := setupTracing(cfg)
if err != nil {
return nil, err
}
return &JaegerTracer{je}, nil
}
func setupMetrics(cfg *MetricsConfig) error {
// setup Prometheus
registry := prom.NewRegistry()
Dependency upgrades (#1378) * build(deps): bump github.com/multiformats/go-multiaddr-dns Bumps [github.com/multiformats/go-multiaddr-dns](https://github.com/multiformats/go-multiaddr-dns) from 0.2.0 to 0.3.1. - [Release notes](https://github.com/multiformats/go-multiaddr-dns/releases) - [Commits](https://github.com/multiformats/go-multiaddr-dns/compare/v0.2.0...v0.3.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/hashicorp/go-hclog from 0.15.0 to 0.16.0 Bumps [github.com/hashicorp/go-hclog](https://github.com/hashicorp/go-hclog) from 0.15.0 to 0.16.0. - [Release notes](https://github.com/hashicorp/go-hclog/releases) - [Commits](https://github.com/hashicorp/go-hclog/compare/v0.15.0...v0.16.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/ipfs/go-unixfs from 0.2.4 to 0.2.5 Bumps [github.com/ipfs/go-unixfs](https://github.com/ipfs/go-unixfs) from 0.2.4 to 0.2.5. - [Release notes](https://github.com/ipfs/go-unixfs/releases) - [Commits](https://github.com/ipfs/go-unixfs/compare/v0.2.4...v0.2.5) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * build(deps): bump github.com/libp2p/go-libp2p-peerstore Bumps [github.com/libp2p/go-libp2p-peerstore](https://github.com/libp2p/go-libp2p-peerstore) from 0.2.6 to 0.2.7. - [Release notes](https://github.com/libp2p/go-libp2p-peerstore/releases) - [Commits](https://github.com/libp2p/go-libp2p-peerstore/compare/v0.2.6...v0.2.7) Signed-off-by: dependabot[bot] <support@github.com> * build(deps): bump go.uber.org/multierr from 1.6.0 to 1.7.0 Bumps [go.uber.org/multierr](https://github.com/uber-go/multierr) from 1.6.0 to 1.7.0. - [Release notes](https://github.com/uber-go/multierr/releases) - [Changelog](https://github.com/uber-go/multierr/blob/master/CHANGELOG.md) - [Commits](https://github.com/uber-go/multierr/compare/v1.6.0...v1.7.0) Signed-off-by: dependabot[bot] <support@github.com> * Chore: update deps * Update changelog * Update to go1.16. Downgrade unixfs. * go mod tidy * travis: use go install * golint no more * Update configuration for dependabot * Fix wrong dependabot config * dependabot * Revert update of go-unixfs * Dependency upgrades * Bump github.com/libp2p/go-libp2p-gorpc from 0.1.2 to 0.1.3 Bumps [github.com/libp2p/go-libp2p-gorpc](https://github.com/libp2p/go-libp2p-gorpc) from 0.1.2 to 0.1.3. - [Release notes](https://github.com/libp2p/go-libp2p-gorpc/releases) - [Commits](https://github.com/libp2p/go-libp2p-gorpc/compare/v0.1.2...v0.1.3) --- updated-dependencies: - dependency-name: github.com/libp2p/go-libp2p-gorpc dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * Fix deprecated objects with prometheus Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-07-06 12:06:04 +00:00
goCollector := collectors.NewGoCollector()
procCollector := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
registry.MustRegister(goCollector, procCollector)
pe, err := prometheus.NewExporter(prometheus.Options{
Namespace: "ipfscluster",
Registry: registry,
})
if err != nil {
return err
}
// register prometheus with opencensus
view.RegisterExporter(pe)
view.SetReportingPeriod(cfg.ReportingInterval)
// register the metrics views of interest
if err := view.Register(DefaultViews...); err != nil {
return err
}
if err := view.Register(
ochttp.ClientCompletedCount,
ochttp.ClientRoundtripLatencyDistribution,
ochttp.ClientReceivedBytesDistribution,
ochttp.ClientSentBytesDistribution,
); err != nil {
return err
}
if err := view.Register(
ochttp.ServerRequestCountView,
ochttp.ServerRequestBytesView,
ochttp.ServerResponseBytesView,
ochttp.ServerLatencyView,
ochttp.ServerRequestCountByMethod,
ochttp.ServerResponseCountByStatusCode,
); err != nil {
return err
}
if err := view.Register(ocgorpc.DefaultServerViews...); err != nil {
return err
}
_, promAddr, err := manet.DialArgs(cfg.PrometheusEndpoint)
if err != nil {
return err
}
go func() {
mux := http.NewServeMux()
zpages.Handle(mux, "/debug")
mux.Handle("/metrics", pe)
mux.Handle("/debug/vars", expvar.Handler())
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/mutex", pprof.Handler("mutex"))
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
if err := http.ListenAndServe(promAddr, mux); err != nil {
logger.Fatalf("Failed to run Prometheus /metrics endpoint: %v", err)
}
}()
return nil
}
// setupTracing configures a OpenCensus Tracing exporter for Jaeger.
func setupTracing(cfg *TracingConfig) (*jaeger.Exporter, error) {
_, agentAddr, err := manet.DialArgs(cfg.JaegerAgentEndpoint)
if err != nil {
return nil, err
}
// setup Jaeger
je, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: agentAddr,
Process: jaeger.Process{
ServiceName: cfg.ServiceName + "-" + cfg.ClusterPeername,
Tags: []jaeger.Tag{
jaeger.StringTag("cluster_id", cfg.ClusterID),
},
},
})
if err != nil {
return nil, err
}
// register jaeger with opencensus
trace.RegisterExporter(je)
// configure tracing
trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(cfg.SamplingProb)})
return je, nil
}