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 1 commit
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
13 changes: 11 additions & 2 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 Down Expand Up @@ -60,7 +61,11 @@ func New() *Background {

// 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),
metrics *prometheus.Metrics,
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
taskType string,
) {
b.wgMu.RLock() // this blocks with ongoing close(b.closing)
defer b.wgMu.RUnlock()
select {
Expand All @@ -73,8 +78,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())
metrics.AddBackgroundGoroutines(taskType)
kokodak marked this conversation as resolved.
Show resolved Hide resolved
go func() {
defer b.wg.Done()
defer func() {
b.wg.Done()
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(),
)
})
}, be.Metrics, "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
Loading