This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
/
elastic.go
73 lines (60 loc) · 1.59 KB
/
elastic.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
package main
import (
"fmt"
"github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type RequestResponse struct {
URL string `json:"url"`
Method string `json:"method"`
StatusCode int `json:"status"`
Duration int64 `json:"duration"`
Timestamp string `json:"timestamp"`
}
type Sink struct {
Index string
Type string
Indexer *core.BulkIndexer
}
func (s Sink) Run() {
done := make(chan bool)
s.Indexer.Run(done)
}
func (s Sink) Put(req *http.Request, resp *http.Response, t time.Duration) error {
// Timestamp is basic_date_time_no_millis
rr := RequestResponse{
URL: req.URL.String(),
Method: req.Method,
StatusCode: resp.StatusCode,
Duration: t.Nanoseconds() / 1000 / 1000,
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
}
log.Printf("url:\"%s\" method:%s status:%d", req.URL.String(), req.Method, resp.StatusCode)
return s.Indexer.Index(s.Index, s.Type, "", "", nil, rr)
}
func NewSink(raw string) (Sink, error) {
esurl, err := url.Parse(raw)
if err != nil {
return Sink{}, err
}
parts := strings.Split(esurl.Host, ":")
paths := strings.Split(esurl.Path, "/")
if len(parts) == 1 {
return Sink{}, fmt.Errorf("No hostname and port found in `%s`", esurl.Host)
}
if len(paths) < 2 {
return Sink{}, fmt.Errorf("Search index not found in `%s`", esurl.Path)
}
if len(parts) == 2 {
api.Domain = parts[0]
api.Port = parts[1]
} else {
api.Domain = parts[0]
}
return Sink{Index: paths[1], Type: "request", Indexer: core.NewBulkIndexerErrors(10, 60)}, nil
}