Merge pull request #5268 from zhaoxiangchun/feature/zxc-gcpMon

谷歌云multicloud监控
This commit is contained in:
yunion-ci-robot
2020-02-28 22:10:30 +08:00
committed by GitHub
207 changed files with 40424 additions and 93 deletions
+34
View File
@@ -48,6 +48,7 @@ const (
GOOGLE_STORAGE_API_VERSION = "v1"
GOOGLE_CLOUDBUILD_API_VERSION = "v1"
GOOGLE_BILLING_API_VERSION = "v1"
GOOGLE_MONITOR_API_VERSION = "v3"
GOOGLE_MANAGER_DOMAIN = "https://cloudresourcemanager.googleapis.com"
GOOGLE_COMPUTE_DOMAIN = "https://www.googleapis.com/compute"
@@ -55,6 +56,7 @@ const (
GOOGLE_CLOUDBUILD_DOMAIN = "https://cloudbuild.googleapis.com"
GOOGLE_STORAGE_UPLOAD_DOMAIN = "https://www.googleapis.com/upload/storage"
GOOGLE_BILLING_DOMAIN = "https://cloudbilling.googleapis.com"
GOOGLE_MONITOR_DOMAIN = "https://monitoring.googleapis.com"
MAX_RETRY = 3
)
@@ -398,6 +400,38 @@ func (self *SGoogleClient) billingListAll(resource string, params map[string]str
return items.Unmarshal(retval)
}
func (self *SGoogleClient) monitorList(resource string, params map[string]string) (jsonutils.JSONObject, error) {
return jsonRequest(self.client, "GET", GOOGLE_MONITOR_DOMAIN, GOOGLE_MONITOR_API_VERSION, resource, params, nil, self.Debug)
}
func (self *SGoogleClient) monitorListAll(resource string, params map[string]string, retval interface{}) error {
if params == nil {
params = map[string]string{}
}
timeSeries := jsonutils.NewArray()
nextPageToken := ""
params["pageSize"] = "5000"
for {
params["pageToken"] = nextPageToken
resp, err := self.monitorList(resource, params)
if err != nil {
return errors.Wrap(err, "monitorList")
}
if resp.Contains("timeSeries") {
_series, err := resp.GetArray("timeSeries")
if err != nil {
return errors.Wrap(err, "resp.GetArray")
}
timeSeries.Add(_series...)
}
nextPageToken, _ = resp.GetString("nextPageToken")
if len(nextPageToken) == 0 {
break
}
}
return timeSeries.Unmarshal(retval)
}
func rawRequest(client *http.Client, method httputils.THttpMethod, domain, apiVersion string, resource string, header http.Header, body io.Reader, debug bool) error {
resource = strings.TrimPrefix(resource, fmt.Sprintf("%s/%s/", domain, apiVersion))
resource = fmt.Sprintf("%s/%s/%s", domain, apiVersion, resource)
+29
View File
@@ -0,0 +1,29 @@
package google
import (
"fmt"
"strconv"
"time"
monitoring "cloud.google.com/go/monitoring/apiv3"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)
func (region *SRegion) GetMonitorData(id, serverName, metricName string, since time.Time,
until time.Time) (*monitoring.TimeSeriesIterator, error) {
params := map[string]string{
"filter": `metric.type="` + metricName + `" AND metric.labels.instance_name="` + serverName + `"`,
//"filter": "metric.type=" + metricName + " AND metric.labels.instance_name=" + serverName,
"interval.startTime": since.Format(time.RFC3339),
"interval.endTime": until.Format(time.RFC3339),
"view": strconv.FormatInt(int64(monitoringpb.ListTimeSeriesRequest_FULL), 10),
}
resource := fmt.Sprintf("%s/%s/%s", "projects", id, "timeSeries")
//client, _ := monitoring.NewMetricClient(context.Background())
rtn := monitoring.TimeSeriesIterator{}
err := region.client.monitorListAll(resource, params, &rtn)
if err != nil {
return nil, err
}
return &rtn, nil
}