Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent data race from global metrics round-tripper #13641

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading