Skip to content

Commit

Permalink
Export Prometheus via OpenCensus and register the DHT metrics
Browse files Browse the repository at this point in the history
This uses OC to export metrics via Prometheus. This also means that other OC metrics, such as the ones in go-libp2p-kad-dht can be exposed (via Prometheus, and whatever other exporters are used). I did some checks that no existing metrics move as a result.
  • Loading branch information
anacrolix committed Apr 23, 2019
1 parent b16f08d commit 1963005
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
15 changes: 14 additions & 1 deletion core/corehttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ import (

core "github.com/ipfs/go-ipfs/core"

dhtMetrics "github.com/libp2p/go-libp2p-kad-dht/metrics"
prometheus "github.com/prometheus/client_golang/prometheus"
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
ocProm "go.opencensus.io/exporter/prometheus"
"go.opencensus.io/stats/view"
)

// This adds the scraping endpoint which Prometheus uses to fetch metrics.
func MetricsScrapingOption(path string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.Handle(path, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
pe, err := ocProm.NewExporter(ocProm.Options{
Registry: prometheus.DefaultGatherer.(*prometheus.Registry),
})
if err != nil {
return mux, err
}
view.RegisterExporter(pe)
if err := view.Register(dhtMetrics.Views...); err != nil {
return mux, err
}
mux.Handle(path, pe)
return mux, nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require (
github.com/libp2p/go-libp2p-crypto v0.0.1
github.com/libp2p/go-libp2p-host v0.0.1
github.com/libp2p/go-libp2p-interface-connmgr v0.0.1
github.com/libp2p/go-libp2p-kad-dht v0.0.9
github.com/libp2p/go-libp2p-kad-dht v0.0.10-0.20190423053654-33dc9eda5233
github.com/libp2p/go-libp2p-kbucket v0.1.1
github.com/libp2p/go-libp2p-loggables v0.0.1
github.com/libp2p/go-libp2p-metrics v0.0.1
Expand Down Expand Up @@ -110,6 +110,7 @@ require (
github.com/whyrusleeping/go-sysinfo v0.0.0-20190219211824-4a357d4b90b1
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c
go.opencensus.io v0.20.2
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/dig v1.7.0 // indirect
go.uber.org/fx v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ github.com/libp2p/go-libp2p-interface-connmgr v0.0.1/go.mod h1:GarlRLH0LdeWcLnYM
github.com/libp2p/go-libp2p-interface-pnet v0.0.1 h1:7GnzRrBTJHEsofi1ahFdPN9Si6skwXQE9UqR2S+Pkh8=
github.com/libp2p/go-libp2p-interface-pnet v0.0.1/go.mod h1:el9jHpQAXK5dnTpKA4yfCNBZXvrzdOU75zz+C6ryp3k=
github.com/libp2p/go-libp2p-kad-dht v0.0.4/go.mod h1:oaBflOQcuC8H+SVV0YN26H6AS+wcUEJyjUGV66vXuSY=
github.com/libp2p/go-libp2p-kad-dht v0.0.9 h1:lgt0TuhnZieDg7jd6e4+aZIL11+Z9MLz7h66duB04Hw=
github.com/libp2p/go-libp2p-kad-dht v0.0.9/go.mod h1:RVllrB76xoaayqqBkLmXT8iIkMRNfLWSBuqAggP7W00=
github.com/libp2p/go-libp2p-kad-dht v0.0.10-0.20190423053654-33dc9eda5233 h1:XFz2azHEr07wTbCuDxgdOUVAIfEV17zqfLe+uN+yU8M=
github.com/libp2p/go-libp2p-kad-dht v0.0.10-0.20190423053654-33dc9eda5233/go.mod h1:RVllrB76xoaayqqBkLmXT8iIkMRNfLWSBuqAggP7W00=
github.com/libp2p/go-libp2p-kbucket v0.0.1 h1:7H5hM851hkEpLOFjrVNSrrxo6J4bWrUQxxv+z1JW9xk=
github.com/libp2p/go-libp2p-kbucket v0.0.1/go.mod h1:Y0iQDHRTk/ZgM8PC4jExoF+E4j+yXWwRkdldkMa5Xm4=
github.com/libp2p/go-libp2p-kbucket v0.1.1 h1:ZrvW3qCM+lAuv7nrNts/zfEiClq+GZe8OIzX4Vb3Dwo=
Expand Down

2 comments on commit 1963005

@GitCop
Copy link

@GitCop GitCop commented on 1963005 Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

@GitCop
Copy link

@GitCop GitCop commented on 1963005 Apr 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were the following issues with your Pull Request

We ask for a few features in the commit message for Open Source licensing hygiene and commit message clarity.
git commit --amend can often help you quickly improve the commit message.
Guidelines and a script are available to help in the long run.
Your feedback on GitCop is welcome on this issue.


This message was auto-generated by https://gitcop.com

Please sign in to comment.