Skip to content

Commit 68b30a6

Browse files
committed
Autogen README (new example)
1 parent 9b28a19 commit 68b30a6

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ In fact the internal code of **go-json-rest** is itself implemented with Middlew
4747
- [Streaming](#streaming)
4848
- [Non JSON payload](#non-json-payload)
4949
- [API Versioning](#api-versioning)
50+
- [Statsd](#statsd)
5051
- [SPDY](#spdy)
5152
- [Google App Engine](#gae)
5253
- [Basic Auth Custom](#basic-auth-custom)
@@ -1125,6 +1126,98 @@ func main() {
11251126

11261127
```
11271128

1129+
#### Statsd
1130+
1131+
Demonstrate how to use OuterMiddlewares to do additional logging and reporting.
1132+
1133+
Here `request.Env["STATUS_CODE"]` and `request.Env["ELAPSED_TIME"]` that are available to outer middlewares are used with the [g2s](https://github.com/peterbourgon/g2s) statsd client to send these metrics to statsd.
1134+
1135+
The curl demo:
1136+
``` sh
1137+
# start statsd server
1138+
# monitor network
1139+
ngrep -d any port 8125
1140+
1141+
curl -i http://127.0.0.1:8080/message
1142+
curl -i http://127.0.0.1:8080/doesnotexist
1143+
1144+
```
1145+
1146+
Go code:
1147+
``` go
1148+
package main
1149+
1150+
import (
1151+
"github.com/ant0ine/go-json-rest/rest"
1152+
"github.com/peterbourgon/g2s"
1153+
"log"
1154+
"net/http"
1155+
"strconv"
1156+
"time"
1157+
)
1158+
1159+
type StatsdMiddleware struct {
1160+
IpPort string
1161+
Prefix string
1162+
}
1163+
1164+
func (mw *StatsdMiddleware) MiddlewareFunc(handler rest.HandlerFunc) rest.HandlerFunc {
1165+
1166+
statsd, err := g2s.Dial("udp", mw.IpPort)
1167+
if err != nil {
1168+
panic(err)
1169+
}
1170+
1171+
keyBase := ""
1172+
if mw.Prefix != "" {
1173+
keyBase += mw.Prefix + "."
1174+
}
1175+
keyBase += "response."
1176+
1177+
return func(writer rest.ResponseWriter, request *rest.Request) {
1178+
1179+
handler(writer, request)
1180+
1181+
statusCode := request.Env["STATUS_CODE"].(int)
1182+
statsd.Counter(1.0, keyBase+"status_code."+strconv.Itoa(statusCode), 1)
1183+
log.Print(keyBase + "status_code." + strconv.Itoa(statusCode))
1184+
1185+
elapsedTime := request.Env["ELAPSED_TIME"].(*time.Duration)
1186+
statsd.Timing(1.0, keyBase+"elapsed_time", *elapsedTime)
1187+
}
1188+
}
1189+
1190+
type Message struct {
1191+
Body string
1192+
}
1193+
1194+
func main() {
1195+
handler := rest.ResourceHandler{
1196+
OuterMiddlewares: []rest.Middleware{
1197+
&StatsdMiddleware{
1198+
IpPort: "localhost:8125",
1199+
},
1200+
},
1201+
}
1202+
err := handler.SetRoutes(
1203+
&rest.Route{"GET", "/message", func(w rest.ResponseWriter, req *rest.Request) {
1204+
1205+
// take more than 1ms so statsd can report it
1206+
time.Sleep(100 * time.Millisecond)
1207+
1208+
w.WriteJson(&Message{
1209+
Body: "Hello World!",
1210+
})
1211+
}},
1212+
)
1213+
if err != nil {
1214+
log.Fatal(err)
1215+
}
1216+
log.Fatal(http.ListenAndServe(":8080", &handler))
1217+
}
1218+
1219+
```
1220+
11281221
#### SPDY
11291222

11301223
Demonstrate how to use SPDY with https://github.com/shykes/spdy-go

0 commit comments

Comments
 (0)