chore: add more pprof labels for various go routines (#19243)

- ReplicaSync
- Notifications
- MetricsAggregator
- DBPurge
This commit is contained in:
Steven Masley
2025-08-07 20:05:32 +00:00
committed by GitHub
parent c65996a041
commit 0a3afeddc8
5 changed files with 25 additions and 14 deletions
+6 -5
View File
@@ -12,6 +12,7 @@ import (
"github.com/coder/coder/v2/coderd/database" "github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime" "github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/pproflabel"
"github.com/coder/quartz" "github.com/coder/quartz"
) )
@@ -38,7 +39,7 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz.
// Start the ticker with the initial delay. // Start the ticker with the initial delay.
ticker := clk.NewTicker(delay) ticker := clk.NewTicker(delay)
doTick := func(start time.Time) { doTick := func(ctx context.Context, start time.Time) {
defer ticker.Reset(delay) defer ticker.Reset(delay)
// Start a transaction to grab advisory lock, we don't want to run // Start a transaction to grab advisory lock, we don't want to run
// multiple purges at the same time (multiple replicas). // multiple purges at the same time (multiple replicas).
@@ -85,21 +86,21 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz.
} }
} }
go func() { pproflabel.Go(ctx, pproflabel.Service(pproflabel.ServiceDBPurge), func(ctx context.Context) {
defer close(closed) defer close(closed)
defer ticker.Stop() defer ticker.Stop()
// Force an initial tick. // Force an initial tick.
doTick(dbtime.Time(clk.Now()).UTC()) doTick(ctx, dbtime.Time(clk.Now()).UTC())
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case tick := <-ticker.C: case tick := <-ticker.C:
ticker.Stop() ticker.Stop()
doTick(dbtime.Time(tick).UTC()) doTick(ctx, dbtime.Time(tick).UTC())
} }
} }
}() })
return &instance{ return &instance{
cancel: cancelFunc, cancel: cancelFunc,
closed: closed, closed: closed,
+4 -3
View File
@@ -11,12 +11,13 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"cdr.dev/slog" "cdr.dev/slog"
"github.com/coder/quartz"
"github.com/coder/coder/v2/coderd/database" "github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/pubsub" "github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/coderd/notifications/dispatch" "github.com/coder/coder/v2/coderd/notifications/dispatch"
"github.com/coder/coder/v2/coderd/pproflabel"
"github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/codersdk"
"github.com/coder/quartz"
) )
var ErrInvalidDispatchTimeout = xerrors.New("dispatch timeout must be less than lease period") var ErrInvalidDispatchTimeout = xerrors.New("dispatch timeout must be less than lease period")
@@ -145,7 +146,7 @@ func (m *Manager) Run(ctx context.Context) {
m.runOnce.Do(func() { m.runOnce.Do(func() {
// Closes when Stop() is called or context is canceled. // Closes when Stop() is called or context is canceled.
go func() { pproflabel.Go(ctx, pproflabel.Service(pproflabel.ServiceNotifications), func(ctx context.Context) {
err := m.loop(ctx) err := m.loop(ctx)
if err != nil { if err != nil {
if xerrors.Is(err, ErrManagerAlreadyClosed) { if xerrors.Is(err, ErrManagerAlreadyClosed) {
@@ -154,7 +155,7 @@ func (m *Manager) Run(ctx context.Context) {
m.log.Error(ctx, "notification manager stopped with error", slog.Error(err)) m.log.Error(ctx, "notification manager stopped with error", slog.Error(err))
} }
} }
}() })
}) })
} }
+9 -1
View File
@@ -21,9 +21,17 @@ const (
ServiceHTTPServer = "http-api" ServiceHTTPServer = "http-api"
ServiceLifecycles = "lifecycle-executor" ServiceLifecycles = "lifecycle-executor"
ServiceMetricCollector = "metrics-collector"
ServicePrebuildReconciler = "prebuilds-reconciler" ServicePrebuildReconciler = "prebuilds-reconciler"
ServiceTerraformProvisioner = "terraform-provisioner" ServiceTerraformProvisioner = "terraform-provisioner"
ServiceDBPurge = "db-purge"
ServiceNotifications = "notifications"
ServiceReplicaSync = "replica-sync"
// ServiceMetricCollector collects metrics from insights in the database and
// exports them in a prometheus collector format.
ServiceMetricCollector = "metrics-collector"
// ServiceAgentMetricAggregator merges agent metrics and exports them in a
// prometheus collector format.
ServiceAgentMetricAggregator = "agent-metrics-aggregator"
RequestTypeTag = "coder_request_type" RequestTypeTag = "coder_request_type"
) )
+4 -4
View File
@@ -11,11 +11,11 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/agentmetrics"
"cdr.dev/slog" "cdr.dev/slog"
agentproto "github.com/coder/coder/v2/agent/proto" agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/agentmetrics"
"github.com/coder/coder/v2/coderd/pproflabel"
) )
const ( const (
@@ -298,7 +298,7 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
done := make(chan struct{}) done := make(chan struct{})
cleanupTicker := time.NewTicker(ma.metricsCleanupInterval) cleanupTicker := time.NewTicker(ma.metricsCleanupInterval)
go func() { pproflabel.Go(ctx, pproflabel.Service(pproflabel.ServiceAgentMetricAggregator), func(ctx context.Context) {
defer close(done) defer close(done)
defer cleanupTicker.Stop() defer cleanupTicker.Stop()
@@ -395,7 +395,7 @@ func (ma *MetricsAggregator) Run(ctx context.Context) func() {
return return
} }
} }
}() })
return func() { return func() {
cancelFunc() cancelFunc()
<-done <-done
+2 -1
View File
@@ -23,6 +23,7 @@ import (
"github.com/coder/coder/v2/coderd/database/dbauthz" "github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime" "github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/database/pubsub" "github.com/coder/coder/v2/coderd/database/pubsub"
"github.com/coder/coder/v2/coderd/pproflabel"
) )
var PubsubEvent = "replica" var PubsubEvent = "replica"
@@ -104,7 +105,7 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, ps pubsub.P
return nil, xerrors.Errorf("subscribe: %w", err) return nil, xerrors.Errorf("subscribe: %w", err)
} }
manager.closeWait.Add(1) manager.closeWait.Add(1)
go manager.loop(ctx) pproflabel.Go(ctx, pproflabel.Service(pproflabel.ServiceReplicaSync), manager.loop)
return manager, nil return manager, nil
} }