forked from mtusi2026/mtusi2026lab6
Реализация на 5
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/shirou/gopsutil/v4/process"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var waiting atomic.Int64
|
||||
|
||||
func ObservePool(pool *pgxpool.Pool, capacity int) {
|
||||
names := []string{"messenger.db.pool.used", "messenger.db.pool.capacity", "messenger.db.pool.waiting", "messenger.worker.busy", "messenger.worker.capacity", "messenger.process.memory", "messenger.process.cpu", "messenger.telemetry.export_errors"}
|
||||
instruments := make([]metric.Float64ObservableGauge, len(names))
|
||||
for i, n := range names {
|
||||
instruments[i], _ = meter.Float64ObservableGauge(n)
|
||||
}
|
||||
p, _ := process.NewProcess(int32(os.Getpid()))
|
||||
obs := make([]metric.Observable, len(instruments))
|
||||
for i, g := range instruments {
|
||||
obs[i] = g
|
||||
}
|
||||
_, _ = meter.RegisterCallback(func(ctx context.Context, o metric.Observer) error {
|
||||
st := pool.Stat()
|
||||
v := []float64{float64(st.AcquiredConns()), float64(st.MaxConns()), float64(waiting.Load()), float64(Busy.Load()), float64(capacity), 0, 0, float64(ExportErrors.Load())}
|
||||
if p != nil {
|
||||
if m, e := p.MemoryInfoWithContext(ctx); e == nil {
|
||||
v[5] = float64(m.RSS)
|
||||
}
|
||||
if t, e := p.TimesWithContext(ctx); e == nil {
|
||||
v[6] = t.User + t.System
|
||||
}
|
||||
}
|
||||
for i, g := range instruments {
|
||||
o.ObserveFloat64(g, v[i])
|
||||
}
|
||||
return nil
|
||||
}, obs...)
|
||||
}
|
||||
|
||||
type DBTracer struct{}
|
||||
type acquireKey struct{}
|
||||
|
||||
func (DBTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, d pgx.TraceQueryStartData) context.Context {
|
||||
op := "OTHER"
|
||||
f := strings.Fields(d.SQL)
|
||||
if len(f) > 0 {
|
||||
switch strings.ToUpper(f[0]) {
|
||||
case "SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK":
|
||||
op = strings.ToUpper(f[0])
|
||||
}
|
||||
}
|
||||
ctx, sp := Start(ctx, "postgresql "+op, trace.SpanKindClient)
|
||||
sp.SetAttributes(attribute.String("db.system.name", "postgresql"), attribute.String("db.operation.name", op))
|
||||
return ctx
|
||||
}
|
||||
func (DBTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, d pgx.TraceQueryEndData) {
|
||||
End(trace.SpanFromContext(ctx), d.Err)
|
||||
}
|
||||
func (DBTracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
||||
waiting.Add(1)
|
||||
return context.WithValue(ctx, acquireKey{}, time.Now())
|
||||
}
|
||||
func (DBTracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, d pgxpool.TraceAcquireEndData) {
|
||||
waiting.Add(-1)
|
||||
if t, ok := ctx.Value(acquireKey{}).(time.Time); ok {
|
||||
Duration(ctx, "messenger.db.acquire.duration", time.Since(t))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user