This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (161 loc) · 3.88 KB
/
main.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 (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/goware/urlx"
"github.com/AnchorFree/prometheus-ha-proxy/merger"
"github.com/sirupsen/logrus"
)
func init() {
// Log as JSON instead of the default ASCII formatter.
logrus.SetFormatter(&logrus.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
logrus.SetOutput(os.Stdout)
// Only log the warning severity or above.
logrus.SetLevel(logrus.InfoLevel)
}
type BackendOutput struct {
Body []byte
Err error
}
type PromData struct {
Logger *logrus.Entry
EndPoints []Endpoint
}
type Endpoint struct {
URL *url.URL
Active bool
Changed time.Time
}
func (ep *Endpoint) Set(url *url.URL) bool {
ep.URL = url
ep.Active = true
ep.Changed = time.Now()
return true
}
func (ep *Endpoint) Disable() bool {
ep.Active = false
ep.Changed = time.Now()
return true
}
func (ep *Endpoint) Enable() bool {
ep.Active = true
ep.Changed = time.Now()
return true
}
func main() {
addresses := os.Args[1:]
logger := logrus.WithFields(logrus.Fields{
"app": "prometheus-ha-proxy",
})
var data PromData
var ep Endpoint
data.Logger = logger
for _, a := range addresses {
logger.Debug("Parsing: ", a)
my_url, err := urlx.Parse(a)
if err != nil {
// we could not do parse address url
logger.Warn("could not parse address due to", err)
continue
}
ep.Set(my_url)
data.EndPoints = append(data.EndPoints, ep)
}
go data.EndpointsProbe()
http.HandleFunc("/", data.PrometheusProxy) // set router
http.HandleFunc("/health", HealthcheckHandler)
err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func (p *PromData) EndpointsProbe() {
// we don't want to probe urls
logger := p.Logger
t, _ := time.ParseDuration("5m")
ticker := time.NewTicker(t)
m, _ := url.Parse("/metrics")
for range ticker.C {
// every t period, do
for i, ep := range p.EndPoints {
//url := ep.URL.ResolveReference(m).String()
url := ep.URL.ResolveReference(m)
_, err := http.Get(url.String())
switch {
case err != nil:
logger.Warning(ep.URL.String(), " is DOWN")
p.EndPoints[i].Disable()
case !ep.Active:
p.EndPoints[i].Enable()
logger.Debug("Enabled ", ep.URL.String())
}
}
}
}
func (p *PromData) PrometheusProxy(w http.ResponseWriter, r *http.Request) {
logger := p.Logger
logger.Info(r.URL.String())
var buffers []*[]byte
ch := make(chan BackendOutput)
if r.Method == "GET" {
var reqs int
for i, ep := range p.EndPoints {
if ep.Active {
logger.Debug("endpoint ", ep.URL.String(), " is active")
go PromGet(logger, &p.EndPoints[i], r.URL, ch)
reqs++
}
}
var cnt int
for out := range ch {
cnt++
if out.Err == nil {
buffer := out.Body
buffers = append(buffers, &buffer)
}
if cnt == reqs {
close(ch)
}
}
merged := new([]byte)
// who needs error logging anyways, right?
_ = merger.MergeNaively(merged, buffers...)
_, _ = w.Write(*merged)
}
}
func PromGet(logger *logrus.Entry, ep *Endpoint, r *url.URL, ch chan BackendOutput) {
var out BackendOutput
url := ep.URL.ResolveReference(r)
logger.Debug(url)
res, err := http.Get(url.String())
if err != nil {
logger.Warning("could not query ", url, "due to: ", err)
ep.Disable()
out.Err = err
ch <- out
return
}
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
out.Body, out.Err = ioutil.ReadAll(res.Body)
ch <- out
} else {
logger.Warning("Result code is not success: ", res.StatusCode)
}
return
}
// HealthcheckHandler /health
// just returns 200 '{ "ok": true }'
func HealthcheckHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if _, err := fmt.Fprintf(w, "{ \"ok\": true }"); err != nil {
fmt.Printf("Error in response writing: %#v", err)
}
}