-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: rework prometheus integration
The peers_total metric used to live deep down in libp2p, it is now nicely separated, and there's space for even more metrics. Tests are a good thing too. The Prometheus library is now gx'ed too. License: MIT Signed-off-by: Lars Gierth <[email protected]>
- Loading branch information
Lars Gierth
committed
Feb 18, 2016
1 parent
ef7b373
commit 91878d4
Showing
5 changed files
with
113 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package corehttp | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
|
||
prometheus "gx/ipfs/QmSH7Koc3CCaB8weo9aDE7YWQuGwAi5G7bqGvxatfux1Y5/prometheus" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
) | ||
|
||
// 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, prometheus.UninstrumentedHandler()) | ||
return mux, nil | ||
} | ||
} | ||
|
||
// This adds collection of net/http-related metrics | ||
func MetricsCollectionOption(handlerName string) ServeOption { | ||
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { | ||
childMux := http.NewServeMux() | ||
mux.HandleFunc("/", prometheus.InstrumentHandler(handlerName, childMux)) | ||
return childMux, nil | ||
} | ||
} | ||
|
||
var ( | ||
peersTotalMetric = prometheus.NewDesc( | ||
prometheus.BuildFQName("ipfs", "p2p", "peers_total"), | ||
"Number of connected peers", nil, nil) | ||
) | ||
|
||
type IpfsNodeCollector struct { | ||
Node *core.IpfsNode | ||
} | ||
|
||
func (_ IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- peersTotalMetric | ||
} | ||
|
||
func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) { | ||
ch <- prometheus.MustNewConstMetric( | ||
peersTotalMetric, | ||
prometheus.GaugeValue, | ||
c.PeersTotalValue(), | ||
) | ||
} | ||
|
||
func (c IpfsNodeCollector) PeersTotalValue() float64 { | ||
return float64(len(c.Node.PeerHost.Network().Conns())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package corehttp | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
core "github.com/ipfs/go-ipfs/core" | ||
context "golang.org/x/net/context" | ||
bhost "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/host/basic" | ||
inet "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/net" | ||
testutil "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/test/util" | ||
) | ||
|
||
// This test is based on go-libp2p/p2p/net/swarm.TestConnectednessCorrect | ||
// It builds 4 nodes and connects them, one being the sole center. | ||
// Then it checks that the center reports the correct number of peers. | ||
func TestPeersTotal(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
hosts := make([]*bhost.BasicHost, 4) | ||
for i := 0; i < 4; i++ { | ||
hosts[i] = testutil.GenHostSwarm(t, ctx) | ||
} | ||
|
||
dial := func(a, b inet.Network) { | ||
testutil.DivulgeAddresses(b, a) | ||
if _, err := a.DialPeer(ctx, b.LocalPeer()); err != nil { | ||
t.Fatalf("Failed to dial: %s", err) | ||
} | ||
} | ||
|
||
dial(hosts[0].Network(), hosts[1].Network()) | ||
dial(hosts[0].Network(), hosts[2].Network()) | ||
dial(hosts[0].Network(), hosts[3].Network()) | ||
|
||
// there's something wrong with dial, i think. it's not finishing | ||
// completely. there must be some async stuff. | ||
<-time.After(100 * time.Millisecond) | ||
|
||
node := &core.IpfsNode{PeerHost: hosts[0]} | ||
collector := IpfsNodeCollector{Node: node} | ||
actual := collector.PeersTotalValue() | ||
if actual != 3 { | ||
t.Fatalf("expected 3 peers, got %d", int(actual)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters