/
gorymartini.go
50 lines (43 loc) · 1.17 KB
/
gorymartini.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
// Riemann middleware for martini framework
//
// Copyright (C) 2014 by Christopher Gilbert <christopher.john.gilbert@gmail.com>
package gorymartini
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/bigdatadev/goryman"
"github.com/go-martini/martini"
)
// NewGoryMartini - Factory
func NewGoryMartini(host string) (*goryman.GorymanClient, martini.Handler) {
riemann := goryman.NewGorymanClient(host)
err := riemann.Connect()
if err != nil {
return nil, nil
}
return riemann, func(res http.ResponseWriter, req *http.Request, c martini.Context, log *log.Logger) {
start := time.Now()
rw := res.(martini.ResponseWriter)
c.Next()
metric := float64(time.Since(start))
err := riemann.SendEvent(&goryman.Event{
Service: "http req",
Metric: metric,
Description: fmt.Sprintf("Completed request %v %s in %v.", rw.Status(), http.StatusText(rw.Status()), metric),
Tags: []string{
"http",
},
Attributes: map[string]string{
"path": req.URL.Path,
"status-code": strconv.Itoa(rw.Status()),
"status-text": http.StatusText(rw.Status()),
},
})
if err != nil {
log.Fatal("Riemann client SendEvent failed!")
}
}
}