/
promcloudwatch.go
164 lines (137 loc) · 4.22 KB
/
promcloudwatch.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
// Copyright © 2018 Dhananjay Balan <dhananjay@cliqz.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// prom_cldwatch_writer implements http server listner and a batch upload
// mechanism to cloudwatch. AWS credentials as read in aws-sdk-go, from either
// one of Environment Variable|awsconfig file|IAM role
package prom_cldwatch_writer
import (
"fmt"
"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/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/prompb"
"io/ioutil"
"net/http"
"os"
"time"
)
var (
debug = false
)
func debugPrint(msg string) {
if debug {
fmt.Fprintf(os.Stderr, "%s\n", msg)
}
}
// StartMetricExporter starts listening on serverAddr and will export metrics posted to Cloudwatch
// with namespace and region awsRegion
func StartMetricExporter(serverAddr, namespace, awsRegion string, verbose bool) error {
debug = verbose
conn := cloudwatch.New(session.New(), &aws.Config{Region: aws.String(awsRegion)})
tsQueue := make(chan *prompb.TimeSeries, 10)
go writeToCloudWatch(conn, tsQueue, namespace)
debugPrint("listening on: " + serverAddr)
return runHTTPServer(serverAddr, tsQueue)
}
func getMetricDatum(ts *prompb.TimeSeries) ([]*cloudwatch.MetricDatum, error) {
if (len(ts.Labels)) > 10 {
return nil, fmt.Errorf("cloudwatch only allow 10 dimensions. got: %v", ts.Labels)
}
m := make(model.Metric, len(ts.Labels))
for _, l := range ts.Labels {
m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
}
mName, ok := m[model.MetricNameLabel]
if !ok {
mName = "unnamed"
}
// get extra dimensions
dims := []*cloudwatch.Dimension{}
for label, value := range m {
if label != model.MetricNameLabel {
d := &cloudwatch.Dimension{}
d.SetName(fmt.Sprint(label))
d.SetValue(fmt.Sprint(value))
dims = append(dims, d)
}
}
datumList := []*cloudwatch.MetricDatum{}
for _, sample := range ts.Samples {
datum := &cloudwatch.MetricDatum{}
datum.SetMetricName(fmt.Sprint(mName))
datum.SetDimensions(dims)
datum.SetTimestamp(time.Unix(0, sample.Timestamp*1e6))
datum.SetValue(sample.Value)
datumList = append(datumList, datum)
}
return datumList, nil
}
func writeToCloudWatch(conn *cloudwatch.CloudWatch, tsQueue <-chan *prompb.TimeSeries, namespace string) {
ticker := time.NewTicker(5 * time.Second)
datums := []*cloudwatch.MetricDatum{}
for {
select {
case ts := <-tsQueue:
datumList, err := getMetricDatum(ts)
if err != nil {
fmt.Fprintf(os.Stderr, "error converting metrics: %v \n", err)
break
}
if len(datumList) <= 0 {
fmt.Fprintf(os.Stderr, "error: emtpy datum!")
break
}
for _, d := range datumList {
datums = append(datums, d)
}
case <-ticker.C:
if len(datums) == 0 {
break
}
debugPrint(fmt.Sprintf("sending %d datapoints to cw", len(datums)))
metricData := &cloudwatch.PutMetricDataInput{
Namespace: &namespace,
MetricData: datums,
}
debugPrint(fmt.Sprintf("writing to cw: %v\n", metricData))
_, err := conn.PutMetricData(metricData)
if err != nil {
fmt.Fprintf(os.Stderr, "error: writing to cw: %v\n", err)
}
// reset datum to send
datums = []*cloudwatch.MetricDatum{}
}
}
}
func runHTTPServer(addr string, tsQueue chan<- *prompb.TimeSeries) error {
http.HandleFunc("/receive", func(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
debugPrint("request failed: " + err.Error())
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
debugPrint("request failed: " + err.Error())
return
}
var req prompb.WriteRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
debugPrint("unmarshal failed: " + err.Error())
return
}
for _, ts := range req.Timeseries {
tsQueue <- ts
}
})
return http.ListenAndServe(addr, nil)
}