-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotifilter.go
208 lines (179 loc) · 5.18 KB
/
notifilter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"time"
"github.com/bittersweet/notifilter-receive/elasticsearch"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/types"
"github.com/kelseyhightower/envconfig"
_ "github.com/lib/pq"
)
const maxPacketSize = 1024 * 1024
// db holds our connection pool to Postgres
var db *sqlx.DB
// C is a global variable that holds loaded config settings
var C Config
// ESClient is a global variable that points to our ES client
var ESClient elasticsearch.Client
// Time the app started up
var startTime = time.Now()
// Config will be populated with settings loaded from the environment or
// local defaults
type Config struct {
AppPort int `default:"8000"`
DBHost string `default:"127.0.0.1"`
DBUser string `default:""`
DBPassword string `default:""`
DBName string `default:"notifilter_development"`
ESHost string `default:"127.0.0.1"`
ESPort int `default:"9200"`
SlackHookURL string `required:"true"`
}
// Event will hold incoming data and will be persisted to ES eventually
type Event struct {
Application string `json:"application"`
Identifier string `json:"identifier"`
requestID string
Data types.JSONText `json:"data"`
}
// dataToMap transforms the raw JSON data into a map
func (e *Event) dataToMap() map[string]interface{} {
m := map[string]interface{}{}
err := e.Data.Unmarshal(&m)
if err != nil {
e.log("Error in dataToMap():", err)
return map[string]interface{}{}
}
return m
}
// persist saves the incoming event to Elasticsearch
func (e *Event) persist() {
err := ESClient.Persist(e.requestID, e.Application, e.Identifier, e.dataToMap())
if err != nil {
e.log("Error persisting to ElasticSearch: %s", err)
}
}
// notify checks to see if we have notifiers set up for this event and if the
// rules for those notifications have been satisfied
func (e *Event) notify() {
notifiers := []Notifier{}
err := db.Select(¬ifiers, "SELECT * FROM notifiers WHERE application=$1 AND event_name=$2", e.Application, e.Identifier)
if err != nil {
log.Fatal("db.Select ", err)
}
e.log("[NOTIFY] found %d notifiers", len(notifiers))
for i := 0; i < len(notifiers); i++ {
notifier := notifiers[i]
notifier.notify(e, notifier.newNotifier())
}
}
func (e *Event) log(msg string, args ...interface{}) {
logStr := fmt.Sprintf(msg, args...)
log.Printf("%s %s\n", e.requestID, logStr)
}
// incomingItems creates a channel that we can place events on so the main loop
// can keep listening to incoming events
func incomingItems() chan<- []byte {
incomingChan := make(chan []byte)
// Open a channel with a capacity of 10.000 events
// This will only block the sender if the buffer fills up.
// If we do not buffer any event that gets sent to the channel will be
// dropped if we can not handle it.
tasks := make(chan Event, 10000)
// Generate unique ID to tag requests
// Thanks to https://blog.cloudflare.com/go-at-cloudflare/
idGenerator := make(chan string)
go func() {
h := sha1.New()
c := []byte(time.Now().String())
for {
h.Write(c)
idGenerator <- fmt.Sprintf("%x", h.Sum(nil))
}
}()
// Use 4 workers that will concurrently grab Events of the channel and
// persist+notify
for i := 0; i < 4; i++ {
go func() {
for event := range tasks {
event.persist()
event.notify()
}
}()
}
go func() {
for {
select {
case b := <-incomingChan:
var event Event
err := json.Unmarshal(b, &event)
if err != nil {
log.Println(err)
}
requestID := <-idGenerator
event.requestID = requestID
tasks <- event
}
}
}()
fmt.Println("incomingItems launched")
return incomingChan
}
// listenToUDP opens a UDP connection that we will listen on
func listenToUDP(conn *net.UDPConn) {
incomingChan := incomingItems()
buffer := make([]byte, maxPacketSize)
for {
bytes, err := conn.Read(buffer)
if err != nil {
log.Println("UDP read error: ", err.Error())
continue
}
msg := make([]byte, bytes)
copy(msg, buffer)
incomingChan <- msg
}
}
// main handles setting up connections for UDP/TCP and connecting to Postgres
// Note, sqlx uses a connection pool internally.
func main() {
err := envconfig.Process("notifilter", &C)
if err != nil {
log.Fatal("Could not load config: ", err.Error())
}
log.Printf("Config loaded: %#v\n", C)
port := fmt.Sprintf(":%d", C.AppPort)
addr, err := net.ResolveUDPAddr("udp", port)
if err != nil {
log.Fatal("ResolveUDPAddr", err)
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatal("ListenUDP", err)
}
go listenToUDP(conn)
pgStr := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable", C.DBHost, C.DBUser, C.DBPassword, C.DBName)
db, err = sqlx.Connect("postgres", pgStr)
if err != nil {
log.Fatal("DB Open()", err)
}
defer db.Close()
ESClient = elasticsearch.Client{
Host: C.ESHost,
Port: C.ESPort,
Index: "notifilter",
}
http.Handle("/v1/count", handleCount(&ESClient))
http.Handle("/v1/statistics", handleStatistics(startTime))
http.Handle("/v1/preview", handlePreview())
fmt.Printf("Will start listening on port %s\n", port)
err = http.ListenAndServe(port, nil)
if err != nil {
log.Fatal("ListenAndServe ", err)
}
}