forked from mtusi2026/mtusi2026lab7
113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"backend/internal/config"
|
|
"context"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
metricpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
|
|
"google.golang.org/protobuf/proto"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNativePoolWaitExportsPendingAcquire(t *testing.T) {
|
|
if os.Getenv("RUN_TELEMETRY_DB_TESTS") != "1" {
|
|
t.Skip("isolated real PostgreSQL required")
|
|
}
|
|
var observed atomic.Int64
|
|
observed.Store(-1)
|
|
sink := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/v1/metrics" {
|
|
raw, _ := io.ReadAll(r.Body)
|
|
data := &metricpb.ExportMetricsServiceRequest{}
|
|
if proto.Unmarshal(raw, data) == nil {
|
|
for _, rm := range data.ResourceMetrics {
|
|
for _, sm := range rm.ScopeMetrics {
|
|
for _, m := range sm.Metrics {
|
|
if m.Name == "db_pool_pending_acquires" {
|
|
for _, p := range m.GetGauge().GetDataPoints() {
|
|
observed.Store(int64(p.GetAsDouble()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
w.Header().Set("Content-Type", "application/x-protobuf")
|
|
w.WriteHeader(200)
|
|
}))
|
|
defer sink.Close()
|
|
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", sink.URL)
|
|
t.Setenv("OTEL_SDK_DISABLED", "false")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
sdk, e := Setup(ctx, "messenger-api")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer func() { sdk.Shutdown(context.Background()); current.Store(nil) }()
|
|
dsn, e := config.ReadSecret("/run/secrets/database_url")
|
|
if e != nil {
|
|
t.Fatal("database file unavailable")
|
|
}
|
|
pc, e := pgxpool.ParseConfig(dsn)
|
|
if e != nil {
|
|
t.Fatal("database config unavailable")
|
|
}
|
|
pc.MaxConns = 1
|
|
pc.ConnConfig.Tracer = DBTracer{}
|
|
pool, e := pgxpool.NewWithConfig(ctx, pc)
|
|
if e != nil {
|
|
t.Fatal("pool unavailable")
|
|
}
|
|
defer pool.Close()
|
|
held, e := pool.Acquire(ctx)
|
|
if e != nil {
|
|
t.Fatal("first acquisition failed")
|
|
}
|
|
defer held.Release()
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
c, e := pool.Acquire(ctx)
|
|
if e == nil {
|
|
c.Release()
|
|
}
|
|
done <- e
|
|
}()
|
|
waiting := false
|
|
for i := 0; i < 1000; i++ {
|
|
if sdk.poolPending.Load() == 1 {
|
|
waiting = true
|
|
break
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
if !waiting {
|
|
t.Fatal("native blocked acquisition not tracked")
|
|
}
|
|
if e = sdk.Gauges(func(context.Context) (map[string]float64, error) {
|
|
return map[string]float64{"db_pool_used": float64(pool.Stat().AcquiredConns()), "db_pool_capacity": 1}, nil
|
|
}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = sdk.meters.ForceFlush(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if observed.Load() != 1 {
|
|
t.Fatal("actual OTLP pending-acquires gauge did not show native pool wait")
|
|
}
|
|
held.Release()
|
|
if e = <-done; e != nil {
|
|
t.Fatal("queued acquisition failed after release")
|
|
}
|
|
if sdk.poolPending.Load() != 0 {
|
|
t.Fatal("completed native acquisition still pending")
|
|
}
|
|
}
|