Skip to content

Instantly share code, notes, and snippets.

@rikatz
Created March 23, 2021 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikatz/d9dc76d27b8371d590924508bd2be6c0 to your computer and use it in GitHub Desktop.
Save rikatz/d9dc76d27b8371d590924508bd2be6c0 to your computer and use it in GitHub Desktop.
Monitoring GCP with Go library
package main
import (
"context"
"fmt"
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
"google.golang.org/protobuf/types/known/durationpb"
)
func main() {
ctx := context.Background()
aClient, err := monitoring.NewAlertPolicyClient(ctx)
if err != nil {
panic(err)
}
defer aClient.Close()
// Need to be the full path
apolReq := &monitoringpb.GetAlertPolicyRequest{
Name: "projects/my-awesome-project/alertPolicies/4762738326720958127",
}
alertPolicy, err := aClient.GetAlertPolicy(ctx, apolReq)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", alertPolicy)
channelReq := &monitoringpb.GetNotificationChannelRequest{
Name: alertPolicy.NotificationChannels[0],
}
chanClient, err := monitoring.NewNotificationChannelClient(ctx)
if err != nil {
panic(err)
}
defer chanClient.Close()
notificationChannel, err := chanClient.GetNotificationChannel(ctx, channelReq)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", notificationChannel)
// Createing new Alert Policy
// Aaargh so many structs inside structs inside structs :P
condType := &monitoringpb.AlertPolicy_Condition_ConditionThreshold{
ConditionThreshold: &monitoringpb.AlertPolicy_Condition_MetricThreshold{
Filter: "resource.type = \"global\" metric.type = \"custom.googleapis.com/opencensus/certificate_expiration_remaining_seconds\"",
Comparison: monitoringpb.ComparisonType_COMPARISON_LT,
ThresholdValue: float64(50),
Duration: &durationpb.Duration{
Seconds: 300,
},
},
}
newCondition := &monitoringpb.AlertPolicy_Condition{
DisplayName: "My Condition",
Condition: condType,
}
cond := []*monitoringpb.AlertPolicy_Condition{newCondition}
aPolicy := &monitoringpb.AlertPolicy{
DisplayName: "testkatz",
Conditions: cond,
Combiner: monitoringpb.AlertPolicy_AND,
NotificationChannels: []string{"projects/my-awesome-project/notificationChannels/12933047419693956583"},
}
req := &monitoringpb.CreateAlertPolicyRequest{
Name: "projects/my-awesome-project",
AlertPolicy: aPolicy,
}
resp, err := aClient.CreateAlertPolicy(ctx, req)
if err != nil {
panic(err)
}
fmt.Printf("Policy created: %+v\n", resp)
}
$ go run main.go
name:"projects/my-awesome-project/alertPolicies/4762738326720958127" display_name:"Certificate expiration seconds below the threshold" conditions:{name:"projects/my-awesome-project/alertPolicies/4762738326720958127/conditions/4762738326720954868" display_name:"OpenCensus/certificate_expiration_remaining_seconds [MEAN]" condition_threshold:{filter:"metric.type=\"custom.googleapis.com/opencensus/certificate_expiration_remaining_seconds\" resource.type=\"global\"" aggregations:{alignment_period:{seconds:300} per_series_aligner:ALIGN_MEAN} comparison:COMPARISON_LT threshold_value:432000 duration:{} trigger:{count:1}}} combiner:OR enabled:{value:true} notification_channels:"projects/my-awesome-project/notificationChannels/12933047419693956583" creation_record:{mutate_time:{seconds:1612966766 nanos:484009169} mutated_by:"me@mail.com"} mutation_record:{mutate_time:{seconds:1612966766 nanos:484009169} mutated_by:"me@mail.com"}
type:"slack" name:"projects/my-awesome-project/notificationChannels/12933047419693956583" display_name:"CertBot" labels:{key:"auth_token" value:"******************************************************"} labels:{key:"channel_name" value:"#cert-monitoring"} enabled:{value:true} 12:"ALOTOFTHINGS" 13:"ALOTOFTHINGS"
Policy created: name:"projects/my-awesome-project/alertPolicies/914161344101573541" display_name:"testkatz1" conditions:{name:"projects/my-awesome-project/alertPolicies/914161344101573541/conditions/914161344101575194" display_name:"My Condition" condition_threshold:{filter:"resource.type = \"global\" metric.type = \"custom.googleapis.com/opencensus/certificate_expiration_remaining_seconds\"" comparison:COMPARISON_LT threshold_value:50 duration:{seconds:300}}} combiner:AND enabled:{value:true} notification_channels:"projects/my-awesome-project/notificationChannels/12933047419693956583" creation_record:{mutate_time:{seconds:1616457361 nanos:809946932} mutated_by:"monittest@my-awesome-project.iam.gserviceaccount.com"} mutation_record:{mutate_time:{seconds:1616457361 nanos:809946932} mutated_by:"monittest@my-awesome-project.iam.gserviceaccount.com"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment