From fb7341265449bf212c7700bbf69630b2be5a3af4 Mon Sep 17 00:00:00 2001
From: Trevor Judice <tjudice@gfx.io>
Date: Fri, 30 Sep 2022 17:54:15 -0500
Subject: [PATCH] tx/query error counts

---
 lib/metrics/pool.go | 52 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 6 deletions(-)

diff --git a/lib/metrics/pool.go b/lib/metrics/pool.go
index b1775d3f..997fc6bc 100644
--- a/lib/metrics/pool.go
+++ b/lib/metrics/pool.go
@@ -8,12 +8,14 @@ import (
 )
 
 type poolMetrics struct {
-	name          string
-	TxLatency     *prometheus.HistogramVec
-	QueryLatency  *prometheus.HistogramVec
-	WaitLatency   *prometheus.HistogramVec
-	ReceivedBytes *prometheus.CounterVec
-	SentBytes     *prometheus.CounterVec
+	name             string
+	TxLatency        *prometheus.HistogramVec
+	QueryLatency     *prometheus.HistogramVec
+	TxErrorCounts    *prometheus.CounterVec
+	QueryErrorCounts *prometheus.CounterVec
+	WaitLatency      *prometheus.HistogramVec
+	ReceivedBytes    *prometheus.CounterVec
+	SentBytes        *prometheus.CounterVec
 }
 
 func PoolMetrics(db string, user string) poolMetrics {
@@ -48,6 +50,22 @@ func newPoolMetrics(db string, user string) poolMetrics {
 				"user": user,
 			},
 		}, []string{}),
+		TxErrorCounts: promauto.NewCounterVec(prometheus.CounterOpts{
+			Name: "pggat_db_transaction_error_count_total",
+			Help: "transaction latency",
+			ConstLabels: prometheus.Labels{
+				"db":   db,
+				"user": user,
+			},
+		}, []string{"error"}),
+		QueryErrorCounts: promauto.NewCounterVec(prometheus.CounterOpts{
+			Name: "pggat_db_query_error_count_total",
+			Help: "transaction latency",
+			ConstLabels: prometheus.Labels{
+				"db":   db,
+				"user": user,
+			},
+		}, []string{"error"}),
 		WaitLatency: promauto.NewHistogramVec(prometheus.HistogramOpts{
 			Name:    "pggat_db_wait_latency",
 			Help:    "wait latency",
@@ -109,3 +127,25 @@ func RecordWaitTime(db string, user string, dur time.Duration) {
 	p := PoolMetrics(db, user)
 	p.WaitLatency.WithLabelValues().Observe(float64(dur.Nanoseconds()))
 }
+
+func RecordTransactionError(db string, user string, err error) {
+	if !On() {
+		return
+	}
+	p := PoolMetrics(db, user)
+	if err == nil {
+		return
+	}
+	p.TxErrorCounts.WithLabelValues(err.Error()).Inc()
+}
+
+func RecordQueryError(db string, user string, err error) {
+	if !On() {
+		return
+	}
+	p := PoolMetrics(db, user)
+	if err == nil {
+		return
+	}
+	p.TxErrorCounts.WithLabelValues(err.Error()).Inc()
+}
-- 
GitLab