forked from albertito/prometheus-expvar-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (167 loc) · 4.51 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
188
189
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
"time"
"unicode"
"golang.org/x/exp/maps"
)
var (
configAddr = flag.String("addr", "127.0.0.1:8000", "Address to listen proxy requests, e.g. 0.0.0.0:8000.")
configTimeout = flag.Duration("timeout", 30*time.Second, "HTTP client timeout.")
)
func main() {
flag.Parse()
log.Printf("listen to %s in Proxy mode, timeout: %v", *configAddr, *configTimeout)
proxy := &Proxy{
Client: http.Client{
Timeout: *configTimeout,
},
}
if err := http.ListenAndServe(*configAddr, proxy); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal("ListenAndServe:", err)
}
}
type Proxy struct {
Client http.Client
}
func (p *Proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
log.Println(req.RemoteAddr, " ", req.Method, " ", req.URL)
metricMap, cerr := p.collect(req.URL)
if cerr != nil {
log.Println("failed to gather metrics: ", cerr)
if errors.Is(cerr, ErrTargetInaccessible) {
p.sendError(wr, http.StatusGatewayTimeout, cerr)
} else {
p.sendError(wr, http.StatusBadGateway, cerr)
}
return
}
metricNames := maps.Keys(metricMap)
sort.Strings(metricNames)
sb := &strings.Builder{}
for _, name := range metricNames {
sb.WriteString(fmt.Sprintf("%s %f\n", name, metricMap[name]))
}
wr.WriteHeader(http.StatusOK)
_, werr := wr.Write([]byte(sb.String()))
if werr != nil {
log.Println("failed to send metrics: ", werr)
}
}
func (p *Proxy) sendError(wr http.ResponseWriter, statusCode int, err error) {
wr.WriteHeader(statusCode)
_, herr := wr.Write([]byte(err.Error()))
if herr != nil {
log.Println("failed to send error: ", herr)
}
}
var ErrTargetInaccessible = errors.New("inaccessible target")
func (p *Proxy) collect(target *url.URL) (map[string]float64, error) {
resp, err := p.Client.Get(target.String())
if err != nil {
return nil, fmt.Errorf("%w; error scraping %q: %w", ErrTargetInaccessible, target, err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%w; error reading body of %q: %w", ErrTargetInaccessible, target, err)
}
// Replace "\xNN" with "?" because the default parser doesn't handle them
// well.
re := regexp.MustCompile(`\\x..`)
body = re.ReplaceAllFunc(body, func(s []byte) []byte {
return []byte("?")
})
var vs map[string]interface{}
err = json.Unmarshal(body, &vs)
if err != nil {
return nil, fmt.Errorf("error unmarshalling JSON from %q: %v", target, err)
}
mm := make(map[string]float64, 1000)
for k, v := range vs {
collectMetrics(mm, k, v)
}
return mm, nil
}
func collectMetrics(mm map[string]float64, k string, v interface{}) {
name := sanitizeMetricName(k)
switch v := v.(type) {
case float64:
mm[name] = v
case bool:
mm[name] = valToFloat(v)
case map[string]interface{}:
for lk, lv := range v {
collectMetrics(mm, k+"_"+lk, lv)
}
case string:
// Not supported by Prometheus.
return
case []interface{}:
// Not supported by Prometheus.
return
default:
fmt.Printf("Not supported unknown type: %q %#v\n", name, v)
return
}
}
func valToFloat(v interface{}) float64 {
switch v := v.(type) {
case float64:
return v
case bool:
if v {
return 1.0
}
return 0.0
}
panic(fmt.Sprintf("unexpected value type: %#v", v))
}
func sanitizeMetricName(n string) string {
// Prometheus metric names must match the regex
// `[a-zA-Z_:][a-zA-Z0-9_:]*`.
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
//
// This function replaces all non-matching ASCII characters with
// underscores.
//
// In particular, it is common that expvar names contain `/` or `-`, which
// we replace with `_` so they end up resembling more Prometheus-ideomatic
// names.
//
// Non-ascii characters are not supported, and will panic as so to force
// users to handle them explicitly. There is no good way to handle all of
// them automatically, as they can't be all reasonably mapped to ascii. In
// the future, we may handle _some_ of them automatically when possible.
// But for now, forcing the users to be explicit is the safest option, and
// also ensures forwards compatibility.
return strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' {
return r
}
if r >= 'A' && r <= 'Z' {
return r
}
if r >= '0' && r <= '9' {
return r
}
if r == '_' || r == ':' {
return r
}
if r > unicode.MaxASCII {
panic(fmt.Sprintf(
"non-ascii character %q is unsupported, please configure the metric %q explicitly",
r, n))
}
return '_'
}, n)
}