Files
uptimesebi/internal/api/timing.go
T
Sebastian UnterschützandClaude Opus 5.5 ec33ef5c56
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m12s
ci / test (pull_request) Successful in 12m47s
ci / build (pull_request) Canceled after 1m41s
ci / image (pull_request) Canceled after 0s
perf(console): faster dashboard for organizations with thousands of monitors
- The range picker no longer looks for the first result: on the real-time
  daily aggregate that aggregated the organization's recent raw results
  on every request. The oldest monitor's creation bounds the ranges now.
- Sorting worked out each row's key per comparison; it does so once now
  and moves each row once (40-60 ms to 5 ms for 3,500 monitors).
- The queries behind the monitor list run in parallel.
- The monitor API reads the latest results of the page's monitors only.
- The dashboard reports its steps in a Server-Timing header and logs
  them when it takes a second or more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-26 21:35:09 +01:00

62 lines
1.5 KiB
Go

package api
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// slowPage is when a console page logs how long its steps took.
const slowPage = time.Second
// stepTimer measures the steps of a request. It reports them to the
// browser in a Server-Timing header and, when the request was slow, in
// the log.
type stepTimer struct {
start, last time.Time
steps []step
}
type step struct {
name string
d time.Duration
}
func newStepTimer() *stepTimer {
now := time.Now()
return &stepTimer{start: now, last: now}
}
// done ends the step name.
func (t *stepTimer) done(name string) {
now := time.Now()
t.steps = append(t.steps, step{name, now.Sub(t.last)})
t.last = now
}
// header sets the Server-Timing header of the steps so far; call it
// before the body is written.
func (t *stepTimer) header(c *gin.Context) {
parts := make([]string, len(t.steps))
for i, s := range t.steps {
parts[i] = fmt.Sprintf("%s;dur=%.1f", s.name, float64(s.d.Microseconds())/1000)
}
c.Header("Server-Timing", strings.Join(parts, ", "))
}
// logIfSlow logs the steps when the request took slowPage or longer.
func (t *stepTimer) logIfSlow(c *gin.Context, log *slog.Logger) {
total := time.Since(t.start)
if total < slowPage {
return
}
attrs := []any{"path", c.Request.URL.Path, "total_ms", total.Milliseconds()}
for _, s := range t.steps {
attrs = append(attrs, s.name+"_ms", s.d.Milliseconds())
}
log.WarnContext(c.Request.Context(), "slow console page", attrs...)
}