fix(monitor): add difference tags for each series (#20618)

This commit is contained in:
Zexi Li
2024-06-22 22:05:51 +08:00
committed by GitHub
parent 81faf84bbc
commit 6434ac2876
5 changed files with 31 additions and 9 deletions
+1 -1
View File
@@ -512,7 +512,7 @@ func fillSerieTags(series *monitor.TimeSeriesSlice) {
}
for _, tag := range []string{
"source", "status", hostconsts.TELEGRAF_TAG_KEY_HOST_TYPE,
hostconsts.TELEGRAF_TAG_KEY_RES_TYPE, "cpu",
hostconsts.TELEGRAF_TAG_KEY_RES_TYPE,
"is_vm", "os_type", "domain_name", "region",
labels.MetricName, translator.UNION_RESULT_NAME,
} {
@@ -120,7 +120,7 @@ func formatRawName(idx int, name string, query *Query, tags map[string]string) s
groupByTags = append(groupByTags, group.Params[0])
}
}
return tsdb.FormatRawName(idx, name, groupByTags, tags)
return tsdb.FormatRawName(idx, name, groupByTags, tags, nil)
}
func (rp *ResponseParser) transformRowToTable(row Row, table *tsdb.Table) *tsdb.Table {
@@ -186,7 +186,7 @@ func transPointToSeries(p *points, query *tsdb.Query) monitor.TimeSeriesSlice {
points := transValuesToTSDBPoints(p.values)
tags := reviseTags(p.tags)
metricName := strings.Join(p.columns, ",")
ts := tsdb.NewTimeSeries(metricName, formatRawName(0, metricName, query, tags), append(p.columns, "time"), points, tags)
ts := tsdb.NewTimeSeries(metricName, formatRawName(0, metricName, query, tags, nil), append(p.columns, "time"), points, tags)
result = append(result, ts)
return result
}
+22 -5
View File
@@ -141,9 +141,26 @@ func translateResponse(resp *Response, query *tsdb.Query) (*tsdb.QueryResult, er
if len(results) > 0 {
_, isUnionResult = results[0].Metric[translator.UNION_RESULT_NAME]
}
// 添加值不同的 tag key
diffTagKeys := sets.NewString()
if len(results) > 1 {
result0 := results[0]
restResults := results[1:]
for tagKey, tagVal := range result0.Metric {
for _, result := range restResults {
resultTagVal := result.Metric[tagKey]
if tagVal != resultTagVal {
diffTagKeys.Insert(tagKey)
break
}
}
}
}
if !isUnionResult {
for _, result := range results {
ss := transformSeries(result, query)
ss := transformSeries(result, query, diffTagKeys)
queryRes.Series = append(queryRes.Series, ss...)
}
} else {
@@ -160,7 +177,7 @@ func translateResponse(resp *Response, query *tsdb.Query) (*tsdb.QueryResult, er
}
// Check VictoriaMetrics response at: https://docs.victoriametrics.com/keyConcepts.html#range-query
func transformSeries(vmResult ResponseDataResult, query *tsdb.Query) monitor.TimeSeriesSlice {
func transformSeries(vmResult ResponseDataResult, query *tsdb.Query, diffTagKeys sets.String) monitor.TimeSeriesSlice {
var result monitor.TimeSeriesSlice
metric := vmResult.Metric
@@ -183,12 +200,12 @@ func transformSeries(vmResult ResponseDataResult, query *tsdb.Query) monitor.Tim
if aliasName != "" {
metricName = aliasName
}
ts := tsdb.NewTimeSeries(metricName, formatRawName(0, metricName, query, tags), []string{metricName, "time"}, points, tags)
ts := tsdb.NewTimeSeries(metricName, formatRawName(0, metricName, query, tags, diffTagKeys), []string{metricName, "time"}, points, tags)
result = append(result, ts)
return result
}
func formatRawName(idx int, name string, query *tsdb.Query, tags map[string]string) string {
func formatRawName(idx int, name string, query *tsdb.Query, tags map[string]string, diffTagKeys sets.String) string {
groupByTags := []string{}
if query != nil {
for _, group := range query.GroupBy {
@@ -197,7 +214,7 @@ func formatRawName(idx int, name string, query *tsdb.Query, tags map[string]stri
}
}
}
return tsdb.FormatRawName(idx, name, groupByTags, tags)
return tsdb.FormatRawName(idx, name, groupByTags, tags, diffTagKeys)
}
func parseTimepoint(val ResponseDataResultValue) (monitor.TimePoint, error) {
+6 -1
View File
@@ -86,7 +86,7 @@ func NewQueryResult() *QueryResult {
}
}
func FormatRawName(idx int, name string, groupByTags []string, tags map[string]string) string {
func FormatRawName(idx int, name string, groupByTags []string, tags map[string]string, diffTagKeys sets.String) string {
// when group by tag specified
if len(groupByTags) != 0 {
for key, val := range tags {
@@ -102,7 +102,12 @@ func FormatRawName(idx int, name string, groupByTags []string, tags map[string]s
hintNames := sets.NewString()
hints := sets.NewString()
tagKeys := sets.NewString()
for _, tagKey := range api.MEASUREMENT_TAG_KEYWORD {
tagKeys.Insert(tagKey)
}
tagKeys = tagKeys.Union(diffTagKeys)
for _, tagKey := range tagKeys.List() {
if tagV, ok := tags[tagKey]; ok {
gHint := genHint(tagKey, tagV)
if strings.Contains(tagKey, "name") {