Prometheus - track wallet requests

This commit is contained in:
Daniel Krol 2022-07-22 19:49:30 -04:00
parent 3ff36f169c
commit 41b14dad44
3 changed files with 32 additions and 0 deletions

19
metrics/metrics.go Normal file
View file

@ -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)
}

View file

@ -8,6 +8,8 @@ import (
"net/http" "net/http"
"net/mail" "net/mail"
"github.com/prometheus/client_golang/prometheus/promhttp"
"lbryio/lbry-id/auth" "lbryio/lbry-id/auth"
"lbryio/lbry-id/store" "lbryio/lbry-id/store"
) )
@ -17,6 +19,8 @@ import (
const ApiVersion = "3" const ApiVersion = "3"
const PathPrefix = "/api/" + ApiVersion const PathPrefix = "/api/" + ApiVersion
const PathPrometheus = "/metrics"
const PathAuthToken = PathPrefix + "/auth/full" const PathAuthToken = PathPrefix + "/auth/full"
const PathRegister = PathPrefix + "/signup" const PathRegister = PathPrefix + "/signup"
const PathPassword = PathPrefix + "/password" const PathPassword = PathPrefix + "/password"
@ -208,6 +212,8 @@ func (s *Server) Serve() {
http.HandleFunc(PathUnknownEndpoint, s.unknownEndpoint) http.HandleFunc(PathUnknownEndpoint, s.unknownEndpoint)
http.HandleFunc(PathWrongApiVersion, s.wrongApiVersion) http.HandleFunc(PathWrongApiVersion, s.wrongApiVersion)
http.Handle(PathPrometheus, promhttp.Handler())
fmt.Println("Serving at localhost:8090") fmt.Println("Serving at localhost:8090")
http.ListenAndServe("localhost:8090", nil) http.ListenAndServe("localhost:8090", nil)
} }

View file

@ -5,7 +5,10 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus"
"lbryio/lbry-id/auth" "lbryio/lbry-id/auth"
"lbryio/lbry-id/metrics"
"lbryio/lbry-id/store" "lbryio/lbry-id/store"
"lbryio/lbry-id/wallet" "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) { func (s *Server) getWallet(w http.ResponseWriter, req *http.Request) {
metrics.RequestsCount.With(prometheus.Labels{"method": "GET wallet"}).Inc()
if !getGetData(w, req) { if !getGetData(w, req) {
return return
} }
@ -118,6 +123,8 @@ func (s *Server) getWallet(w http.ResponseWriter, req *http.Request) {
// current wallet's sequence // current wallet's sequence
// 500: Update unsuccessful for unanticipated reasons // 500: Update unsuccessful for unanticipated reasons
func (s *Server) postWallet(w http.ResponseWriter, req *http.Request) { func (s *Server) postWallet(w http.ResponseWriter, req *http.Request) {
metrics.RequestsCount.With(prometheus.Labels{"method": "POST wallet"}).Inc()
var walletRequest WalletRequest var walletRequest WalletRequest
if !getPostData(w, req, &walletRequest) { if !getPostData(w, req, &walletRequest) {
return return