File: middleware.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (116 lines) | stat: -rw-r--r-- 3,597 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package api

import (
	"fmt"
	"math"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/rs/zerolog/log"
)

// Only use base path as label value (e.g.: /api/repos) because of time series cardinality
// See https://prometheus.io/docs/practices/naming/#labels
func getBasePath(c *gin.Context) string {
	segment0, err := getURLSegment(c.Request.URL.Path, 0)
	if err != nil {
		return "/"
	}
	segment1, err := getURLSegment(c.Request.URL.Path, 1)
	if err != nil {
		return *segment0
	}

	return *segment0 + *segment1
}

func getURLSegment(url string, idx int) (*string, error) {
	urlSegments := strings.Split(url, "/")
	// Remove segment at index 0 because it's an empty string
	urlSegments = urlSegments[1:cap(urlSegments)]

	if len(urlSegments) <= idx {
		return nil, fmt.Errorf("index %d out of range, only has %d url segments", idx, len(urlSegments))
	}

	segmentAtIndex := urlSegments[idx]
	s := fmt.Sprintf("/%s", segmentAtIndex)
	return &s, nil
}

func instrumentHandlerInFlight(g *prometheus.GaugeVec, pathFunc func(*gin.Context) string) func(*gin.Context) {
	return func(c *gin.Context) {
		g.WithLabelValues(c.Request.Method, pathFunc(c)).Inc()
		defer g.WithLabelValues(c.Request.Method, pathFunc(c)).Dec()
		c.Next()
	}
}

func instrumentHandlerCounter(counter *prometheus.CounterVec, pathFunc func(*gin.Context) string) func(*gin.Context) {
	return func(c *gin.Context) {
		c.Next()
		counter.WithLabelValues(strconv.Itoa(c.Writer.Status()), c.Request.Method, pathFunc(c)).Inc()
	}
}

func instrumentHandlerRequestSize(obs prometheus.ObserverVec, pathFunc func(*gin.Context) string) func(*gin.Context) {
	return func(c *gin.Context) {
		c.Next()
		obs.WithLabelValues(strconv.Itoa(c.Writer.Status()), c.Request.Method, pathFunc(c)).Observe(float64(c.Request.ContentLength))
	}
}

func instrumentHandlerResponseSize(obs prometheus.ObserverVec, pathFunc func(*gin.Context) string) func(*gin.Context) {
	return func(c *gin.Context) {
		c.Next()
		var responseSize = math.Max(float64(c.Writer.Size()), 0)
		obs.WithLabelValues(strconv.Itoa(c.Writer.Status()), c.Request.Method, pathFunc(c)).Observe(responseSize)
	}
}

func instrumentHandlerDuration(obs prometheus.ObserverVec, pathFunc func(*gin.Context) string) func(*gin.Context) {
	return func(c *gin.Context) {
		now := time.Now()
		c.Next()
		obs.WithLabelValues(strconv.Itoa(c.Writer.Status()), c.Request.Method, pathFunc(c)).Observe(time.Since(now).Seconds())
	}
}

// JSONLogger is a gin middleware that takes an instance of Logger and uses it for writing access
// logs that include error messages if there are any.
func JSONLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		// Start timer
		start := time.Now()
		path := c.Request.URL.Path
		raw := c.Request.URL.RawQuery

		// Process request
		c.Next()

		ts := time.Now()
		if raw != "" {
			path = path + "?" + raw
		}

		errorMessage := strings.TrimSuffix(c.Errors.ByType(gin.ErrorTypePrivate).String(), "\n")
		l := log.With().Str("remote", c.ClientIP()).Logger().
			With().Str("method", c.Request.Method).Logger().
			With().Str("path", path).Logger().
			With().Str("protocol", c.Request.Proto).Logger().
			With().Str("code", fmt.Sprint(c.Writer.Status())).Logger().
			With().Str("latency", ts.Sub(start).String()).Logger().
			With().Str("agent", c.Request.UserAgent()).Logger()

		if c.Writer.Status() >= 400 && c.Writer.Status() < 500 {
			l.Warn().Msg(errorMessage)
		} else if c.Writer.Status() >= 500 {
			l.Error().Msg(errorMessage)
		} else {
			l.Info().Msg(errorMessage)
		}
	}
}