@cryptotaxi247 / kubo / commits / 27b046f98

Move register exporter to metrics file

Marco Munizaga committed Aug 12, 2022 at 08:31 UTC 27b046f98e0de1ffb5dc46696e3895f7c98edbcc
3 files changed +26 -11
cmd/ipfs/daemon.go
+1
@@ -649,6 +649,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
649 var opts = []corehttp.ServeOption{
650 corehttp.MetricsCollectionOption("api"),
651 corehttp.MetricsOpenCensusCollectionOption(),
652 + corehttp.MetricsOpenCensusDefaultPrometheusRegistry(),
653 corehttp.CheckVersionOption(),
654 corehttp.CommandsOption(*cctx),
655 corehttp.WebUIOption,
core/corehttp/metrics.go
+24
@@ -51,6 +51,30 @@ func MetricsOpenCensusCollectionOption() ServeOption {
51 }
52 }
53
54 +// MetricsOpenCensusDefaultPrometheusRegistry registers the default prometheus
55 +// registry as an exporter to OpenCensus metrics. This means that OpenCensus
56 +// metrics will show up in the prometheus metrics endpoint
57 +func MetricsOpenCensusDefaultPrometheusRegistry() ServeOption {
58 + return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
59 + log.Info("Init OpenCensus with default prometheus registry")
60 +
61 + pe, err := ocprom.NewExporter(ocprom.Options{
62 + Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
63 + OnError: func(err error) {
64 + log.Errorw("OC default registry ERROR", "error", err)
65 + },
66 + })
67 + if err != nil {
68 + return nil, err
69 + }
70 +
71 + // register prometheus with opencensus
72 + view.RegisterExporter(pe)
73 +
74 + return mux, nil
75 + }
76 +}
77 +
78 // MetricsCollectionOption adds collection of net/http-related metrics.
79 func MetricsCollectionOption(handlerName string) ServeOption {
80 return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
core/node/libp2p/rcmgr.go
+1 -11
@@ -7,7 +7,6 @@ import (
7 "path/filepath"
8 "strings"
9
10 - ocprom "contrib.go.opencensus.io/exporter/prometheus"
10 "github.com/benbjohnson/clock"
11 logging "github.com/ipfs/go-log/v2"
12 config "github.com/ipfs/kubo/config"
@@ -21,7 +20,6 @@ import (
20 rcmgr "github.com/libp2p/go-libp2p-resource-manager"
21 rcmgrObs "github.com/libp2p/go-libp2p-resource-manager/obs"
22 "github.com/multiformats/go-multiaddr"
24 - "github.com/prometheus/client_golang/prometheus"
23 "go.opencensus.io/stats/view"
24
25 "go.uber.org/fx"
@@ -86,18 +84,10 @@ func ResourceManager(cfg config.SwarmConfig) interface{} {
84 log.Infof("Setting allowlist to: %v", mas)
85 }
86
89 - // Hook up the trace reporter metrics
90 - err = view.Register(rcmgrObs.DefaultViews...)
87 + view.Register(rcmgrObs.DefaultViews...)
88 if err != nil {
89 return nil, opts, fmt.Errorf("registering rcmgr obs views: %w", err)
90 }
94 - _, err = ocprom.NewExporter(ocprom.Options{
95 - Registry: prometheus.DefaultRegisterer.(*prometheus.Registry),
96 - Namespace: "rcmgr_trace_metrics",
97 - })
98 - if err != nil {
99 - return nil, opts, fmt.Errorf("creating new prom exporter: %w", err)
100 - }
91
92 if os.Getenv("LIBP2P_DEBUG_RCMGR") != "" {
93 traceFilePath := filepath.Join(repoPath, NetLimitTraceFilename)