-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_example_test.go
44 lines (35 loc) · 1.01 KB
/
handler_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
// +build go1.7
package xstats_test
import (
"log"
"net"
"net/http"
"time"
"github.com/rs/xhandler"
"github.com/rs/xstats"
"github.com/rs/xstats/dogstatsd"
)
func ExampleNewHandler() {
c := xhandler.Chain{}
// Install the metric handler with dogstatsd backend client and some env tags
flushInterval := 5 * time.Second
tags := []string{"role:my-service"}
statsdWriter, err := net.Dial("udp", "127.0.0.1:8126")
if err != nil {
log.Fatal(err)
}
c.Use(xstats.NewHandler(dogstatsd.New(statsdWriter, flushInterval), tags))
// Here is your handler
h := c.HandlerH(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the xstats request's instance from the context. You can safely assume it will
// be always there, if the handler is removed, xstats.FromContext will return a nop
// instance.
m := xstats.FromRequest(r)
// Count something
m.Count("requests", 1, "route:index")
}))
http.Handle("/", h)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}