Merge pull request #12215 from zhaoxiangchun/automated-cherry-pick-of-#12214-upstream-release-3.8

Automated cherry pick of #12214: fix(cloudmon): Azure metric pull support metric interval
This commit is contained in:
Zexi Li
2021-09-15 10:02:40 +08:00
committed by GitHub
4 changed files with 32 additions and 15 deletions

View File

@@ -62,7 +62,7 @@ func (self *SAzureCloudReport) collectRegionMetricOfHost(region cloudprovider.IC
}
metricNames := strings.Join(metricNameArr, ",")
azureReg.GetClient().Debug(true)
rtnMetrics, err := azureReg.GetMonitorData(metricNames, ns, external_id, since, until)
rtnMetrics, err := azureReg.GetMonitorData(metricNames, ns, external_id, since, until, self.Args.MetricInterval)
if err != nil {
log.Errorf("get metrics for server %s error: %v", srvPrefix, err)
continue

View File

@@ -18,6 +18,7 @@ import (
"fmt"
"net/http"
"net/url"
"time"
"golang.org/x/net/http/httpproxy"
@@ -220,12 +221,25 @@ func (self *CloudReportBase) CollectRegionMetric(region cloudprovider.ICloudRegi
func (self *CloudReportBase) ListAllResources(manager modulebase.Manager,
query *jsonutils.JSONDict) ([]jsonutils.JSONObject, error) {
return ListAllResources(manager, self.Session, query)
}
func ListAllResources(manager modulebase.Manager, session *mcclient.ClientSession,
query *jsonutils.JSONDict) ([]jsonutils.JSONObject, error) {
offsetIndex := 0
resources := make([]jsonutils.JSONObject, 0)
tryTimes := 5
i := 0
for {
i++
query.Add(jsonutils.NewInt(int64(offsetIndex)), "offset")
resList, err := manager.List(self.Session, query)
resList, err := manager.List(session, query)
if err != nil {
if i <= tryTimes {
time.Sleep(3 * time.Second)
continue
}
return nil, err
}
resources = append(resources, resList.Data...)

View File

@@ -75,13 +75,14 @@ var OtherHostTag = map[string]string{
}
type ReportOptions struct {
Batch int `help:"batch"`
Count int `help:"count" json:"count"`
Interval string `help:"interval""`
Timeout int64 `help:"command timeout unit:second" default:"10"`
SinceTime string `help:"sinceTime"`
EndTime string `help:"endTime"`
Provider []string `help:"List objects from the provider" choices:"VMware|Aliyun|Qcloud|Azure|Aws|Huawei|ZStack|Google|Apsara|JDcloud|Ecloud|HCSO" json:"provider,omitempty"`
Batch int `help:"batch"`
Count int `help:"count" json:"count"`
Interval string `help:"interval""`
Timeout int64 `help:"command timeout unit:second" default:"10"`
SinceTime string `help:"sinceTime"`
EndTime string `help:"endTime"`
Provider []string `help:"List objects from the provider" choices:"VMware|Aliyun|Qcloud|Azure|Aws|Huawei|ZStack|Google|Apsara|JDcloud|Ecloud|HCSO" json:"provider,omitempty"`
MetricInterval string `help:"metric interval eg:PT1M"`
PingProbeOptions
}
@@ -415,7 +416,7 @@ func SendMetrics(s *mcclient.ClientSession, metrics []influxdb.SMetricData, debu
func ReportCloudMetricOfoperatorType(operatorType string, session *mcclient.ClientSession, args *ReportOptions) error {
query := jsonutils.NewDict()
query.Add(jsonutils.NewString("0"), KEY_LIMIT)
query.Add(jsonutils.NewString("10"), KEY_LIMIT)
query.Add(jsonutils.NewString("true"), KEY_ADMIN)
//query.Add(jsonutils.NewString("true"), KEY_USABLE)
if len(args.Provider) > 0 {
@@ -423,7 +424,7 @@ func ReportCloudMetricOfoperatorType(operatorType string, session *mcclient.Clie
query.Add(jsonutils.NewString(val), "provider")
}
}
cloudProviderList, err := (&modules.Cloudproviders).List(session, query)
cloudProviderList, err := ListAllResources(&modules.Cloudproviders, session, query)
if err != nil {
return errors.Wrap(err, "cloudProviders get list error")
}
@@ -432,8 +433,8 @@ func ReportCloudMetricOfoperatorType(operatorType string, session *mcclient.Clie
if args.Count == 0 {
args.Count = 1
}
for i := 0; i < len(cloudProviderList.Data); i++ {
provider := cloudProviderList.Data[i]
for i := 0; i < len(cloudProviderList); i++ {
provider := cloudProviderList[i]
status, err := provider.GetString("status")
if err != nil {
return errors.Wrap(err, "provider get status error")

View File

@@ -85,12 +85,14 @@ type MetricValue struct {
Count *float64 `json:"count,omitempty"`
}
func (self *SRegion) GetMonitorData(name string, ns string, resourceId string, since time.Time,
until time.Time) (*ResponseMetirc, error) {
func (self *SRegion) GetMonitorData(name string, ns string, resourceId string, since time.Time, until time.Time, interval string) (*ResponseMetirc, error) {
params := url.Values{}
params.Set("metricnamespace", ns)
params.Set("metricnames", name)
params.Set("interval", "PT1M")
if len(interval) != 0 {
params.Set("interval", interval)
}
params.Set("aggregation", "Average")
params.Set("api-version", "2018-01-01")
if !since.IsZero() && !until.IsZero() {