|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/caddyserver/caddy/caddyhttp/httpserver" |
| 11 | +) |
| 12 | + |
| 13 | +func (m *Metrics) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { |
| 14 | + next := m.next |
| 15 | + |
| 16 | + hostname := m.hostname |
| 17 | + |
| 18 | + if hostname == "" { |
| 19 | + originalHostname, err := host(r) |
| 20 | + if err != nil { |
| 21 | + hostname = "-" |
| 22 | + } else { |
| 23 | + hostname = originalHostname |
| 24 | + } |
| 25 | + } |
| 26 | + start := time.Now() |
| 27 | + |
| 28 | + // Record response to get status code and size of the reply. |
| 29 | + rw := httpserver.NewResponseRecorder(w) |
| 30 | + // Get time to first write. |
| 31 | + tw := newTimedResponseWriter(rw) |
| 32 | + |
| 33 | + status, err := next.ServeHTTP(tw, r) |
| 34 | + |
| 35 | + // If nothing was explicitly written, consider the request written to |
| 36 | + // now that it has completed. |
| 37 | + tw.didWrite() |
| 38 | + |
| 39 | + // Transparently capture the status code so as to not side effect other plugins |
| 40 | + stat := status |
| 41 | + if err != nil && status == 0 { |
| 42 | + // Some middlewares set the status to 0, but return an non nil error: map these to status 500 |
| 43 | + stat = 500 |
| 44 | + } else if status == 0 { |
| 45 | + // 'proxy' returns a status code of 0, but the actual status is available on rw. |
| 46 | + // Note that if 'proxy' encounters an error, it returns the appropriate status code (such as 502) |
| 47 | + // from ServeHTTP and is captured above with 'stat := status'. |
| 48 | + stat = rw.Status() |
| 49 | + } |
| 50 | + |
| 51 | + fam := "1" |
| 52 | + if isIPv6(r.RemoteAddr) { |
| 53 | + fam = "2" |
| 54 | + } |
| 55 | + |
| 56 | + proto := strconv.Itoa(r.ProtoMajor) |
| 57 | + proto = proto + "." + strconv.Itoa(r.ProtoMinor) |
| 58 | + |
| 59 | + statusStr := strconv.Itoa(stat) |
| 60 | + |
| 61 | + requestCount.WithLabelValues(hostname, fam, proto).Inc() |
| 62 | + requestDuration.WithLabelValues(hostname, fam, proto).Observe(time.Since(start).Seconds()) |
| 63 | + responseSize.WithLabelValues(hostname, fam, proto, statusStr).Observe(float64(rw.Size())) |
| 64 | + responseStatus.WithLabelValues(hostname, fam, proto, statusStr).Inc() |
| 65 | + responseLatency.WithLabelValues(hostname, fam, proto, statusStr).Observe(tw.firstWrite().Sub(start).Seconds()) |
| 66 | + |
| 67 | + return status, err |
| 68 | +} |
| 69 | + |
| 70 | +func host(r *http.Request) (string, error) { |
| 71 | + host, _, err := net.SplitHostPort(r.Host) |
| 72 | + if err != nil { |
| 73 | + if !strings.Contains(r.Host, ":") { |
| 74 | + return strings.ToLower(r.Host), nil |
| 75 | + } |
| 76 | + return "", err |
| 77 | + } |
| 78 | + return strings.ToLower(host), nil |
| 79 | +} |
| 80 | + |
| 81 | +func isIPv6(addr string) bool { |
| 82 | + if host, _, err := net.SplitHostPort(addr); err == nil { |
| 83 | + // Strip away the port. |
| 84 | + addr = host |
| 85 | + } |
| 86 | + ip := net.ParseIP(addr) |
| 87 | + return ip != nil && ip.To4() == nil |
| 88 | +} |
| 89 | + |
| 90 | +// A responseWriterWithFirstWrite is an http.Responsewrite with the ability to |
| 91 | +// tracks the time when the first response write happened. |
| 92 | +type responseWriterWithFirstWrite interface { |
| 93 | + http.ResponseWriter |
| 94 | + didWrite() |
| 95 | + firstWrite() time.Time |
| 96 | +} |
| 97 | + |
| 98 | +// A timedResponseWriter tracks the time when the first response write |
| 99 | +// happened. |
| 100 | +type timedResponseWriter struct { |
| 101 | + firstWriteTime time.Time |
| 102 | + http.ResponseWriter |
| 103 | +} |
| 104 | + |
| 105 | +// A timedResponseWriterHijacker is a timedResponseWriter and http.Hijacker. |
| 106 | +type timedResponseWriterHijacker struct { |
| 107 | + *timedResponseWriter |
| 108 | + http.Hijacker |
| 109 | +} |
| 110 | + |
| 111 | +// NewLoggedResponseWriter wraps the provided http.ResponseWriter with Status |
| 112 | +// preserving the support to hijack the connection if supported by the provided |
| 113 | +// http.ResponseWriter. |
| 114 | +func newTimedResponseWriter(w http.ResponseWriter) responseWriterWithFirstWrite { |
| 115 | + tw := &timedResponseWriter{ResponseWriter: w} |
| 116 | + if hj, ok := w.(http.Hijacker); ok { |
| 117 | + return &timedResponseWriterHijacker{timedResponseWriter: tw, Hijacker: hj} |
| 118 | + } |
| 119 | + |
| 120 | + return tw |
| 121 | +} |
| 122 | + |
| 123 | +func (w *timedResponseWriter) didWrite() { |
| 124 | + if w.firstWriteTime.IsZero() { |
| 125 | + w.firstWriteTime = time.Now() |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func (w *timedResponseWriter) firstWrite() time.Time { |
| 130 | + return w.firstWriteTime |
| 131 | +} |
| 132 | + |
| 133 | +func (w *timedResponseWriter) Write(data []byte) (int, error) { |
| 134 | + w.didWrite() |
| 135 | + return w.ResponseWriter.Write(data) |
| 136 | +} |
| 137 | + |
| 138 | +func (w *timedResponseWriter) WriteHeader(statuscode int) { |
| 139 | + // We consider this a write as it's valid to respond to a request by |
| 140 | + // just setting a status code and returning. |
| 141 | + w.didWrite() |
| 142 | + w.ResponseWriter.WriteHeader(statuscode) |
| 143 | +} |
0 commit comments