Skip to content

Commit

Permalink
Modify the Background struct to have metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kokodak committed Aug 18, 2024
1 parent 601eb83 commit 8870618
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 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
11 changes: 7 additions & 4 deletions server/backend/background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,23 @@ 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),
metrics *prometheus.Metrics,
taskType string,
) {
b.wgMu.RLock() // this blocks with ongoing close(b.closing)
Expand All @@ -78,11 +81,11 @@ func (b *Background) AttachGoroutine(
// now safe to add since WaitGroup wait has not started yet
b.wg.Add(1)
routineLogger := logging.New(b.routineID.next())
metrics.AddBackgroundGoroutines(taskType)
b.metrics.AddBackgroundGoroutines(taskType)
go func() {
defer func() {
b.wg.Done()
metrics.RemoveBackgroundGoroutines(taskType)
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(),
)
}, be.Metrics, "pushpull")
}, "pushpull")
}

return respPack, nil
Expand Down

0 comments on commit 8870618

Please sign in to comment.