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

Add customized labels based on HTTP header #74

Open
wants to merge 4 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
2 changes: 2 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type HTTPReqProperties struct {
Method string
// Code is the response of the request.
Code string
// CustomizedLabels is the customized header
Labels map[string]string
}

// HTTPProperties are the metric properties for the global server metrics.
Expand Down
33 changes: 29 additions & 4 deletions metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prometheus

import (
"context"
"sort"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -30,6 +31,8 @@ type Config struct {
MethodLabel string
// ServiceLabel is the name that will be set to the service label, by default is `service`.
ServiceLabel string
// CustomizedLabels is the labels from the header
CustomizedLabels []string
}

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

customLabels := []string{}
if cfg.CustomizedLabels != nil && len(cfg.CustomizedLabels) > 0 {
customLabels = cfg.CustomizedLabels
sort.Strings(customLabels)
}

labels := append([]string{cfg.ServiceLabel, cfg.HandlerIDLabel, cfg.MethodLabel, cfg.StatusCodeLabel}, 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}),
}, labels),

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}),
}, labels),

httpRequestsInflight: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: cfg.Prefix,
Expand All @@ -107,12 +117,27 @@ func NewRecorder(cfg Config) metrics.Recorder {
return r
}

func (r recorder) getLabelValues(p metrics.HTTPReqProperties) []string {
keys := []string{}
for key, _ := range p.Labels {
keys = append(keys, key)
}
sort.Strings(keys)
labels := []string{p.Service, p.ID, p.Method, p.Code}
for _, key := range keys {
labels = append(labels, p.Labels[key])
}
return labels
}

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())
labels := r.getLabelValues(p)
r.httpRequestDurHistogram.WithLabelValues(labels...).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))
labels := r.getLabelValues(p)
r.httpResponseSizeHistogram.WithLabelValues(labels...).Observe(float64(sizeBytes))
}

func (r recorder) AddInflightRequests(_ context.Context, p metrics.HTTPProperties, quantity int) {
Expand Down
2 changes: 2 additions & 0 deletions middleware/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ func (r *reporter) URLPath() string { return r.c.Request().URL.Path }
func (r *reporter) StatusCode() int { return r.c.Response().Status }

func (r *reporter) BytesWritten() int64 { return r.c.Response().Size }

func (r *reporter) CustomHeaders() map[string]string { return make(map[string]string) }
2 changes: 2 additions & 0 deletions middleware/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ func (r reporter) StatusCode() int {
func (r reporter) BytesWritten() int64 {
return int64(len(r.c.Response.Body()))
}

func (r reporter) CustomHeaders() map[string]string { return make(map[string]string) }
2 changes: 2 additions & 0 deletions middleware/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ func (r *reporter) URLPath() string { return r.c.Request.URL.Path }
func (r *reporter) StatusCode() int { return r.c.Writer.Status() }

func (r *reporter) BytesWritten() int64 { return int64(r.c.Writer.Size()) }

func (r *reporter) CustomHeaders() map[string]string { return make(map[string]string) }
2 changes: 2 additions & 0 deletions middleware/gorestful/gorestful.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ func (r *reporter) URLPath() string { return r.req.Request.URL.Path }
func (r *reporter) StatusCode() int { return r.resp.StatusCode() }

func (r *reporter) BytesWritten() int64 { return int64(r.resp.ContentLength()) }

func (r *reporter) CustomHeaders() map[string]string { return make(map[string]string) }
6 changes: 6 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ func (m Middleware) Measure(handlerID string, reporter Reporter, next func()) {
code = strconv.Itoa(reporter.StatusCode())
}

labels := make(map[string]string)
for k, v := range reporter.CustomHeaders() {
labels[k] = v
}
props := metrics.HTTPReqProperties{
Service: m.cfg.Service,
ID: hid,
Method: reporter.Method(),
Code: code,
Labels: labels,
}
m.cfg.Recorder.ObserveHTTPRequestDuration(ctx, props, duration)

Expand All @@ -124,5 +129,6 @@ type Reporter interface {
Context() context.Context
URLPath() string
StatusCode() int
CustomHeaders() map[string]string
BytesWritten() int64
}
2 changes: 2 additions & 0 deletions middleware/std/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (s *stdReporter) StatusCode() int { return s.w.statusCode }

func (s *stdReporter) BytesWritten() int64 { return int64(s.w.bytesWritten) }

func (s *stdReporter) CustomHeaders() map[string]string { return make(map[string]string) }

// responseWriterInterceptor is a simple wrapper to intercept set data on a
// ResponseWriter.
type responseWriterInterceptor struct {
Expand Down