Skip to content

Commit

Permalink
Merge pull request #18631 from ioito/hotfix/qx-project-tags-cloudmon
Browse files Browse the repository at this point in the history
fix(cloudmon): collect metric with project tags
  • Loading branch information
zexi authored Nov 10, 2023
2 parents c5c57cd + 7560dae commit 42d64e8
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 73 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/compute/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compute/cloudaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/compute/dbinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compute/elasticcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compute/elasticip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 18 additions & 11 deletions pkg/apis/compute/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ package compute

import (
"fmt"
"strings"
"time"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"

"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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/compute/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/compute/kube_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compute/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/apis/compute/modelarts_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/compute/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions pkg/apis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package apis

import (
"strings"
"time"

"yunion.io/x/onecloud/pkg/httperrors"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"`
Expand Down
53 changes: 1 addition & 52 deletions pkg/cloudmon/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 42d64e8

Please sign in to comment.