From 9016d85718386ab93862d1c88c4a31b035559fe6 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Wed, 6 Mar 2024 12:47:26 -0600 Subject: [PATCH] metrics: measure run operations for exec operations This measures the duration of exec operations. It does not factor in whether the operation was cached or not so this should include the amount of time to determine whether an operation was cached. Signed-off-by: Jonathan A. Sternberg --- util/progress/metricwriter.go | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/util/progress/metricwriter.go b/util/progress/metricwriter.go index 5a37c997..7eb90d1f 100644 --- a/util/progress/metricwriter.go +++ b/util/progress/metricwriter.go @@ -24,6 +24,7 @@ func newMetrics(mp metric.MeterProvider, attrs attribute.Set) *metricWriter { recorders: []metricRecorder{ newLocalSourceTransferMetricRecorder(meter, attrs), newImageSourceTransferMetricRecorder(meter, attrs), + newExecMetricRecorder(meter, attrs), }, attrs: attrs, } @@ -238,3 +239,40 @@ var reImageSourceType = regexp.MustCompile(`^\[.*] FROM `) func detectImageSourceType(vertexName string) bool { return reImageSourceType.MatchString(vertexName) } + +type ( + execMetricRecorder struct { + // Attributes holds the attributes for this metric recorder. + Attributes attribute.Set + + // Duration tracks the duration of exec statements. + Duration metric.Float64Counter + } +) + +func newExecMetricRecorder(meter metric.Meter, attrs attribute.Set) *execMetricRecorder { + mr := &execMetricRecorder{ + Attributes: attrs, + } + mr.Duration, _ = meter.Float64Counter("exec.command.time", + metric.WithDescription("Measures the length of time spent executing run statements."), + metric.WithUnit("ms")) + return mr +} + +func (mr *execMetricRecorder) Record(ss *client.SolveStatus) { + for _, v := range ss.Vertexes { + if v.Started == nil || v.Completed == nil || !detectExecType(v.Name) { + continue + } + + dur := float64(v.Completed.Sub(*v.Started)) / float64(time.Millisecond) + mr.Duration.Add(context.Background(), dur, metric.WithAttributeSet(mr.Attributes)) + } +} + +var reExecType = regexp.MustCompile(`^\[.*] RUN `) + +func detectExecType(vertexName string) bool { + return reExecType.MatchString(vertexName) +}