1
0
mirror of https://github.com/prometheus/alertmanager.git synced 2026-10-23 08:48:56 +03:00
Files
alertmanager/kafka/errors.go
Christoph Maser 1908e94627 chore: udate minimum Go version to 1.26 (#5461)
Bumps `.github/workflows`, `promu` and UI build container  to 1.27. We 
generally keep the minimum required version (1.26) and these versions 
exactly one minor version apart.

Fixes done via `golanglint-ci run --fix`.

Also bumps GOLANGCI_LINT_VERSION. This version is required, 
since the previous was not compatible with Go 1.27.

Signed-off-by: Christoph Maser <christoph.maser+github@gmail.com>
Signed-off-by: Solomon Jacobs <solomonjacobs@protonmail.com>
Co-authored-by: Solomon Jacobs <solomonjacobs@protonmail.com>
2026-08-24 09:20:28 +02:00

62 lines
2.0 KiB
Go

// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kafka
import (
"context"
"errors"
"net"
"strings"
)
// ErrorCategory is a coarse bucket for a Kafka error, suitable for use
// as a bounded-cardinality Prometheus label value.
type ErrorCategory string
// Error categories returned by ClassifyError.
const (
ErrorCategoryNone ErrorCategory = "none"
ErrorCategoryTimeout ErrorCategory = "timeout"
ErrorCategoryNetwork ErrorCategory = "network"
ErrorCategoryBroker ErrorCategory = "broker"
ErrorCategoryUnknown ErrorCategory = "unknown"
)
// ClassifyError buckets a franz-go error into one of the
// ErrorCategory* constants. It keeps Prometheus metric label
// cardinality bounded regardless of the specific error string franz-go
// surfaces.
func ClassifyError(err error) ErrorCategory {
if err == nil {
return ErrorCategoryNone
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return ErrorCategoryTimeout
}
if netErr, ok := errors.AsType[net.Error](err); ok {
if netErr.Timeout() {
return ErrorCategoryTimeout
}
return ErrorCategoryNetwork
}
// franz-go surfaces broker-side errors typed as kerr.Error; we
// detect them via the error string so this package doesn't have
// to depend on kerr just for the type assertion.
msg := err.Error()
if strings.Contains(msg, "broker") || strings.Contains(msg, "kafka:") {
return ErrorCategoryBroker
}
return ErrorCategoryUnknown
}