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
12 changes: 8 additions & 4 deletions pkg/metrics/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -26,15 +27,18 @@ func (rt *InstrumentedRoundTripper) RoundTrip(request *http.Request) (*http.Resp
response, err := rt.base.RoundTrip(request)
duration := time.Since(startTime)

LoadBalancerResponseTimeHistogram.
HTTPRequestDurationHistogram.
With(prometheus.Labels{operationLabel: operation}).
Observe(float64(duration.Seconds()))
LoadBalancerRequestCount.
HTTPRequestCount.
With(prometheus.Labels{operationLabel: operation}).
Inc()

if response != nil && response.StatusCode >= http.StatusInternalServerError {
LoadBalancerErrorCount.Inc()
if response != nil && response.StatusCode >= 400 {
HTTPErrorCount.With(prometheus.Labels{
"method": request.Method,
"code": strconv.Itoa(response.StatusCode),
}).Inc()
}

return response, err
Expand Down
114 changes: 114 additions & 0 deletions pkg/metrics/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package metrics

import (
"net/http"
"net/http/httptest"
"net/url"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
dto "github.com/prometheus/client_model/go"
)

var _ = Describe("Metrics", func() {
Expand All @@ -22,4 +26,114 @@ var _ = Describe("Metrics", func() {
Entry("get load-balancers", "GET", "/v2/projects/6-a-4-8-c/regions/eu01/load-balancers", "get_load-balancers"),
Entry("get load-balancers instance", "GET", "/v2/projects/6-a-4-8-c/regions/eu01/load-balancers/id", "get_load-balancers_instance"),
)

Describe("InstrumentedRoundTripper", func() {
It("increments HTTPRequestCount for responses", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

labels := prometheus.Labels{
operationLabel: "get_request-count-test",
}
before := testutil.ToFloat64(HTTPRequestCount.With(labels))

response, err := NewInstrumentedHTTPClient().Get(server.URL + "/request-count-test")
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()

after := testutil.ToFloat64(HTTPRequestCount.With(labels))
Expect(after - before).To(Equal(float64(1)))
})

It("records HTTPRequestDurationHistogram observations for responses", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

labels := prometheus.Labels{
operationLabel: "get_request-duration-test",
}
before := histogramSampleCount(HTTPRequestDurationHistogram.With(labels))

response, err := NewInstrumentedHTTPClient().Get(server.URL + "/request-duration-test")
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()

after := histogramSampleCount(HTTPRequestDurationHistogram.With(labels))
Expect(after - before).To(Equal(uint64(1)))
})

It("increments HTTPErrorCount for 400 responses", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
defer server.Close()

labels := prometheus.Labels{
"method": http.MethodGet,
"code": "400",
}
before := testutil.ToFloat64(HTTPErrorCount.With(labels))

response, err := NewInstrumentedHTTPClient().Get(server.URL)
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()

after := testutil.ToFloat64(HTTPErrorCount.With(labels))
Expect(after - before).To(Equal(float64(1)))
})

It("increments HTTPErrorCount for 500 responses", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()

labels := prometheus.Labels{
"method": http.MethodPost,
"code": "500",
}
before := testutil.ToFloat64(HTTPErrorCount.With(labels))

response, err := NewInstrumentedHTTPClient().Post(server.URL, "application/json", nil)
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()

after := testutil.ToFloat64(HTTPErrorCount.With(labels))
Expect(after - before).To(Equal(float64(1)))
})

It("does not increment HTTPErrorCount for successful responses", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

labels := prometheus.Labels{
"method": http.MethodGet,
"code": "200",
}
before := testutil.ToFloat64(HTTPErrorCount.With(labels))

response, err := NewInstrumentedHTTPClient().Get(server.URL)
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()

after := testutil.ToFloat64(HTTPErrorCount.With(labels))
Expect(after - before).To(Equal(float64(0)))
})
})
})

func histogramSampleCount(observer prometheus.Observer) uint64 {
metric, ok := observer.(prometheus.Metric)
Expect(ok).To(BeTrue())

dtoMetric := &dto.Metric{}
Expect(metric.Write(dtoMetric)).To(Succeed())

return dtoMetric.GetHistogram().GetSampleCount()
}
36 changes: 16 additions & 20 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ import (

const (
cloudProviderMetricPrefix = "cloud_provider_stackit"
loadBalancerSubSystem = "lb"
operationLabel = "op"
)

var (
LoadBalancerRequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
HTTPRequestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: cloudProviderMetricPrefix,
Subsystem: loadBalancerSubSystem,
Name: "requests_total",
Help: "the number of requests to the load balancer API",
Name: "http_requests_total",
Help: "The number of requests to external APIs",
ConstLabels: nil,
}, []string{operationLabel})

LoadBalancerErrorCount = prometheus.NewCounter(prometheus.CounterOpts{
HTTPErrorCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: cloudProviderMetricPrefix,
Subsystem: loadBalancerSubSystem,
Name: "errors_total",
Help: "the number of server errors reported when calling the load balancer API",
Name: "http_errors_total",
Help: "Number of HTTP errors returned by external APIs",
ConstLabels: nil,
})
}, []string{"method", "code"})

LoadBalancerResponseTimeHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
HTTPRequestDurationHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cloudProviderMetricPrefix,
Subsystem: loadBalancerSubSystem,
Name: "request_duration_seconds",
Help: "the response times of the load balancer API",
Name: "http_request_duration_seconds",
Help: "The response times of external API requests",
ConstLabels: nil,
Buckets: nil,
}, []string{operationLabel})
Expand All @@ -55,13 +51,13 @@ func (e *Exporter) Collect(metrics chan<- prometheus.Metric) {
}

func (e *Exporter) describeCloudProvider(descs chan<- *prometheus.Desc) {
LoadBalancerRequestCount.Describe(descs)
LoadBalancerErrorCount.Describe(descs)
LoadBalancerResponseTimeHistogram.Describe(descs)
HTTPRequestCount.Describe(descs)
HTTPErrorCount.Describe(descs)
HTTPRequestDurationHistogram.Describe(descs)
}

func (e *Exporter) collectCloudProvider(metrics chan<- prometheus.Metric) {
LoadBalancerRequestCount.Collect(metrics)
LoadBalancerErrorCount.Collect(metrics)
LoadBalancerResponseTimeHistogram.Collect(metrics)
HTTPRequestCount.Collect(metrics)
HTTPErrorCount.Collect(metrics)
HTTPRequestDurationHistogram.Collect(metrics)
}