Skip to content

Commit

Permalink
add buffer count metric
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoshenka committed Mar 23, 2020
1 parent fff54b3 commit 599e22c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/lbryio/lbrytv/app/publish"
"github.com/lbryio/lbrytv/app/users"
"github.com/lbryio/lbrytv/internal/status"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// InstallRoutes sets up global API handlers
Expand All @@ -31,6 +31,7 @@ func InstallRoutes(proxyService *proxy.ProxyService, r *mux.Router) {
internalRouter.Handle("/metrics", promhttp.Handler())
internalRouter.HandleFunc("/status", status.GetStatus)
internalRouter.HandleFunc("/whoami", status.WhoAMI)
internalRouter.HandleFunc("/ui_metric", status.TrackUIMetric)

player.InstallRoutes(r)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/rubenv/sql-migrate v0.0.0-20190618074426-f4d34eae5a5c
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.0
github.com/spf13/cobra v0.0.5
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
8 changes: 8 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
nsIAPI = "iapi"
nsProxy = "proxy"
nsLbrynet = "lbrynet"
nsUI = "ui"

LabelSource = "source"
LabelInstance = "instance"
Expand Down Expand Up @@ -120,4 +121,11 @@ var (
Name: "count",
Help: "Number of wallets currently loaded",
}, []string{LabelSource})

UIBufferCount = promauto.NewCounter(prometheus.CounterOpts{
Namespace: nsUI,
Subsystem: "buffer",
Name: "count",
Help: "Video buffer events",
})
)
23 changes: 22 additions & 1 deletion internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/lbryio/lbrytv/app/router"
"github.com/lbryio/lbrytv/internal/metrics"
"github.com/lbryio/lbrytv/internal/monitor"

ljsonrpc "github.com/lbryio/lbry.go/v2/extras/jsonrpc"
Expand Down Expand Up @@ -63,7 +64,7 @@ func GetStatus(w http.ResponseWriter, req *http.Request) {
sdks := router.GetSDKServerList()
for _, s := range sdks {
c := ljsonrpc.NewClient(s.Address)
c.SetRPCTimeout(5*time.Second)
c.SetRPCTimeout(5 * time.Second)
status, err := c.Status()
srv := ServerItem{Address: s.Address, Status: StatusOK}
if err != nil {
Expand Down Expand Up @@ -118,3 +119,23 @@ func WhoAMI(w http.ResponseWriter, req *http.Request) {
respByte, _ := json.MarshalIndent(&details, "", " ")
w.Write(respByte)
}

func TrackUIMetric(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json; charset=utf-8")
resp := make(map[string]string)

metricName := req.FormValue("name")
resp["name"] = metricName

switch metricName {
case "buffer":
metrics.UIBufferCount.Inc()
resp["status"] = "ok"
default:
w.WriteHeader(http.StatusBadRequest)
resp["error"] = "Unknown metric"
}

respByte, _ := json.MarshalIndent(&resp, "", " ")
w.Write(respByte)
}

0 comments on commit 599e22c

Please sign in to comment.