diff --git a/cmd/cgat/main.go b/cmd/cgat/main.go index ec5e7939667c13ad30527ab95d712a529d745995..b7cf5ae6c0baf74fdb8481c3bdf330917f151f3d 100644 --- a/cmd/cgat/main.go +++ b/cmd/cgat/main.go @@ -16,7 +16,7 @@ import ( func main() { http.Handle("/metrics", promhttp.Handler()) - go http.ListenAndServe("localhost:6060", nil) + go http.ListenAndServe(":6060", nil) confLocation := os.Getenv("CONFIG_LOCATION") if confLocation == "" { diff --git a/lib/gat/pool/transaction/pool.go b/lib/gat/pool/transaction/pool.go index f3fe57768da8d51f982e85d84d899959bc6429de..a41d331d9dbd57bbcf3b32c52da7dec04711690b 100644 --- a/lib/gat/pool/transaction/pool.go +++ b/lib/gat/pool/transaction/pool.go @@ -2,12 +2,14 @@ package transaction import ( "context" - "gfx.cafe/gfx/pggat/lib/config" - "gfx.cafe/gfx/pggat/lib/gat" - "gfx.cafe/gfx/pggat/lib/gat/protocol" "runtime" "sync/atomic" "time" + + "gfx.cafe/gfx/pggat/lib/config" + "gfx.cafe/gfx/pggat/lib/gat" + "gfx.cafe/gfx/pggat/lib/gat/protocol" + "gfx.cafe/gfx/pggat/lib/metrics" ) type Pool struct { @@ -44,7 +46,7 @@ func (c *Pool) GetDatabase() gat.Database { func (c *Pool) getWorker() *worker { start := time.Now() defer func() { - c.database.GetStats().AddWaitTime(time.Now().Sub(start).Microseconds()) + metrics.RecordWaitTime(c.database.GetName(), c.user.Name, time.Since(start)) }() select { case w := <-c.workerPool: diff --git a/lib/gat/pool/transaction/worker.go b/lib/gat/pool/transaction/worker.go index a441568be21e4f2d79c29897090160cd6cf7ac17..25176cd0aa0fd6445c928a14647a388a42c3333e 100644 --- a/lib/gat/pool/transaction/worker.go +++ b/lib/gat/pool/transaction/worker.go @@ -13,6 +13,7 @@ import ( "gfx.cafe/gfx/pggat/lib/gat/pool/transaction/shard" "gfx.cafe/gfx/pggat/lib/gat/protocol" "gfx.cafe/gfx/pggat/lib/gat/protocol/pg_error" + "gfx.cafe/gfx/pggat/lib/metrics" ) // a single use worker with an embedded connection database. @@ -110,6 +111,11 @@ func (w *worker) HandleDescribe(ctx context.Context, c gat.Client, d *protocol.D defer done() } + start := time.Now() + defer func() { + metrics.RecordTransactionTime(w.w.GetDatabase().GetName(), w.w.user.Name, time.Since(start)) + }() + errch := make(chan error) go func() { defer close(errch) @@ -136,6 +142,11 @@ func (w *worker) HandleExecute(ctx context.Context, c gat.Client, e *protocol.Ex defer done() } + start := time.Now() + defer func() { + metrics.RecordTransactionTime(w.w.GetDatabase().GetName(), w.w.user.Name, time.Since(start)) + }() + errch := make(chan error) go func() { defer close(errch) @@ -162,6 +173,11 @@ func (w *worker) HandleFunction(ctx context.Context, c gat.Client, fn *protocol. defer done() } + start := time.Now() + defer func() { + metrics.RecordQueryTime(w.w.GetDatabase().GetName(), w.w.user.Name, time.Since(start)) + }() + errch := make(chan error) go func() { defer close(errch) @@ -190,8 +206,7 @@ func (w *worker) HandleSimpleQuery(ctx context.Context, c gat.Client, query stri start := time.Now() defer func() { - // w.w.user.Name - w.w.database.GetStats().AddQueryTime(time.Now().Sub(start).Microseconds()) + metrics.RecordQueryTime(w.w.GetDatabase().GetName(), w.w.user.Name, time.Since(start)) }() errch := make(chan error) @@ -223,8 +238,7 @@ func (w *worker) HandleTransaction(ctx context.Context, c gat.Client, query stri start := time.Now() defer func() { - w.w.database.GetStats().AddXactTime(time.Now().Sub(start).Microseconds()) - // metrics.PoolMetrics(w.., w.w.user.Name) + metrics.RecordTransactionTime(w.w.GetDatabase().GetName(), w.w.user.Name, time.Since(start)) }() errch := make(chan error) diff --git a/lib/metrics/pool.go b/lib/metrics/pool.go index 908b196089dc7fb92f6a98b1cf75593acb8fa00c..b1775d3f7d30d6964016db63cba9cc02afd366f6 100644 --- a/lib/metrics/pool.go +++ b/lib/metrics/pool.go @@ -66,7 +66,7 @@ func newPoolMetrics(db string, user string) poolMetrics { }, }, []string{}), SentBytes: promauto.NewCounterVec(prometheus.CounterOpts{ - Name: "pggat_received_bytes_total", + Name: "pggat_sent_bytes_total", Help: "total number of bytes received", ConstLabels: prometheus.Labels{ "db": db, @@ -91,5 +91,21 @@ func RecordQueryTime(db string, user string, dur time.Duration) { return } p := PoolMetrics(db, user) - p.QueryLatency.WithLabelValues().Observe(float64(dur.Microseconds())) + p.QueryLatency.WithLabelValues().Observe(float64(dur.Nanoseconds())) +} + +func RecordTransactionTime(db string, user string, dur time.Duration) { + if !On() { + return + } + p := PoolMetrics(db, user) + p.TxLatency.WithLabelValues().Observe(float64(dur.Nanoseconds())) +} + +func RecordWaitTime(db string, user string, dur time.Duration) { + if !On() { + return + } + p := PoolMetrics(db, user) + p.WaitLatency.WithLabelValues().Observe(float64(dur.Nanoseconds())) } diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 3b8a6e2373b75b90dcf01ce1c2a911ed667e0939..b4b23dd15c2b9a0fc8af1f9a940613ad2be370b6 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -24,3 +24,4 @@ services: PSQL_PRI_DB_HOST: db ports: - 6432:6432 + - 6060:6060