@@ -160,12 +160,26 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri
160
160
161
161
c .apiCallsTotalMetric .Inc ()
162
162
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 ))
164
178
165
179
endTime := time .Now ().UTC ().Add (c .metricsOffset * - 1 )
166
180
startTime := endTime .Add (c .metricsInterval * - 1 )
167
181
168
- for _ , metricDescriptor := range page . MetricDescriptors {
182
+ for _ , metricDescriptor := range uniqueDescriptors {
169
183
wg .Add (1 )
170
184
go func (metricDescriptor * monitoring.MetricDescriptor , ch chan <- prometheus.Metric ) {
171
185
defer wg .Done ()
0 commit comments