Add prometheus metrics to measure hook execution time (#2312)

* Add prometheus metrics to measure hook execution time

* Move metrics to separate file and reduce buckets
This commit is contained in:
Miccah 2024-01-22 11:47:45 -08:00 committed by GitHub
parent 383f8a1f67
commit 2d96b89554
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View file

@ -183,6 +183,10 @@ func (jp *JobProgress) TrackProgress(progress *Progress) {
// executeHooks is a helper method to execute all the hooks for the given
// closure.
func (jp *JobProgress) executeHooks(todo func(hook JobProgressHook)) {
defer func(start time.Time) {
elapsed := time.Since(start).Milliseconds()
hooksExecTime.WithLabelValues().Observe(float64(elapsed))
}(time.Now())
for _, hook := range jp.hooks {
// TODO: Non-blocking?
todo(hook)

17
pkg/sources/metrics.go Normal file
View file

@ -0,0 +1,17 @@
package sources
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)
var (
hooksExecTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "hooks_exec_time_ms",
Help: "Time spent executing hooks (ms)",
Buckets: []float64{5, 50, 500, 1000},
}, nil)
)