Skip to content

Commit

Permalink
feat: add bodySizeLimit to service and pod monitors (#6349)
Browse files Browse the repository at this point in the history
* feat: add EnforcedBodySizeLimit to service and monitor
  • Loading branch information
jamshidi799 committed Mar 12, 2024
1 parent 4030eb7 commit bc0b842
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 13 deletions.
66 changes: 65 additions & 1 deletion Documentation/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bundle.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/blang/semver/v4 v4.0.0
github.com/cespare/xxhash/v2 v2.2.0
Expand Down Expand Up @@ -44,7 +45,6 @@ require (
)

require (
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
Expand Down
5 changes: 5 additions & 0 deletions jsonnet/prometheus-operator/podmonitors-crd.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
},
"type": "object"
},
"bodySizeLimit": {
"description": "When defined, bodySizeLimit specifies a job level limit on the size of uncompressed response body that will be accepted by Prometheus. \n It requires Prometheus >= v2.28.0.",
"pattern": "(^0|([0-9]*[.])?[0-9]+((K|M|G|T|E|P)i?)?B)$",
"type": "string"
},
"jobLabel": {
"description": "The label to use to retrieve the job name from. `jobLabel` selects the label from the associated Kubernetes `Pod` object which will be used as the `job` label for all metrics. \n For example if `jobLabel` is set to `foo` and the Kubernetes `Pod` object is labeled with `foo: bar`, then Prometheus adds the `job=\"bar\"` label to all ingested metrics. \n If the value of this field is empty, the `job` label of the metrics defaults to the namespace and name of the PodMonitor object (e.g. `<namespace>/<name>`).",
"type": "string"
Expand Down
5 changes: 5 additions & 0 deletions jsonnet/prometheus-operator/servicemonitors-crd.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
},
"type": "object"
},
"bodySizeLimit": {
"description": "When defined, bodySizeLimit specifies a job level limit on the size of uncompressed response body that will be accepted by Prometheus. \n It requires Prometheus >= v2.28.0.",
"pattern": "(^0|([0-9]*[.])?[0-9]+((K|M|G|T|E|P)i?)?B)$",
"type": "string"
},
"endpoints": {
"description": "List of endpoints part of this ServiceMonitor.",
"items": {
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/monitoring/v1/podmonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ type PodMonitorSpec struct {
// +optional
// +kubebuilder:validation:MinLength=1
ScrapeClassName *string `json:"scrapeClass,omitempty"`

// When defined, bodySizeLimit specifies a job level limit on the size
// of uncompressed response body that will be accepted by Prometheus.
//
// It requires Prometheus >= v2.28.0.
//
// +optional
BodySizeLimit *ByteSize `json:"bodySizeLimit,omitempty"`
}

// PodMonitorList is a list of PodMonitors.
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/monitoring/v1/servicemonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ type ServiceMonitorSpec struct {
// +optional
// +kubebuilder:validation:MinLength=1
ScrapeClassName *string `json:"scrapeClass,omitempty"`

// When defined, bodySizeLimit specifies a job level limit on the size
// of uncompressed response body that will be accepted by Prometheus.
//
// It requires Prometheus >= v2.28.0.
//
// +optional
BodySizeLimit *ByteSize `json:"bodySizeLimit,omitempty"`
}

// ServiceMonitorList is a list of ServiceMonitors.
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/monitoring/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/client/applyconfiguration/monitoring/v1/podmonitorspec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions pkg/prometheus/promcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sort"
"strings"

"github.com/alecthomas/units"
"github.com/blang/semver/v4"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -1034,8 +1035,8 @@ func (cg *ConfigGenerator) generatePodMonitorConfig(
cfg = cg.AddLimitsToYAML(cfg, keepDroppedTargetsKey, m.Spec.KeepDroppedTargets, cpf.EnforcedKeepDroppedTargets)
cfg = cg.AddScrapeProtocols(cfg, m.Spec.ScrapeProtocols)

if cpf.EnforcedBodySizeLimit != "" {
cfg = cg.WithMinimumVersion("2.28.0").AppendMapItem(cfg, "body_size_limit", cpf.EnforcedBodySizeLimit)
if bodySizeLimit := getLowerByteSize(m.Spec.BodySizeLimit, &cpf); !isByteSizeEmpty(bodySizeLimit) {
cfg = cg.WithMinimumVersion("2.28.0").AppendMapItem(cfg, "body_size_limit", bodySizeLimit)
}

cfg = append(cfg, yaml.MapItem{Key: "metric_relabel_configs", Value: generateRelabelConfig(labeler.GetRelabelingConfigs(m.TypeMeta, m.ObjectMeta, ep.MetricRelabelConfigs))})
Expand Down Expand Up @@ -1546,8 +1547,8 @@ func (cg *ConfigGenerator) generateServiceMonitorConfig(
cfg = cg.AddLimitsToYAML(cfg, keepDroppedTargetsKey, m.Spec.KeepDroppedTargets, cpf.EnforcedKeepDroppedTargets)
cfg = cg.AddScrapeProtocols(cfg, m.Spec.ScrapeProtocols)

if cpf.EnforcedBodySizeLimit != "" {
cfg = cg.WithMinimumVersion("2.28.0").AppendMapItem(cfg, "body_size_limit", cpf.EnforcedBodySizeLimit)
if bodySizeLimit := getLowerByteSize(m.Spec.BodySizeLimit, &cpf); !isByteSizeEmpty(bodySizeLimit) {
cfg = cg.WithMinimumVersion("2.28.0").AppendMapItem(cfg, "body_size_limit", bodySizeLimit)
}

cfg = append(cfg, yaml.MapItem{Key: "metric_relabel_configs", Value: generateRelabelConfig(labeler.GetRelabelingConfigs(m.TypeMeta, m.ObjectMeta, ep.MetricRelabelConfigs))})
Expand Down Expand Up @@ -3413,3 +3414,26 @@ func (cg *ConfigGenerator) getScrapeClassOrDefault(name *string) *monitoringv1.S
}
return nil
}

func getLowerByteSize(v *monitoringv1.ByteSize, cpf *monitoringv1.CommonPrometheusFields) *monitoringv1.ByteSize {
if isByteSizeEmpty(&cpf.EnforcedBodySizeLimit) {
return v
}

if isByteSizeEmpty(v) {
return &cpf.EnforcedBodySizeLimit
}

vBytes, _ := units.ParseBase2Bytes(string(*v))
pBytes, _ := units.ParseBase2Bytes(string(cpf.EnforcedBodySizeLimit))

if vBytes > pBytes {
return &cpf.EnforcedBodySizeLimit
}

return v
}

func isByteSizeEmpty(v *monitoringv1.ByteSize) bool {
return v == nil || *v == ""
}

0 comments on commit bc0b842

Please sign in to comment.