forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistresources.go
115 lines (96 loc) · 3.01 KB
/
listresources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command listresources lists the Google Cloud Monitoring v3 Environment against an authenticated user.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/monitoring/v3"
)
const metric = "compute.googleapis.com/instance/cpu/usage_time"
func projectResource(projectID string) string {
return "projects/" + projectID
}
// listMonitoredResourceDescriptor lists all the resources available to be monitored in the API.
func listMonitoredResourceDescriptors(s *monitoring.Service, projectID string) error {
resp, err := s.Projects.MonitoredResourceDescriptors.List(projectResource(projectID)).Do()
if err != nil {
return fmt.Errorf("Could not list time series: %v", err)
}
log.Printf("listMonitoredResourceDescriptors: %s\n", formatResource(resp))
return nil
}
// listMetricDescriptors lists the metrics specified by the metric constant.
func listMetricDescriptors(s *monitoring.Service, projectID string) error {
resp, err := s.Projects.MetricDescriptors.List(projectResource(projectID)).
Filter(fmt.Sprintf("metric.type=%q", metric)).
Do()
if err != nil {
return fmt.Errorf("Could not list metric descriptors: %v", err)
}
log.Printf("listMetricDescriptors %s\n", formatResource(resp))
return nil
}
// listTimesSeries lists all the timeseries created for metric created in a 5
// minute interval an hour ago
func listTimeSeries(s *monitoring.Service, projectID string) error {
startTime := time.Now().UTC().Add(-time.Hour)
endTime := startTime.Add(5 * time.Minute)
resp, err := s.Projects.TimeSeries.List(projectResource(projectID)).
PageSize(3).
Filter(fmt.Sprintf("metric.type=\"%s\"", metric)).
IntervalStartTime(startTime.Format(time.RFC3339)).
IntervalEndTime(endTime.Format(time.RFC3339)).
Do()
if err != nil {
return fmt.Errorf("Could not list time series: %v", err)
}
log.Printf("listTimeseries %s\n", formatResource(resp))
return nil
}
func createService(ctx context.Context) (*monitoring.Service, error) {
hc, err := google.DefaultClient(ctx, monitoring.MonitoringScope)
if err != nil {
return nil, err
}
s, err := monitoring.New(hc)
if err != nil {
return nil, err
}
return s, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: listresources <project_id>")
return
}
ctx := context.Background()
s, err := createService(ctx)
if err != nil {
log.Fatal(err)
}
projectID := os.Args[1]
if err := listMonitoredResourceDescriptors(s, projectID); err != nil {
log.Fatal(err)
}
if err := listMetricDescriptors(s, projectID); err != nil {
log.Fatal(err)
}
if err := listTimeSeries(s, projectID); err != nil {
log.Fatal(err)
}
}
// formatResource marshals a response objects as JSON.
func formatResource(resource interface{}) []byte {
b, err := json.MarshalIndent(resource, "", " ")
if err != nil {
panic(err)
}
return b
}