Skip to content

Commit 6dd0d51

Browse files
committed
Ensure metrics are fetcheds once for each metric descriptor
1 parent 59b7f38 commit 6dd0d51

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

collectors/monitoring_collector.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,26 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri
160160

161161
c.apiCallsTotalMetric.Inc()
162162

163-
errChannel := make(chan error, len(page.MetricDescriptors))
163+
// It has been noticed that the same metric descriptor can be obtained from different GCP
164+
// projects. When that happens, metrics are fetched twice and it provokes the error:
165+
// "collected metric xxx was collected before with the same name and label values"
166+
//
167+
// Metric descriptor project is irrelevant when it comes to fetch metrics, as they will be
168+
// fetched from all the delegated projects filtering by metric type. Considering that, we
169+
// can filter descriptors to keep just one per type.
170+
//
171+
// The following makes sure metric descriptors are unique to avoid fetching more than once
172+
uniqueDescriptors := make(map[string]*monitoring.MetricDescriptor)
173+
for _, descriptor := range page.MetricDescriptors {
174+
uniqueDescriptors[descriptor.Type] = descriptor
175+
}
176+
177+
errChannel := make(chan error, len(uniqueDescriptors))
164178

165179
endTime := time.Now().UTC().Add(c.metricsOffset * -1)
166180
startTime := endTime.Add(c.metricsInterval * -1)
167181

168-
for _, metricDescriptor := range page.MetricDescriptors {
182+
for _, metricDescriptor := range uniqueDescriptors {
169183
wg.Add(1)
170184
go func(metricDescriptor *monitoring.MetricDescriptor, ch chan<- prometheus.Metric) {
171185
defer wg.Done()

0 commit comments

Comments
 (0)