Skip to content

Instantly share code, notes, and snippets.

@zdunecki
Last active September 28, 2020 13:58
Show Gist options
  • Select an option

  • Save zdunecki/fd341ad8461cd998726b51ec52cf51f2 to your computer and use it in GitHub Desktop.

Select an option

Save zdunecki/fd341ad8461cd998726b51ec52cf51f2 to your computer and use it in GitHub Desktop.
script to delete all custom metrics from a stackdriver account
package main
import (
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"context"
"google.golang.org/api/iterator"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
"log"
)
func deleteCustomMetrics(projectID string) {
ctx := context.Background()
c, err := monitoring.NewMetricClient(ctx)
if err != nil {
log.Fatal(err)
}
req := &monitoringpb.ListMetricDescriptorsRequest{
Name: "projects/" + projectID,
Filter: `metric.type = starts_with("custom.googleapis.com")`,
}
metricDescriptors := c.ListMetricDescriptors(ctx, req)
for {
resp, err := metricDescriptors.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
deleteReq := &monitoringpb.DeleteMetricDescriptorRequest{
Name: resp.Name,
}
if err := c.DeleteMetricDescriptor(ctx, deleteReq); err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment