From 41b14dad4452a6beecae549e613abf21cb870fe7 Mon Sep 17 00:00:00 2001
From: Daniel Krol <orblivion@gmail.com>
Date: Fri, 22 Jul 2022 19:49:30 -0400
Subject: [PATCH] Prometheus - track wallet requests

---
 metrics/metrics.go | 19 +++++++++++++++++++
 server/server.go   |  6 ++++++
 server/wallet.go   |  7 +++++++
 3 files changed, 32 insertions(+)
 create mode 100644 metrics/metrics.go

diff --git a/metrics/metrics.go b/metrics/metrics.go
new file mode 100644
index 0000000..9cd02d9
--- /dev/null
+++ b/metrics/metrics.go
@@ -0,0 +1,19 @@
+package metrics
+
+import (
+	"github.com/prometheus/client_golang/prometheus"
+)
+
+var (
+	RequestsCount = prometheus.NewCounterVec(
+		prometheus.CounterOpts{
+			Name: "requests_count",
+			Help: "Total number of requests to various endpoints",
+		},
+		[]string{"method"},
+	)
+)
+
+func init() {
+	prometheus.MustRegister(RequestsCount)
+}
diff --git a/server/server.go b/server/server.go
index 9596afe..2f46b27 100644
--- a/server/server.go
+++ b/server/server.go
@@ -8,6 +8,8 @@ import (
 	"net/http"
 	"net/mail"
 
+	"github.com/prometheus/client_golang/prometheus/promhttp"
+
 	"lbryio/lbry-id/auth"
 	"lbryio/lbry-id/store"
 )
@@ -17,6 +19,8 @@ import (
 const ApiVersion = "3"
 const PathPrefix = "/api/" + ApiVersion
 
+const PathPrometheus = "/metrics"
+
 const PathAuthToken = PathPrefix + "/auth/full"
 const PathRegister = PathPrefix + "/signup"
 const PathPassword = PathPrefix + "/password"
@@ -208,6 +212,8 @@ func (s *Server) Serve() {
 	http.HandleFunc(PathUnknownEndpoint, s.unknownEndpoint)
 	http.HandleFunc(PathWrongApiVersion, s.wrongApiVersion)
 
+	http.Handle(PathPrometheus, promhttp.Handler())
+
 	fmt.Println("Serving at localhost:8090")
 	http.ListenAndServe("localhost:8090", nil)
 }
diff --git a/server/wallet.go b/server/wallet.go
index da4ba97..3721f6c 100644
--- a/server/wallet.go
+++ b/server/wallet.go
@@ -5,7 +5,10 @@ import (
 	"fmt"
 	"net/http"
 
+	"github.com/prometheus/client_golang/prometheus"
+
 	"lbryio/lbry-id/auth"
+	"lbryio/lbry-id/metrics"
 	"lbryio/lbry-id/store"
 	"lbryio/lbry-id/wallet"
 )
@@ -66,6 +69,8 @@ func getWalletParams(req *http.Request) (token auth.TokenString, err error) {
 }
 
 func (s *Server) getWallet(w http.ResponseWriter, req *http.Request) {
+	metrics.RequestsCount.With(prometheus.Labels{"method": "GET wallet"}).Inc()
+
 	if !getGetData(w, req) {
 		return
 	}
@@ -118,6 +123,8 @@ func (s *Server) getWallet(w http.ResponseWriter, req *http.Request) {
 //     current wallet's sequence
 //   500: Update unsuccessful for unanticipated reasons
 func (s *Server) postWallet(w http.ResponseWriter, req *http.Request) {
+	metrics.RequestsCount.With(prometheus.Labels{"method": "POST wallet"}).Inc()
+
 	var walletRequest WalletRequest
 	if !getPostData(w, req, &walletRequest) {
 		return