forked from mtusi2026/mtusi2026lab6
Реализовать наблюдаемость мессенджера для ЛР 6 на четыре
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
func WatchPool(pool *pgxpool.Pool, waiting *atomic.Int64) {
|
||||
connections := instrument(meter.Int64ObservableGauge("messenger_db_pool_connections"))
|
||||
acquiring := instrument(meter.Int64ObservableGauge("messenger_db_pool_waiting"))
|
||||
waitTime := instrument(meter.Float64ObservableCounter("messenger_db_pool_wait", metric.WithUnit("s")))
|
||||
_, _ = meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
|
||||
stats := pool.Stat()
|
||||
o.ObserveInt64(connections, int64(stats.AcquiredConns()), metric.WithAttributes(attribute.String("state", "used")))
|
||||
o.ObserveInt64(connections, int64(stats.MaxConns()), metric.WithAttributes(attribute.String("state", "capacity")))
|
||||
o.ObserveInt64(acquiring, waiting.Load())
|
||||
o.ObserveFloat64(waitTime, stats.EmptyAcquireWaitTime().Seconds())
|
||||
return nil
|
||||
}, connections, acquiring, waitTime)
|
||||
}
|
||||
|
||||
type QueueSample struct {
|
||||
Depth int64
|
||||
OldestSeconds float64
|
||||
At time.Time
|
||||
}
|
||||
|
||||
func WatchQueue(queue string) func(QueueSample) {
|
||||
depth := instrument(meter.Int64ObservableGauge("messenger_queue_depth"))
|
||||
age := instrument(meter.Float64ObservableGauge("messenger_queue_oldest_age", metric.WithUnit("s")))
|
||||
freshness := instrument(meter.Float64ObservableGauge("messenger_queue_sample_age", metric.WithUnit("s")))
|
||||
var current atomic.Pointer[QueueSample]
|
||||
attrs := metric.WithAttributes(attribute.String("queue", queue))
|
||||
_, _ = meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
|
||||
if sample := current.Load(); sample != nil {
|
||||
o.ObserveInt64(depth, sample.Depth, attrs)
|
||||
o.ObserveFloat64(age, sample.OldestSeconds, attrs)
|
||||
o.ObserveFloat64(freshness, time.Since(sample.At).Seconds(), attrs)
|
||||
}
|
||||
return nil
|
||||
}, depth, age, freshness)
|
||||
return func(sample QueueSample) { current.Store(&sample) }
|
||||
}
|
||||
|
||||
var workerBusy atomic.Int64
|
||||
|
||||
func WatchWorker(lanes int) {
|
||||
busy := instrument(meter.Int64ObservableGauge("messenger_worker_busy"))
|
||||
capacity := instrument(meter.Int64ObservableGauge("messenger_worker_capacity"))
|
||||
_, _ = meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
|
||||
o.ObserveInt64(busy, workerBusy.Load())
|
||||
o.ObserveInt64(capacity, int64(lanes))
|
||||
return nil
|
||||
}, busy, capacity)
|
||||
}
|
||||
|
||||
func WorkerAttempt() func() {
|
||||
workerBusy.Add(1)
|
||||
return func() { workerBusy.Add(-1) }
|
||||
}
|
||||
Reference in New Issue
Block a user