Skip to content

Commit 2fd2481

Browse files
committed
Refactor metrics
1 parent d9ddb7d commit 2fd2481

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

pkg/metastore/fsm/fsm.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ func (fsm *FSM) applyCommand(cmd *raft.Log) any {
275275
return errResponse(cmd, fmt.Errorf("unknown command type: %d", e.Type))
276276
}
277277

278-
fsm.metrics.contextRegistrySize.Set(float64(fsm.contextRegistry.Size()))
279-
280278
// Apply is never called concurrently with Restore, so we don't need
281279
// to lock the FSM: db.boltdb is guaranteed to be in a consistent state.
282280
rawTx, err := fsm.db.boltdb.Begin(true)

pkg/metastore/fsm/metrics.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type metrics struct {
1515
fsmRestoreSnapshotDuration prometheus.Histogram
1616
fsmApplyCommandSize *prometheus.HistogramVec
1717
fsmApplyCommandDuration *prometheus.HistogramVec
18-
contextRegistrySize prometheus.Gauge
1918
}
2019

2120
func newMetrics(reg prometheus.Registerer) *metrics {
@@ -64,11 +63,6 @@ func newMetrics(reg prometheus.Registerer) *metrics {
6463
NativeHistogramMaxBucketNumber: 50,
6564
NativeHistogramMinResetDuration: time.Hour,
6665
}, []string{"command"}),
67-
68-
contextRegistrySize: prometheus.NewGauge(prometheus.GaugeOpts{
69-
Name: "fsm_context_registry_size",
70-
Help: "Number of contexts currently stored in the registry for tracing propagation",
71-
}),
7266
}
7367
if reg != nil {
7468
util.RegisterOrGet(reg, m.boltDBPersistSnapshotSize)
@@ -77,7 +71,6 @@ func newMetrics(reg prometheus.Registerer) *metrics {
7771
util.RegisterOrGet(reg, m.fsmRestoreSnapshotDuration)
7872
util.RegisterOrGet(reg, m.fsmApplyCommandSize)
7973
util.RegisterOrGet(reg, m.fsmApplyCommandDuration)
80-
util.RegisterOrGet(reg, m.contextRegistrySize)
8174
}
8275
return m
8376
}

