Skip to content

Commit

Permalink
add blobstore metric
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <crichter@owncloud.com>
  • Loading branch information
dragonchaser committed Sep 26, 2022
1 parent ed2be2a commit 6dc82d8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
5 changes: 5 additions & 0 deletions changelog/unreleased/add-s3ng-metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add s3ng metrics

We have added rx/tx metrics to the s3ng blobstore driver.

https://github.com/cs3org/reva/pull/3272
38 changes: 37 additions & 1 deletion pkg/storage/fs/s3ng/blobstore/blobstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)

// Blobstore provides an interface to an s3 compatible blobstore
Expand All @@ -39,6 +40,18 @@ type Blobstore struct {
bucket string
}

type PrometheusAwareReader struct {
r io.Reader
m *prometheus.CounterVec
}

type PrometheusAwareReadCloser struct {
r io.ReadCloser
m *prometheus.CounterVec
}

var metrics = NewMetrics()

// New returns a new Blobstore
func New(endpoint, region, bucket, accessKey, secretKey string) (*Blobstore, error) {
u, err := url.Parse(endpoint)
Expand All @@ -62,8 +75,28 @@ func New(endpoint, region, bucket, accessKey, secretKey string) (*Blobstore, err
}, nil
}

func (p *PrometheusAwareReader) Read(b []byte) (n int, err error) {
n, err = p.r.Read(b)
p.m.WithLabelValues().Add(float64(n))
return
}

func (p *PrometheusAwareReadCloser) Read(b []byte) (n int, err error) {
n, err = p.r.Read(b)
p.m.WithLabelValues().Add(float64(n))
return
}

func (p *PrometheusAwareReadCloser) Close() error {
return p.r.Close()
}

// Upload stores some data in the blobstore under the given key
func (bs *Blobstore) Upload(node *node.Node, reader io.Reader) error {
reader = &PrometheusAwareReader{
r: reader,
m: metrics.Tx,
}
size := int64(-1)
if file, ok := reader.(*os.File); ok {
info, err := file.Stat()
Expand All @@ -87,7 +120,10 @@ func (bs *Blobstore) Download(node *node.Node) (io.ReadCloser, error) {
if err != nil {
return nil, errors.Wrapf(err, "could not download object '%s' from bucket '%s'", bs.path(node), bs.bucket)
}
return reader, nil
return &PrometheusAwareReadCloser{
r: reader,
m: metrics.Rx,
}, nil
}

// Delete deletes a blob from the blobstore
Expand Down
39 changes: 39 additions & 0 deletions pkg/storage/fs/s3ng/blobstore/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package blobstore

import "github.com/prometheus/client_golang/prometheus"

var (
// Namespace defines the namespace for the defines metrics.
Namespace = "ocis"

// Subsystem defines the subsystem for the defines metrics.
Subsystem = "thumbnails"
)

// Metrics defines the available metrics of this service.
type Metrics struct {
Rx *prometheus.CounterVec
Tx *prometheus.CounterVec
}

// New initializes the available metrics.
func NewMetrics() *Metrics {
m := &Metrics{
Rx: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: Subsystem,
Name: "Rx",
Help: "Storage access rx",
}, []string{}),
Tx: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: Subsystem,
Name: "Tx",
Help: "Storage access tx",
}, []string{}),
}
_ = prometheus.Register(m.Rx)
_ = prometheus.Register(m.Tx)

return m
}

0 comments on commit 6dc82d8

Please sign in to comment.