feat(api): Prometheus metrics for operators
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s
The API serves Prometheus metrics on a listener of its own (UPTIME_API_METRICS_LISTEN, default :9090, "off" disables it), so the public ingress never exposes them: - requests by route and status, with durations except for long polls and agent streams - dispatched checks, stored and rejected results per region, how late results arrive, live checkers and overdue checks per platform region - notification and subscriber mail deliveries, incidents opened and resolved - runs, duration and last success of the background jobs - platform totals (orgs, users, monitors by type, status pages, subscribers, open incidents, pending notifications), read at most every 15 seconds when scraped - the Postgres and Redis connection pools, Go runtime and build info The Helm chart exposes the port on the API service and can add a ServiceMonitor for the Prometheus Operator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
ab25c1f218
commit
e9984f8271
30 files changed
+1005
-20
No files matched your search
+15
-1
@@ -298,6 +298,7 @@ func (s *Server) agentResults(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
s.metrics.Rejected("duplicate", len(req.Results)-len(fresh))
|
||||
req.Results = fresh
|
||||
ids := make([]uuid.UUID, len(req.Results))
|
||||
for i, r := range req.Results {
|
||||
@@ -308,6 +309,8 @@ func (s *Server) agentResults(c *gin.Context) {
|
||||
var (
|
||||
accepted []agentapi.Result
|
||||
changed []uuid.UUID
|
||||
unknown int
|
||||
badTime int
|
||||
)
|
||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
a := agentOf(c)
|
||||
@@ -325,7 +328,12 @@ func (s *Server) agentResults(c *gin.Context) {
|
||||
// A checker may only report monitors of its own region, and
|
||||
// timestamps far from now are rejected rather than skewing
|
||||
// history.
|
||||
if !ok || r.StartedAt.After(now.Add(time.Minute)) || r.StartedAt.Before(now.Add(-24*time.Hour)) {
|
||||
switch {
|
||||
case !ok:
|
||||
unknown++
|
||||
continue
|
||||
case r.StartedAt.After(now.Add(time.Minute)) || r.StartedAt.Before(now.Add(-24*time.Hour)):
|
||||
badTime++
|
||||
continue
|
||||
}
|
||||
row := store.InsertCheckResultsParams{
|
||||
@@ -353,6 +361,12 @@ func (s *Server) agentResults(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
s.metrics.Rejected("unknown_monitor", unknown)
|
||||
s.metrics.Rejected("bad_time", badTime)
|
||||
label := regionLabel(agentOf(c))
|
||||
for _, r := range accepted {
|
||||
s.metrics.Result(label, r.OK, now.Sub(r.StartedAt))
|
||||
}
|
||||
s.pagesChanged(ctx, changed...)
|
||||
s.evaluateResults(ctx, region, accepted, monitors)
|
||||
s.storeEvidence(ctx, region, instance, accepted, monitors)
|
||||
|
||||
@@ -128,6 +128,7 @@ func (s *Server) transition(ctx context.Context, m store.MonitorsForResultsRow,
|
||||
}
|
||||
}
|
||||
s.log.InfoContext(ctx, "incident "+event, "monitor", m.ID, "incident", inc.ID, "channels", len(channels))
|
||||
s.metrics.Incident(event)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -167,7 +168,9 @@ func (s *Server) deliverBatch(ctx context.Context) int {
|
||||
return 0
|
||||
}
|
||||
for _, n := range rows {
|
||||
sent := time.Now()
|
||||
sendErr := s.send(ctx, n.ChannelType, n.ChannelConfig, eventOf(n))
|
||||
s.metrics.Notification(n.ChannelType, sendErr, time.Since(sent))
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
if sendErr == nil {
|
||||
return q.MarkNotificationSent(ctx, n.ID)
|
||||
|
||||
@@ -81,7 +81,7 @@ func (s *Server) RunDomainWatch(ctx context.Context) {
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if ok, err := s.rdb.SetNX(ctx, keyDomainWatch, 1, domainWatchEvery-5*time.Second).Result(); err == nil && ok {
|
||||
if err := s.WatchDomains(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("domain_watch", func() error { return s.WatchDomains(ctx, time.Now()) }); err != nil && ctx.Err() == nil {
|
||||
s.log.WarnContext(ctx, "checking custom domains failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,10 +129,12 @@ func (s *Server) RunHeartbeats(ctx context.Context) {
|
||||
if err != nil || !got {
|
||||
continue
|
||||
}
|
||||
if err := s.CheckHeartbeats(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("heartbeats", func() error { return s.CheckHeartbeats(ctx, time.Now()) }); err != nil && ctx.Err() == nil {
|
||||
s.log.ErrorContext(ctx, "checking heartbeats failed", "error", err)
|
||||
}
|
||||
if err := s.CheckAlertmanagerMonitors(ctx, nil, time.Now(), false); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("alertmanager_monitors", func() error {
|
||||
return s.CheckAlertmanagerMonitors(ctx, nil, time.Now(), false)
|
||||
}); err != nil && ctx.Err() == nil {
|
||||
s.log.ErrorContext(ctx, "checking alertmanager monitors failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (s *Server) RunHousekeeping(ctx context.Context) {
|
||||
tick := time.NewTicker(time.Hour)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if err := s.Housekeep(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("housekeeping", func() error { return s.Housekeep(ctx, time.Now()) }); err != nil && ctx.Err() == nil {
|
||||
s.log.ErrorContext(ctx, "housekeeping failed", "error", err)
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
// Platform metrics are read from the database and Redis when scraped, at
|
||||
// most once per platformMetricsTTL, so scrapes stay cheap. Every replica
|
||||
// reports the same platform totals; dashboards take their max().
|
||||
const (
|
||||
platformMetricsTTL = 15 * time.Second
|
||||
// checksLateAfter leaves the runs alone that the next dispatch, every
|
||||
// second, takes anyway.
|
||||
checksLateAfter = 10 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
descCheckers = prometheus.NewDesc("uptimesebi_checker_instances",
|
||||
"Live checker instances per platform region.", []string{"region"}, nil)
|
||||
descOverdue = prometheus.NewDesc("uptimesebi_checks_overdue",
|
||||
"Checks of a platform region due for more than 10 seconds, as when no checker of the region is live.", []string{"region"}, nil)
|
||||
descRegions = prometheus.NewDesc("uptimesebi_regions",
|
||||
"Platform regions by whether a checker is live.", []string{"online"}, nil)
|
||||
descOrgs = prometheus.NewDesc("uptimesebi_orgs",
|
||||
"Organizations on the platform.", nil, nil)
|
||||
descUsers = prometheus.NewDesc("uptimesebi_users",
|
||||
"User accounts on the platform.", nil, nil)
|
||||
descMonitors = prometheus.NewDesc("uptimesebi_monitors",
|
||||
"Monitors by type and whether they are enabled.", []string{"type", "enabled"}, nil)
|
||||
descPages = prometheus.NewDesc("uptimesebi_status_pages",
|
||||
"Status pages on the platform.", nil, nil)
|
||||
descSubscribers = prometheus.NewDesc("uptimesebi_status_page_subscribers",
|
||||
"Confirmed subscribers of status pages.", nil, nil)
|
||||
descOpenIncidents = prometheus.NewDesc("uptimesebi_incidents_open",
|
||||
"Incidents not resolved yet.", nil, nil)
|
||||
descPending = prometheus.NewDesc("uptimesebi_notifications_pending",
|
||||
"Notifications waiting for delivery, by kind: channel or subscriber.", []string{"kind"}, nil)
|
||||
descScrapeError = prometheus.NewDesc("uptimesebi_platform_metrics_error",
|
||||
"1 if reading the platform metrics failed on the last attempt.", nil, nil)
|
||||
)
|
||||
|
||||
type platformSnapshot struct {
|
||||
counts store.PlatformCountsRow
|
||||
monitors []store.MonitorCountsRow
|
||||
regions []regionLoad
|
||||
}
|
||||
|
||||
type regionLoad struct {
|
||||
key string
|
||||
checkers int64
|
||||
overdue int64
|
||||
}
|
||||
|
||||
// platformCollector reports the platform's totals, checkers and backlog.
|
||||
type platformCollector struct {
|
||||
s *Server
|
||||
mu sync.Mutex
|
||||
last platformSnapshot
|
||||
err error
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
// Describe implements prometheus.Collector.
|
||||
func (p *platformCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
for _, d := range []*prometheus.Desc{
|
||||
descCheckers, descOverdue, descRegions, descOrgs, descUsers, descMonitors,
|
||||
descPages, descSubscribers, descOpenIncidents, descPending, descScrapeError,
|
||||
} {
|
||||
ch <- d
|
||||
}
|
||||
}
|
||||
|
||||
// Collect implements prometheus.Collector. It reads the database and
|
||||
// Redis at most once per platformMetricsTTL.
|
||||
func (p *platformCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
p.mu.Lock()
|
||||
if time.Now().After(p.expires) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
snap, err := p.s.platformSnapshot(ctx)
|
||||
cancel()
|
||||
// A failed read keeps the last good values, flagged.
|
||||
if err == nil {
|
||||
p.last = snap
|
||||
} else {
|
||||
p.s.log.Warn("reading platform metrics failed", "error", err)
|
||||
}
|
||||
p.err, p.expires = err, time.Now().Add(platformMetricsTTL)
|
||||
}
|
||||
snap, failed := p.last, p.err != nil
|
||||
p.mu.Unlock()
|
||||
|
||||
gauge := func(d *prometheus.Desc, v int64, labels ...string) {
|
||||
ch <- prometheus.MustNewConstMetric(d, prometheus.GaugeValue, float64(v), labels...)
|
||||
}
|
||||
var online, offline int64
|
||||
for _, r := range snap.regions {
|
||||
gauge(descCheckers, r.checkers, r.key)
|
||||
gauge(descOverdue, r.overdue, r.key)
|
||||
if r.checkers > 0 {
|
||||
online++
|
||||
} else {
|
||||
offline++
|
||||
}
|
||||
}
|
||||
gauge(descRegions, online, "true")
|
||||
gauge(descRegions, offline, "false")
|
||||
c := snap.counts
|
||||
gauge(descOrgs, c.Orgs)
|
||||
gauge(descUsers, c.Users)
|
||||
gauge(descPages, c.StatusPages)
|
||||
gauge(descSubscribers, c.Subscribers)
|
||||
gauge(descOpenIncidents, c.OpenIncidents)
|
||||
gauge(descPending, c.PendingNotifications, "channel")
|
||||
gauge(descPending, c.PendingSubscriberNotifications, "subscriber")
|
||||
for _, m := range snap.monitors {
|
||||
enabled := "false"
|
||||
if m.Enabled {
|
||||
enabled = "true"
|
||||
}
|
||||
gauge(descMonitors, m.Monitors, m.Type, enabled)
|
||||
}
|
||||
var flag int64
|
||||
if failed {
|
||||
flag = 1
|
||||
}
|
||||
gauge(descScrapeError, flag)
|
||||
}
|
||||
|
||||
func (s *Server) platformSnapshot(ctx context.Context) (platformSnapshot, error) {
|
||||
var (
|
||||
snap platformSnapshot
|
||||
regions []store.Region
|
||||
)
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
var err error
|
||||
if snap.counts, err = q.PlatformCounts(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if snap.monitors, err = q.MonitorCounts(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
regions, err = q.ListPlatformRegions(ctx)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return snap, err
|
||||
}
|
||||
for _, r := range regions {
|
||||
n, err := s.sched.Instances(ctx, r.ID.String())
|
||||
if err != nil {
|
||||
return snap, err
|
||||
}
|
||||
late, err := s.sched.Overdue(ctx, r.ID.String(), checksLateAfter)
|
||||
if err != nil {
|
||||
return snap, err
|
||||
}
|
||||
snap.regions = append(snap.regions, regionLoad{key: r.Key, checkers: n, overdue: late})
|
||||
}
|
||||
return snap, nil
|
||||
}
|
||||
|
||||
// regionLabel names a checker's region in metrics: the key of a platform
|
||||
// region, or "own" for organizations' checkers, whose regions are many and
|
||||
// not the operator's.
|
||||
func regionLabel(a store.GetAgentByHashRow) string {
|
||||
if a.OrgID != nil {
|
||||
return "own"
|
||||
}
|
||||
if a.RegionKey != nil {
|
||||
return *a.RegionKey
|
||||
}
|
||||
return "none"
|
||||
}
|
||||
|
||||
// StreamingRoutes stay open on purpose: the agents' streams and the
|
||||
// checkers' long polls. Request metrics count them but do not time them.
|
||||
func StreamingRoutes() []string {
|
||||
return []string{agentapi.Prefix + agentapi.StreamPath, agentapi.Prefix + "/jobs"}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
)
|
||||
|
||||
// scrape returns the value of every series of the API's metrics, keyed by
|
||||
// name and labels as the text format prints them.
|
||||
func (e *env) scrape(t *testing.T) map[string]float64 {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
e.metrics.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/metrics", nil))
|
||||
if rec.Code != http.StatusOK || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/plain") {
|
||||
t.Fatalf("metrics answered %d %q", rec.Code, rec.Header().Get("Content-Type"))
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
out := map[string]float64{}
|
||||
for _, line := range strings.Split(string(body), "\n") {
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
i := strings.LastIndexByte(line, ' ')
|
||||
v, err := strconv.ParseFloat(line[i+1:], 64)
|
||||
if err != nil {
|
||||
t.Fatalf("bad metric line %q", line)
|
||||
}
|
||||
out[line[:i]] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestMetrics(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
key := uniq("metrics-")
|
||||
tok, region := e.platformChecker(t, key)
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("id")+".test", uniq("id-"))
|
||||
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
||||
"name": "m", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region},
|
||||
})
|
||||
monitor := uuid.MustParse(m["id"].(string))
|
||||
client := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "test").WithHTTPClient(e.srv.Client())
|
||||
at := time.Now().Truncate(time.Millisecond)
|
||||
batch := []agentapi.Result{
|
||||
{MonitorID: monitor, ScheduledAt: at, StartedAt: at, OK: true},
|
||||
{MonitorID: monitor, ScheduledAt: at.Add(time.Second), StartedAt: at.Add(time.Second), OK: false},
|
||||
{MonitorID: uuid.New(), ScheduledAt: at, StartedAt: at, OK: true},
|
||||
}
|
||||
for range 2 {
|
||||
if err := client.Report(context.Background(), batch); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
got := e.scrape(t)
|
||||
for series, want := range map[string]float64{
|
||||
`uptimesebi_check_results_total{ok="true",region="` + key + `"}`: 1,
|
||||
`uptimesebi_check_results_total{ok="false",region="` + key + `"}`: 1,
|
||||
`uptimesebi_check_results_rejected_total{reason="duplicate"}`: 3,
|
||||
`uptimesebi_check_results_rejected_total{reason="unknown_monitor"}`: 1,
|
||||
`uptimesebi_checker_instances{region="` + key + `"}`: 0,
|
||||
`uptimesebi_platform_metrics_error`: 0,
|
||||
} {
|
||||
if v, ok := got[series]; !ok || v != want {
|
||||
t.Errorf("%s = %v (present %v), want %v", series, v, ok, want)
|
||||
}
|
||||
}
|
||||
// Other tests share the database, so totals are at least ours.
|
||||
for series, least := range map[string]float64{
|
||||
`uptimesebi_orgs`: 1,
|
||||
`uptimesebi_users`: 1,
|
||||
`uptimesebi_monitors{enabled="true",type="tcp"}`: 1,
|
||||
`uptimesebi_regions{online="false"}`: 1,
|
||||
} {
|
||||
if got[series] < least {
|
||||
t.Errorf("%s = %v, want at least %v", series, got[series], least)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -534,6 +534,7 @@ func (s *Server) RunScheduler(ctx context.Context) {
|
||||
return
|
||||
case <-dispatch.C:
|
||||
n, err := s.sched.Dispatch(ctx)
|
||||
s.metrics.Dispatched(n)
|
||||
if err != nil && ctx.Err() == nil {
|
||||
s.log.WarnContext(ctx, "dispatch failed", "error", err)
|
||||
}
|
||||
@@ -555,19 +556,22 @@ func (s *Server) reconcile(ctx context.Context) {
|
||||
// Taken before reading the database: monitors upserted later survive.
|
||||
start := time.Now()
|
||||
defer func() { s.log.DebugContext(ctx, "schedule reconciled", "took", time.Since(start)) }()
|
||||
var rows []store.ListEnabledMonitorsForScheduleRow
|
||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
var err error
|
||||
rows, err = q.ListEnabledMonitorsForSchedule(ctx)
|
||||
return err
|
||||
})
|
||||
if err == nil {
|
||||
err = s.metrics.Job("reconcile", func() error {
|
||||
var rows []store.ListEnabledMonitorsForScheduleRow
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
var err error
|
||||
rows, err = q.ListEnabledMonitorsForSchedule(ctx)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
want := make([]schedule.Monitor, len(rows))
|
||||
for i, r := range rows {
|
||||
want[i] = schedule.Monitor{ID: r.ID, Interval: time.Duration(r.IntervalSeconds) * time.Second, Regions: r.Regions}
|
||||
}
|
||||
err = s.sched.Reconcile(ctx, want, start)
|
||||
}
|
||||
return s.sched.Reconcile(ctx, want, start)
|
||||
})
|
||||
if err != nil && ctx.Err() == nil {
|
||||
s.log.WarnContext(ctx, "schedule reconciliation failed", "error", err)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func (s *Server) runOrgPurges(ctx context.Context) {
|
||||
tick := time.NewTicker(orgPurgeEvery)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if err := s.PurgeDeletedOrgs(ctx); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("org_purge", func() error { return s.PurgeDeletedOrgs(ctx) }); err != nil && ctx.Err() == nil {
|
||||
s.log.ErrorContext(ctx, "purging deleted organizations failed", "error", err)
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -39,7 +39,7 @@ func (s *Server) RunRegionWatch(ctx context.Context) {
|
||||
tick := time.NewTicker(regionWatchEvery)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
if err := s.WatchRegions(ctx, time.Now()); err != nil && ctx.Err() == nil {
|
||||
if err := s.metrics.Job("region_watch", func() error { return s.WatchRegions(ctx, time.Now()) }); err != nil && ctx.Err() == nil {
|
||||
s.log.WarnContext(ctx, "watching regions failed", "error", err)
|
||||
}
|
||||
select {
|
||||
|
||||
+13
-1
@@ -18,6 +18,7 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/evaluate"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/metrics"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/requestid"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/schedule"
|
||||
@@ -72,6 +73,8 @@ type Server struct {
|
||||
publicRegions publicRegionsCache
|
||||
// ogImages keeps the preview image of the public pages.
|
||||
ogImages appImages
|
||||
// metrics counts what the instance does, for Prometheus.
|
||||
metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// Deps are the external resources a Server needs.
|
||||
@@ -81,6 +84,8 @@ type Deps struct {
|
||||
Redis redis.UniversalClient
|
||||
Keys *sealed.Keyring
|
||||
Catalog *i18n.Catalog
|
||||
// Metrics, if nil, are created for the Server alone.
|
||||
Metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// New returns a Server. It does not register any routes; see Mount.
|
||||
@@ -88,7 +93,12 @@ type Deps struct {
|
||||
func New(cfg config.API, d Deps) *Server {
|
||||
allowPrivate := cfg.SSOAllowPrivate()
|
||||
mail, _ := notify.ParseSMTP(cfg.SMTPURL)
|
||||
return &Server{
|
||||
m := d.Metrics
|
||||
if m == nil {
|
||||
m = metrics.New(string(cfg.Edition))
|
||||
}
|
||||
s := &Server{
|
||||
metrics: m,
|
||||
orgPurges: make(chan struct{}, 1),
|
||||
eval: evaluate.New(d.Redis),
|
||||
smtp: mail,
|
||||
@@ -111,6 +121,8 @@ func New(cfg config.API, d Deps) *Server {
|
||||
support: newVersioned[supportSettings]("support"),
|
||||
pageHome: newVersioned[string]("statuspage-home"),
|
||||
}
|
||||
m.Register(&platformCollector{s: s})
|
||||
return s
|
||||
}
|
||||
|
||||
// EndLongPollsOn makes the checkers' long polls return as soon as ch is
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/metrics"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/platform"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/sealed"
|
||||
@@ -37,6 +38,8 @@ type env struct {
|
||||
dbPool *pgxpool.Pool
|
||||
rdb *redis.Client
|
||||
api *api.Server
|
||||
// metrics are the API's own, as cmd/api serves them.
|
||||
metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
func newEnv(t *testing.T) *env {
|
||||
@@ -104,7 +107,8 @@ func newEnvWith(t *testing.T, adjust func(*config.API)) *env {
|
||||
if adjust != nil {
|
||||
adjust(&cfg)
|
||||
}
|
||||
e.api = api.New(cfg, api.Deps{Log: log, DB: tenant.New(pool), Redis: rdb, Keys: keys, Catalog: catalog})
|
||||
e.metrics = metrics.New(string(cfg.Edition))
|
||||
e.api = api.New(cfg, api.Deps{Log: log, DB: tenant.New(pool), Redis: rdb, Keys: keys, Catalog: catalog, Metrics: e.metrics})
|
||||
e.api.Mount(r)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -314,6 +314,7 @@ func (s *Server) deliverSubscriberBatch(ctx context.Context) int {
|
||||
}
|
||||
for _, n := range rows {
|
||||
sendErr := s.smtp.Send(ctx, s.subscriberMail(n))
|
||||
s.metrics.SubscriberMail(sendErr)
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
if sendErr == nil {
|
||||
return q.MarkSubscriberNotificationSent(ctx, n.ID)
|
||||
|
||||
Reference in new issue
Block a user