Skip to content

Commit

Permalink
adapt state and config limits to globalerror
Browse files Browse the repository at this point in the history
  • Loading branch information
titolins committed Oct 2, 2024
1 parent 3bd9b5a commit 1fa4dfa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
29 changes: 23 additions & 6 deletions pkg/alertmanager/api_grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (

"github.com/grafana/mimir/pkg/alertmanager/alertspb"
"github.com/grafana/mimir/pkg/util"
"github.com/grafana/mimir/pkg/util/globalerror"
util_log "github.com/grafana/mimir/pkg/util/log"
"github.com/grafana/mimir/pkg/util/validation"
)

const (
Expand All @@ -38,6 +40,17 @@ const (
statusError = "error"
)

var (
maxGrafanaConfigSizeMsgFormat = globalerror.AlertmanagerMaxGrafanaConfigSize.MessageWithPerTenantLimitConfig(
errConfigurationTooBig,
validation.AlertmanagerMaxGrafanaConfigSizeFlag,
)
maxGrafanaStateSizeMsgFormat = globalerror.AlertmanagerMaxGrafanaStateSize.MessageWithPerTenantLimitConfig(
errStateTooBig,
validation.AlertmanagerMaxGrafanaStateSizeFlag,
)
)

type GrafanaAlertmanagerConfig struct {
Templates map[string]string `json:"template_files"`
AlertmanagerConfig definition.PostableApiAlertingConfig `json:"alertmanager_config"`
Expand Down Expand Up @@ -180,10 +193,12 @@ func (am *MultitenantAlertmanager) SetUserGrafanaState(w http.ResponseWriter, r
payload, err := io.ReadAll(input)
if err != nil {
if maxBytesErr := (&http.MaxBytesError{}); errors.As(err, &maxBytesErr) {
msg := fmt.Sprintf(errStateTooBig, maxStateSize)
level.Warn(logger).Log("msg", msg)
level.Warn(logger).Log("msg", fmt.Sprintf(errStateTooBig, maxStateSize))
w.WriteHeader(http.StatusBadRequest)
util.WriteJSONResponse(w, errorResult{Status: statusError, Error: msg})
util.WriteJSONResponse(w, errorResult{
Status: statusError,
Error: fmt.Sprintf(maxGrafanaStateSizeMsgFormat, maxStateSize),
})
return
}

Expand Down Expand Up @@ -336,10 +351,12 @@ func (am *MultitenantAlertmanager) SetUserGrafanaConfig(w http.ResponseWriter, r
payload, err := io.ReadAll(input)
if err != nil {
if maxBytesErr := (&http.MaxBytesError{}); errors.As(err, &maxBytesErr) {
msg := fmt.Sprintf(errConfigurationTooBig, maxConfigSize)
level.Warn(logger).Log("msg", msg)
level.Warn(logger).Log("msg", fmt.Sprintf(errConfigurationTooBig, maxConfigSize))
w.WriteHeader(http.StatusBadRequest)
util.WriteJSONResponse(w, errorResult{Status: statusError, Error: msg})
util.WriteJSONResponse(w, errorResult{
Status: statusError,
Error: fmt.Sprintf(maxGrafanaConfigSizeMsgFormat, maxConfigSize),
})
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/alertmanager/api_grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func TestMultitenantAlertmanager_SetUserGrafanaConfig(t *testing.T) {
expStatusCode: http.StatusBadRequest,
expResponseBody: `
{
"error": "Alertmanager configuration is too big, limit: 10 bytes",
"error": "Alertmanager configuration is too big, limit: 10 bytes (err-mimir-alertmanager-max-grafana-config-size). To adjust the related per-tenant limit, configure -alertmanager.max-grafana-config-size-bytes, or contact your service administrator.",
"status": "error"
}
`,
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestMultitenantAlertmanager_SetUserGrafanaState(t *testing.T) {
expStatusCode: http.StatusBadRequest,
expResponseBody: `
{
"error": "Alertmanager state is too big, limit: 10 bytes",
"error": "Alertmanager state is too big, limit: 10 bytes (err-mimir-alertmanager-max-grafana-state-size). To adjust the related per-tenant limit, configure -alertmanager.max-grafana-state-size-bytes, or contact your service administrator.",
"status": "error"
}
`,
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/globalerror/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ const (
NativeHistogramNegativeBucketCount ID = "native-histogram-negative-bucket-count"
NativeHistogramSpanNegativeOffset ID = "native-histogram-span-negative-offset"
NativeHistogramSpansBucketsMismatch ID = "native-histogram-spans-buckets-mismatch"

// Alertmanager errors
AlertmanagerMaxGrafanaConfigSize ID = "alertmanager-max-grafana-config-size"
AlertmanagerMaxGrafanaStateSize ID = "alertmanager-max-grafana-state-size"
)

// Message returns the provided msg, appending the error id.
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/validation/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const (
resultsCacheTTLForOutOfOrderWindowFlag = "query-frontend.results-cache-ttl-for-out-of-order-time-window"
alignQueriesWithStepFlag = "query-frontend.align-queries-with-step"
QueryIngestersWithinFlag = "querier.query-ingesters-within"
AlertmanagerMaxGrafanaConfigSizeFlag = "alertmanager.max-grafana-config-size-bytes"
AlertmanagerMaxGrafanaStateSizeFlag = "alertmanager.max-grafana-state-size-bytes"

// MinCompactorPartialBlockDeletionDelay is the minimum partial blocks deletion delay that can be configured in Mimir.
MinCompactorPartialBlockDeletionDelay = 4 * time.Hour
Expand Down

0 comments on commit 1fa4dfa

Please sign in to comment.