Skip to content

Commit

Permalink
Add metric collection for the number of goroutines attached in the Ba…
Browse files Browse the repository at this point in the history
…ckground task (#963)

This PR adds a metric to collect the number of background routines.

Prometheus provides its own thread-safe Gauge metric, which can be
used to accurately collect the number of concurrent goroutines.

Also, although the only task running in the background so far is
PushPull, this commit implements the AttachGoroutine() method to take
a string called taskType as a parameter, since it seems to be focused
on reusability.
  • Loading branch information
kokodak authored Aug 19, 2024
1 parent ed6e974 commit d73a0f0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func New(

// 03. Create the background instance. The background instance is used to
// manage background tasks.
bg := background.New()
bg := background.New(metrics)

// 04. Create the database instance. If the MongoDB configuration is given,
// create a MongoDB instance. Otherwise, create a memory database instance.
Expand Down
18 changes: 15 additions & 3 deletions server/backend/background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync/atomic"

"github.com/yorkie-team/yorkie/server/logging"
"github.com/yorkie-team/yorkie/server/profiling/prometheus"
)

type routineID int32
Expand All @@ -49,18 +50,25 @@ type Background struct {

// routineID is used to generate routine ID.
routineID routineID

// metrics is used to collect metrics with prometheus.
metrics *prometheus.Metrics
}

// New creates a new background service.
func New() *Background {
func New(metrics *prometheus.Metrics) *Background {
return &Background{
closing: make(chan struct{}),
metrics: metrics,
}
}

// AttachGoroutine creates a goroutine on a given function and tracks it using
// the background's WaitGroup.
func (b *Background) AttachGoroutine(f func(ctx context.Context)) {
func (b *Background) AttachGoroutine(
f func(ctx context.Context),
taskType string,
) {
b.wgMu.RLock() // this blocks with ongoing close(b.closing)
defer b.wgMu.RUnlock()
select {
Expand All @@ -73,8 +81,12 @@ func (b *Background) AttachGoroutine(f func(ctx context.Context)) {
// now safe to add since WaitGroup wait has not started yet
b.wg.Add(1)
routineLogger := logging.New(b.routineID.next())
b.metrics.AddBackgroundGoroutines(taskType)
go func() {
defer b.wg.Done()
defer func() {
b.wg.Done()
b.metrics.RemoveBackgroundGoroutines(taskType)
}()
f(logging.With(context.Background(), routineLogger))
}()
}
Expand Down
2 changes: 1 addition & 1 deletion server/packs/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func PushPull(
be.Metrics.ObservePushPullSnapshotDurationSeconds(
gotime.Since(start).Seconds(),
)
})
}, "pushpull")
}

return respPack, nil
Expand Down
23 changes: 23 additions & 0 deletions server/profiling/prometheus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
projectIDLabel = "project_id"
projectNameLabel = "project_name"
hostnameLabel = "hostname"
taskTypeLabel = "task_type"
)

var (
Expand All @@ -61,6 +62,8 @@ type Metrics struct {
pushPullSnapshotDurationSeconds prometheus.Histogram
pushPullSnapshotBytesTotal prometheus.Counter

backgroundGoroutinesTotal *prometheus.GaugeVec

userAgentTotal *prometheus.CounterVec
}

Expand Down Expand Up @@ -134,6 +137,12 @@ func NewMetrics() (*Metrics, error) {
Name: "snapshot_bytes_total",
Help: "The total bytes of snapshots for response packs in PushPull.",
}),
backgroundGoroutinesTotal: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "background",
Name: "goroutines_total",
Help: "The total number of goroutines attached by a particular background task.",
}, []string{taskTypeLabel}),
userAgentTotal: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "user_agent",
Expand Down Expand Up @@ -234,6 +243,20 @@ func (m *Metrics) AddServerHandledCounter(
}).Inc()
}

// AddBackgroundGoroutines adds the number of goroutines attached by a particular background task.
func (m *Metrics) AddBackgroundGoroutines(taskType string) {
m.backgroundGoroutinesTotal.With(prometheus.Labels{
taskTypeLabel: taskType,
}).Inc()
}

// RemoveBackgroundGoroutines removes the number of goroutines attached by a particular background task.
func (m *Metrics) RemoveBackgroundGoroutines(taskType string) {
m.backgroundGoroutinesTotal.With(prometheus.Labels{
taskTypeLabel: taskType,
}).Dec()
}

// Registry returns the registry of this metrics.
func (m *Metrics) Registry() *prometheus.Registry {
return m.registry
Expand Down
6 changes: 5 additions & 1 deletion test/integration/retention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func TestRetention(t *testing.T) {
patch, err := monkey.PatchInstanceMethodByName(
reflect.TypeOf(b),
"AttachGoroutine",
func(_ *background.Background, f func(c context.Context)) {
func(
_ *background.Background,
f func(c context.Context),
_ string,
) {
f(context.Background())
},
)
Expand Down
6 changes: 5 additions & 1 deletion test/integration/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func TestSnapshot(t *testing.T) {
patch, err := monkey.PatchInstanceMethodByName(
reflect.TypeOf(b),
"AttachGoroutine",
func(_ *background.Background, f func(c context.Context)) {
func(
_ *background.Background,
f func(c context.Context),
_ string,
) {
f(context.Background())
},
)
Expand Down

0 comments on commit d73a0f0

Please sign in to comment.