From 7560dae53b70be12caadf8d1802b114fa7439665 Mon Sep 17 00:00:00 2001 From: ioito Date: Fri, 10 Nov 2023 16:06:59 +0800 Subject: [PATCH] fix(cloudmon): collect metric with project tags --- pkg/apis/compute/bucket.go | 2 +- pkg/apis/compute/cloudaccount.go | 2 +- pkg/apis/compute/dbinstance.go | 3 +- pkg/apis/compute/elasticcache.go | 2 +- pkg/apis/compute/elasticip.go | 2 +- pkg/apis/compute/guests.go | 29 ++++++++++------ pkg/apis/compute/host.go | 3 +- pkg/apis/compute/kube_cluster.go | 3 +- pkg/apis/compute/loadbalancer.go | 2 +- pkg/apis/compute/modelarts_pool.go | 3 +- pkg/apis/compute/storage.go | 2 +- pkg/apis/output.go | 27 +++++++++++++++ pkg/cloudmon/resources/resources.go | 53 +---------------------------- 13 files changed, 60 insertions(+), 73 deletions(-) diff --git a/pkg/apis/compute/bucket.go b/pkg/apis/compute/bucket.go index 1c3fb0a11cf..8696e7a5c8d 100644 --- a/pkg/apis/compute/bucket.go +++ b/pkg/apis/compute/bucket.go @@ -80,7 +80,7 @@ func (self BucketDetails) GetMetricTags() map[string]string { "tenant_id": self.ProjectId, "external_id": self.ExternalId, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } func (self BucketDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/cloudaccount.go b/pkg/apis/compute/cloudaccount.go index 75b3d509f1f..66a502a9f13 100644 --- a/pkg/apis/compute/cloudaccount.go +++ b/pkg/apis/compute/cloudaccount.go @@ -370,7 +370,7 @@ func (self CloudaccountDetail) GetMetricTags() map[string]string { "project_domain": self.ProjectDomain, "currency": self.Currency, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } func (self CloudaccountDetail) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/dbinstance.go b/pkg/apis/compute/dbinstance.go index d3168bf3745..4f4afab0e26 100644 --- a/pkg/apis/compute/dbinstance.go +++ b/pkg/apis/compute/dbinstance.go @@ -345,7 +345,8 @@ func (self DBInstanceDetails) GetMetricTags() map[string]string { if len(self.IpAddrs) > 0 { ret["rds_ip"] = strings.ReplaceAll(self.IpAddrs, ",", "|") } - return ret + + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } func (self DBInstanceDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/elasticcache.go b/pkg/apis/compute/elasticcache.go index 48f051f09ee..e5321573f63 100644 --- a/pkg/apis/compute/elasticcache.go +++ b/pkg/apis/compute/elasticcache.go @@ -57,7 +57,7 @@ func (self ElasticcacheDetails) GetMetricTags() map[string]string { "project_domain": self.ProjectDomain, "external_id": self.ExternalId, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } func (self ElasticcacheDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/elasticip.go b/pkg/apis/compute/elasticip.go index 2f00a46c40c..ebdfe1e5c0f 100644 --- a/pkg/apis/compute/elasticip.go +++ b/pkg/apis/compute/elasticip.go @@ -113,7 +113,7 @@ func (self ElasticipDetails) GetMetricTags() map[string]string { "ip_addr": self.IpAddr, "external_id": self.ExternalId, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } type ElasticipSyncstatusInput struct { diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index cdd056a73ac..93858d6fd74 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -16,7 +16,6 @@ package compute import ( "fmt" - "strings" "time" "yunion.io/x/jsonutils" @@ -24,7 +23,6 @@ import ( "yunion.io/x/onecloud/pkg/apis" "yunion.io/x/onecloud/pkg/apis/billing" - "yunion.io/x/onecloud/pkg/apis/cloudcommon/db" imageapi "yunion.io/x/onecloud/pkg/apis/image" "yunion.io/x/onecloud/pkg/httperrors" ) @@ -279,6 +277,22 @@ type Cdrom struct { BootIndex int8 `json:"boot_index"` } +type IMetricResource interface { + GetMetricTags() map[string]string +} + +func AppendMetricTags(ret map[string]string, res ...IMetricResource) map[string]string { + if ret == nil { + ret = map[string]string{} + } + for _, r := range res { + for k, v := range r.GetMetricTags() { + ret[k] = v + } + } + return ret +} + func (self ServerDetails) GetMetricTags() map[string]string { ret := map[string]string{ "id": self.Id, @@ -307,15 +321,8 @@ func (self ServerDetails) GetMetricTags() map[string]string { "account_id": self.AccountId, "external_id": self.ExternalId, } - for k, v := range self.Metadata { - if strings.HasPrefix(k, db.USER_TAG_PREFIX) { - if strings.Contains(k, "login_key") || strings.Contains(v, "=") { - continue - } - ret[k] = v - } - } - return ret + + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } func (self ServerDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index eaa80f3b685..47138500890 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -255,7 +255,8 @@ func (self HostDetails) GetMetricTags() map[string]string { "account_id": self.AccountId, "external_id": self.ExternalId, } - return ret + + return AppendMetricTags(ret, self.MetadataResourceInfo) } func (self HostDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/kube_cluster.go b/pkg/apis/compute/kube_cluster.go index 43a0f7ce865..82cedc0da16 100644 --- a/pkg/apis/compute/kube_cluster.go +++ b/pkg/apis/compute/kube_cluster.go @@ -88,7 +88,8 @@ func (self KubeClusterDetails) GetMetricTags() map[string]string { "account_id": self.AccountId, "external_id": self.ExternalId, } - return ret + + return AppendMetricTags(ret, self.MetadataResourceInfo) } func (self KubeClusterDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/compute/loadbalancer.go b/pkg/apis/compute/loadbalancer.go index ac48b022975..3efa9898b6e 100644 --- a/pkg/apis/compute/loadbalancer.go +++ b/pkg/apis/compute/loadbalancer.go @@ -246,5 +246,5 @@ func (self LoadbalancerDetails) GetMetricTags() map[string]string { "tenant_id": self.ProjectId, "external_id": self.ExternalId, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } diff --git a/pkg/apis/compute/modelarts_pool.go b/pkg/apis/compute/modelarts_pool.go index 259a81a9a69..848362d7fdf 100644 --- a/pkg/apis/compute/modelarts_pool.go +++ b/pkg/apis/compute/modelarts_pool.go @@ -73,7 +73,8 @@ func (self ModelartsPoolDetails) GetMetricTags() map[string]string { "account": self.Account, "external_id": self.ExternalId, } - return ret + + return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo) } type ModelartsPoolListInput struct { diff --git a/pkg/apis/compute/storage.go b/pkg/apis/compute/storage.go index 2744a69fbfb..ee7d5531bc3 100644 --- a/pkg/apis/compute/storage.go +++ b/pkg/apis/compute/storage.go @@ -177,7 +177,7 @@ func (self StorageDetails) GetMetricTags() map[string]string { "project_domain": self.ProjectDomain, "external_id": self.ExternalId, } - return ret + return AppendMetricTags(ret, self.MetadataResourceInfo) } func (self StorageDetails) GetMetricPairs() map[string]string { diff --git a/pkg/apis/output.go b/pkg/apis/output.go index 7ff7b296ffd..b0a2840b4ec 100644 --- a/pkg/apis/output.go +++ b/pkg/apis/output.go @@ -15,6 +15,7 @@ package apis import ( + "strings" "time" "yunion.io/x/onecloud/pkg/httperrors" @@ -133,6 +134,19 @@ type MetadataResourceInfo struct { Metadata map[string]string `json:"metadata"` } +func (self MetadataResourceInfo) GetMetricTags() map[string]string { + ret := map[string]string{} + for k, v := range self.Metadata { + if strings.HasPrefix(k, "user:") { + if strings.Contains(k, "login_key") || strings.Contains(v, "=") { + continue + } + ret[k] = v + } + } + return ret +} + type StatusDomainLevelUserResourceDetails struct { StatusDomainLevelResourceDetails @@ -180,6 +194,19 @@ type ProjectizedResourceInfo struct { ProjectMetadata map[string]string `json:"project_metadata"` } +func (self ProjectizedResourceInfo) GetMetricTags() map[string]string { + ret := map[string]string{} + for k, v := range self.ProjectMetadata { + if strings.HasPrefix(k, "user:") || strings.HasPrefix(k, "org:") { + if strings.Contains(v, "=") { + continue + } + ret["project:"+k] = v + } + } + return ret +} + type ScopedResourceBaseInfo struct { ProjectizedResourceInfo Scope string `json:"scope"` diff --git a/pkg/cloudmon/resources/resources.go b/pkg/cloudmon/resources/resources.go index 746bea14d0e..39776277769 100644 --- a/pkg/cloudmon/resources/resources.go +++ b/pkg/cloudmon/resources/resources.go @@ -27,7 +27,6 @@ import ( "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/tsdb" "yunion.io/x/onecloud/pkg/cloudmon/options" "yunion.io/x/onecloud/pkg/cloudmon/providerdriver" @@ -50,32 +49,6 @@ type sBaseInfo struct { Metadata map[string]string } -type sProjectTag struct { - lock sync.Mutex - tags map[string]map[string]string -} - -func (self *sProjectTag) SetTags(projectId string, tags map[string]string) { - self.lock.Lock() - defer self.lock.Unlock() - self.tags[projectId] = tags -} - -func (self *sProjectTag) GetTags(projectId string) map[string]string { - tags, _ := self.tags[projectId] - return tags -} - -func (self *sProjectTag) RemoveTags(projectId string) { - self.lock.Lock() - defer self.lock.Unlock() - delete(self.tags, projectId) -} - -var projectTags = &sProjectTag{ - tags: map[string]map[string]string{}, -} - type SBaseResources struct { manager modulebase.Manager @@ -134,9 +107,6 @@ func (self *SBaseResources) init(ctx context.Context) error { self.manager.GetKeyword() != identity.Projects.GetKeyword()) { continue } - if self.manager.GetKeyword() == identity.Projects.GetKeyword() { - projectTags.SetTags(baseInfo.Id, baseInfo.Metadata) - } key := baseInfo.ExternalId if len(key) == 0 { key = baseInfo.Id @@ -208,9 +178,6 @@ func (self *SBaseResources) increment(ctx context.Context) error { self.manager.GetKeyword() != compute.Cloudaccounts.GetKeyword()) { continue } - if self.manager.GetKeyword() == identity.Projects.GetKeyword() { - projectTags.SetTags(baseInfo.Id, baseInfo.Metadata) - } key := baseInfo.ExternalId if len(key) == 0 { key = baseInfo.Id @@ -275,9 +242,6 @@ func (self *SBaseResources) decrement(ctx context.Context) error { self.manager.GetKeyword() != identity.Projects.GetKeyword() { continue } - if self.manager.GetKeyword() == identity.Projects.GetKeyword() { - projectTags.RemoveTags(baseInfo.Id) - } key := baseInfo.ExternalId if len(key) == 0 { key = baseInfo.Id @@ -332,9 +296,6 @@ func (self *SBaseResources) update(ctx context.Context) error { for i := range ret { baseInfo := sBaseInfo{} ret[i].Unmarshal(&baseInfo) - if self.manager.GetKeyword() == identity.Projects.GetKeyword() { - projectTags.SetTags(baseInfo.Id, baseInfo.Metadata) - } key := baseInfo.ExternalId if len(key) == 0 { key = baseInfo.Id @@ -736,19 +697,7 @@ func (self *SResources) CollectMetrics(ctx context.Context, userCred mcclient.To if err != nil { log.Errorf("unmarsha server resources error: %v", err) } - for t := range servers { - tags := projectTags.GetTags(servers[t].ProjectId) - if servers[t].Metadata == nil { - server := servers[t] - server.Metadata = map[string]string{} - servers[t] = server - } - for k, v := range tags { - if strings.HasPrefix(k, db.USER_TAG_PREFIX) { - servers[t].Metadata[k] = v - } - } - } + if len(servers) > 0 { err = driver.CollectServerMetrics(ctx, manager, provider, servers, startTime, endTime) if err != nil && errors.Cause(err) != cloudprovider.ErrNotImplemented && errors.Cause(err) != cloudprovider.ErrNotSupported {