Skip to content

Commit

Permalink
Record datastore metrics for non-default datastores
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Tommi Virtanen <tv@eagain.net>
  • Loading branch information
tv42 committed Jul 15, 2015
1 parent f918a6c commit fc949cb
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/measure"
repo "github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/common"
config "github.com/ipfs/go-ipfs/repo/config"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/ipfs/go-ipfs/thirdparty/eventlog"
u "github.com/ipfs/go-ipfs/util"
util "github.com/ipfs/go-ipfs/util"
"github.com/ipfs/go-ipfs/util/datastore2"
)

// version number that we are currently expecting to see
Expand Down Expand Up @@ -300,7 +302,41 @@ func (r *FSRepo) openDatastore() error {
if err != nil {
return err
}
r.ds = d

// Wrap it with metrics gathering
//
// Add our PeerID to metrics paths to keep them unique
//
// As some tests just pass a zero-value Config to fsrepo.Init,
// cope with missing PeerID.
id := r.config.Identity.PeerID
if id == "" {
// the tests pass in a zero Config; cope with it
id = fmt.Sprintf("uninitialized_%p", r)
}
prefix := "fsrepo." + id + ".datastore"
dMetr := measure.New(prefix, d)

r.ds = &metricsWrap{dMetr, d}
return nil
}

type metricsWrap struct {
measure.DatastoreCloser
backend datastore2.ThreadSafeDatastoreCloser
}

var _ ds.ThreadSafeDatastore = (*metricsWrap)(nil)

func (*metricsWrap) IsThreadSafe() {}

func (m *metricsWrap) Close() error {
if err := m.backend.Close(); err != nil {
return err
}
if err := m.DatastoreCloser.Close(); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit fc949cb

Please sign in to comment.