Skip to content

Commit bd3c7b2

Browse files
Add block opened metrics (grafana/phlare#785)
* Add block opened metrics * Add fake metrics to tests to fix it * Update pkg/phlaredb/metrics.go Co-authored-by: Christian Simon <[email protected]> --------- Co-authored-by: Christian Simon <[email protected]>
1 parent 71a812a commit bd3c7b2

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

pkg/phlaredb/block_querier.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ func NewSingleBlockQuerierFromMeta(phlarectx context.Context, bucketReader phlar
373373

374374
func (b *singleBlockQuerier) Close() error {
375375
b.openLock.Lock()
376-
defer b.openLock.Unlock()
376+
defer func() {
377+
b.openLock.Unlock()
378+
b.metrics.blockOpened.Dec()
379+
}()
377380
errs := multierror.New()
378381
if b.index != nil {
379382
err := b.index.Close()
@@ -1076,6 +1079,7 @@ func (q *singleBlockQuerier) Open(ctx context.Context) error {
10761079
if err := q.openFiles(ctx); err != nil {
10771080
return err
10781081
}
1082+
q.metrics.blockOpened.Inc()
10791083
q.opened = true
10801084
return nil
10811085
}

pkg/phlaredb/block_querier_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ func TestQuerierBlockEviction(t *testing.T) {
101101
for _, tc := range testCases {
102102
q := BlockQuerier{queriers: make([]*singleBlockQuerier, len(tc.blocks))}
103103
for i, b := range tc.blocks {
104-
q.queriers[i] = &singleBlockQuerier{meta: &block.Meta{ULID: ulid.MustParse(b)}}
104+
q.queriers[i] = &singleBlockQuerier{
105+
meta: &block.Meta{ULID: ulid.MustParse(b)},
106+
metrics: newBlocksMetrics(nil),
107+
}
105108
}
106109

107110
evicted, err := q.evict(ulid.MustParse(blockToEvict))

pkg/phlaredb/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type headMetrics struct {
2929

3030
sampleValuesIngested *prometheus.CounterVec
3131
sampleValuesReceived *prometheus.CounterVec
32+
samples prometheus.Gauge
3233

3334
flushedFileSizeBytes *prometheus.HistogramVec
3435
flushedBlockSizeBytes prometheus.Histogram
@@ -145,6 +146,10 @@ func newHeadMetrics(reg prometheus.Registerer) *headMetrics {
145146
// [512KB, 1MB, 2MB, 4MB, 8MB, 16MB, 32MB, 64MB, 128MB, 256MB, 512MB]
146147
Buckets: prometheus.ExponentialBuckets(512*1024, 2, 11),
147148
}),
149+
samples: prometheus.NewGauge(prometheus.GaugeOpts{
150+
Name: "pyroscope_head_samples",
151+
Help: "Number of samples in the head.",
152+
}),
148153
}
149154

150155
m.register(reg)
@@ -192,6 +197,7 @@ type blocksMetrics struct {
192197
query *query.Metrics
193198

194199
blockOpeningLatency prometheus.Histogram
200+
blockOpened prometheus.Gauge
195201
}
196202

197203
func newBlocksMetrics(reg prometheus.Registerer) *blocksMetrics {
@@ -201,8 +207,13 @@ func newBlocksMetrics(reg prometheus.Registerer) *blocksMetrics {
201207
Name: "pyroscopedb_block_opening_duration",
202208
Help: "Latency of opening a block in seconds",
203209
}),
210+
blockOpened: prometheus.NewGauge(prometheus.GaugeOpts{
211+
Name: "pyroscopedb_blocks_currently_open",
212+
Help: "Number of blocks opened",
213+
}),
204214
}
205215
m.blockOpeningLatency = util.RegisterOrGet(reg, m.blockOpeningLatency)
216+
m.blockOpened = util.RegisterOrGet(reg, m.blockOpened)
206217
return m
207218
}
208219

pkg/phlaredb/profile_store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (s *profileStore) cutRowGroup(count int) (err error) {
279279
defer s.profilesLock.Unlock()
280280
for i := range s.slice[:count] {
281281
// don't retain profiles and samples in memory as re-slice.
282+
s.metrics.samples.Sub(float64(len(s.slice[i].Samples)))
282283
s.slice[i] = nil
283284
}
284285
// reset slice and metrics
@@ -420,6 +421,7 @@ func (s *profileStore) ingest(_ context.Context, profiles []*schemav1.Profile, l
420421

421422
// add to slice
422423
s.slice = append(s.slice, p)
424+
s.metrics.samples.Add(float64(len(p.Samples)))
423425

424426
}
425427

0 commit comments

Comments
 (0)