forked from apiaryio/heroku-datadog-drain-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
175 lines (145 loc) · 3.63 KB
/
server.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
package main
import (
"bufio"
"errors"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
)
const bufferLen = 500
type logData struct {
app *string
tags *[]string
prefix *string
line *string
}
type ServerCtx struct {
Port string
AppTags map[string][]string
AppPrefix map[string]string
StatsdUrl string
BasicAuthUsername string
BasicAuthPassword string
Debug bool
in chan *logData
out chan *logMetrics
}
//STATSD_URL=.. Required. Default: localhost:8125
//DATADOG_DRAIN_DEBUG= Optional. If DEBUG is set, a lot of stuff w
func loadServerCtx() *ServerCtx {
s := &ServerCtx{"8080",
make(map[string][]string),
make(map[string]string),
"localhost:8125",
"",
"",
false,
nil,
nil,
}
port := os.Getenv("PORT")
if port != "" {
s.Port = port
}
if os.Getenv("DATADOG_DRAIN_DEBUG") != "" {
s.Debug = true
}
s.StatsdUrl = os.Getenv("STATSD_URL")
if s.StatsdUrl == "" {
log.Panic(errors.New("Missing STATSD_URL"))
}
s.BasicAuthUsername = os.Getenv("BASIC_AUTH_USERNAME")
if s.BasicAuthUsername == "" {
log.Panic(errors.New("Missing BASIC_AUTH_USERNAME"))
}
s.BasicAuthPassword = os.Getenv("BASIC_AUTH_PASSWORD")
if s.BasicAuthPassword == "" {
log.Panic(errors.New("Missing BASIC_AUTH_PASSWORD"))
}
log.WithFields(log.Fields{
"port": s.Port,
"AppTags": s.AppTags,
"AppPrefix": s.AppPrefix,
"StatsdUrl": s.StatsdUrl,
"Debug": s.Debug,
}).Info("Configuration loaded")
return s
}
func init() {
// Output to stderr instead of stdout
log.SetOutput(os.Stderr)
// Only log the Info severity or above.
log.SetLevel(log.InfoLevel)
}
func (s *ServerCtx) getTags(c *gin.Context, app string) []string {
requestTags := c.DefaultQuery("tags", "")
if requestTags == "" {
return s.AppTags[app]
} else {
return strings.Split(requestTags, ",")
}
}
func (s *ServerCtx) processLogs(c *gin.Context) {
app := c.DefaultQuery("app", "")
if app == "" {
log.Error(errors.New("app query parameter not passed"))
c.String(500, "Missing app query parameter")
return
}
tags := s.getTags(c, app)
tags = append(tags, "app:"+app)
prefix := c.DefaultQuery("prefix", s.AppPrefix[app])
scanner := bufio.NewScanner(c.Request.Body)
for scanner.Scan() {
line := scanner.Text()
s.in <- &logData{&app, &tags, &prefix, &line}
}
if err := scanner.Err(); err != nil {
log.Error(err)
}
c.String(200, "OK")
}
func main() {
gin.SetMode(gin.ReleaseMode)
s := loadServerCtx()
if s.Debug {
log.SetLevel(log.DebugLevel)
gin.SetMode(gin.DebugMode)
}
c, err := statsdClient(s.StatsdUrl)
if err != nil {
log.WithField("statsdUrl", s.StatsdUrl).Fatal("Could not connect to statsd")
}
if v := os.Getenv("EXCLUDED_TAGS"); v != "" {
for _, t := range strings.Split(v, ",") {
c.ExcludedTags[t] = true
}
}
r := gin.New()
r.Use(gin.Recovery())
r.GET("/status", func(c *gin.Context) {
c.String(200, "OK")
})
accounts := map[string]string{}
accounts[s.BasicAuthUsername] = s.BasicAuthPassword
auth := r.Group("/", gin.BasicAuth(accounts))
auth.POST("/", s.processLogs)
s.in = make(chan *logData, bufferLen)
defer close(s.in)
s.out = make(chan *logMetrics, bufferLen)
defer close(s.out)
go logProcess(s.in, s.out)
go c.sendToStatsd(s.out)
go func() {
for {
c.Gauge("heroku_logdrain.queue.in.length", float64(len(s.in)), []string{}, sampleRate)
c.Gauge("heroku_logdrain.queue.out.length", float64(len(s.out)), []string{}, sampleRate)
time.Sleep(10 * time.Second)
}
}()
log.Infoln("Server ready ...")
r.Run(":" + s.Port)
}