Skip to content

Commit

Permalink
chore(blocker): add metrics (#2576)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Oct 7, 2021
1 parent c84b047 commit 29eb941
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Blocker struct {
wakeupCh chan struct{}
quit chan struct{}
closeWg sync.WaitGroup
metrics metrics
}

func New(dis p2p.Blocklister, flagTimeout, blockDuration, wakeUpTime time.Duration, logger logging.Logger) *Blocker {
Expand All @@ -43,6 +44,7 @@ func New(dis p2p.Blocklister, flagTimeout, blockDuration, wakeUpTime time.Durati
quit: make(chan struct{}),
logger: logger,
closeWg: sync.WaitGroup{},
metrics: newMetrics(),
}

b.closeWg.Add(1)
Expand Down Expand Up @@ -76,6 +78,7 @@ func (b *Blocker) block() {
}

if !peer.blockAfter.IsZero() && time.Now().After(peer.blockAfter) {
b.metrics.Blocklist.Inc()
if err := b.disconnector.Blocklist(peer.addr, b.blockDuration, "blocker: flag timeout"); err != nil {
b.logger.Warningf("blocker: blocking peer %s failed: %v", peer.addr, err)
}
Expand All @@ -89,6 +92,7 @@ func (b *Blocker) Flag(addr swarm.Address) {
defer b.mux.Unlock()

if _, ok := b.peers[addr.ByteString()]; !ok {
b.metrics.Flag.Inc()
b.peers[addr.ByteString()] = &peer{
blockAfter: time.Now().Add(b.flagTimeout),
addr: addr,
Expand All @@ -100,6 +104,8 @@ func (b *Blocker) Unflag(addr swarm.Address) {
b.mux.Lock()
defer b.mux.Unlock()

b.metrics.Unflag.Inc()

delete(b.peers, addr.ByteString())
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/blocker/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package blocker

import (
m "github.com/ethersphere/bee/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)

type metrics struct {
Flag prometheus.Counter
Unflag prometheus.Counter
Blocklist prometheus.Counter
}

func newMetrics() metrics {
subsystem := "blocker"

return metrics{
Flag: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "flag",
Help: "The nubmer of times peers have been flagged.",
}),
Unflag: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "unflag",
Help: "The nubmer of times peers have been unflagged.",
}),
Blocklist: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "blocklist",
Help: "The nubmer of times peers have been blocklisted.",
}),
}
}

func (s *Blocker) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
}
2 changes: 1 addition & 1 deletion pkg/topology/kademlia/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ func newMetrics() metrics {

// Metrics returns set of prometheus collectors.
func (k *Kad) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(k.metrics)
return append(m.PrometheusCollectorsFromFields(k.metrics), k.blocker.Metrics()...)
}

0 comments on commit 29eb941

Please sign in to comment.