new metrics for scan job reports

This commit is contained in:
ahmed 2024-08-13 13:35:48 -04:00
parent 97f8a4834b
commit 1d643f71c4
4 changed files with 95 additions and 1 deletions

29
main.go
View file

@ -13,6 +13,7 @@ import (
"strings"
"sync"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/felixge/fgprof"
@ -542,7 +543,23 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
}
}()
startTime := time.Now()
var totalBytes int64
for metrics := range finishedMetrics {
reportGenerationTotal.Inc()
unitType := "unknown"
if metrics.Unit != nil {
_, unitKind := metrics.Unit.SourceUnitID()
unitType = string(unitKind)
}
unitMetricsTotal.WithLabelValues(unitType).Inc()
if len(metrics.Errors) > 0 {
unitMetricsWithErrors.WithLabelValues(unitType).Inc()
}
metrics.Errors = common.ExportErrors(metrics.Errors...)
details, err := json.Marshal(map[string]any{
"version": 1,
@ -550,12 +567,22 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
})
if err != nil {
ctx.Logger().Error(err, "error marshalling job details")
reportGenerationErrors.WithLabelValues("marshal").Inc()
continue
}
if _, err := jobReportWriter.Write(append(details, '\n')); err != nil {
details = append(details, '\n')
totalBytes += int64(len(details))
if _, err := jobReportWriter.Write(details); err != nil {
ctx.Logger().Error(err, "error writing to file")
reportGenerationErrors.WithLabelValues("write").Inc()
}
}
duration := time.Since(startTime)
reportGenerationDuration.Observe(duration.Seconds())
reportFileSize.Set(float64(totalBytes))
}()
}

59
metrics.go Normal file
View file

@ -0,0 +1,59 @@
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
)
var (
reportGenerationTotal = promauto.NewCounter(prometheus.CounterOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "report_generation_total",
Help: "Total number of report generation attempts",
})
reportGenerationErrors = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "report_generation_errors",
Help: "Total number of errors encountered during report generation, by error type",
},
[]string{"error_type"},
)
unitMetricsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "unit_metrics_total",
Help: "Total number of UnitMetrics generated, by unit type",
},
[]string{"unit_type"},
)
unitMetricsWithErrors = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "unit_metrics_with_errors_total",
Help: "Total number of UnitMetrics containing errors, by unit type",
},
[]string{"unit_type"},
)
reportFileSize = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "report_file_size_bytes",
Help: "Size of the generated report file in bytes",
})
reportGenerationDuration = promauto.NewHistogram(prometheus.HistogramOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "report_generation_duration_seconds",
Help: "Time taken to generate a report",
Buckets: prometheus.ExponentialBuckets(0.1, 2, 10),
})
)

View file

@ -39,6 +39,7 @@ func NewUnitHook(ctx context.Context, opts ...UnitHookOpt) (*UnitHook, <-chan Un
logBackPressure: func() {
once.Do(func() {
ctx.Logger().Info("back pressure detected in unit hook")
backPressureOccurrences.Inc()
})
},
}

View file

@ -21,4 +21,11 @@ var (
Name: "hooks_channel_size",
Help: "Total number of metrics waiting in the finished channel.",
}, nil)
backPressureOccurrences = promauto.NewCounter(prometheus.CounterOpts{
Namespace: common.MetricsNamespace,
Subsystem: common.MetricsSubsystem,
Name: "back_pressure_occurrences_total",
Help: "Total number of times back pressure was detected",
})
)