Skip to content

Commit

Permalink
Merge pull request #5 from codesome/num-series-read-only
Browse files Browse the repository at this point in the history
NumSeries in Meta() of Head
  • Loading branch information
krasi-georgiev committed Jul 16, 2019
2 parents fb75682 + 1cf409f commit 0a645ab
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions head.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Head struct {
logger log.Logger
appendPool sync.Pool
bytesPool sync.Pool
numSeries uint64

minTime, maxTime int64 // Current min and max of the samples included in the head.
minValidTime int64 // Mint allowed to be added to the head. It shouldn't be lower than the maxt of the last persisted block.
Expand All @@ -85,7 +86,7 @@ type Head struct {

type headMetrics struct {
activeAppenders prometheus.Gauge
series prometheus.Gauge
series prometheus.GaugeFunc
seriesCreated prometheus.Counter
seriesRemoved prometheus.Counter
seriesNotFound prometheus.Counter
Expand Down Expand Up @@ -113,9 +114,11 @@ func newHeadMetrics(h *Head, r prometheus.Registerer) *headMetrics {
Name: "prometheus_tsdb_head_active_appenders",
Help: "Number of currently active appender transactions",
})
m.series = prometheus.NewGauge(prometheus.GaugeOpts{
m.series = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "prometheus_tsdb_head_series",
Help: "Total number of series in the head block.",
}, func() float64 {
return float64(h.NumSeries())
})
m.seriesCreated = prometheus.NewCounter(prometheus.CounterOpts{
Name: "prometheus_tsdb_head_series_created_total",
Expand Down Expand Up @@ -699,11 +702,18 @@ func (h *rangeHead) MaxTime() int64 {
return h.maxt
}

func (h *rangeHead) NumSeries() uint64 {
return h.head.NumSeries()
}

func (h *rangeHead) Meta() BlockMeta {
return BlockMeta{
MinTime: h.MinTime(),
MaxTime: h.MaxTime(),
ULID: h.head.Meta().ULID,
Stats: BlockStats{
NumSeries: h.NumSeries(),
},
}
}

Expand Down Expand Up @@ -1031,9 +1041,10 @@ func (h *Head) gc() {
seriesRemoved := len(deleted)

h.metrics.seriesRemoved.Add(float64(seriesRemoved))
h.metrics.series.Sub(float64(seriesRemoved))
h.metrics.chunksRemoved.Add(float64(chunksRemoved))
h.metrics.chunks.Sub(float64(chunksRemoved))
// Ref: https://golang.org/pkg/sync/atomic/#AddUint64
atomic.AddUint64(&h.numSeries, ^uint64(seriesRemoved-1))

// Remove deleted series IDs from the postings lists.
h.postings.Delete(deleted)
Expand Down Expand Up @@ -1110,6 +1121,11 @@ func (h *Head) chunksRange(mint, maxt int64) *headChunkReader {
return &headChunkReader{head: h, mint: mint, maxt: maxt}
}

// NumSeries returns the number of active series in the head.
func (h *Head) NumSeries() uint64 {
return atomic.LoadUint64(&h.numSeries)
}

// Meta returns meta information about the head.
func (h *Head) Meta() BlockMeta {
var id [16]byte
Expand All @@ -1118,6 +1134,9 @@ func (h *Head) Meta() BlockMeta {
MinTime: h.MinTime(),
MaxTime: h.MaxTime(),
ULID: ulid.ULID(id),
Stats: BlockStats{
NumSeries: h.NumSeries(),
},
}
}

Expand Down Expand Up @@ -1367,8 +1386,8 @@ func (h *Head) getOrCreateWithID(id, hash uint64, lset labels.Labels) (*memSerie
return s, false
}

h.metrics.series.Inc()
h.metrics.seriesCreated.Inc()
atomic.AddUint64(&h.numSeries, 1)

h.postings.Add(id, lset)

Expand Down

0 comments on commit 0a645ab

Please sign in to comment.