Files

96 lines
3.5 KiB
Go

package telemetry
import (
"backend/internal/domain/model"
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)
func (t *Telemetry) Gauges(source func(context.Context) (map[string]float64, error)) error {
if t == nil {
return nil
}
meter := t.meters.Meter("messenger")
names := []string{"queue_depth", "queue_oldest_age_seconds", "db_pool_used", "db_pool_capacity", "database_observer_up", "db_pool_pending_acquires", "worker_busy", "worker_capacity", "process_resident_memory_bytes", "go_heap_bytes", "ready_slo_matured_10s", "ready_slo_timely_10s", "ready_slo_matured_30s", "ready_slo_timely_30s", "ready_slo_matured_300s", "ready_slo_timely_300s"}
gauges := make([]metric.Float64ObservableGauge, len(names))
obs := make([]metric.Observable, len(names))
for i, name := range names {
g, e := meter.Float64ObservableGauge(name)
if e != nil {
return e
}
gauges[i] = g
obs[i] = g
}
cpu, e := meter.Float64ObservableCounter("process_cpu", metric.WithUnit("s"))
if e != nil {
return e
}
failures, e := meter.Float64ObservableCounter("database_observer_failures")
if e != nil {
return e
}
obs = append(obs, cpu, failures)
_, e = meter.RegisterCallback(func(ctx context.Context, o metric.Observer) error {
ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
values, err := source(ctx)
if err != nil {
values = nil
t.observationFailures.Add(1)
t.Event(ctx, "telemetry.database_observer", "unavailable")
}
if values == nil {
values = map[string]float64{}
}
o.ObserveFloat64(failures, float64(t.observationFailures.Load()))
values["database_observer_up"] = 1
if err != nil {
values["database_observer_up"] = 0
}
values["db_pool_pending_acquires"] = float64(t.poolPending.Load())
if t.worker {
values["worker_capacity"] = 1
values["worker_busy"] = float64(t.busy.Load())
}
var self, children syscall.Rusage
if syscall.Getrusage(syscall.RUSAGE_SELF, &self) == nil && syscall.Getrusage(syscall.RUSAGE_CHILDREN, &children) == nil {
o.ObserveFloat64(cpu, float64(self.Utime.Sec+self.Stime.Sec+children.Utime.Sec+children.Stime.Sec)+float64(self.Utime.Usec+self.Stime.Usec+children.Utime.Usec+children.Stime.Usec)/1e6)
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
values["go_heap_bytes"] = float64(mem.HeapAlloc)
if raw, e := os.ReadFile("/proc/self/statm"); e == nil {
parts := strings.Fields(string(raw))
if len(parts) > 1 {
n, e := strconv.ParseUint(parts[1], 10, 64)
if e == nil {
values["process_resident_memory_bytes"] = float64(n * uint64(os.Getpagesize()))
}
}
}
for i, name := range names {
if v, ok := values[name]; ok {
if name == "queue_depth" || name == "queue_oldest_age_seconds" {
o.ObserveFloat64(gauges[i], v, metric.WithAttributes(attribute.String("queue", "attachment-jobs")))
} else {
o.ObserveFloat64(gauges[i], v)
}
}
}
return nil
}, obs...)
return e
}
func PipelineValues(m model.PipelineMetrics) map[string]float64 {
return map[string]float64{"queue_depth": float64(m.Depth), "queue_oldest_age_seconds": m.Oldest, "ready_slo_matured_10s": float64(m.ReadyMatured[0]), "ready_slo_timely_10s": float64(m.ReadyTimely[0]), "ready_slo_matured_30s": float64(m.ReadyMatured[1]), "ready_slo_timely_30s": float64(m.ReadyTimely[1]), "ready_slo_matured_300s": float64(m.ReadyMatured[2]), "ready_slo_timely_300s": float64(m.ReadyTimely[2])}
}