Last active
September 28, 2020 13:58
-
-
Save zdunecki/fd341ad8461cd998726b51ec52cf51f2 to your computer and use it in GitHub Desktop.
script to delete all custom metrics from a stackdriver account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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