From 3c0301e3b4325a649321ac77831fa16c8b4a1c78 Mon Sep 17 00:00:00 2001 From: Vasilchenko Anton Date: Fri, 5 May 2023 18:46:07 +0700 Subject: [PATCH] Add endpoint labels for pod targets discovered form endpoint but has different ports (#4253) Signed-off-by: Vasilchenko Anton --- .../discovery/kubernetes/endpoints.go | 17 +++++------------ .../discovery/kubernetes/endpointslice.go | 5 +++++ lib/promscrape/discovery/kubernetes/pod.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/promscrape/discovery/kubernetes/endpoints.go b/lib/promscrape/discovery/kubernetes/endpoints.go index 4e659d105..e2b77788e 100644 --- a/lib/promscrape/discovery/kubernetes/endpoints.go +++ b/lib/promscrape/discovery/kubernetes/endpoints.go @@ -114,10 +114,6 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels { logger.Warnf(`the number of targets for "role: endpoints" %q exceeds 1000 and will be truncated in the next k8s releases; please use "role: endpointslice" instead`, eps.Metadata.key()) } - // Prometheus sets endpoints_name and namespace labels for all endpoints - // Even if port is not matching service port. - // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154 - commonEpLabels := eps.getCommonLabels() // Append labels for skipped ports on seen pods. portSeen := func(port int, ports []int) bool { for _, p := range ports { @@ -138,7 +134,11 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels { m.Add("__address__", addr) p.appendCommonLabels(m, gw) p.appendContainerLabels(m, c, &cp) - m.AddFrom(commonEpLabels) + + // Prometheus sets endpoints_name and namespace labels for all endpoints + // Even if port is not matching service port. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154 + p.appendEndpointLabels(m, eps) if svc != nil { svc.appendCommonLabels(m) } @@ -150,13 +150,6 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels { return ms } -func (eps *Endpoints) getCommonLabels() *promutils.Labels { - m := promutils.GetLabels() - m.Add("__meta_kubernetes_namespace", eps.Metadata.Namespace) - m.Add("__meta_kubernetes_endpoints_name", eps.Metadata.Name) - return m -} - func appendEndpointLabelsForAddresses(ms []*promutils.Labels, gw *groupWatcher, podPortsSeen map[*Pod][]int, eps *Endpoints, eas []EndpointAddress, epp EndpointPort, svc *Service, ready string) []*promutils.Labels { for _, ea := range eas { diff --git a/lib/promscrape/discovery/kubernetes/endpointslice.go b/lib/promscrape/discovery/kubernetes/endpointslice.go index a7506b184..b270c47a4 100644 --- a/lib/promscrape/discovery/kubernetes/endpointslice.go +++ b/lib/promscrape/discovery/kubernetes/endpointslice.go @@ -83,6 +83,11 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []*promutils.Labels m.Add("__address__", addr) p.appendCommonLabels(m, gw) p.appendContainerLabels(m, c, &cp) + + // Prometheus sets endpoints_name and namespace labels for all endpoints + // Even if port is not matching service port. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154 + p.appendEndpointSliceLabels(m, eps) if svc != nil { svc.appendCommonLabels(m) } diff --git a/lib/promscrape/discovery/kubernetes/pod.go b/lib/promscrape/discovery/kubernetes/pod.go index 7256c8b57..475269d95 100644 --- a/lib/promscrape/discovery/kubernetes/pod.go +++ b/lib/promscrape/discovery/kubernetes/pod.go @@ -178,6 +178,16 @@ func (p *Pod) appendContainerLabels(m *promutils.Labels, c Container, cp *Contai } } +func (p *Pod) appendEndpointLabels(m *promutils.Labels, eps *Endpoints) { + m.Add("__meta_kubernetes_endpoints_name", eps.Metadata.Name) + eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpoints", m) +} + +func (p *Pod) appendEndpointSliceLabels(m *promutils.Labels, eps *EndpointSlice) { + m.Add("__meta_kubernetes_endpointslice_name", eps.Metadata.Name) + eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpointslice", m) +} + func (p *Pod) appendCommonLabels(m *promutils.Labels, gw *groupWatcher) { if gw.attachNodeMetadata { m.Add("__meta_kubernetes_node_name", p.Spec.NodeName)