Skip to content

Commit

Permalink
feat: add basic metrics for sharky (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci authored and alok committed Jan 27, 2022
1 parent e656c1d commit d43925e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
62 changes: 62 additions & 0 deletions pkg/sharky/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 sharky

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

// metrics groups sharky related prometheus counters.
type metrics struct {
TotalWriteCalls prometheus.Counter
TotalWriteCallsErr prometheus.Counter
TotalReadCalls prometheus.Counter
TotalReadCallsErr prometheus.Counter
ShardCount prometheus.Gauge
}

// newMetrics is a convenient constructor for creating new metrics.
func newMetrics() metrics {
const subsystem = "sharky"

return metrics{
TotalWriteCalls: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_write_calls",
Help: "The total write calls made.",
}),
TotalWriteCallsErr: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_write_calls_err",
Help: "The total write calls ended up with error.",
}),
TotalReadCalls: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_read_calls",
Help: "The total read calls made.",
}),
TotalReadCallsErr: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_read_calls_err",
Help: "The total read calls ended up with error.",
}),
ShardCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "shard_count",
Help: "The number of shards.",
}),
}
}

// Metrics returns set of prometheus collectors.
func (s *Store) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(s.metrics)
}
24 changes: 17 additions & 7 deletions pkg/sharky/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
)

var (
// Error returned by Write if the blob length exceeds the max blobsize
// ErrTooLong returned by Write if the blob length exceeds the max blobsize.
ErrTooLong = errors.New("data too long")
// Error returned by Write when the store is Closed before the write completes
// ErrQuitting returned by Write when the store is Closed before the write completes.
ErrQuitting = errors.New("quitting")
)

Expand All @@ -36,6 +36,7 @@ type Store struct {
shards []*shard // shards
wg *sync.WaitGroup // count started operations
quit chan struct{} // quit channel
metrics metrics
}

// New constructs a sharded blobstore
Expand All @@ -48,22 +49,25 @@ func New(basedir string, shardCnt int, limit uint32, datasize int) (*Store, erro
pool := &sync.Pool{New: func() interface{} {
return make(chan entry)
}}
sh := &Store{
store := &Store{
datasize: datasize,
pool: pool,
writes: make(chan write),
shards: make([]*shard, shardCnt),
wg: &sync.WaitGroup{},
quit: make(chan struct{}),
metrics: newMetrics(),
}
for i := range sh.shards {
s, err := sh.create(uint8(i), limit, datasize, basedir)
for i := range store.shards {
s, err := store.create(uint8(i), limit, datasize, basedir)
if err != nil {
return nil, err
}
sh.shards[i] = s
store.shards[i] = s
}
return sh, nil
store.metrics.ShardCount.Set(float64(len(store.shards)))

return store, nil
}

// Close closes each shard and return incidental errors from each shard
Expand Down Expand Up @@ -116,12 +120,14 @@ func (s *Store) Read(ctx context.Context, loc Location, buf []byte) (err error)
sh := s.shards[loc.Shard]
select {
case sh.reads <- read{buf[:loc.Length], loc.Slot, 0}:
s.metrics.TotalReadCalls.Inc()
case <-ctx.Done():
return ctx.Err()
}

select {
case err = <-sh.errc:
s.metrics.TotalReadCallsErr.Inc()
return err
case <-s.quit:
return ErrQuitting
Expand All @@ -144,6 +150,7 @@ func (s *Store) Write(ctx context.Context, data []byte) (loc Location, err error

select {
case s.writes <- write{data, c}:
s.metrics.TotalWriteCalls.Inc()
case <-s.quit:
return loc, ErrQuitting
case <-ctx.Done():
Expand All @@ -152,6 +159,9 @@ func (s *Store) Write(ctx context.Context, data []byte) (loc Location, err error

select {
case e := <-c:
if e.err != nil {
s.metrics.TotalWriteCallsErr.Inc()
}
return e.loc, e.err
case <-s.quit:
return loc, ErrQuitting
Expand Down

0 comments on commit d43925e

Please sign in to comment.