From fdabba1a28d30bd26d28798bcf8ade5e1c10c45a Mon Sep 17 00:00:00 2001 From: louis Date: Wed, 15 Apr 2020 13:57:58 +0200 Subject: [PATCH] add basic test, add metrics section in readme --- README.md | 20 +++++++++++++++++++ main_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 main_test.go diff --git a/README.md b/README.md index d69157c..688402d 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,26 @@ $GOPATH/bin/prometheus-jitsi-meet-exporter docker run -p 9888:9888 systemli/prometheus-jitsi-meet-exporter:latest -videobridge-url http://jitsi:8888/stats ``` +## Metrics + +``` +# HELP jitsi_total_participants Participants counter +# TYPE jitsi_total_participants counter +jitsi_total_participants 18 +# HELP jitsi_total_conferences_created Number of conferences created +# TYPE jitsi_total_conferences_created counter +jitsi_total_conferences_created 14 +# HELP jitsi_largest_conference Participants in the largest conference +# TYPE jitsi_largest_conference gauge +jitsi_largest_conference 3 +# HELP jitsi_conferences Current number of active conferences +# TYPE jitsi_conferences gauge +jitsi_conferences 2 +# HELP jitsi_participants Current number of active participants +# TYPE jitsi_participants gauge +jitsi_participants 4 +``` + ## License GPLv3 diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..fb8bb4d --- /dev/null +++ b/main_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "log" + "net/http" + "net/http/httptest" + "testing" +) + +func TestGetMetrics(t *testing.T) { + req, err := http.NewRequest("GET", "/metrics", nil) + if err != nil { + t.Fatal(err) + } + + go func() { + http.HandleFunc("/stats", getJitsiVideobridgeStats) + if err := http.ListenAndServe(":8888", nil); err != nil { + log.Fatal(err) + } + }() + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(serveMetrics) + handler.ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) + } + + var expected = `# HELP jitsi_total_participants Participants counter +# TYPE jitsi_total_participants counter +jitsi_total_participants 18 +# HELP jitsi_total_conferences_created Number of conferences created +# TYPE jitsi_total_conferences_created counter +jitsi_total_conferences_created 14 +# HELP jitsi_largest_conference Participants in the largest conference +# TYPE jitsi_largest_conference gauge +jitsi_largest_conference 3 +# HELP jitsi_conferences Current number of active conferences +# TYPE jitsi_conferences gauge +jitsi_conferences 2 +# HELP jitsi_participants Current number of active participants +# TYPE jitsi_participants gauge +jitsi_participants 4 +` + + if rr.Body.String() != expected { + t.Error("Response does not match the expected string") + } +} + +func getJitsiVideobridgeStats(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte(`{"largest_conference":3,"total_sip_call_failures":0,"total_participants":18,"conference_sizes":[0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"bridge_selector":{"total_least_loaded_in_region":0,"total_split_due_to_load":0,"total_not_loaded_in_region_in_conference":0,"total_least_loaded_in_region_in_conference":0,"total_not_loaded_in_region":0,"total_split_due_to_region":0,"bridge_count":1,"operational_bridge_count":1,"total_least_loaded_in_conference":0,"total_least_loaded":3},"total_conferences_created":14,"total_recording_failures":0,"conferences":2,"total_live_streaming_failures":0,"participants":4}`)) +}