diff --git a/lib/promscrape/discovery/kubernetes/endpoints.go b/lib/promscrape/discovery/kubernetes/endpoints.go index 25497f4f0..7ab1a3591 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) } @@ -151,13 +151,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 3d66c2264..cfd1b5e55 100644 --- a/lib/promscrape/discovery/kubernetes/endpointslice.go +++ b/lib/promscrape/discovery/kubernetes/endpointslice.go @@ -84,6 +84,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)