|
| 1 | +package reverseproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | +) |
| 11 | + |
| 12 | +type Metrics struct { |
| 13 | + RequestsInFlight *prometheus.GaugeVec |
| 14 | + RequestsTotal *prometheus.CounterVec |
| 15 | + RequestLatency *prometheus.HistogramVec |
| 16 | + RequestSize *prometheus.HistogramVec |
| 17 | + ResponseSize *prometheus.HistogramVec |
| 18 | +} |
| 19 | + |
| 20 | +func NewMetrics(subsystem string) *Metrics { |
| 21 | + sizeBuckets := prometheus.ExponentialBuckets(256, 4, 8) |
| 22 | + return &Metrics{ |
| 23 | + RequestsInFlight: prometheus.NewGaugeVec( |
| 24 | + prometheus.GaugeOpts{ |
| 25 | + Namespace: "piko", |
| 26 | + Subsystem: subsystem, |
| 27 | + Name: "requests_in_flight", |
| 28 | + Help: "Number of requests currently handled by this server.", |
| 29 | + }, |
| 30 | + []string{"endpoint"}, |
| 31 | + ), |
| 32 | + RequestsTotal: prometheus.NewCounterVec( |
| 33 | + prometheus.CounterOpts{ |
| 34 | + Namespace: "piko", |
| 35 | + Subsystem: subsystem, |
| 36 | + Name: "requests_total", |
| 37 | + Help: "Total requests.", |
| 38 | + }, |
| 39 | + []string{"status", "method", "endpoint"}, |
| 40 | + ), |
| 41 | + RequestLatency: prometheus.NewHistogramVec( |
| 42 | + prometheus.HistogramOpts{ |
| 43 | + Namespace: "piko", |
| 44 | + Subsystem: subsystem, |
| 45 | + Name: "request_latency_seconds", |
| 46 | + Help: "Request latency.", |
| 47 | + Buckets: prometheus.DefBuckets, |
| 48 | + }, |
| 49 | + []string{"status", "method", "endpoint"}, |
| 50 | + ), |
| 51 | + RequestSize: prometheus.NewHistogramVec( |
| 52 | + prometheus.HistogramOpts{ |
| 53 | + Namespace: "piko", |
| 54 | + Subsystem: subsystem, |
| 55 | + Name: "request_size_bytes", |
| 56 | + Help: "Request size", |
| 57 | + Buckets: sizeBuckets, |
| 58 | + }, |
| 59 | + []string{"endpoint"}, |
| 60 | + ), |
| 61 | + ResponseSize: prometheus.NewHistogramVec( |
| 62 | + prometheus.HistogramOpts{ |
| 63 | + Namespace: "piko", |
| 64 | + Subsystem: subsystem, |
| 65 | + Name: "response_size_bytes", |
| 66 | + Help: "Response size", |
| 67 | + Buckets: sizeBuckets, |
| 68 | + }, |
| 69 | + []string{"endpoint"}, |
| 70 | + ), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (m *Metrics) Handler(endpoint string) gin.HandlerFunc { |
| 75 | + return func(c *gin.Context) { |
| 76 | + m.RequestsInFlight.With(prometheus.Labels{ |
| 77 | + "endpoint": endpoint, |
| 78 | + }).Inc() |
| 79 | + defer m.RequestsInFlight.With(prometheus.Labels{ |
| 80 | + "endpoint": endpoint, |
| 81 | + }).Dec() |
| 82 | + |
| 83 | + start := time.Now() |
| 84 | + |
| 85 | + // Process request. |
| 86 | + c.Next() |
| 87 | + |
| 88 | + m.RequestsTotal.With(prometheus.Labels{ |
| 89 | + "status": strconv.Itoa(c.Writer.Status()), |
| 90 | + "method": c.Request.Method, |
| 91 | + "endpoint": endpoint, |
| 92 | + }).Inc() |
| 93 | + m.RequestLatency.With(prometheus.Labels{ |
| 94 | + "status": strconv.Itoa(c.Writer.Status()), |
| 95 | + "method": c.Request.Method, |
| 96 | + "endpoint": endpoint, |
| 97 | + }).Observe(float64(time.Since(start).Milliseconds()) / 1000) |
| 98 | + m.RequestSize.With(prometheus.Labels{ |
| 99 | + "endpoint": endpoint, |
| 100 | + }).Observe(float64(computeApproximateRequestSize(c.Request))) |
| 101 | + m.ResponseSize.With(prometheus.Labels{ |
| 102 | + "endpoint": endpoint, |
| 103 | + }).Observe(float64(c.Writer.Size())) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func (m *Metrics) Register(registry *prometheus.Registry) { |
| 108 | + registry.MustRegister( |
| 109 | + m.RequestsInFlight, |
| 110 | + m.RequestsTotal, |
| 111 | + m.RequestLatency, |
| 112 | + m.RequestSize, |
| 113 | + m.ResponseSize, |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +func computeApproximateRequestSize(r *http.Request) int { |
| 118 | + s := 0 |
| 119 | + if r.URL != nil { |
| 120 | + s += len(r.URL.String()) |
| 121 | + } |
| 122 | + |
| 123 | + s += len(r.Method) |
| 124 | + s += len(r.Proto) |
| 125 | + for name, values := range r.Header { |
| 126 | + s += len(name) |
| 127 | + for _, value := range values { |
| 128 | + s += len(value) |
| 129 | + } |
| 130 | + } |
| 131 | + s += len(r.Host) |
| 132 | + |
| 133 | + if r.ContentLength != -1 { |
| 134 | + s += int(r.ContentLength) |
| 135 | + } |
| 136 | + return s |
| 137 | +} |
0 commit comments