59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package metrics
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestMetricsHandlerExposesText(t *testing.T) {
|
|
t.Parallel()
|
|
m := New()
|
|
m.Connections.WithLabelValues("ws").Set(3)
|
|
m.Connections.WithLabelValues("tcp").Set(2)
|
|
m.EndpointsTotal.Set(10)
|
|
m.DeliveriesPending.Set(4)
|
|
m.MessagesScheduled.Set(1)
|
|
m.DispatchToPushSeconds.Observe(0.05)
|
|
m.AckSeconds.Observe(0.02)
|
|
m.WriteQueueLength.Set(7)
|
|
m.WriteCommitSeconds.Observe(0.001)
|
|
m.PasswordHashQueue.Set(2)
|
|
m.ErrorsTotal.WithLabelValues("busy").Inc()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
rec := httptest.NewRecorder()
|
|
m.Handler().ServeHTTP(rec, req)
|
|
resp := rec.Result()
|
|
defer func() { _ = resp.Body.Close() }()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status=%d", resp.StatusCode)
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(body)
|
|
for _, want := range []string{
|
|
"nixmsg_connections",
|
|
`transport="ws"`,
|
|
`transport="tcp"`,
|
|
"nixmsg_endpoints",
|
|
"nixmsg_deliveries_pending",
|
|
"nixmsg_messages_scheduled",
|
|
"nixmsg_dispatch_to_push_duration_seconds",
|
|
"nixmsg_ack_duration_seconds",
|
|
"nixmsg_write_queue_length",
|
|
"nixmsg_write_batch_commit_duration_seconds",
|
|
"nixmsg_password_hash_queue_length",
|
|
"nixmsg_errors_total",
|
|
`code="busy"`,
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("metrics text missing %q\n%s", want, text)
|
|
}
|
|
}
|
|
}
|