Skip to content

Commit

Permalink
fix: metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Jan 28, 2024
1 parent d293754 commit 147b931
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/storer/internal/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *store) NewTransaction() (Transaction, func()) {

b, _ := s.bstore.Batch(context.TODO())
indexTrx := &indexTrx{s.bstore, b}
sharyTrx := &sharkyTrx{sharky: s.sharky}
sharyTrx := &sharkyTrx{sharky: s.sharky, metrics: s.metrics}

t := &transaction{
batch: b,
Expand Down Expand Up @@ -138,7 +138,7 @@ type readOnly struct {

func (s *store) ReadOnly() ReadOnlyStore {
indexStore := &indexTrx{store: s.bstore}
sharyTrx := &sharkyTrx{sharky: s.sharky}
sharyTrx := &sharkyTrx{sharky: s.sharky, metrics: s.metrics}
return &readOnly{indexStore, &chunkStoreTrx{indexStore, sharyTrx}}
}

Expand Down Expand Up @@ -227,6 +227,16 @@ func (t *transaction) Commit() (err error) {
return err
}

defer func(ti time.Time) {
if err != nil {
t.metrics.MethodCalls.WithLabelValues("sharky_release", "failure").Inc()
t.metrics.MethodDuration.WithLabelValues("sharky_release", "failure").Observe(float64(time.Since(ti)))
} else {
t.metrics.MethodCalls.WithLabelValues("sharky_release", "success").Inc()
t.metrics.MethodDuration.WithLabelValues("sharky_release", "success").Observe(float64(time.Since(ti)))
}
}(time.Now())

for _, l := range t.sharkyTrx.releasedLocs {
if rerr := t.sharkyTrx.sharky.Release(context.TODO(), l); rerr != nil {
err = errors.Join(err, fmt.Errorf("failed releasing location afer commit %s: %w", l, rerr))
Expand All @@ -240,13 +250,25 @@ type sharkyTrx struct {
sharky *sharky.Store
writtenLocs []sharky.Location
releasedLocs []sharky.Location
metrics metrics
}

func (s *sharkyTrx) Read(ctx context.Context, loc sharky.Location, buf []byte) error {
return s.sharky.Read(ctx, loc, buf)
}

func (s *sharkyTrx) Write(ctx context.Context, data []byte) (sharky.Location, error) {
var err error
defer func(ti time.Time) {
if err != nil {
s.metrics.MethodCalls.WithLabelValues("sharky_write", "failure").Inc()
s.metrics.MethodDuration.WithLabelValues("sharky_write", "failure").Observe(float64(time.Since(ti)))
} else {
s.metrics.MethodCalls.WithLabelValues("sharky_write", "success").Inc()
s.metrics.MethodDuration.WithLabelValues("sharky_write", "success").Observe(float64(time.Since(ti)))
}
}(time.Now())

loc, err := s.sharky.Write(ctx, data)
if err != nil {
return sharky.Location{}, err
Expand Down

0 comments on commit 147b931

Please sign in to comment.