Skip to content

Commit

Permalink
check for the existence of metric descriptors before creating (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Oct 22, 2020
1 parent ec19c7e commit 402639d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions exporter/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,7 @@ func (me *metricExporter) exportMetricDescriptor(ctx context.Context, cps export
// the descriptor does not exist in global cache (me.mdCache).
// See details in #26.
for kmd, md := range mds {
req := &monitoringpb.CreateMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s", me.o.ProjectID),
MetricDescriptor: md,
}
_, err := me.client.CreateMetricDescriptor(ctx, req)
err := me.createMetricDescriptorIfNeeded(ctx, md)
if err != nil {
return err
}
Expand All @@ -229,6 +225,26 @@ func (me *metricExporter) exportMetricDescriptor(ctx context.Context, cps export
return nil
}

func (me *metricExporter) createMetricDescriptorIfNeeded(ctx context.Context, md *googlemetricpb.MetricDescriptor) error {
mdReq := &monitoringpb.GetMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", me.o.ProjectID, md.Type),
}
_, err := me.client.GetMetricDescriptor(ctx, mdReq)
if err == nil {
// If the metric descriptor already exists, skip the CreateMetricDescriptor call.
// Metric descriptors cannot be updated without deleting them first, so there
// isn't anything we can do here:
// https://cloud.google.com/monitoring/custom-metrics/creating-metrics#md-modify
return nil
}
req := &monitoringpb.CreateMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s", me.o.ProjectID),
MetricDescriptor: md,
}
_, err = me.client.CreateMetricDescriptor(ctx, req)
return err
}

// exportTimeSeriees create TimeSeries from the records in cps.
// res should be the common resource among all TimeSeries, such as instance id, application name and so on.
func (me *metricExporter) exportTimeSeries(ctx context.Context, cps export.CheckpointSet) error {
Expand Down

0 comments on commit 402639d

Please sign in to comment.