pkg/metastore/metastore.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,18 @@ func New(
119119
placementMgr *placement.Manager,
120120
) (*Metastore, error) {
121121
m := &Metastore{
122-
config: config,
123-
overrides: overrides,
124-
logger: logger,
125-
reg: reg,
126-
health: healthService,
127-
bucket: bucket,
128-
placement: placementMgr,
129-
raftNodeClient: client,
122+
config: config,
123+
overrides: overrides,
124+
logger: logger,
125+
reg: reg,
126+
health: healthService,
127+
bucket: bucket,
128+
placement: placementMgr,
129+
raftNodeClient: client,
130+
contextRegistry: tracing.NewContextRegistry(reg),
130131
}
131132

132133
var err error
133-
// Create the context registry that will be shared between FSM and Node.
134-
m.contextRegistry = tracing.NewContextRegistry()
135134
if m.fsm, err = fsm.New(m.logger, m.reg, m.config.FSM, m.contextRegistry); err != nil {
136135
return nil, fmt.Errorf("failed to initialize store: %w", err)
137136
}

pkg/metastore/raftnode/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (n *Node) TransferLeadership() (err error) {
313313
// Propose makes an attempt to apply the given command to the FSM.
314314
// The function returns an error if node is not the leader.
315315
func (n *Node) Propose(ctx context.Context, t fsm.RaftLogEntryType, m proto.Message) (resp proto.Message, err error) {
316-
span, ctx := opentracing.StartSpanFromContext(ctx, "FSM.Propose")
316+
span, ctx := opentracing.StartSpanFromContext(ctx, "node.Propose")
317317
defer func() {
318318
if err != nil {
319319
ext.LogError(span, err)

pkg/metastore/tracing/context_registry.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"context"
55
"sync"
66
"time"
7+
8+
"github.com/prometheus/client_golang/prometheus"
9+
10+
"github.com/grafana/pyroscope/pkg/util"
711
)
812

913
// ContextRegistry maintains a mapping of IDs to contexts for tracing purposes.
@@ -21,6 +25,9 @@ type ContextRegistry struct {
2125
entryTTL time.Duration
2226
stop chan struct{}
2327
done chan struct{}
28+
29+
// sizeMetric tracks the number of entries in the registry
30+
sizeMetric prometheus.Gauge
2431
}
2532

2633
type contextEntry struct {
@@ -34,25 +41,34 @@ const (
3441
)
3542

3643
// NewContextRegistry creates a new context registry with background cleanup.
37-
func NewContextRegistry() *ContextRegistry {
38-
return newContextRegistry(defaultCleanupInterval, defaultEntryTTL)
44+
func NewContextRegistry(reg prometheus.Registerer) *ContextRegistry {
45+
return newContextRegistry(defaultCleanupInterval, defaultEntryTTL, reg)
3946
}
4047

41-
// NewContextRegistry creates a new context registry with background cleanup.
42-
func newContextRegistry(cleanupInterval, entryTTL time.Duration) *ContextRegistry {
48+
// newContextRegistry creates a new context registry with background cleanup.
49+
func newContextRegistry(cleanupInterval, entryTTL time.Duration, reg prometheus.Registerer) *ContextRegistry {
4350
if cleanupInterval <= 0 {
4451
cleanupInterval = defaultCleanupInterval
4552
}
4653
if entryTTL <= 0 {
4754
entryTTL = defaultEntryTTL
4855
}
4956

57+
sizeMetric := prometheus.NewGauge(prometheus.GaugeOpts{
58+
Name: "context_registry_size",
59+
Help: "Number of contexts currently stored in the registry for tracing propagation",
60+
})
61+
if reg != nil {
62+
util.RegisterOrGet(reg, sizeMetric)
63+
}
64+
5065
r := &ContextRegistry{
5166
entries: make(map[string]*contextEntry),
5267
cleanupInterval: cleanupInterval,
5368
entryTTL: entryTTL,
5469
stop: make(chan struct{}),
5570
done: make(chan struct{}),
71+
sizeMetric: sizeMetric,
5672
}
5773

5874
go r.cleanupLoop()
@@ -113,6 +129,10 @@ func (r *ContextRegistry) cleanup() {
113129
delete(r.entries, id)
114130
}
115131
}
132+
133+
if r.sizeMetric != nil {
134+
r.sizeMetric.Set(float64(len(r.entries)))
135+
}
116136
}
117137

118138
// Shutdown stops the cleanup loop.

pkg/metastore/tracing/context_registry_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
type contextKey string
1414

1515
func TestContextRegistry_StoreAndRetrieve(t *testing.T) {
16-
r := newContextRegistry(1*time.Second, 5*time.Second)
16+
r := newContextRegistry(1*time.Second, 5*time.Second, nil)
1717
defer r.Shutdown()
1818

1919
ctx := context.WithValue(context.Background(), contextKey("key"), "value")
@@ -25,7 +25,7 @@ func TestContextRegistry_StoreAndRetrieve(t *testing.T) {
2525
}
2626

2727
func TestContextRegistry_RetrieveNotFound(t *testing.T) {
28-
r := newContextRegistry(1*time.Second, 5*time.Second)
28+
r := newContextRegistry(1*time.Second, 5*time.Second, nil)
2929
defer r.Shutdown()
3030

3131
retrieved, found := r.Retrieve("nonexistent-id")
@@ -34,7 +34,7 @@ func TestContextRegistry_RetrieveNotFound(t *testing.T) {
3434
}
3535

3636
func TestContextRegistry_Delete(t *testing.T) {
37-
r := newContextRegistry(1*time.Second, 5*time.Second)
37+
r := newContextRegistry(1*time.Second, 5*time.Second, nil)
3838
defer r.Shutdown()
3939

4040
ctx := context.WithValue(context.Background(), contextKey("key"), "value")
@@ -51,7 +51,7 @@ func TestContextRegistry_Delete(t *testing.T) {
5151

5252
func TestContextRegistry_Cleanup(t *testing.T) {
5353
// Use short TTL for a faster test
54-
r := newContextRegistry(100*time.Millisecond, 200*time.Millisecond)
54+
r := newContextRegistry(100*time.Millisecond, 200*time.Millisecond, nil)
5555
defer r.Shutdown()
5656

5757
ctx := context.WithValue(context.Background(), contextKey("key"), "value")
@@ -69,7 +69,7 @@ func TestContextRegistry_Cleanup(t *testing.T) {
6969
}
7070

7171
func TestContextRegistry_Size(t *testing.T) {
72-
r := newContextRegistry(1*time.Second, 5*time.Second)
72+
r := newContextRegistry(1*time.Second, 5*time.Second, nil)
7373
defer r.Shutdown()
7474

7575
assert.Equal(t, 0, r.Size())
@@ -86,7 +86,7 @@ func TestContextRegistry_Size(t *testing.T) {
8686
}
8787

8888
func TestContextRegistry_ConcurrentAccess(t *testing.T) {
89-
r := newContextRegistry(1*time.Second, 5*time.Second)
89+
r := newContextRegistry(1*time.Second, 5*time.Second, nil)
9090
defer r.Shutdown()
9191

9292
done := make(chan bool)

0 commit comments

Comments
 (0)