-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappflow.go
66 lines (60 loc) · 1.7 KB
/
appflow.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
// Package appflow provides middleware to export HTTP application flow data.
package appflow
import (
"encoding/json"
"net"
"net/http"
)
// Destination represents a flow destination.
type Destination struct {
conn *net.UDPConn
}
// HTTPFlowData represents flow data that will be
// serialized and sent to a collector.
type HTTPFlowData struct {
Method string `json:"method"`
URL string `json:"url"`
Proto string `json:"proto"`
Header map[string][]string `json:"header"`
ContentLength int `json:"contentLength"`
Host string `json:"host"`
RemoteAddr string `json:"remoteAddr"`
}
// NewDestination creates a new Destination that sends
// flow data to the given address.
func NewDestination(address string) (*Destination, error) {
udpAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return nil, err
}
return &Destination{
conn: conn,
}, nil
}
// Emit serializes an http.Request and sends
// it to the Destination.
func (d *Destination) Emit(r *http.Request) {
flowData := HTTPFlowData{
Method: r.Method,
URL: r.URL.String(),
Proto: r.Proto,
Header: map[string][]string(r.Header),
ContentLength: int(r.ContentLength),
Host: r.Host,
RemoteAddr: r.RemoteAddr,
}
json.NewEncoder(d.conn).Encode(flowData)
}
// Decode unmarshals JSON-encoded HTTPFlowData from b.
func Decode(b []byte) (*HTTPFlowData, error) {
var flowData HTTPFlowData
err := json.Unmarshal(b, &flowData)
if err != nil {
return nil, err
}
return &flowData, nil
}