Skip to content

Commit

Permalink
fix: Prevent data race from global metrics round-tripper (#13641)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Melnick <meln5674@kettering.edu>
  • Loading branch information
meln5674 authored Sep 23, 2024
1 parent 7b35b11 commit 5244064
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions workflow/metrics/metrics_k8s_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ func addK8sRequests(_ context.Context, m *Metrics) error {
return err
}

type metricsRoundTripperContext struct {
ctx context.Context
metrics *Metrics
}

type metricsRoundTripper struct {
ctx context.Context
*metricsRoundTripperContext
roundTripper http.RoundTripper
metrics *Metrics
}

// This is a messy global as we need to register as a roundtripper before
// we can instantiate metrics
var k8sMetrics metricsRoundTripper
var k8sMetrics metricsRoundTripperContext

func (m metricsRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
startTime := time.Now()
Expand All @@ -71,9 +75,7 @@ func AddMetricsTransportWrapper(ctx context.Context, config *rest.Config) *rest.
if wrap != nil {
rt = wrap(rt)
}
k8sMetrics.ctx = ctx
k8sMetrics.roundTripper = rt
return &k8sMetrics
return &metricsRoundTripper{roundTripper: rt, metricsRoundTripperContext: &k8sMetrics}
}
return config
}

0 comments on commit 5244064

Please sign in to comment.