Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom labels #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor/
.test_coverage.txt
.test_coverage.txt

.idea/
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/slok/go-http-metrics

go 1.17

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/emicklei/go-restful/v3 v3.7.2
Expand Down Expand Up @@ -90,5 +92,3 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

go 1.17
127 changes: 0 additions & 127 deletions go.sum

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions internal/mocks/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ Package mocks will have all the mocks of the library.
*/
package mocks // import "github.com/slok/go-http-metrics/internal/mocks"

//go:generate mockery -output ./metrics -outpkg metrics -dir ../../metrics -name Recorder
//go:generate mockery -output ./middleware -outpkg middleware -dir ../../middleware -name Reporter
//go:generate mockery --output ./metrics --outpkg metrics --dir ../../metrics --name Recorder
//go:generate mockery --output ./middleware --outpkg middleware --dir ../../middleware --name Reporter
//go:generate mockery --output ./middleware --outpkg middleware --dir ../../middleware --name CustomLabelReporter
2 changes: 1 addition & 1 deletion internal/mocks/metrics/Recorder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions internal/mocks/middleware/CustomLabelReporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/mocks/middleware/Reporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type HTTPReqProperties struct {
Method string
// Code is the response of the request.
Code string

// CustomLabels hold values of the custom labels, if any.
CustomLabels []string
}

// HTTPProperties are the metric properties for the global server metrics.
Expand All @@ -24,6 +27,9 @@ type HTTPProperties struct {
Service string
// ID is the id of the request handler.
ID string

// CustomLabels hold values of the custom labels, if any.
CustomLabels []string
}

// Recorder knows how to record and measure the metrics. This
Expand Down
39 changes: 33 additions & 6 deletions metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Config struct {
MethodLabel string
// ServiceLabel is the name that will be set to the service label, by default is `service`.
ServiceLabel string

// CustomLabels hold names of the custom labels, if any.
CustomLabels []string
}

func (c *Config) defaults() {
Expand Down Expand Up @@ -73,29 +76,47 @@ type recorder struct {
func NewRecorder(cfg Config) metrics.Recorder {
cfg.defaults()

perReqLabels := append(
[]string{
cfg.ServiceLabel,
cfg.HandlerIDLabel,
cfg.MethodLabel,
cfg.StatusCodeLabel,
},
cfg.CustomLabels...,
)

serviceLabels := append(
[]string{
cfg.ServiceLabel,
cfg.HandlerIDLabel,
},
cfg.CustomLabels...,
)

r := &recorder{
httpRequestDurHistogram: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.Prefix,
Subsystem: "http",
Name: "request_duration_seconds",
Help: "The latency of the HTTP requests.",
Buckets: cfg.DurationBuckets,
}, []string{cfg.ServiceLabel, cfg.HandlerIDLabel, cfg.MethodLabel, cfg.StatusCodeLabel}),
}, perReqLabels),

httpResponseSizeHistogram: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.Prefix,
Subsystem: "http",
Name: "response_size_bytes",
Help: "The size of the HTTP responses.",
Buckets: cfg.SizeBuckets,
}, []string{cfg.ServiceLabel, cfg.HandlerIDLabel, cfg.MethodLabel, cfg.StatusCodeLabel}),
}, perReqLabels),

httpRequestsInflight: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: cfg.Prefix,
Subsystem: "http",
Name: "requests_inflight",
Help: "The number of inflight requests being handled at the same time.",
}, []string{cfg.ServiceLabel, cfg.HandlerIDLabel}),
}, serviceLabels),
}

