From 07c602660af1942d1c66f4f5d80d0e75a4df9929 Mon Sep 17 00:00:00 2001 From: ning <710leo@gmail.com> Date: Mon, 8 Jan 2024 17:56:44 +0800 Subject: [PATCH] fix: append labels --- pushgw/router/router_remotewrite.go | 35 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pushgw/router/router_remotewrite.go b/pushgw/router/router_remotewrite.go index c0700e38d..2eb143764 100644 --- a/pushgw/router/router_remotewrite.go +++ b/pushgw/router/router_remotewrite.go @@ -21,9 +21,9 @@ func extractMetricFromTimeSeries(s *prompb.TimeSeries) string { return "" } -func extractIdentFromTimeSeries(s *prompb.TimeSeries, ignoreIdent bool, identMetrics []string) string { +func extractIdentFromTimeSeries(s *prompb.TimeSeries, ignoreIdent bool, identMetrics []string) (string, string) { if s == nil { - return "" + return "", "" } labelMap := make(map[string]int) @@ -32,6 +32,7 @@ func extractIdentFromTimeSeries(s *prompb.TimeSeries, ignoreIdent bool, identMet } var ident string + var heartbeatIdent string // agent_hostname for grafana-agent and categraf if idx, ok := labelMap["agent_hostname"]; ok { s.Labels[idx].Name = "ident" @@ -46,6 +47,11 @@ func extractIdentFromTimeSeries(s *prompb.TimeSeries, ignoreIdent bool, identMet } } + if idx, ok := labelMap["ident"]; ok { + ident = s.Labels[idx].Value + } + + heartbeatIdent = ident if len(identMetrics) > 0 { metricFound := false for _, identMetric := range identMetrics { @@ -56,15 +62,11 @@ func extractIdentFromTimeSeries(s *prompb.TimeSeries, ignoreIdent bool, identMet } if !metricFound { - return "" + heartbeatIdent = "" } } - if idx, ok := labelMap["ident"]; ok { - ident = s.Labels[idx].Value - } - - return ident + return ident, heartbeatIdent } func duplicateLabelKey(series *prompb.TimeSeries) bool { @@ -99,22 +101,15 @@ func (rt *Router) remoteWrite(c *gin.Context) { return } - var ( - ident string - ids = make(map[string]struct{}) - ) + var ids = make(map[string]struct{}) for i := 0; i < count; i++ { if duplicateLabelKey(&req.Timeseries[i]) { continue } - ident = extractIdentFromTimeSeries(&req.Timeseries[i], ginx.QueryBool(c, "ignore_ident", false), rt.Pushgw.IdentMetrics) + ident, heartbeatIdent := extractIdentFromTimeSeries(&req.Timeseries[i], ginx.QueryBool(c, "ignore_ident", false), rt.Pushgw.IdentMetrics) if len(ident) > 0 { - // has ident tag or agent_hostname tag - // register host in table target - ids[ident] = struct{}{} - // enrich host labels target, has := rt.TargetCache.Get(ident) if has { @@ -122,6 +117,12 @@ func (rt *Router) remoteWrite(c *gin.Context) { } } + if len(heartbeatIdent) > 0 { + // has ident tag or agent_hostname tag + // register host in table target + ids[ident] = struct{}{} + } + if len(ident) > 0 { rt.ForwardByIdent(c.ClientIP(), ident, &req.Timeseries[i]) } else {