Files
probo/pkg/certmanager/metrics.go
Bryan Frimin 81b7ee5fad Close cert provisioning correctness gaps
Several race and validity gaps could leave certificate provisioning
stuck, unusable, or noisy:

- Accept the HTTP-01 challenge only after the key authorization is
  committed, so the CA cannot hit the token before this instance can
  serve it and invalidate the order.
- Persist challenge metadata under a blocking write-back lock; a row
  merely locked by a competing transaction no longer silently drops the
  accepted order.
- Abandon a recovered VALID order and restart instead of issuing it
  with a freshly generated key that cannot match the existing cert.
- Exclude rate-limited rows from the ten-minute stale reset so the
  resumable order survives the ACME cooldown.
- Size the provisioning poll lease to exceed the max processing window
  so a released claim lock cannot let another worker process the same
  row concurrently.
- Parse Retry-After as unsigned seconds and clamp overflow so malformed
  values fall back to the default cooldown instead of disabling it.
- Normalize the acme_errors problem_type label to the RFC 8555 set to
  bound Prometheus cardinality.

Signed-off-by: Bryan Frimin <bryan@probo.com>
2026-07-22 13:20:53 +02:00

184 lines
5.2 KiB
Go

// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package certmanager
import (
"errors"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type (
provisionPhase string
provisionResult string
)
const (
provisionPhaseCreateOrder provisionPhase = "create_order"
provisionPhasePollOrder provisionPhase = "poll_order"
provisionPhaseIssueCert provisionPhase = "issue_cert"
provisionPhaseDNSCheck provisionPhase = "dns_check"
provisionResultOK provisionResult = "ok"
provisionResultNotReady provisionResult = "not_ready"
provisionResultError provisionResult = "error"
provisionResultRateLimited provisionResult = "rate_limited"
provisionResultDNSError provisionResult = "dns_error"
)
type metrics struct {
provisionSteps *prometheus.CounterVec
acmeErrors *prometheus.CounterVec
acmeCooldown prometheus.Gauge
stepDuration *prometheus.HistogramVec
}
func newMetrics(registerer prometheus.Registerer) *metrics {
if registerer == nil {
registerer = prometheus.DefaultRegisterer
}
return &metrics{
provisionSteps: registerCollector(
registerer,
prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "certmanager",
Name: "certificate_provision_steps_total",
Help: "Certificate provisioning steps by phase and result.",
},
[]string{"phase", "result"},
),
),
acmeErrors: registerCollector(
registerer,
prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "certmanager",
Name: "certificate_acme_errors_total",
Help: "ACME errors by problem type.",
},
[]string{"problem_type"},
),
),
acmeCooldown: registerCollector(
registerer,
prometheus.NewGauge(
prometheus.GaugeOpts{
Subsystem: "certmanager",
Name: "certificate_acme_cooldown",
Help: "1 while the ACME client is in a global rate-limit cooldown.",
},
),
),
stepDuration: registerCollector(
registerer,
prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: "certmanager",
Name: "certificate_provision_step_duration_seconds",
Help: "Duration of certificate provisioning steps in seconds.",
},
[]string{"phase"},
),
),
}
}
func registerCollector[T prometheus.Collector](
registerer prometheus.Registerer,
collector T,
) T {
if err := registerer.Register(collector); err != nil {
if already, ok := errors.AsType[prometheus.AlreadyRegisteredError](err); ok {
if existing, ok := already.ExistingCollector.(T); ok {
return existing
}
}
panic(err)
}
return collector
}
func (m *metrics) observeStep(phase provisionPhase, result provisionResult, started time.Time) {
m.provisionSteps.WithLabelValues(string(phase), string(result)).Inc()
m.stepDuration.WithLabelValues(string(phase)).Observe(time.Since(started).Seconds())
}
func (m *metrics) recordACMEError(problemType string) {
m.acmeErrors.WithLabelValues(normalizeProblemType(problemType)).Inc()
}
const acmeProblemTypePrefix = "urn:ietf:params:acme:error:"
var knownACMEProblemTypes = map[string]struct{}{
"accountdoesnotexist": {},
"alreadyrevoked": {},
"badcsr": {},
"badnonce": {},
"badpublickey": {},
"badrevocationreason": {},
"badsignaturealgorithm": {},
"caa": {},
"compound": {},
"connection": {},
"dns": {},
"externalaccountrequired": {},
"incorrectresponse": {},
"invalidcontact": {},
"malformed": {},
"ordernotready": {},
"ratelimited": {},
"rejectedidentifier": {},
"serverinternal": {},
"tls": {},
"unauthorized": {},
"unsupportedcontact": {},
"unsupportedidentifier": {},
"useractionrequired": {},
}
func normalizeProblemType(problemType string) string {
suffix, ok := strings.CutPrefix(strings.ToLower(problemType), acmeProblemTypePrefix)
if !ok {
return "unknown"
}
if _, ok := knownACMEProblemTypes[suffix]; !ok {
return "unknown"
}
return suffix
}
func (m *metrics) setCooldown(active bool) {
if active {
m.acmeCooldown.Set(1)
return
}
m.acmeCooldown.Set(0)
}