-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add buffer count metric and time to start
- Loading branch information
Showing
8 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package metrics | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/lbryio/lbrytv/internal/monitor" | ||
|
||
"github.com/spf13/cast" | ||
) | ||
|
||
var Logger = monitor.NewModuleLogger("metrics") | ||
|
||
func TrackUIMetric(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Add("content-type", "application/json; charset=utf-8") | ||
resp := make(map[string]string) | ||
code := http.StatusOK | ||
|
||
metricName := req.FormValue("name") | ||
resp["name"] = metricName | ||
|
||
switch metricName { | ||
case "buffer": | ||
UIBufferCount.Inc() | ||
case "time_to_start": | ||
t := cast.ToFloat64(req.FormValue("value")) | ||
if t == 0 { | ||
Logger.Log().Errorf("Time to start cannot be 0") | ||
code = http.StatusBadRequest | ||
resp["error"] = "Time to start cannot be 0" | ||
} else { | ||
UITimeToStart.Observe(t) | ||
} | ||
default: | ||
Logger.Log().Errorf("invalid UI metric name: %s", metricName) | ||
code = http.StatusBadRequest | ||
resp["error"] = "Invalid metric name" | ||
} | ||
|
||
w.WriteHeader(code) | ||
respByte, _ := json.Marshal(&resp) | ||
w.Write(respByte) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package metrics_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/lbryio/lbrytv/api" | ||
"github.com/lbryio/lbrytv/app/proxy" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBufferEvent(t *testing.T) { | ||
name := "buffer" | ||
rr := testMetricUIEvent(t, http.MethodPost, name, "") | ||
assert.Equal(t, http.StatusOK, rr.Code) | ||
assert.Equal(t, `{"name":"`+name+`"}`, rr.Body.String()) | ||
} | ||
|
||
func TestInvalidEvent(t *testing.T) { | ||
name := "win-the-lottery" | ||
rr := testMetricUIEvent(t, http.MethodPost, name, "") | ||
assert.Equal(t, http.StatusBadRequest, rr.Code) | ||
assert.Equal(t, `{"error":"Invalid metric name","name":"`+name+`"}`, rr.Body.String()) | ||
} | ||
|
||
func TestInvalidMethod(t *testing.T) { | ||
rr := testMetricUIEvent(t, http.MethodGet, "", "") | ||
assert.Equal(t, http.StatusMethodNotAllowed, rr.Code) | ||
} | ||
|
||
func TestTimeToStartEvent(t *testing.T) { | ||
name := "time_to_start" | ||
rr := testMetricUIEvent(t, http.MethodPost, name, "0.3") | ||
assert.Equal(t, http.StatusOK, rr.Code) | ||
assert.Equal(t, `{"name":"`+name+`"}`, rr.Body.String()) | ||
} | ||
|
||
func TestTimeToStartEventNoValue(t *testing.T) { | ||
name := "time_to_start" | ||
rr := testMetricUIEvent(t, http.MethodPost, name, "") | ||
assert.Equal(t, http.StatusBadRequest, rr.Code) | ||
} | ||
|
||
func testMetricUIEvent(t *testing.T, method, name, value string) *httptest.ResponseRecorder { | ||
req, err := http.NewRequest(method, "/api/v1/metric/ui", nil) | ||
require.Nil(t, err) | ||
|
||
q := req.URL.Query() | ||
q.Add("name", name) | ||
if value != "" { | ||
q.Add("value", value) | ||
} | ||
req.URL.RawQuery = q.Encode() | ||
|
||
r := mux.NewRouter() | ||
api.InstallRoutes(proxy.NewService(proxy.Opts{}), r) | ||
rr := httptest.NewRecorder() | ||
r.ServeHTTP(rr, req) | ||
return rr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters