-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitoring.go
201 lines (176 loc) · 5.53 KB
/
monitoring.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
package main
import (
"fmt"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
const defaultNamespace = "go-fish"
// MonitoringConfiguration allows you to configure how record processing metrics are exposed
type monitoringConfiguration struct {
MonitoringService string // Type of monitoring to expose. Supported types are "prometheus" and "cloudwatch"
Prometheus prometheusMonitoringService
CloudWatch cloudWatchMonitoringService
}
type monitoringService interface {
init(*mux.Router) error
incrPipelines(string)
incrEventReceived(string)
}
func (m *monitoringConfiguration) init(r *mux.Router) (monitoringService, error) {
var service monitoringService
switch m.MonitoringService {
case "prometheus":
service = &m.Prometheus
case "cloudwatch":
service = &m.CloudWatch
case "":
service = &noopMonitoringService{}
default:
return service, fmt.Errorf("Invalid monitoring service type %s", m.MonitoringService)
}
return service, service.init(r)
}
type noopMonitoringService struct{}
func (n *noopMonitoringService) init(_ *mux.Router) error { return nil }
func (n *noopMonitoringService) incrPipelines(string) {}
func (n *noopMonitoringService) incrEventReceived(string) {}
type prometheusMonitoringService struct {
Namespace string
pipelines *prometheus.GaugeVec
events *prometheus.CounterVec
}
func (p *prometheusMonitoringService) init(r *mux.Router) error {
if p.Namespace == "" {
p.Namespace = defaultNamespace
}
p.pipelines = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: p.Namespace + `Pipelines`,
Help: "The number of pipelines configured",
}, []string{"pipelineName"})
p.events = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: p.Namespace + `EventsReceived`,
Help: "The number of events received",
}, []string{"pipelineName"})
metrics := []prometheus.Collector{
p.pipelines,
p.events,
}
for _, metric := range metrics {
err := prometheus.Register(metric)
if err != nil {
return err
}
}
r.Handle("/metrics", promhttp.Handler())
return nil
}
func (p *prometheusMonitoringService) incrPipelines(pipelineName string) {
p.pipelines.With(prometheus.Labels{"pipelineName": pipelineName}).Add(float64(1))
}
func (p *prometheusMonitoringService) incrEventReceived(pipelineName string) {
p.events.With(prometheus.Labels{"pipelineName": pipelineName}).Add(float64(1))
}
type cloudWatchMonitoringService struct {
Namespace string
// What granularity we should send metrics to CW at. Note setting this to 1 will cost quite a bit of money
// At the time of writing (March 2018) about US$200 per month
ResolutionSec int
svc cloudwatchiface.CloudWatchAPI
pipelineMetrics map[string]*cloudWatchMetrics
}
type cloudWatchMetrics struct {
pipelines float64
eventsReceived float64
sync.Mutex
}
func (cw *cloudWatchMonitoringService) init(_ *mux.Router) error {
if cw.Namespace == "" {
cw.Namespace = defaultNamespace
}
if cw.ResolutionSec == 0 {
cw.ResolutionSec = 60
}
session, err := session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
},
)
if err != nil {
return err
}
cw.svc = cloudwatch.New(session)
return nil
}
func (cw *cloudWatchMonitoringService) flushDaemon() {
previousFlushTime := time.Now()
resolutionDuration := time.Duration(cw.ResolutionSec) * time.Second
for {
time.Sleep(resolutionDuration - time.Now().Sub(previousFlushTime))
cw.flush()
previousFlushTime = time.Now()
}
}
func (cw *cloudWatchMonitoringService) flush() {
for pipeline, metric := range cw.pipelineMetrics {
metric.Lock()
metricTimestamp := time.Now()
_, err := cw.svc.PutMetricData(&cloudwatch.PutMetricDataInput{
Namespace: aws.String(cw.Namespace),
MetricData: []*cloudwatch.MetricDatum{
&cloudwatch.MetricDatum{
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("Pipeline"),
Value: &pipeline,
},
},
MetricName: aws.String("Pipelines"),
Unit: aws.String("Count"),
Timestamp: &metricTimestamp,
Value: aws.Float64(metric.pipelines),
},
&cloudwatch.MetricDatum{
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("Pipeline"),
Value: &pipeline,
},
},
MetricName: aws.String("EventsReceived"),
Unit: aws.String("Count"),
Timestamp: &metricTimestamp,
Value: aws.Float64(metric.eventsReceived),
},
},
})
metric.Unlock()
if err != nil {
log.Errorln("Error sending logs to CloudWatch", err)
}
}
}
// With all the locking this is probably really innefficent at scale
func (cw *cloudWatchMonitoringService) incrPipelines(pipelineName string) {
if _, ok := cw.pipelineMetrics[pipelineName]; !ok {
cw.pipelineMetrics[pipelineName] = &cloudWatchMetrics{}
}
cw.pipelineMetrics[pipelineName].Lock()
defer cw.pipelineMetrics[pipelineName].Unlock()
cw.pipelineMetrics[pipelineName].pipelines += float64(1)
}
func (cw *cloudWatchMonitoringService) incrEventReceived(pipelineName string) {
if _, ok := cw.pipelineMetrics[pipelineName]; !ok {
cw.pipelineMetrics[pipelineName] = &cloudWatchMetrics{}
}
cw.pipelineMetrics[pipelineName].Lock()
defer cw.pipelineMetrics[pipelineName].Unlock()
cw.pipelineMetrics[pipelineName].eventsReceived += float64(1)
}