snapshot of eee7b5cfc0776c68816d7fdaa8af9d7a0e0da2e6 Annotations about the code that implements koment.

internal/metrics/middleware.go

1 package metrics
2
3 import (
4 "net/http"
5 "time"
6 )
7
8 // Instrument records a handler's requests. It takes a Recorder rather than
9 // *Metrics so that a server with metrics off pays a nil-ish interface call and
10 // nothing else.
11 func Instrument(recorder Recorder, name string, next http.Handler) http.Handler {
12 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13 started := time.Now()
14 recording := &recordingWriter{ResponseWriter: w, code: http.StatusOK}
15 next.ServeHTTP(recording, r)
16 recorder.ObserveHTTP(name, recording.code, time.Since(started))
17 })
18 }
19
20 type recordingWriter struct {
21 http.ResponseWriter
22 code int
23 written bool
24 }
25
26 func (w *recordingWriter) WriteHeader(code int) {
27 if !w.written {
28 w.code = code
29 w.written = true
30 }
31 w.ResponseWriter.WriteHeader(code)
32 }
33
34 func (w *recordingWriter) Write(b []byte) (int, error) {
35 w.written = true
36 return w.ResponseWriter.Write(b)
37 }

Find an annotation

Search file paths, rationale, kinds, and authors.

moveEnter openEsc close