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

Commit d230c67

Browse files
authored
Merge pull request #649 from prometheus/lr-inject-metrics
wal: Inject LiveReader metrics rather than registry
2 parents 7ef1018 + 078895b commit d230c67

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Master / unreleased
22

3+
## 0.9.1
4+
5+
- [CHANGE] LiveReader metrics are now injected rather than global.
6+
37
## 0.9.0
48

59
- [FEATURE] Provide option to compress WAL records using Snappy. [#609](https://github.com/prometheus/tsdb/pull/609)
@@ -9,7 +13,7 @@
913
- [BUGFIX] `prometheus_tsdb_compactions_failed_total` is now incremented on any compaction failure.
1014
- [CHANGE] The meta file `BlockStats` no longer holds size information. This is now dynamically calculated and kept in memory. It also includes the meta file size which was not included before.
1115
- [CHANGE] Create new clean segment when starting the WAL.
12-
- [CHANGE] Renamed metric from `prometheus_tsdb_wal_reader_corruption_errors` to `prometheus_tsdb_wal_reader_corruption_errors_total`
16+
- [CHANGE] Renamed metric from `prometheus_tsdb_wal_reader_corruption_errors` to `prometheus_tsdb_wal_reader_corruption_errors_total`.
1317
- [ENHANCEMENT] Improved atomicity of .tmp block replacement during compaction for usual case.
1418
- [ENHANCEMENT] Improved postings intersection matching.
1519
- [ENHANCEMENT] Reduced disk usage for WAL for small setups.

wal/live_reader.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,40 @@ import (
2727
"github.com/prometheus/client_golang/prometheus"
2828
)
2929

30+
// liveReaderMetrics holds all metrics exposed by the LiveReader.
31+
type liveReaderMetrics struct {
32+
readerCorruptionErrors *prometheus.CounterVec
33+
}
34+
35+
// LiveReaderMetrics instatiates, registers and returns metrics to be injected
36+
// at LiveReader instantiation.
37+
func NewLiveReaderMetrics(reg prometheus.Registerer) *liveReaderMetrics {
38+
m := &liveReaderMetrics{
39+
readerCorruptionErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
40+
Name: "prometheus_tsdb_wal_reader_corruption_errors_total",
41+
Help: "Errors encountered when reading the WAL.",
42+
}, []string{"error"}),
43+
}
44+
45+
if reg != nil {
46+
reg.Register(m.readerCorruptionErrors)
47+
}
48+
49+
return m
50+
}
51+
3052
// NewLiveReader returns a new live reader.
31-
func NewLiveReader(logger log.Logger, reg prometheus.Registerer, r io.Reader) *LiveReader {
53+
func NewLiveReader(logger log.Logger, metrics *liveReaderMetrics, r io.Reader) *LiveReader {
3254
lr := &LiveReader{
33-
logger: logger,
34-
rdr: r,
55+
logger: logger,
56+
rdr: r,
57+
metrics: metrics,
3558

3659
// Until we understand how they come about, make readers permissive
3760
// to records spanning pages.
3861
permissive: true,
3962
}
4063

41-
lr.readerCorruptionErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
42-
Name: "prometheus_tsdb_wal_reader_corruption_errors_total",
43-
Help: "Errors encountered when reading the WAL.",
44-
}, []string{"error"})
45-
46-
if reg != nil {
47-
reg.MustRegister(lr.readerCorruptionErrors)
48-
}
49-
5064
return lr
5165
}
5266

@@ -74,7 +88,7 @@ type LiveReader struct {
7488
// NB the non-ive Reader implementation allows for this.
7589
permissive bool
7690

77-
readerCorruptionErrors *prometheus.CounterVec
91+
metrics *liveReaderMetrics
7892
}
7993

8094
// Err returns any errors encountered reading the WAL. io.EOFs are not terminal
@@ -282,7 +296,7 @@ func (r *LiveReader) readRecord() ([]byte, int, error) {
282296
if !r.permissive {
283297
return nil, 0, fmt.Errorf("record would overflow current page: %d > %d", r.readIndex+recordHeaderSize+length, pageSize)
284298
}
285-
r.readerCorruptionErrors.WithLabelValues("record_span_page").Inc()
299+
r.metrics.readerCorruptionErrors.WithLabelValues("record_span_page").Inc()
286300
level.Warn(r.logger).Log("msg", "record spans page boundaries", "start", r.readIndex, "end", recordHeaderSize+length, "pageSize", pageSize)
287301
}
288302
if recordHeaderSize+length > pageSize {

wal/reader_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var readerConstructors = map[string]func(io.Reader) reader{
5151
return NewReader(r)
5252
},
5353
"LiveReader": func(r io.Reader) reader {
54-
lr := NewLiveReader(log.NewNopLogger(), nil, r)
54+
lr := NewLiveReader(log.NewNopLogger(), NewLiveReaderMetrics(nil), r)
5555
lr.eofNonErr = true
5656
return lr
5757
},
@@ -216,7 +216,7 @@ func TestReader_Live(t *testing.T) {
216216
// Read from a second FD on the same file.
217217
readFd, err := os.Open(writeFd.Name())
218218
testutil.Ok(t, err)
219-
reader := NewLiveReader(logger, nil, readFd)
219+
reader := NewLiveReader(logger, NewLiveReaderMetrics(nil), readFd)
220220
for _, exp := range testReaderCases[i].exp {
221221
for !reader.Next() {
222222
testutil.Assert(t, reader.Err() == io.EOF, "expect EOF, got: %v", reader.Err())
@@ -518,7 +518,7 @@ func TestLiveReaderCorrupt_RecordTooLongAndShort(t *testing.T) {
518518
testutil.Ok(t, err)
519519
defer seg.Close()
520520

521-
r := NewLiveReader(logger, nil, seg)
521+
r := NewLiveReader(logger, NewLiveReaderMetrics(nil), seg)
522522
testutil.Assert(t, r.Next() == false, "expected no records")
523523
testutil.Assert(t, r.Err().Error() == "record length greater than a single page: 65542 > 32768", "expected error, got: %v", r.Err())
524524
}

0 commit comments

Comments
 (0)