Skip to content

Commit

Permalink
增加influxdb的tag 的一些filter
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxiangchun committed Jul 6, 2020
1 parent 21d67a4 commit 5db3587
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
36 changes: 36 additions & 0 deletions pkg/monitor/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -42,6 +43,7 @@ import (

var (
DataSourceManager *SDataSourceManager
compile = regexp.MustCompile(`\w{8}(-\w{4}){3}-\w{12}`)
)

const (
Expand Down Expand Up @@ -333,6 +335,9 @@ func getAttributesOnMeasurement(database, tp string, output *monitor.InfluxMeasu
tmpDict := jsonutils.NewDict()
tmpArr := jsonutils.NewArray()
for i := range res.Values {
if filterTagKey(res.Values[i][0].(*jsonutils.JSONString).Value()) {
continue
}
tmpArr.Add(res.Values[i][0])
}
tmpDict.Add(tmpArr, res.Columns[0])
Expand All @@ -355,14 +360,45 @@ func getTagValue(database string, output *monitor.InfluxMeasurement, db *influxd
}
res := dbRtn[0][0]
tagValue := make(map[string][]string, 0)
keys := strings.Join(output.TagKey, ",")
for i := range res.Values {
val := res.Values[i][0].(*jsonutils.JSONString)
if !strings.Contains(keys, val.Value()) {
continue
}
if _, ok := tagValue[val.Value()]; !ok {
tagValue[val.Value()] = make([]string, 0)
}
tag := res.Values[i][1].(*jsonutils.JSONString)
if filterTagValue(tag.Value()) {
delete(tagValue, val.Value())
continue
}
tagValue[val.Value()] = append(tagValue[val.Value()], tag.Value())
}
output.TagValue = tagValue
//TagKey == TagValue.keys
tagK := make([]string, 0)
for tag, _ := range output.TagValue {
tagK = append(tagK, tag)
}
output.TagKey = tagK
return nil
}

func filterTagKey(key string) bool {
if strings.Contains(key, "_id") {
return true
}
if key == "perf_instance" {
return true
}
return false
}

func filterTagValue(val string) bool {
if compile.MatchString(val) {
return true
}
return false
}
10 changes: 2 additions & 8 deletions pkg/monitor/models/suggestsysalert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// Licens"yunion.io/x/onecloud/pkg/util/stringutils2"ed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
Expand All @@ -26,7 +26,6 @@ import (

"yunion.io/x/onecloud/pkg/apis/monitor"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
computemodels "yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/stringutils2"
Expand Down Expand Up @@ -124,12 +123,7 @@ func (manager *SSuggestSysAlertManager) ListItemFilter(
q = q.In("provider", query.Brands)
}
if len(query.Cloudaccount) > 0 {
cpq := computemodels.CloudaccountManager.Query().SubQuery()
subcpq := cpq.Query(cpq.Field("id")).Filter(sqlchemy.OR(
sqlchemy.In(cpq.Field("id"), query.Cloudaccount),
sqlchemy.In(cpq.Field("name"), query.Cloudaccount),
)).SubQuery()
q.In("cloudaccount", subcpq)
q.In("cloudaccount", query.Cloudaccount)
}
if len(query.CloudEnv) > 0 {
q = q.Equals("cloud_env", query.CloudEnv)
Expand Down
4 changes: 2 additions & 2 deletions pkg/monitor/service/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func addCommonAlertDispatcher(prefix string, app *appsrv.Application) {
}

func performHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
_, query, body := fetchEnv(ctx, w, r)
appsrv.SendJSON(w, wrap(jsonutils.NewDict(), "subscription"))
SubscriptionWorkerManager.Run(func() {
_, query, body := fetchEnv(ctx, w, r)
ctx = context.WithValue(context.Background(), auth.AUTH_TOKEN, auth.AdminCredential())
subscriptionmodel.SubscriptionManager.PerformWrite(ctx, auth.AdminCredential(), query, body)
appsrv.SendJSON(w, wrap(jsonutils.NewDict(), "subscription"))
}, nil, nil)

}
Expand Down

0 comments on commit 5db3587

Please sign in to comment.