code refactor for append labels
This commit is contained in:
parent
6d9846f1f5
commit
8ce71de693
|
@ -0,0 +1,36 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"github.com/didi/nightingale/v5/src/models"
|
||||
"github.com/didi/nightingale/v5/src/server/memsto"
|
||||
"github.com/prometheus/prometheus/prompb"
|
||||
)
|
||||
|
||||
func AppendLabels(pt *prompb.TimeSeries, target *models.Target) {
|
||||
if target == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for key, value := range target.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
if target.GroupId > 0 {
|
||||
bg := memsto.BusiGroupCache.GetByBusiGroupId(target.GroupId)
|
||||
if bg == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if bg.LabelEnable == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: "busigroup",
|
||||
Value: bg.LabelValue,
|
||||
})
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/toolkits/pkg/logger"
|
||||
|
||||
"github.com/didi/nightingale/v5/src/models"
|
||||
"github.com/didi/nightingale/v5/src/server/common"
|
||||
"github.com/didi/nightingale/v5/src/server/config"
|
||||
"github.com/didi/nightingale/v5/src/server/memsto"
|
||||
"github.com/didi/nightingale/v5/src/server/naming"
|
||||
|
@ -166,13 +167,7 @@ func pushMetrics() {
|
|||
logger.Errorf("handle_idents: insert target(%s) fail: %v", active, err)
|
||||
}
|
||||
} else {
|
||||
// target already exists
|
||||
for label, value := range target.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: label,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(pt, target)
|
||||
}
|
||||
|
||||
series = append(series, pt)
|
||||
|
@ -199,12 +194,7 @@ func pushMetrics() {
|
|||
Value: ident,
|
||||
})
|
||||
|
||||
for label, value := range dead.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: label,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(pt, dead)
|
||||
series = append(series, pt)
|
||||
}
|
||||
|
||||
|
|
|
@ -123,19 +123,6 @@ func syncTargets() error {
|
|||
lst[i].TagsMap[arr[0]] = arr[1]
|
||||
}
|
||||
|
||||
// handle BusiGroup's LabelValue
|
||||
// BusiGroup的LabelValue就相当于一个特殊的标签来对待
|
||||
if lst[i].GroupId > 0 {
|
||||
bg := BusiGroupCache.GetByBusiGroupId(lst[i].GroupId)
|
||||
if bg == nil {
|
||||
return errors.New("busi group cache not ready")
|
||||
}
|
||||
|
||||
if bg.LabelEnable == 1 {
|
||||
lst[i].TagsMap["busigroup"] = bg.LabelValue
|
||||
}
|
||||
}
|
||||
|
||||
m[lst[i].Ident] = lst[i]
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/didi/nightingale/v5/src/server/common"
|
||||
"github.com/didi/nightingale/v5/src/server/config"
|
||||
"github.com/didi/nightingale/v5/src/server/idents"
|
||||
"github.com/didi/nightingale/v5/src/server/memsto"
|
||||
|
@ -238,12 +239,7 @@ func datadogSeries(c *gin.Context) {
|
|||
// fill tags
|
||||
target, has := memsto.TargetCache.Get(ident)
|
||||
if has {
|
||||
for key, value := range target.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(pt, target)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/didi/nightingale/v5/src/server/common"
|
||||
"github.com/didi/nightingale/v5/src/server/config"
|
||||
"github.com/didi/nightingale/v5/src/server/idents"
|
||||
"github.com/didi/nightingale/v5/src/server/memsto"
|
||||
|
@ -206,12 +207,7 @@ func falconPush(c *gin.Context) {
|
|||
// fill tags
|
||||
target, has := memsto.TargetCache.Get(ident)
|
||||
if has {
|
||||
for key, value := range target.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(pt, target)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/prometheus/common/model"
|
||||
"github.com/prometheus/prometheus/prompb"
|
||||
|
||||
"github.com/didi/nightingale/v5/src/server/common"
|
||||
"github.com/didi/nightingale/v5/src/server/config"
|
||||
"github.com/didi/nightingale/v5/src/server/idents"
|
||||
"github.com/didi/nightingale/v5/src/server/memsto"
|
||||
|
@ -190,12 +191,7 @@ func handleOpenTSDB(c *gin.Context) {
|
|||
// fill tags
|
||||
target, has := memsto.TargetCache.Get(host)
|
||||
if has {
|
||||
for key, value := range target.TagsMap {
|
||||
pt.Labels = append(pt.Labels, &prompb.Label{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(pt, target)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/prometheus/prometheus/prompb"
|
||||
"github.com/toolkits/pkg/ginx"
|
||||
|
||||
"github.com/didi/nightingale/v5/src/server/common"
|
||||
"github.com/didi/nightingale/v5/src/server/config"
|
||||
"github.com/didi/nightingale/v5/src/server/engine"
|
||||
"github.com/didi/nightingale/v5/src/server/idents"
|
||||
|
@ -92,12 +93,7 @@ func remoteWrite(c *gin.Context) {
|
|||
// fill tags
|
||||
target, has := memsto.TargetCache.Get(ident)
|
||||
if has {
|
||||
for key, value := range target.TagsMap {
|
||||
req.Timeseries[i].Labels = append(req.Timeseries[i].Labels, &prompb.Label{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
common.AppendLabels(req.Timeseries[i], target)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue