Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Export the current segment index as a metic. (#601)
Browse files Browse the repository at this point in the history
* Export the current segment index as a metic.

Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan authored and krasi-georgiev committed May 17, 2019
1 parent 96a8784 commit bce663e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type WAL struct {
pageCompletions prometheus.Counter
truncateFail prometheus.Counter
truncateTotal prometheus.Counter
currentSegment prometheus.Gauge
}

// New returns a new WAL over the given directory.
Expand Down Expand Up @@ -218,8 +219,12 @@ func NewSize(logger log.Logger, reg prometheus.Registerer, dir string, segmentSi
Name: "prometheus_tsdb_wal_truncations_total",
Help: "Total number of WAL truncations attempted.",
})
w.currentSegment = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "prometheus_tsdb_wal_segment_current",
Help: "WAL segment index that TSDB is currently writing to.",
})
if reg != nil {
reg.MustRegister(w.fsyncDuration, w.pageFlushes, w.pageCompletions, w.truncateFail, w.truncateTotal)
reg.MustRegister(w.fsyncDuration, w.pageFlushes, w.pageCompletions, w.truncateFail, w.truncateTotal, w.currentSegment)
}

_, j, err := w.Segments()
Expand Down Expand Up @@ -413,7 +418,7 @@ func (w *WAL) setSegment(segment *Segment) error {
return err
}
w.donePages = int(stat.Size() / pageSize)

w.currentSegment.Set(float64(segment.Index()))
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"testing"

client_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/tsdb/testutil"
)

Expand Down Expand Up @@ -318,6 +319,35 @@ func TestClose(t *testing.T) {
testutil.NotOk(t, w.Close())
}

func TestSegmentMetric(t *testing.T) {
var (
segmentSize = pageSize
recordSize = (pageSize / 2) - recordHeaderSize
)

dir, err := ioutil.TempDir("", "segment_metric")
testutil.Ok(t, err)
defer func() {
testutil.Ok(t, os.RemoveAll(dir))
}()
w, err := NewSize(nil, nil, dir, segmentSize)
testutil.Ok(t, err)

initialSegment := client_testutil.ToFloat64(w.currentSegment)

// Write 3 records, each of which is half the segment size, meaning we should rotate to the next segment.
for i := 0; i < 3; i++ {
buf := make([]byte, recordSize)
_, err := rand.Read(buf)
testutil.Ok(t, err)

err = w.Log(buf)
testutil.Ok(t, err)
}
testutil.Assert(t, client_testutil.ToFloat64(w.currentSegment) == initialSegment+1, "segment metric did not increment after segment rotation")
testutil.Ok(t, w.Close())
}

func BenchmarkWAL_LogBatched(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_logbatch")
testutil.Ok(b, err)
Expand Down

0 comments on commit bce663e

Please sign in to comment.