Skip to content

Commit

Permalink
修改框架查询influxdb的时间单位
Browse files Browse the repository at this point in the history
epoch=ms,与以前虚拟机监控时间单位进行统一
  • Loading branch information
zhaoxiangchun committed Jun 8, 2020
1 parent ecd4a94 commit 86e6870
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/monitor/unifiedmonitor_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ var (
UNIFIED_MONITOR_GROUPBY_OPT_VALUE = map[string][]string{
"fill": {"linear", "none", "previous", "0"},
}

MEASUREMENT_TAG_KEYWORD = map[string]string{
"cpu": "host",
"disk": "device",
"mem": "host",
"net": "host",
"netstat": "host",
"vm_cpu": "vm_name",
"vm_diskio": "vm_name",
"vm_mem": "vm_name",
"vm_netio": "vm_name",
}
)

type MetricFunc struct {
Expand Down
11 changes: 7 additions & 4 deletions pkg/monitor/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"database/sql"
"fmt"
"strings"
"time"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -280,10 +279,14 @@ func getAttributesOnMeasurement(database, tp string, output *monitor.InfluxMeasu
}

func getTagValue(database string, output *monitor.InfluxMeasurement, db *influxdb.SInfluxdb) error {

dbRtn, err := db.Query(fmt.Sprintf("SHOW TAG VALUES ON %s FROM %s WITH KEY IN (%s)", database, output.Measurement, strings.Join(output.TagKey, ",")))
if len(output.TagKey) == 0 {
return nil
}
tagKeyStr := jsonutils.NewStringArray(output.TagKey).String()
tagKeyStr = tagKeyStr[1 : len(tagKeyStr)-1]
dbRtn, err := db.Query(fmt.Sprintf("SHOW TAG VALUES ON %s FROM %s WITH KEY IN (%s)", database, output.Measurement, tagKeyStr))
if err != nil {
return errors.Wrap(err, "SHOW MEASUREMENTS")
return err
}
res := dbRtn[0][0]
tagValue := make(map[string][]string, 0)
Expand Down
56 changes: 56 additions & 0 deletions pkg/monitor/models/unifiedmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
mq "yunion.io/x/onecloud/pkg/monitor/metricquery"
"yunion.io/x/onecloud/pkg/monitor/tsdb"
"yunion.io/x/onecloud/pkg/monitor/validators"
)

Expand Down Expand Up @@ -154,10 +155,22 @@ func (self *SUnifiedMonitorManager) PerformQuery(ctx context.Context, userCred m
return jsonutils.NewDict(), err
}
}

var groupByTag = make([]string, 0)
for _, query := range inputQuery.MetricQuery {
for _, group := range query.Model.GroupBy {
if group.Type == "tag" {
groupByTag = append(groupByTag, group.Params[0])
}
}
}

rtn, err := doQuery(*inputQuery)
if err != nil {
return jsonutils.NewDict(), err
}

setSerieRowName(&rtn.Series, groupByTag)
return jsonutils.Marshal(rtn), nil
}

Expand Down Expand Up @@ -234,3 +247,46 @@ func setDataSourceId(query *monitor.AlertQuery) {
datasource, _ := DataSourceManager.GetDefaultSource()
query.DataSourceId = datasource.Id
}

func setSerieRowName(series *tsdb.TimeSeriesSlice, groupTag []string) {
//Add rowname,The front end displays the curve according to rowname
var index, unknownIndex = 1, 1
for i, serie := range *series {
//setRowName by groupTag
if len(groupTag) != 0 {
for key, val := range serie.Tags {

if strings.Contains(strings.Join(groupTag, ","), key) {
serie.RawName = fmt.Sprintf("%s", val)
(*series)[i] = serie
break
}
}
continue
}

//sep measurement set RowName by spe param
measurement := strings.Split(serie.Name, ".")[0]
if key, ok := monitor.MEASUREMENT_TAG_KEYWORD[measurement]; ok {
serie.RawName = serie.Tags[key]
(*series)[i] = serie
continue
}

//other condition set RowName
for key, val := range serie.Tags {
if strings.Contains(key, "id") {
serie.RawName = fmt.Sprintf("%d: %s", index, val)
(*series)[i] = serie
index++
break
}
}
if serie.RawName == "" {
serie.RawName = fmt.Sprintf("unknown-%d", unknownIndex)
(*series)[i] = serie
unknownIndex++
}
}

}
2 changes: 1 addition & 1 deletion pkg/monitor/tsdb/driver/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (e *InfluxdbExecutor) createRequest(dsInfo *tsdb.DataSource, query string)

params := req.URL.Query()
params.Set("db", dsInfo.Database)
params.Set("epoch", "s")
params.Set("epoch", "ms")

req.Header.Set("Content-type", "application/x-www-form-urlencoded")

Expand Down

0 comments on commit 86e6870

Please sign in to comment.