This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
150 lines (127 loc) · 3 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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type ReceiverFunc func(key string, value float64)
func (receiver ReceiverFunc) Receive(key string, value float64) {
receiver(key, value)
}
type Receiver interface {
Receive(key string, value float64)
}
func WalkJSON(path string, jsonData interface{}, receiver Receiver) {
switch v := jsonData.(type) {
case int:
receiver.Receive(path, float64(v))
case float64:
receiver.Receive(path, v)
case bool:
n := 0.0
if v {
n = 1.0
}
receiver.Receive(path, n)
case string:
// ignore
case nil:
// ignore
case []interface{}:
prefix := path + "__"
for i, x := range v {
WalkJSON(fmt.Sprintf("%s%d", prefix, i), x, receiver)
}
case map[string]interface{}:
prefix := ""
if path != "" {
prefix = path + "::"
}
for k, x := range v {
WalkJSON(fmt.Sprintf("%s%s", prefix, k), x, receiver)
}
default:
log.Printf("unkown type: %#v", v)
}
}
func doProbe(client *http.Client, target string) (interface{}, error) {
resp, err := client.Get(target)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var jsonData interface{}
err = json.Unmarshal([]byte(bytes), &jsonData)
if err != nil {
return nil, err
}
return jsonData, nil
}
var httpClient *http.Client
func init() {
httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
func probeHandler(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
target := params.Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}
prefix := params.Get("prefix")
jsonData, err := doProbe(httpClient, target)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// log.Printf("Retrieved value %v", jsonData)
registry := prometheus.NewRegistry()
WalkJSON("", jsonData, ReceiverFunc(func(key string, value float64) {
g := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: prefix + key,
Help: "Retrieved value",
},
)
registry.MustRegister(g)
g.Set(value)
}))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
var indexHTML = []byte(`<html>
<head><title>Json Exporter</title></head>
<body>
<h1>Json Exporter</h1>
<p><a href="/probe">Run a probe</a></p>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`)
func main() {
addr := flag.String("listen-address", ":9116", "The address to listen on for HTTP requests.")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(indexHTML)
})
http.HandleFunc("/probe", probeHandler)
http.Handle("/metrics", promhttp.Handler())
log.Printf("listenning on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
}