Merge pull request #16118 from ioito/hotfix/qx-history-metric-pull

fix(cloudmon): support collect history metric
This commit is contained in:
Zexi Li
2023-03-03 17:15:57 +08:00
committed by GitHub
3 changed files with 29 additions and 3 deletions
+2
View File
@@ -31,6 +31,8 @@ type CloudMonOptions struct {
DisableServiceMetric bool `help:"disable service metric collect"`
CollectServiceMetricIntervalMinute int64 `help:"Collect Service metirc Interval unit:minute" default:"5"`
HistoryMetricPullDays int `help:"pull history metrics" default:"-1"`
CloudAccountCollectMetricsBatchCount int `help:"Cloud Account Collect Metrics Batch Count" default:"10"`
CloudResourceCollectMetricsBatchCount int `help:"Cloud Resource Collect Metrics BatchC ount" default:"40"`
}
+8 -2
View File
@@ -372,6 +372,7 @@ type TResource interface {
}
type SResources struct {
init bool
Cloudaccounts TResource
Cloudproviders TResource
DBInstances TResource
@@ -387,6 +388,10 @@ type SResources struct {
Projects TResource
}
func (self *SResources) IsInit() bool {
return self.init
}
func NewResources() *SResources {
return &SResources{
Cloudaccounts: NewBaseResources(&compute.Cloudaccounts),
@@ -466,6 +471,7 @@ func (self *SResources) Init(ctx context.Context, userCred mcclient.TokenCredent
if err != nil {
log.Errorf("Resource init error: %v", err)
}
self.init = true
}
}
@@ -658,8 +664,8 @@ func (self *SResources) CollectMetrics(ctx context.Context, userCred mcclient.To
resources := self.Cloudproviders.getResources(ctx, "")
cloudproviders := map[string]api.CloudproviderDetails{}
jsonutils.Update(&cloudproviders, resources)
sh, _ := time.LoadLocation("Asia/Shanghai")
_endTime := taskStartTime.In(sh)
az, _ := time.LoadLocation(options.Options.TimeZone)
_endTime := taskStartTime.In(az)
_startTime := _endTime.Add(-1 * time.Minute * time.Duration(options.Options.CollectMetricInterval))
var wg sync.WaitGroup
for i := range cloudproviders {
+19 -1
View File
@@ -64,9 +64,27 @@ func StartService() {
go cron.Start()
}
ctx := context.Background()
if opts.HistoryMetricPullDays > 0 {
go func() {
for !res.IsInit() {
log.Infof("wait resources init...")
time.Sleep(time.Second * 10)
}
now := time.Now()
start := now.AddDate(0, 0, -1*opts.HistoryMetricPullDays)
s := auth.GetAdminSession(ctx, opts.BaseOptions.Region)
for start.Before(now) {
log.Infof("start collect history metric from %s", start.Format(time.RFC3339))
res.CollectMetrics(ctx, s.GetToken(), start, false)
start = start.Add(time.Duration(opts.CollectMetricInterval) * time.Minute)
}
log.Infof("collect history metric end")
}()
}
app := app_common.InitApp(baseOpts, true).
OnException(func(method, path string, body jsonutils.JSONObject, err error) {
ctx := context.Background()
session := auth.GetAdminSession(ctx, commonOpts.Region)
notifyclient.EventNotifyServiceAbnormal(ctx, session.GetToken(), consts.GetServiceType(), method, path, body, err)
})