Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a metric to collect the number of background routines #963

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
kokodak marked this conversation as resolved.
Show resolved Hide resolved

// 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()
}
kokodak marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading