Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (pm *Metrics) Observe(res *vegeta.Result) {
pm.requestBytesOutCounter.WithLabelValues(res.Method, res.URL, code).Add(float64(res.BytesOut))
pm.requestLatencyHistogram.WithLabelValues(res.Method, res.URL, code).Observe(res.Latency.Seconds())
if res.Error != "" {
pm.requestFailCounter.WithLabelValues(res.Method, res.URL, code, res.Error)
pm.requestFailCounter.WithLabelValues(res.Method, res.URL, code, res.Error).Inc()
}
}

Expand Down
41 changes: 41 additions & 0 deletions lib/prom/prom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/prometheus/model/textparse"
vegeta "github.com/tsenart/vegeta/v12/lib"
)
Expand Down Expand Up @@ -88,3 +89,43 @@ func TestMetrics_Observe(t *testing.T) {
t.Errorf("missing metrics: %v", want)
}
}

func TestMetrics_Observe_FailCounterIncremented(t *testing.T) {
pm := NewMetrics()
reg := prometheus.NewRegistry()

if err := pm.Register(reg); err != nil {
t.Fatal("error registering metrics", err)
}

pm.Observe(&vegeta.Result{
URL: "http://example.com/fail",
Method: "GET",
Code: 500,
Error: "Internal Server Error",
Latency: 100 * time.Millisecond,
})
pm.Observe(&vegeta.Result{
URL: "http://example.com/fail",
Method: "GET",
Code: 500,
Error: "Internal Server Error",
Latency: 200 * time.Millisecond,
})
pm.Observe(&vegeta.Result{
URL: "http://example.com/ok",
Method: "GET",
Code: 200,
Latency: 50 * time.Millisecond,
})

var m dto.Metric
failCounter := pm.requestFailCounter.WithLabelValues("GET", "http://example.com/fail", "500", "Internal Server Error")
if err := failCounter.Write(&m); err != nil {
t.Fatalf("error writing metric: %v", err)
}

if got, want := m.GetCounter().GetValue(), float64(2); got != want {
t.Errorf("request_fail_count: got %v, want %v", got, want)
}
}