Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize Prometheus scrape target discovery #1663

2 changes: 1 addition & 1 deletion hack/load-tests/metric-agent-test-setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
- key: app.kubernetes.io/name
operator: In
values:
- metric-agent-load-generator
Expand Down
8 changes: 7 additions & 1 deletion internal/otelcollector/config/metric/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,17 @@ type StaticDiscoveryConfig struct {
}

type KubernetesDiscoveryConfig struct {
Role Role `yaml:"role"`
Role Role `yaml:"role"`
Selectors []K8SDiscoverySelector `yaml:"selectors,omitempty"`
}

type Role string

type K8SDiscoverySelector struct {
Role Role `yaml:"role"`
Field string `yaml:"field"`
}

const (
RoleEndpoints Role = "endpoints"
RolePod Role = "pod"
Expand Down
61 changes: 46 additions & 15 deletions internal/otelcollector/config/metric/agent/prometheus_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const (
type AnnotatedResource string

const (
AnnotatedPod AnnotatedResource = "pod"
AnnotatedService AnnotatedResource = "service"
AnnotatedPod AnnotatedResource = "pod"
AnnotatedService AnnotatedResource = "service"
PodNodeSelectorFieldExpression string = "spec.nodeName=${MY_NODE_NAME}"
)

const (
Expand All @@ -35,11 +36,21 @@ func makePrometheusConfigForPods() *PrometheusReceiver {
var config PrometheusReceiver

scrapeConfig := ScrapeConfig{
ScrapeInterval: scrapeInterval,
SampleLimit: sampleLimit,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{{Role: RolePod}},
JobName: appPodsJobName,
RelabelConfigs: makePrometheusPodsRelabelConfigs(),
ScrapeInterval: scrapeInterval,
SampleLimit: sampleLimit,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{
{
Role: RolePod,
Selectors: []K8SDiscoverySelector{
{
Role: RolePod,
Field: PodNodeSelectorFieldExpression,
},
},
},
},
k15r marked this conversation as resolved.
Show resolved Hide resolved
JobName: appPodsJobName,
RelabelConfigs: makePrometheusPodsRelabelConfigs(),
}

config.Config.ScrapeConfigs = append(config.Config.ScrapeConfigs, scrapeConfig)
Expand All @@ -55,9 +66,19 @@ func makePrometheusConfigForServices(opts BuildOptions) *PrometheusReceiver {
var config PrometheusReceiver

baseScrapeConfig := ScrapeConfig{
ScrapeInterval: scrapeInterval,
SampleLimit: sampleLimit,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{{Role: RoleEndpoints}},
ScrapeInterval: scrapeInterval,
SampleLimit: sampleLimit,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{
{
Role: RoleEndpoints,
Selectors: []K8SDiscoverySelector{
{
Role: RolePod,
Field: PodNodeSelectorFieldExpression,
},
},
},
},
}

httpScrapeConfig := baseScrapeConfig
Expand Down Expand Up @@ -142,11 +163,21 @@ func makePrometheusIstioConfig() *PrometheusReceiver {
Config: PrometheusConfig{
ScrapeConfigs: []ScrapeConfig{
{
JobName: "istio-proxy",
SampleLimit: sampleLimit,
MetricsPath: "/stats/prometheus",
ScrapeInterval: scrapeInterval,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{{Role: RolePod}},
JobName: "istio-proxy",
SampleLimit: sampleLimit,
MetricsPath: "/stats/prometheus",
ScrapeInterval: scrapeInterval,
KubernetesDiscoveryConfigs: []KubernetesDiscoveryConfig{
{
Role: RolePod,
Selectors: []K8SDiscoverySelector{
{
Role: RolePod,
Field: PodNodeSelectorFieldExpression,
},
},
},
},
RelabelConfigs: []RelabelConfig{
keepIfRunningOnSameNode(NodeAffiliatedPod),
keepIfIstioProxy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ receivers:
action: replace
kubernetes_sd_configs:
- role: pod
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
prometheus/app-services:
config:
scrape_configs:
Expand Down Expand Up @@ -223,6 +226,9 @@ receivers:
action: replace
kubernetes_sd_configs:
- role: endpoints
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
- job_name: app-services-secure
sample_limit: 50000
scrape_interval: 30s
Expand Down Expand Up @@ -268,6 +274,9 @@ receivers:
action: replace
kubernetes_sd_configs:
- role: endpoints
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
tls_config:
ca_file: /etc/istio-output-certs/root-cert.pem
cert_file: /etc/istio-output-certs/cert-chain.pem
Expand Down Expand Up @@ -299,6 +308,9 @@ receivers:
action: keep
kubernetes_sd_configs:
- role: pod
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
processors:
batch:
send_batch_size: 1024
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ receivers:
action: replace
kubernetes_sd_configs:
- role: pod
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
prometheus/app-services:
config:
scrape_configs:
Expand Down Expand Up @@ -212,6 +215,9 @@ receivers:
action: replace
kubernetes_sd_configs:
- role: endpoints
selectors:
- role: pod
field: spec.nodeName=${MY_NODE_NAME}
processors:
batch:
send_batch_size: 1024
Expand Down
Loading