@@ -20,6 +20,7 @@ import (
20
20
"os"
21
21
"slices"
22
22
"strings"
23
+ "sync"
23
24
24
25
"github.com/PuerkitoBio/rehttp"
25
26
"github.com/alecthomas/kingpin/v2"
@@ -185,6 +186,9 @@ type handler struct {
185
186
metricsExtraFilters []collectors.MetricFilter
186
187
additionalGatherer prometheus.Gatherer
187
188
m * monitoring.Service
189
+
190
+ mu sync.RWMutex
191
+ collectors map [string ]* collectors.MonitoringCollector
188
192
}
189
193
190
194
func (h * handler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
@@ -210,28 +214,46 @@ func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters
210
214
metricsExtraFilters : metricExtraFilters ,
211
215
additionalGatherer : additionalGatherer ,
212
216
m : m ,
217
+ collectors : map [string ]* collectors.MonitoringCollector {},
213
218
}
214
219
215
220
h .handler = h .innerHandler (nil )
216
221
return h
217
222
}
218
223
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
+
219
252
func (h * handler ) innerHandler (filters map [string ]bool ) http.Handler {
220
253
registry := prometheus .NewRegistry ()
221
254
222
255
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 )
235
257
if err != nil {
236
258
h .logger .Error ("error creating monitoring collector" , "err" , err )
237
259
os .Exit (1 )
0 commit comments