forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
type instruments struct {
|
||||
httpRequests metric.Int64Counter
|
||||
httpDuration metric.Float64Histogram
|
||||
jobs metric.Int64Counter
|
||||
jobDuration metric.Float64Histogram
|
||||
ready metric.Float64Histogram
|
||||
busy atomic.Int64
|
||||
waiting atomic.Int64
|
||||
}
|
||||
|
||||
// newInstruments defines stable names and units once for every provider instance.
|
||||
func newInstruments(meter metric.Meter) instruments {
|
||||
requests, _ := meter.Int64Counter("messenger_http_requests")
|
||||
duration, _ := meter.Float64Histogram("messenger_http_duration_seconds", metric.WithUnit("s"))
|
||||
jobs, _ := meter.Int64Counter("messenger_jobs")
|
||||
jobDuration, _ := meter.Float64Histogram("messenger_job_duration_seconds", metric.WithUnit("s"))
|
||||
ready, _ := meter.Float64Histogram("messenger_ready_duration_seconds", metric.WithUnit("s"))
|
||||
return instruments{httpRequests: requests, httpDuration: duration, jobs: jobs, jobDuration: jobDuration, ready: ready}
|
||||
}
|
||||
|
||||
// recordHTTP avoids raw URLs and identifiers in metric attributes.
|
||||
func (r *Runtime) recordHTTP(ctx context.Context, method, route, class string, seconds float64) {
|
||||
attrs := metric.WithAttributes(attribute.String("method", method), attribute.String("route", route), attribute.String("status_class", class))
|
||||
r.instruments.httpRequests.Add(ctx, 1, attrs)
|
||||
r.instruments.httpDuration.Record(ctx, seconds, attrs)
|
||||
}
|
||||
|
||||
// JobStarted records occupancy independently from queue length.
|
||||
func (r *Runtime) JobStarted() {
|
||||
r.instruments.busy.Add(1)
|
||||
}
|
||||
|
||||
// JobFinished records a real attempt's result and releases its occupancy gauge.
|
||||
func (r *Runtime) JobFinished(ctx context.Context, elapsed time.Duration, err error) {
|
||||
r.instruments.busy.Add(-1)
|
||||
outcome := "ok"
|
||||
if err != nil {
|
||||
outcome = "error"
|
||||
}
|
||||
attrs := metric.WithAttributes(attribute.String("job_type", "attachment.process.v1"), attribute.String("outcome", outcome))
|
||||
r.instruments.jobs.Add(ctx, 1, attrs)
|
||||
r.instruments.jobDuration.Record(ctx, elapsed.Seconds(), attrs)
|
||||
}
|
||||
|
||||
// Ready records end-to-end latency only after a confirmed durable publication.
|
||||
func (r *Runtime) Ready(ctx context.Context, accepted time.Time) {
|
||||
r.instruments.ready.Record(ctx, time.Since(accepted).Seconds())
|
||||
}
|
||||
Reference in New Issue
Block a user