From 1421f388aeda7e7c0d733184d9ec51e346843569 Mon Sep 17 00:00:00 2001 From: Michael Mandrus <41969079+mmandrus@users.noreply.github.com> Date: Tue, 25 Apr 2023 13:05:37 -0400 Subject: [PATCH] Caching: Fix concurrent HTTP Header read/write in caching middleware (#67231) read the response header synchronously, defer the metric only --- .../clientmiddleware/caching_middleware.go | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/services/pluginsintegration/clientmiddleware/caching_middleware.go b/pkg/services/pluginsintegration/clientmiddleware/caching_middleware.go index e9e0b50bc0b..194b92832db 100644 --- a/pkg/services/pluginsintegration/clientmiddleware/caching_middleware.go +++ b/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 {