import (
"context"
"fmt"
"io"
monitoring "cloud.google.com/go/monitoring/apiv3"
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
)
// getMetricDescriptor gets the descriptor for the given metricType and prints
// information about it. metricType is the type of the metric, for example
// compute.googleapis.com/firewall/dropped_packets_count.
func getMetricDescriptor(w io.Writer, projectID, metricType string) error {
ctx := context.Background()
c, err := monitoring.NewMetricClient(ctx)
if err != nil {
return fmt.Errorf("NewMetricClient: %w", err)
}
defer c.Close()
req := &monitoringpb.GetMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", projectID, metricType),
}
resp, err := c.GetMetricDescriptor(ctx, req)
if err != nil {
return fmt.Errorf("could not get custom metric: %w", err)
}
fmt.Fprintf(w, "Name: %v\n", resp.GetName())
fmt.Fprintf(w, "Description: %v\n", resp.GetDescription())
fmt.Fprintf(w, "Type: %v\n", resp.GetType())
fmt.Fprintf(w, "Metric Kind: %v\n", resp.GetMetricKind())
fmt.Fprintf(w, "Value Type: %v\n", resp.GetValueType())
fmt.Fprintf(w, "Unit: %v\n", resp.GetUnit())
fmt.Fprintf(w, "Labels:\n")
for _, l := range resp.GetLabels() {
fmt.Fprintf(w, "\t%s (%s) - %s", l.GetKey(), l.GetValueType(), l.GetDescription())
}
return nil
}