Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Only register unregistered metric descriptors (#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Jan 12, 2022
1 parent 878fd94 commit 8b0e829
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions internal/metricsregistrar/create_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package metricsregistrar

import (
"context"
"errors"
"fmt"
"runtime"
"time"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/google/exposure-notifications-server/pkg/observability"
"github.com/hashicorp/go-multierror"
"golang.org/x/sync/semaphore"
"google.golang.org/api/iterator"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)

Expand All @@ -47,6 +49,25 @@ func (s *Server) createMetrics(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to create metrics client: %w", err)
}
defer client.Close()

// Get the list of descriptors that are already registered.
iter := client.ListMetricDescriptors(context.Background(), &monitoringpb.ListMetricDescriptorsRequest{
Name: "projects/" + projectID,
})
existingMetricDescriptors := make(map[string]struct{})
for {
resp, err := iter.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return fmt.Errorf("failed to list metrics: %w", err)
}

typ := resp.GetType()
existingMetricDescriptors[typ] = struct{}{}
}

// Create the Stackdriver exporter.
exporter, err := observability.NewStackdriver(context.Background(), &observability.StackdriverConfig{
Expand Down Expand Up @@ -74,8 +95,6 @@ func (s *Server) createMetrics(ctx context.Context) error {
return fmt.Errorf("failed to acquire semaphore: %w", err)
}

logger.Infow("registering metrics exporter", "view", view.Name)

go func() {
defer sem.Release(1)

Expand All @@ -85,6 +104,17 @@ func (s *Server) createMetrics(ctx context.Context) error {
return
}

if _, ok := existingMetricDescriptors[metricDescriptor.Type]; ok {
logger.Infow("skipping registration, already registered",
"view", view.Name,
"type", metricDescriptor.Type)
return
}

logger.Infow("registering metrics exporter",
"view", view.Name,
"type", metricDescriptor.Type)

req := &monitoringpb.CreateMetricDescriptorRequest{
Name: "projects/" + projectID,
MetricDescriptor: metricDescriptor,
Expand Down

0 comments on commit 8b0e829

Please sign in to comment.