Skip to content

Commit

Permalink
Merge pull request #50 from jihonrado/fix-problem-with-duplicated-met…
Browse files Browse the repository at this point in the history
…ric-descriptors

Ensure metrics are fetched once for each metric descriptor
  • Loading branch information
frodenas authored Jun 10, 2019
2 parents 59b7f38 + 6dd0d51 commit d863a36
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions collectors/monitoring_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,26 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri

c.apiCallsTotalMetric.Inc()

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

errChannel := make(chan error, len(uniqueDescriptors))

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

for _, metricDescriptor := range page.MetricDescriptors {
for _, metricDescriptor := range uniqueDescriptors {
wg.Add(1)
go func(metricDescriptor *monitoring.MetricDescriptor, ch chan<- prometheus.Metric) {
defer wg.Done()
Expand Down

0 comments on commit d863a36

Please sign in to comment.