Files
hatchet/pkg/config/loader/debugger.go
T
abelanger5 2cdee59aea refactor: optimize v0.50.0 release (#975)
- Simplifies architecture for splitting engine services into different components. The three supported services are now `grpc-api`, `scheduler`, and `controllers`. The `grpc-api` service is the only one which needs to be exposed for workers. The other two can run as unexposed services.
- Fixes a set of bugs and race conditions in the `v2` scheduler
- Adds a `lastActive` time to the `Queue` table and includes a migration which sets this `lastActive` time for the most recent 24 hours of queues. Effectively this means that the max scheduling time in a queue is 24 hours. 
- Rewrites the `ListWorkflowsForEvent` query to improve performance and select far fewer rows.
2024-10-23 12:05:16 +00:00

88 lines
1.3 KiB
Go

package loader
import (
"context"
"fmt"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog"
)
type debugger struct {
callerCounts map[string]int
callerMu sync.Mutex
lastPrint *time.Time
l *zerolog.Logger
}
func (d *debugger) beforeAcquire(ctx context.Context, conn *pgx.Conn) bool {
_, file, line, ok := runtime.Caller(4)
caller := "unknown"
if ok {
if strings.Contains(file, "tx.go") {
_, file, line, _ = runtime.Caller(5)
}
caller = fmt.Sprintf("%s:%d", file, line)
}
d.callerMu.Lock()
d.callerCounts[caller]++
d.callerMu.Unlock()
if d.lastPrint == nil || time.Since(*d.lastPrint) > 15*time.Second {
d.printCallerCounts()
}
return true
}
type callerCount struct {
caller string
count int
}
func (d *debugger) printCallerCounts() {
d.callerMu.Lock()
defer d.callerMu.Unlock()
var counts []callerCount
for caller, count := range d.callerCounts {
counts = append(counts, callerCount{caller, count})
}
sort.Slice(counts, func(i, j int) bool {
return counts[i].count > counts[j].count
})
sl := d.l.Debug()
for i, c := range counts {
// print only the top 20 callers
if i >= 20 {
break
}
sl.Int(
c.caller,
c.count,
)
}
sl.Msg("top 20 callers")
d.callerCounts = make(map[string]int)
now := time.Now()
d.lastPrint = &now
}