// createGet creates an example uptime check on a GET request.
func createGet(w io.Writer, projectID string) (*monitoringpb.UptimeCheckConfig, error) {
ctx := context.Background()
client, err := monitoring.NewUptimeCheckClient(ctx)
if err != nil {
return nil, fmt.Errorf("NewUptimeCheckClient: %w", err)
}
defer client.Close()
req := &monitoringpb.CreateUptimeCheckConfigRequest{
Parent: "projects/" + projectID,
UptimeCheckConfig: &monitoringpb.UptimeCheckConfig{
DisplayName: "new GET uptime check",
Resource: &monitoringpb.UptimeCheckConfig_MonitoredResource{
MonitoredResource: &monitoredres.MonitoredResource{
Type: "uptime_url",
Labels: map[string]string{
"host": "example.com",
},
},
},
CheckRequestType: &monitoringpb.UptimeCheckConfig_HttpCheck_{
HttpCheck: &monitoringpb.UptimeCheckConfig_HttpCheck{
RequestMethod: monitoringpb.UptimeCheckConfig_HttpCheck_GET,
Path: "/",
Port: 80,
},
},
Timeout: &duration.Duration{Seconds: 10},
Period: &duration.Duration{Seconds: 300},
},
}
config, err := client.CreateUptimeCheckConfig(ctx, req)
if err != nil {
return nil, fmt.Errorf("CreateUptimeCheckConfig GET: %w", err)
}
fmt.Fprintf(w, "Successfully created GET uptime check %q\n", config.GetDisplayName())
return config, nil
}
// createPost creates an example uptime check on a POST request.
func createPost(w io.Writer, projectID string) (*monitoringpb.UptimeCheckConfig, error) {
ctx := context.Background()
client, err := monitoring.NewUptimeCheckClient(ctx)
if err != nil {
return nil, fmt.Errorf("NewUptimeCheckClient: %w", err)
}
defer client.Close()
req := &monitoringpb.CreateUptimeCheckConfigRequest{
Parent: "projects/" + projectID,
UptimeCheckConfig: &monitoringpb.UptimeCheckConfig{
DisplayName: "new POST uptime check",
Resource: &monitoringpb.UptimeCheckConfig_MonitoredResource{
MonitoredResource: &monitoredres.MonitoredResource{
Type: "uptime_url",
Labels: map[string]string{
"host": "example.com",
},
},
},
CheckRequestType: &monitoringpb.UptimeCheckConfig_HttpCheck_{
HttpCheck: &monitoringpb.UptimeCheckConfig_HttpCheck{
RequestMethod: monitoringpb.UptimeCheckConfig_HttpCheck_POST,
ContentType: monitoringpb.UptimeCheckConfig_HttpCheck_URL_ENCODED,
Path: "/",
Port: 80,
Body: []byte(base64.URLEncoding.EncodeToString([]byte("key: value"))),
},
},
Timeout: &duration.Duration{Seconds: 10},
Period: &duration.Duration{Seconds: 300},
},
}
config, err := client.CreateUptimeCheckConfig(ctx, req)
if err != nil {
return nil, fmt.Errorf("CreateUptimeCheckConfig POST: %w", err)
}
fmt.Fprintf(w, "Successfully created POST uptime check %q\n", config.GetDisplayName())
return config, nil
}