@@ -20,6 +20,7 @@ import (
2020 "os"
2121 "slices"
2222 "strings"
23+ "sync"
2324
2425 "github.com/PuerkitoBio/rehttp"
2526 "github.com/alecthomas/kingpin/v2"
@@ -185,6 +186,9 @@ type handler struct {
185186 metricsExtraFilters []collectors.MetricFilter
186187 additionalGatherer prometheus.Gatherer
187188 m * monitoring.Service
189+
190+ mu sync.RWMutex
191+ collectors map [string ]* collectors.MonitoringCollector
188192}
189193
190194func (h * handler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
@@ -210,28 +214,46 @@ func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters
210214 metricsExtraFilters : metricExtraFilters ,
211215 additionalGatherer : additionalGatherer ,
212216 m : m ,
217+ collectors : map [string ]* collectors.MonitoringCollector {},
213218 }
214219
215220 h .handler = h .innerHandler (nil )
216221 return h
217222}
218223
224+ func (h * handler ) getCollector (project string , filters map [string ]bool ) (* collectors.MonitoringCollector , error ) {
225+ h .mu .Lock ()
226+ defer h .mu .Unlock ()
227+
228+ collectorKey := fmt .Sprintf ("%s-%v" , project , filters )
229+ if c , ok := h .collectors [collectorKey ]; ok {
230+ return c , nil
231+ }
232+
233+ collector , err := collectors .NewMonitoringCollector (project , h .m , collectors.MonitoringCollectorOptions {
234+ MetricTypePrefixes : h .filterMetricTypePrefixes (filters ),
235+ ExtraFilters : h .metricsExtraFilters ,
236+ RequestInterval : * monitoringMetricsInterval ,
237+ RequestOffset : * monitoringMetricsOffset ,
238+ IngestDelay : * monitoringMetricsIngestDelay ,
239+ FillMissingLabels : * collectorFillMissingLabels ,
240+ DropDelegatedProjects : * monitoringDropDelegatedProjects ,
241+ AggregateDeltas : * monitoringMetricsAggregateDeltas ,
242+ DescriptorCacheTTL : * monitoringDescriptorCacheTTL ,
243+ DescriptorCacheOnlyGoogle : * monitoringDescriptorCacheOnlyGoogle ,
244+ }, h .logger , delta .NewInMemoryCounterStore (h .logger , * monitoringMetricsDeltasTTL ), delta .NewInMemoryHistogramStore (h .logger , * monitoringMetricsDeltasTTL ))
245+ if err != nil {
246+ return nil , err
247+ }
248+ h .collectors [collectorKey ] = collector
249+ return collector , nil
250+ }
251+
219252func (h * handler ) innerHandler (filters map [string ]bool ) http.Handler {
220253 registry := prometheus .NewRegistry ()
221254
222255 for _ , project := range h .projectIDs {
223- monitoringCollector , err := collectors .NewMonitoringCollector (project , h .m , collectors.MonitoringCollectorOptions {
224- MetricTypePrefixes : h .filterMetricTypePrefixes (filters ),
225- ExtraFilters : h .metricsExtraFilters ,
226- RequestInterval : * monitoringMetricsInterval ,
227- RequestOffset : * monitoringMetricsOffset ,
228- IngestDelay : * monitoringMetricsIngestDelay ,
229- FillMissingLabels : * collectorFillMissingLabels ,
230- DropDelegatedProjects : * monitoringDropDelegatedProjects ,
231- AggregateDeltas : * monitoringMetricsAggregateDeltas ,
232- DescriptorCacheTTL : * monitoringDescriptorCacheTTL ,
233- DescriptorCacheOnlyGoogle : * monitoringDescriptorCacheOnlyGoogle ,
234- }, h .logger , delta .NewInMemoryCounterStore (h .logger , * monitoringMetricsDeltasTTL ), delta .NewInMemoryHistogramStore (h .logger , * monitoringMetricsDeltasTTL ))
256+ monitoringCollector , err := h .getCollector (project , filters )
235257 if err != nil {
236258 h .logger .Error ("error creating monitoring collector" , "err" , err )
237259 os .Exit (1 )
0 commit comments