Caching: Fix concurrent HTTP Header read/write in caching middleware (#67231)

read the response header synchronously, defer the metric only
pull/67238/head
Michael Mandrus 2 years ago committed by GitHub
parent 2a67b8ad32
commit 1421f388ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      pkg/services/pluginsintegration/clientmiddleware/caching_middleware.go

@ -56,16 +56,16 @@ func (m *CachingMiddleware) QueryData(ctx context.Context, req *backend.QueryDat
// First look in the query cache if enabled
hit, cr := m.caching.HandleQueryRequest(ctx, req)
defer func() {
// record request duration if caching was used
if ch := reqCtx.Resp.Header().Get(caching.XCacheHeader); ch != "" {
// record request duration if caching was used
if ch := reqCtx.Resp.Header().Get(caching.XCacheHeader); ch != "" {
defer func() {
QueryCachingRequestHistogram.With(prometheus.Labels{
"datasource_type": req.PluginContext.DataSourceInstanceSettings.Type,
"cache": ch,
"query_type": getQueryType(reqCtx),
}).Observe(time.Since(start).Seconds())
}
}()
}()
}
// Cache hit; return the response
if hit {
@ -102,15 +102,15 @@ func (m *CachingMiddleware) CallResource(ctx context.Context, req *backend.CallR
// First look in the resource cache if enabled
hit, cr := m.caching.HandleResourceRequest(ctx, req)
defer func() {
// record request duration if caching was used
if ch := reqCtx.Resp.Header().Get(caching.XCacheHeader); ch != "" {
// record request duration if caching was used
if ch := reqCtx.Resp.Header().Get(caching.XCacheHeader); ch != "" {
defer func() {
ResourceCachingRequestHistogram.With(prometheus.Labels{
"plugin_id": req.PluginContext.PluginID,
"cache": ch,
}).Observe(time.Since(start).Seconds())
}
}()
}()
}
// Cache hit; send the response and return
if hit {

Loading…
Cancel
Save