fix(cloudmon): k8s metric

This commit is contained in:
ioito
2022-08-31 17:44:17 +08:00
parent 85ec1ef5d8
commit 01cf4864b4
2 changed files with 26 additions and 27 deletions
+6
View File
@@ -15,6 +15,8 @@
package providerdriver
import (
"time"
api "yunion.io/x/onecloud/pkg/apis/compute"
)
@@ -30,6 +32,10 @@ func (self *AliyunCollect) IsSupportMetrics() bool {
return true
}
func (self *AliyunCollect) GetDelayDuration() time.Duration {
return time.Minute * 3
}
func init() {
Register(&AliyunCollect{})
}
+20 -27
View File
@@ -625,43 +625,36 @@ func (self *SAliyunClient) GetElbMetrics(opts *cloudprovider.MetricListOptions)
}
func (self *SAliyunClient) GetK8sMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
metricTags, tagKey := map[string]string{}, ""
metricName := ""
switch opts.MetricType {
case cloudprovider.K8S_NODE_METRIC_TYPE_CPU_USAGE:
metricTags = map[string]string{
"node.cpu.utilization": "",
}
metricName = "node.cpu.utilization"
case cloudprovider.K8S_NODE_METRIC_TYPE_MEM_USAGE:
metricTags = map[string]string{
"node.memory.utilization": "",
}
metricName = "node.memory.utilization"
default:
return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "%s", opts.MetricType)
}
result, err := self.ListMetrics("acs_k8s", metricName, opts.StartTime, opts.EndTime)
if err != nil {
return nil, errors.Wrapf(err, "ListMetrics(%s)", metricName)
}
ret := []cloudprovider.MetricValues{}
for metric, tag := range metricTags {
result, err := self.ListMetrics("acs_k8s", metric, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("ListMetric(%s) error: %v", metric, err)
continue
}
for i := range result {
tags := map[string]string{}
if len(tag) > 0 && len(tagKey) > 0 {
tags[tagKey] = tag
if len(result[i].Node) > 0 {
tags[cloudprovider.METRIC_TAG_NODE] = result[i].Node
}
for i := range result {
ret = append(ret, cloudprovider.MetricValues{
Id: result[i].Cluster,
MetricType: opts.MetricType,
Values: []cloudprovider.MetricValue{
{
Timestamp: time.UnixMilli(result[i].Timestamp),
Value: result[i].GetValue(),
Tags: tags,
},
ret = append(ret, cloudprovider.MetricValues{
Id: result[i].Cluster,
MetricType: opts.MetricType,
Values: []cloudprovider.MetricValue{
{
Timestamp: time.UnixMilli(result[i].Timestamp),
Value: result[i].GetValue(),
Tags: tags,
},
})
}
},
})
}
return ret, nil
}