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

feat: add bodySizeLimit to service and pod monitors #6349

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
jamshidi799 marked this conversation as resolved.
Show resolved Hide resolved
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.

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.

28 changes: 24 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); bodySizeLimit != nil {
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); bodySizeLimit != nil {
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 @@ -3409,3 +3410,22 @@ func (cg *ConfigGenerator) getScrapeClassOrDefault(name *string) *monitoringv1.S
}
return nil
}

func getLowerByteSize(v *monitoringv1.ByteSize, cpf *monitoringv1.CommonPrometheusFields) *monitoringv1.ByteSize {
jamshidi799 marked this conversation as resolved.
Show resolved Hide resolved
if cpf.EnforcedBodySizeLimit == "" {
return v
}

if v == nil {
return &cpf.EnforcedBodySizeLimit
}

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

if vBytes > pBytes {
return &cpf.EnforcedBodySizeLimit
}

return v
}