Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/shirou/gopsutil/v4/process"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
// ObservePool samples the running process and pool on the metric reader's
|
||||
// collection cycle. RSS is resident process memory, including native memory.
|
||||
func ObservePool(pool *pgxpool.Pool) error {
|
||||
m := otel.Meter("messenger.runtime")
|
||||
acquired, err := m.Int64ObservableGauge("messenger.db.pool.acquired")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
capacity, err := m.Int64ObservableGauge("messenger.db.pool.max")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wait, err := m.Float64ObservableCounter("messenger.db.pool.wait", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cpu, err := m.Float64ObservableCounter("messenger.process.cpu", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rss, err := m.Int64ObservableGauge("messenger.process.resident", metric.WithUnit("By"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p, err := process.NewProcess(int32(os.Getpid()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.RegisterCallback(func(ctx context.Context, o metric.Observer) error {
|
||||
stats := pool.Stat()
|
||||
o.ObserveInt64(acquired, int64(stats.AcquiredConns()))
|
||||
o.ObserveInt64(capacity, int64(stats.MaxConns()))
|
||||
o.ObserveFloat64(wait, stats.EmptyAcquireWaitTime().Seconds())
|
||||
times, err := p.TimesWithContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.ObserveFloat64(cpu, times.System+times.User)
|
||||
mem, err := p.MemoryInfoWithContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.ObserveInt64(rss, int64(mem.RSS))
|
||||
return nil
|
||||
}, acquired, capacity, wait, cpu, rss)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user