mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-10-17 15:50:03 +00:00
* feat: add bounded CPU profiling * fix(profiling): defer cooldown-skipped samples and configure triggers
32 lines
951 B
Go
32 lines
951 B
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/profiling"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func configureProfiling(ctx context.Context, pool *pgxpool.Pool) *profiling.Recorder {
|
|
r, err := profiling.Start(ctx, os.Getenv("BEACON_CPU_PROFILE_DIR"), os.Getenv("BEACON_CPU_PROFILE_UNTIL"), []string{"reconfirm"}, func() any {
|
|
s := pool.Stat()
|
|
return struct {
|
|
Acquired, Idle, Total, Max int32
|
|
Acquires, EmptyAcquires, CanceledAcquires int64
|
|
AcquireDurationNS int64
|
|
}{
|
|
s.AcquiredConns(), s.IdleConns(), s.TotalConns(), s.MaxConns(),
|
|
s.AcquireCount(), s.EmptyAcquireCount(), s.CanceledAcquireCount(), int64(s.AcquireDuration()),
|
|
}
|
|
})
|
|
if err != nil {
|
|
slog.Warn("CPU profiling unavailable", "component", "profiling", "error", err)
|
|
}
|
|
return r
|
|
}
|