cfg.Registry.MustRegister(
Expand All @@ -108,13 +129,19 @@ func NewRecorder(cfg Config) metrics.Recorder {
}

func (r recorder) ObserveHTTPRequestDuration(_ context.Context, p metrics.HTTPReqProperties, duration time.Duration) {
r.httpRequestDurHistogram.WithLabelValues(p.Service, p.ID, p.Method, p.Code).Observe(duration.Seconds())
lvs := []string{p.Service, p.ID, p.Method, p.Code}
lvs = append(lvs, p.CustomLabels...)
r.httpRequestDurHistogram.WithLabelValues(lvs...).Observe(duration.Seconds())
}

func (r recorder) ObserveHTTPResponseSize(_ context.Context, p metrics.HTTPReqProperties, sizeBytes int64) {
r.httpResponseSizeHistogram.WithLabelValues(p.Service, p.ID, p.Method, p.Code).Observe(float64(sizeBytes))
lvs := []string{p.Service, p.ID, p.Method, p.Code}
lvs = append(lvs, p.CustomLabels...)
r.httpResponseSizeHistogram.WithLabelValues(lvs...).Observe(float64(sizeBytes))
}

func (r recorder) AddInflightRequests(_ context.Context, p metrics.HTTPProperties, quantity int) {
r.httpRequestsInflight.WithLabelValues(p.Service, p.ID).Add(float64(quantity))
lvs := []string{p.Service, p.ID}
lvs = append(lvs, p.CustomLabels...)
r.httpRequestsInflight.WithLabelValues(lvs...).Add(float64(quantity))
}
36 changes: 31 additions & 5 deletions metrics/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestPrometheusRecorder(t *testing.T) {
},
},
{
name: "Using a custom labels in the configuration should measure with those labels.",
name: "Using a custom label names in the configuration should measure with those labels.",
config: libprometheus.Config{
HandlerIDLabel: "route_id",
StatusCodeLabel: "status_code",
Expand All @@ -186,12 +186,38 @@ func TestPrometheusRecorder(t *testing.T) {
`http_request_duration_seconds_count{http_method="GET",http_service="svc1",route_id="test1",status_code="200"} 2`,
},
},
{
name: "Using a custom labels in the configuration should measure with those labels.",
config: libprometheus.Config{
DurationBuckets: []float64{1, 10},
CustomLabels: []string{"user_id"},
},
recordMetrics: func(r metrics.Recorder) {
r.ObserveHTTPRequestDuration(context.TODO(), metrics.HTTPReqProperties{
Service: "svc1",
ID: "test1",
Method: http.MethodGet,
Code: "200",
CustomLabels: []string{"userVIP"},
}, 6*time.Second)
r.AddInflightRequests(context.TODO(), metrics.HTTPProperties{
Service: "svc1",
ID: "test1",
CustomLabels: []string{"userVIP"},
}, 1)
},
expMetrics: []string{
`http_request_duration_seconds_bucket{code="200",handler="test1",method="GET",service="svc1",user_id="userVIP",le="1"} 0`,
`http_request_duration_seconds_bucket{code="200",handler="test1",method="GET",service="svc1",user_id="userVIP",le="10"} 1`,
`http_request_duration_seconds_bucket{code="200",handler="test1",method="GET",service="svc1",user_id="userVIP",le="+Inf"} 1`,
`http_request_duration_seconds_count{code="200",handler="test1",method="GET",service="svc1",user_id="userVIP"} 1`,
`http_requests_inflight{handler="test1",service="svc1",user_id="userVIP"} 1`,
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)

reg := prometheus.NewRegistry()
test.config.Registry = reg
mrecorder := libprometheus.NewRecorder(test.config)
Expand All @@ -205,10 +231,10 @@ func TestPrometheusRecorder(t *testing.T) {
resp := rec.Result()

// Check all metrics are present.
if assert.Equal(http.StatusOK, resp.StatusCode) {
if assert.Equal(t, http.StatusOK, resp.StatusCode) {
body, _ := ioutil.ReadAll(resp.Body)
for _, expMetric := range test.expMetrics {
assert.Contains(string(body), expMetric, "metric not present on the result")
assert.Contains(t, string(body), expMetric, "metric not present on the result")
}
}
})
Expand Down
Loading