From 9658de6ef245228b3fc9ef5ab1d4d74d245a25f4 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Wed, 23 Sep 2026 20:27:00 +0300 Subject: [PATCH 1/3] fix(monitoring): give the kube-apiserver scrape its own size limit kube-apiserver's /metrics grows with the number of resources and CRDs, and on a full install it outgrows the global 32MB -promscrape.maxScrapeSize. vmagent then refuses every scrape of it, so up is 0 and KubeAPIDown and TargetDown fire against a healthy API server while every rule over API server metrics goes blind. Set max_scrape_size on the apiserver endpoint instead of raising the global flag. vmagent uses it in place of the flag for that job only, so the 32MB guard for every other target stays as it is. vmagent reads the whole uncompressed body into memory even with streamParse, so the per-job value is a memory ceiling and stays finite (128MB), exposed as vmagent.apiserverMaxScrapeSize. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit 4a268f6c0114bb4d875851034b9a4032c6974791) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- .../templates/apiserver-scrape.yaml | 3 ++ .../tests/vmagent_scrape_size_test.yaml | 45 +++++++++++++++++++ packages/system/monitoring-agents/values.yaml | 5 +++ 3 files changed, 53 insertions(+) create mode 100644 packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml diff --git a/packages/system/monitoring-agents/templates/apiserver-scrape.yaml b/packages/system/monitoring-agents/templates/apiserver-scrape.yaml index f3f7b9826..5750b55f9 100644 --- a/packages/system/monitoring-agents/templates/apiserver-scrape.yaml +++ b/packages/system/monitoring-agents/templates/apiserver-scrape.yaml @@ -9,6 +9,9 @@ spec: - bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token port: https scheme: https + {{- with .Values.vmagent.apiserverMaxScrapeSize }} + max_scrape_size: {{ . | quote }} + {{- end }} relabelConfigs: - action: labeldrop regex: (endpoint|namespace|pod|container) diff --git a/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml new file mode 100644 index 000000000..479a55cce --- /dev/null +++ b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml @@ -0,0 +1,45 @@ +suite: kube-apiserver scrape size limit + +# kube-apiserver's /metrics grows with the number of resources and CRDs, and a +# full install pushes it past the global 32MB -promscrape.maxScrapeSize. vmagent +# then refuses every scrape of it, so up == 0 and KubeAPIDown fires on a +# healthy API server. The apiserver endpoint carries its own max_scrape_size, +# which replaces the global flag for that job only (upward too), so the guard +# for every other target stays at 32MB. vmagent buffers the whole uncompressed +# body even with streamParse, so the per-job limit is a memory ceiling and must +# stay finite. +release: + name: monitoring-agents + namespace: cozy-monitoring +tests: + - it: apiserver endpoint gets its own scrape size limit by default + template: templates/apiserver-scrape.yaml + asserts: + - equal: + path: spec.endpoints[0].max_scrape_size + value: 128MB + + - it: apiserver scrape size limit follows the value + template: templates/apiserver-scrape.yaml + set: + vmagent.apiserverMaxScrapeSize: 512MB + asserts: + - equal: + path: spec.endpoints[0].max_scrape_size + value: 512MB + + - it: an empty value falls back to the global flag + template: templates/apiserver-scrape.yaml + set: + vmagent.apiserverMaxScrapeSize: "" + asserts: + - notExists: + path: spec.endpoints[0].max_scrape_size + + - it: the global limit for every other target is not loosened + template: templates/vmagent.yaml + asserts: + - equal: + path: spec.extraArgs["promscrape.maxScrapeSize"] + value: 32MB + diff --git a/packages/system/monitoring-agents/values.yaml b/packages/system/monitoring-agents/values.yaml index 6433a74d0..fe9ab6d40 100644 --- a/packages/system/monitoring-agents/values.yaml +++ b/packages/system/monitoring-agents/values.yaml @@ -283,6 +283,11 @@ vmagent: - http://vminsert-shortterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus - http://vminsert-longterm.{{ .Values.global.target }}.svc:8480/insert/0/prometheus extraArgs: {} + # Replaces the global promscrape.maxScrapeSize (32MB) for the kube-apiserver + # job only. Its /metrics grows with the number of CRDs and outgrows 32MB on a + # full install. vmagent buffers the whole uncompressed response even with + # streamParse, so keep this finite. An empty value falls back to the global. + apiserverMaxScrapeSize: 128MB fluent-bit: rbac: From 0be2b2e41c9d05b67912618295537aee1b7f8766 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Wed, 23 Sep 2026 20:28:29 +0300 Subject: [PATCH 2/3] feat(monitoring): alert when vmagent refuses an oversized scrape A target whose response exceeds the scrape size limit reports up == 0, which is indistinguishable from the target being down: TargetDown and KubeAPIDown fire and nothing names the real cause. Alert on vm_promscrape_max_scrape_size_exceeded_errors_total so the first alert says what is wrong. The counter has no target label, so the description points at the vmagent targets page to find the target. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit a17c5b26678d5fa2cbcc099f89d6616b9b1a7bc9) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- .../monitoring-agents/alerts/vmagent.yaml | 25 +++++++++++++++++++ .../tests/vmagent_scrape_size_test.yaml | 14 ++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 packages/system/monitoring-agents/alerts/vmagent.yaml diff --git a/packages/system/monitoring-agents/alerts/vmagent.yaml b/packages/system/monitoring-agents/alerts/vmagent.yaml new file mode 100644 index 000000000..67985f38e --- /dev/null +++ b/packages/system/monitoring-agents/alerts/vmagent.yaml @@ -0,0 +1,25 @@ +apiVersion: operator.victoriametrics.com/v1beta1 +kind: VMRule +metadata: + name: alerts-vmagent +spec: + groups: + - name: vmagent + params: {} + rules: + - alert: VMAgentScrapeSizeExceeded + expr: increase(vm_promscrape_max_scrape_size_exceeded_errors_total[10m]) > 0 + for: 5m + labels: + severity: warning + exported_instance: '{{ $labels.namespace }}/{{ $labels.pod }}' + service: vmagent + annotations: + summary: vmagent refuses scrape responses larger than its size limit. + description: >- + vmagent {{ $labels.namespace }}/{{ $labels.pod }} dropped scrape responses that exceed + -promscrape.maxScrapeSize or the scrape's max_scrape_size. The affected targets report + up == 0, so TargetDown or KubeAPIDown may fire for a healthy target. The counter carries + no target label: find the target on the vmagent /targets page, where the last error names + the size limit, then raise max_scrape_size on that scrape (vmagent.apiserverMaxScrapeSize + for kube-apiserver). diff --git a/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml index 479a55cce..eaacd5f8d 100644 --- a/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml +++ b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml @@ -1,4 +1,4 @@ -suite: kube-apiserver scrape size limit +suite: kube-apiserver scrape size limit and its alert # kube-apiserver's /metrics grows with the number of resources and CRDs, and a # full install pushes it past the global 32MB -promscrape.maxScrapeSize. vmagent @@ -43,3 +43,15 @@ tests: path: spec.extraArgs["promscrape.maxScrapeSize"] value: 32MB + - it: a refused oversized scrape raises its own alert + template: templates/alerts.yaml + documentSelector: + path: metadata.name + value: alerts-vmagent + asserts: + - equal: + path: spec.groups[0].rules[0].alert + value: VMAgentScrapeSizeExceeded + - equal: + path: spec.groups[0].rules[0].expr + value: increase(vm_promscrape_max_scrape_size_exceeded_errors_total[10m]) > 0 From 01db49d1386f73f776c3db761ad08290dbbb4009 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Wed, 23 Sep 2026 20:56:07 +0300 Subject: [PATCH 3/3] fix(monitoring): pin the scrape size rendering and sharpen the alert The per-job limit replaces the global flag downward as well, so an install that raised promscrape.maxScrapeSize past it through extraArgs has to raise this value too; say so where the value is set. Pin that a numeric value still renders as a string, which the CRD field requires, and pin the alert severity. Name the cluster in the alert and say where the apiserver value lives, since a guest cluster cannot set it. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit 380c446cda49571ba496ae156a7cfd543b5882c3) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- .../system/monitoring-agents/alerts/vmagent.yaml | 13 +++++++------ .../tests/vmagent_scrape_size_test.yaml | 12 ++++++++++++ packages/system/monitoring-agents/values.yaml | 4 +++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/system/monitoring-agents/alerts/vmagent.yaml b/packages/system/monitoring-agents/alerts/vmagent.yaml index 67985f38e..3fb69fde2 100644 --- a/packages/system/monitoring-agents/alerts/vmagent.yaml +++ b/packages/system/monitoring-agents/alerts/vmagent.yaml @@ -17,9 +17,10 @@ spec: annotations: summary: vmagent refuses scrape responses larger than its size limit. description: >- - vmagent {{ $labels.namespace }}/{{ $labels.pod }} dropped scrape responses that exceed - -promscrape.maxScrapeSize or the scrape's max_scrape_size. The affected targets report - up == 0, so TargetDown or KubeAPIDown may fire for a healthy target. The counter carries - no target label: find the target on the vmagent /targets page, where the last error names - the size limit, then raise max_scrape_size on that scrape (vmagent.apiserverMaxScrapeSize - for kube-apiserver). + vmagent {{ $labels.namespace }}/{{ $labels.pod }} in cluster {{ $labels.cluster }} dropped + scrape responses that exceed -promscrape.maxScrapeSize or the scrape's max_scrape_size. + The affected targets report up == 0, so TargetDown or KubeAPIDown may fire for a healthy + target. The counter carries no target label: find the target on the vmagent /targets + page, where the last error names the size limit, then raise max_scrape_size on that + scrape. For kube-apiserver on the management cluster this is the + vmagent.apiserverMaxScrapeSize value of the monitoring-agents component. diff --git a/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml index eaacd5f8d..b4e3809d9 100644 --- a/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml +++ b/packages/system/monitoring-agents/tests/vmagent_scrape_size_test.yaml @@ -28,6 +28,15 @@ tests: path: spec.endpoints[0].max_scrape_size value: 512MB + - it: a byte count renders as a string, as the CRD field requires + template: templates/apiserver-scrape.yaml + set: + vmagent.apiserverMaxScrapeSize: 134217728 + asserts: + - matchRegex: + path: spec.endpoints[0].max_scrape_size + pattern: ^[0-9.e+]+$ + - it: an empty value falls back to the global flag template: templates/apiserver-scrape.yaml set: @@ -55,3 +64,6 @@ tests: - equal: path: spec.groups[0].rules[0].expr value: increase(vm_promscrape_max_scrape_size_exceeded_errors_total[10m]) > 0 + - equal: + path: spec.groups[0].rules[0].labels.severity + value: warning diff --git a/packages/system/monitoring-agents/values.yaml b/packages/system/monitoring-agents/values.yaml index fe9ab6d40..ddf9506f9 100644 --- a/packages/system/monitoring-agents/values.yaml +++ b/packages/system/monitoring-agents/values.yaml @@ -286,7 +286,9 @@ vmagent: # Replaces the global promscrape.maxScrapeSize (32MB) for the kube-apiserver # job only. Its /metrics grows with the number of CRDs and outgrows 32MB on a # full install. vmagent buffers the whole uncompressed response even with - # streamParse, so keep this finite. An empty value falls back to the global. + # streamParse, so keep this finite. It replaces the global in both + # directions: if extraArgs raises promscrape.maxScrapeSize above this value, + # raise this one too. An empty value falls back to the global. apiserverMaxScrapeSize: 128MB fluent-bit: