Documentation
¶
Overview ¶
Package checkprometheus provides a check that reads one numeric value out of a Prometheus metrics endpoint (or a Prometheus server, via PromQL) and grades it against warning/critical thresholds.
It is the first check type that inspects a *value* rather than a service: every other type answers "is it up?", while this one answers "is the number the target reports still acceptable?" — a queue depth, a free-disk gauge, an error counter. The graded outcome uses the shared two-tier model: critical breached → StatusDown (pages), warning breached → StatusWarning (amber, counts as up, no incident), otherwise StatusUp.
Index ¶
- Constants
- type PrometheusChecker
- func (c *PrometheusChecker) Execute(ctx context.Context, config checkerdef.Config) (*checkerdef.Result, error)
- func (c *PrometheusChecker) GetSampleConfigs(_ *checkerdef.ListSampleOptions) []checkerdef.CheckSpec
- func (c *PrometheusChecker) Type() checkerdef.CheckType
- func (c *PrometheusChecker) Validate(spec *checkerdef.CheckSpec) error
- type PrometheusConfig
- func (c *PrometheusConfig) EffectiveMatch() string
- func (c *PrometheusConfig) EffectiveMode() string
- func (c *PrometheusConfig) EffectiveOnMissing() string
- func (c *PrometheusConfig) EffectiveOperator() string
- func (c *PrometheusConfig) EffectiveTimeout() time.Duration
- func (c *PrometheusConfig) FromMap(configMap map[string]any) error
- func (c *PrometheusConfig) GetConfig() map[string]any
- func (c *PrometheusConfig) Validate() error
Constants ¶
const ( // ModeScrape fetches the URL and parses the Prometheus text exposition // format, selecting a series by metric name + label subset. ModeScrape = "scrape" // ModePromQL treats the URL as a Prometheus server base URL and runs an // instant query against /api/v1/query. It is also the escape hatch for // rate() over counters — this checker does NO client-side rate // computation (that needs state between executions; out of scope). ModePromQL = "promql" )
Modes.
const ( // MatchSingle is the default: more than one matching series is an error, // which forces the operator to write an unambiguous selector. MatchSingle = "single" MatchMin = "min" MatchMax = "max" MatchSum = "sum" MatchAvg = "avg" )
Multi-series match strategies.
const ( // OnMissingDown is the default: an absent metric usually means the target // is broken, not healthy. OnMissingDown = "down" OnMissingWarning = "warning" OnMissingUp = "up" )
onMissing behaviors, applied when the selector matches nothing.
const ( OpGreater = ">" OpGreaterEqual = ">=" OpLess = "<" OpLessEqual = "<=" OpEqual = "==" OpNotEqual = "!=" )
Comparison operators. The check fires when `value <operator> threshold` is true.
const DefaultOperator = OpGreater
DefaultOperator is used when `operator` is absent from the config. `>` is the overwhelmingly common shape ("alert when the number gets too big").
const ( // MaxScrapeBytes caps the scrape-mode response body. A body over the cap // is REFUSED (StatusDown, with the cap named in the output), never // truncated: a truncated exposition body parses into wrong values, which // is strictly worse than an error. promql responses are bounded by the // query itself and are not capped here. MaxScrapeBytes = 5 * 1024 * 1024 )
const ( // MetricKeyValue is the result metric name. It is deliberately // unsuffixed: the aggregation job's suffix convention falls through to // the type-based default for a float64, which is "average" — so the // monitored value rolls up and graphs over time exactly like a latency. MetricKeyValue = "value" )
Output keys specific to this checker.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrometheusChecker ¶
type PrometheusChecker struct{}
PrometheusChecker implements the Checker interface for Prometheus metric checks.
func (*PrometheusChecker) Execute ¶
func (c *PrometheusChecker) Execute( ctx context.Context, config checkerdef.Config, ) (*checkerdef.Result, error)
Execute performs the check and grades the resolved value.
func (*PrometheusChecker) GetSampleConfigs ¶
func (c *PrometheusChecker) GetSampleConfigs(_ *checkerdef.ListSampleOptions) []checkerdef.CheckSpec
GetSampleConfigs returns sample Prometheus check configurations: one per mode. Both carry an explicit Slug — a sample without one regresses the checkdnsbl/checksip default-slug fix.
func (*PrometheusChecker) Type ¶
func (c *PrometheusChecker) Type() checkerdef.CheckType
Type returns the check type identifier.
func (*PrometheusChecker) Validate ¶
func (c *PrometheusChecker) Validate(spec *checkerdef.CheckSpec) error
Validate checks the configuration and fills in a default name/slug.
type PrometheusConfig ¶
type PrometheusConfig struct {
// URL is the metrics endpoint (scrape mode) or the Prometheus server
// base URL (promql mode).
URL string `json:"url"`
// Mode is "scrape" (default) or "promql".
Mode string `json:"mode,omitempty"`
// Metric is the metric (series) name to select, scrape mode only.
// Histogram/summary families are addressed through their flattened
// series names: `<name>_sum`, `<name>_count`, `<name>_bucket` (with an
// `le` label), or `<name>` with a `quantile` label.
Metric string `json:"metric,omitempty"`
// Labels narrows the selection to series carrying at least these
// label/value pairs (exact-match subset), scrape mode only.
Labels map[string]string `json:"labels,omitempty"`
// Query is the PromQL instant query, promql mode only.
Query string `json:"query,omitempty"`
// Operator is the comparison applied against the thresholds.
Operator string `json:"operator,omitempty"`
// WarningValue, when breached, yields StatusWarning (amber, counts as
// up, never pages). A config with only a warning tier is valid — it can
// never produce StatusDown, exactly like `warningDays` on domain/ssl.
WarningValue *float64 `json:"warningValue,omitempty"`
// CriticalValue, when breached, yields StatusDown (pages).
CriticalValue *float64 `json:"criticalValue,omitempty"`
// Match decides what happens when more than one series matches.
Match string `json:"match,omitempty"`
// OnMissing is the status reported when nothing matches.
OnMissing string `json:"onMissing,omitempty"`
// Headers are sent with the request (bearer/basic auth on the metrics
// endpoint is routine). Same shape — and same at-rest handling — as the
// HTTP check's `headers`: stored in the public config, not encrypted.
Headers map[string]string `json:"headers,omitempty"`
// Timeout caps the HTTP request; default 15s, max 60s.
Timeout time.Duration `json:"timeout,omitempty"`
}
PrometheusConfig defines the configuration for a Prometheus metric check.
WarningValue / CriticalValue are POINTERS on purpose. Thresholds are float64 and 0 is a perfectly legal threshold ("alert when free slots hit 0"), so "is it set?" cannot be answered by the zero value the way checkdomain answers it for days — presence has to be tracked explicitly or `warningValue: 0` silently means "unset".
func (*PrometheusConfig) EffectiveMatch ¶
func (c *PrometheusConfig) EffectiveMatch() string
EffectiveMatch returns the resolved multi-series strategy, defaulting to `single`.
func (*PrometheusConfig) EffectiveMode ¶
func (c *PrometheusConfig) EffectiveMode() string
EffectiveMode returns the resolved mode, defaulting to scrape.
func (*PrometheusConfig) EffectiveOnMissing ¶
func (c *PrometheusConfig) EffectiveOnMissing() string
EffectiveOnMissing returns the resolved missing-series behavior, defaulting to `down`.
func (*PrometheusConfig) EffectiveOperator ¶
func (c *PrometheusConfig) EffectiveOperator() string
EffectiveOperator returns the resolved operator, defaulting to `>`.
func (*PrometheusConfig) EffectiveTimeout ¶
func (c *PrometheusConfig) EffectiveTimeout() time.Duration
EffectiveTimeout returns the resolved request timeout.
func (*PrometheusConfig) FromMap ¶
func (c *PrometheusConfig) FromMap(configMap map[string]any) error
FromMap populates the configuration from a map (the JSONB `config` shape).
func (*PrometheusConfig) GetConfig ¶
func (c *PrometheusConfig) GetConfig() map[string]any
GetConfig serializes the config back to a map.
func (*PrometheusConfig) Validate ¶
func (c *PrometheusConfig) Validate() error
Validate checks the configuration without performing any network call.