Skip to content

Commit

Permalink
rename metric to eventing_epp_requests_duration_seconds and adjust bu…
Browse files Browse the repository at this point in the history
…ckets (#18301)

* rename metric to eventing_epp_requests_duration_milliseconds and adjust buckets

* fix lint

* fix buckets and test

* fix lint job

* bump images
  • Loading branch information
k15r authored Oct 18, 2023
1 parent 6c4c008 commit 6a3c6f7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/env"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/metrics"

"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/legacy/api"

Expand All @@ -24,7 +25,6 @@ import (
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/cloudevents/eventtype"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/handler/health"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/legacy"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/metrics"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/options"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/receiver"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/sender"
Expand Down
11 changes: 6 additions & 5 deletions pkg/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
backendLatencyHelp = "The duration of sending events to the messaging server in milliseconds"

// durationKey name of the duration metric.
durationKey = "eventing_epp_requests_duration_milliseconds"
durationKey = "eventing_epp_requests_duration_seconds"
// durationHelp help text for the duration metric.
durationHelp = "The duration of processing an incoming request (includes sending to the backend)"

Expand Down Expand Up @@ -95,12 +95,13 @@ func NewCollector(latency histogram.BucketsProvider) *Collector {
[]string{eventTypeLabel, eventSourceLabel, responseCodeLabel},
),

//nolint:promlinter // we follow the same pattern as istio. so a millisecond unit if fine here
duration: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: durationKey,
Help: durationHelp,
Buckets: latency.Buckets(),
Name: durationKey,
Help: durationHelp,
Buckets: []float64{0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.050, 0.075,
0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9,
1, 1.5, 2, 3, 5},
},
[]string{responseCodeLabel, methodLabel, pathLabel},
),
Expand Down
78 changes: 78 additions & 0 deletions pkg/metrics/collector_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package metrics

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"

"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/metrics/histogram/mocks"
"github.com/kyma-project/kyma/components/event-publisher-proxy/pkg/metrics/latency"
)

func TestNewCollector(t *testing.T) {
Expand All @@ -24,3 +31,74 @@ func TestNewCollector(t *testing.T) {
assert.NotNil(t, collector.eventType.MetricVec)
latency.AssertExpectations(t)
}

func TestCollector_MetricsMiddleware(t *testing.T) {
router := mux.NewRouter()
c := NewCollector(latency.BucketsProvider{})
router.Use(c.MetricsMiddleware())
router.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
time.Sleep(6 * time.Millisecond)
writer.WriteHeader(http.StatusOK)
})
srv := httptest.NewServer(router)
defer srv.Close()
http.Get(srv.URL + "/test") //nolint: errcheck // this call never fails as it is a testserver
//nolint: lll // prometheus tef follows
tef := `
# HELP eventing_epp_requests_duration_seconds The duration of processing an incoming request (includes sending to the backend)
# TYPE eventing_epp_requests_duration_seconds histogram
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.001"} 0
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.002"} 0
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.004"} 0
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.008"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.016"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.032"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.05"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.075"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.1"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.15"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.2"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.25"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.3"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.35"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.4"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.45"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.5"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.6"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.7"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.8"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="0.9"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="1"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="1.5"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="2"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="3"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="5"} 1
eventing_epp_requests_duration_seconds_bucket{code="200",method="get",path="/test",le="+Inf"} 1
eventing_epp_requests_duration_seconds_sum{code="200",method="get",path="/test"} 0.006837792
eventing_epp_requests_duration_seconds_count{code="200",method="get",path="/test"} 1
# HELP eventing_epp_requests_total The total number of requests
# TYPE eventing_epp_requests_total counter
eventing_epp_requests_total{code="200",method="get",path="/test"} 1
`
if err := ignoreErr(testutil.CollectAndCompare(c, strings.NewReader(tef)),
"eventing_epp_requests_duration_seconds_sum"); err != nil {
t.Fatalf("%v", err)
}
}

// Hack to filter out validation of the sum calculated by the metric.
func ignoreErr(err error, metric string) error {
for _, line := range strings.Split(err.Error(), "\n") {
if line == "--- metric output does not match expectation; want" || line == "+++ got:" {
continue
}
if strings.HasPrefix(strings.TrimSpace(line), "+") ||
strings.HasPrefix(strings.TrimSpace(line), "-") {
if !(strings.HasPrefix(strings.TrimSpace(line), "+"+metric) ||
strings.HasPrefix(strings.TrimSpace(line), "-"+metric)) {
return err
}
}
}
return nil
}

0 comments on commit 6a3c6f7

Please sign in to comment.