-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
108 lines (88 loc) · 3.15 KB
/
example_test.go
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
package tracer_test
import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"time"
"github.com/kamilsk/tracer"
)
type Data struct {
Title string `json:"title"`
Subtitle string `json:"subtitle"`
}
func Example() {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/message",
strings.NewReader(`{"title": "tracer", "subtitle": "🧶 Simple, lightweight tracing mechanism."}`))
req.Header.Set("X-Request-Id", "ca7a87c4-58d0-4fdf-857c-ef49fc3bf271")
handler := InjectTracer(FlushTracer(http.HandlerFunc(Handle)))
handler.ServeHTTP(rec, req)
_, _ = io.Copy(os.Stdout, strings.NewReader(stabilize(rec.Body.String())))
// Output:
// allocates at call stack: 0, detailed call stack:
// call Handle [ca7a87c4-58d0-4fdf-857c-ef49fc3bf271]: 12.345678ms, allocates: 2
// checkpoint [serialize]: 1.234567ms
// checkpoint [store]: 1.234567ms
// call FetchData: 1.234567ms, allocates: 0
// call StoreIntoDatabase: 12.345678ms, allocates: 0
}
func FlushTracer(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
handler.ServeHTTP(rw, req)
_, _ = rw.Write([]byte(tracer.Fetch(req.Context()).String()))
})
}
func InjectTracer(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
req = req.WithContext(tracer.Inject(req.Context(), make([]*tracer.Call, 0, 10)))
handler.ServeHTTP(rw, req)
})
}
func Handle(rw http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithTimeout(req.Context(), time.Second)
defer cancel()
call := tracer.Fetch(req.Context()).Start(req.Header.Get("X-Request-Id"))
defer call.Stop()
time.Sleep(time.Millisecond)
call.Checkpoint("serialize")
data, err := FetchData(ctx, req.Body)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
time.Sleep(time.Millisecond)
call.Checkpoint("store")
if err := StoreIntoDatabase(ctx, data); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
}
func FetchData(ctx context.Context, r io.Reader) (Data, error) {
defer tracer.Fetch(ctx).Start().Stop()
time.Sleep(time.Millisecond)
var data Data
err := json.NewDecoder(r).Decode(&data)
return data, err
}
func StoreIntoDatabase(ctx context.Context, data Data) error {
defer tracer.Fetch(ctx).Start().Stop()
time.Sleep(10 * time.Millisecond)
return gob.NewEncoder(bytes.NewBuffer(nil)).Encode(data)
}
func stabilize(raw string) string {
raw = strings.Replace(raw, "tracer_test.", "", -1)
raw = regexp.MustCompile(`Handle (.+): (\d{2}\.\d+ms)`).ReplaceAllString(raw, "Handle $1: 12.345678ms")
raw = regexp.MustCompile(`\[serialize]: (\d\.\d+ms)`).ReplaceAllString(raw, "[serialize]: 1.234567ms")
raw = regexp.MustCompile(`\[store]: (\d\.\d+ms)`).ReplaceAllString(raw, "[store]: 1.234567ms")
raw = regexp.MustCompile(`FetchData: (\d\.\d+ms)`).ReplaceAllString(raw, "FetchData: 1.234567ms")
raw = regexp.MustCompile(`StoreIntoDatabase: (\d{2}\.\d+ms)`).ReplaceAllString(raw, "StoreIntoDatabase: 12.345678ms")
return raw
}