-
Notifications
You must be signed in to change notification settings - Fork 69
/
srv.go
187 lines (156 loc) · 4.15 KB
/
srv.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
package main
import (
"io/ioutil"
"net/http"
"time"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/storage/remote"
"gopkg.in/tylerb/graceful.v1"
)
type p2cRequest struct {
name string
tags []string
val float64
ts time.Time
}
type p2cServer struct {
requests chan *p2cRequest
mux *http.ServeMux
conf *config
writer *p2cWriter
reader *p2cReader
rx prometheus.Counter
}
func NewP2CServer(conf *config) (*p2cServer, error) {
var err error
c := new(p2cServer)
c.requests = make(chan *p2cRequest, conf.ChanSize)
c.mux = http.NewServeMux()
c.conf = conf
c.writer, err = NewP2CWriter(conf, c.requests)
if err != nil {
fmt.Printf("Error creating clickhouse writer: %s\n", err.Error())
return c, err
}
c.reader, err = NewP2CReader(conf)
if err != nil {
fmt.Printf("Error creating clickhouse reader: %s\n", err.Error())
return c, err
}
c.rx = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "received_samples_total",
Help: "Total number of received samples.",
},
)
prometheus.MustRegister(c.rx)
c.mux.HandleFunc(c.conf.HTTPWritePath, func(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req remote.WriteRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
c.process(req)
})
c.mux.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var req remote.ReadRequest
if err := proto.Unmarshal(reqBuf, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var resp *remote.ReadResponse
resp, err = c.reader.Read(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := proto.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Content-Encoding", "snappy")
compressed = snappy.Encode(nil, data)
if _, err := w.Write(compressed); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
c.mux.Handle(c.conf.HTTPMetricsPath, prometheus.InstrumentHandler(
c.conf.HTTPMetricsPath, prometheus.UninstrumentedHandler(),
))
return c, nil
}
func (c *p2cServer) process(req remote.WriteRequest) {
for _, series := range req.Timeseries {
c.rx.Add(float64(len(series.Samples)))
var (
name string
tags []string
)
for _, label := range series.Labels {
if model.LabelName(label.Name) == model.MetricNameLabel {
name = label.Value
}
// store tags in <key>=<value> format
// allows for has(tags, "key=val") searches
// probably impossible/difficult to do regex searches on tags
t := fmt.Sprintf("%s=%s", label.Name, label.Value)
tags = append(tags, t)
}
for _, sample := range series.Samples {
p2c := new(p2cRequest)
p2c.name = name
p2c.ts = time.Unix(sample.TimestampMs/1000, 0)
p2c.val = sample.Value
p2c.tags = tags
c.requests <- p2c
}
}
}
func (c *p2cServer) Start() error {
fmt.Println("HTTP server starting...")
c.writer.Start()
return graceful.RunWithErr(c.conf.HTTPAddr, c.conf.HTTPTimeout, c.mux)
}
func (c *p2cServer) Shutdown() {
close(c.requests)
c.writer.Wait()
wchan := make(chan struct{})
go func() {
c.writer.Wait()
close(wchan)
}()
select {
case <-wchan:
fmt.Println("Writer shutdown cleanly..")
// All done!
case <-time.After(10 * time.Second):
fmt.Println("Writer shutdown timed out, samples will be lost..")
}
}