-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (110 loc) · 4.1 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
package main
import (
"encoding/json"
"net/http"
"log"
"bytes"
"io"
"fmt"
"flag"
)
type MetricsObject struct {
Cpu float64 `json:"cpu"`
Date int64 `json:"date"`
Error int `json:"error"`
Memory float64 `json:"memory"`
Queued int `json:"queued"`
Rejected int `json:"rejected"`
Successful int `json:"successful"`
Timedout int `json:"timedout"`
}
type ExporterConfig struct {
Prefix *string
BrowserlessHost *string
BrowserlessPort *int
ExporterHost *string
ExporterPort *int
}
var (
lastMetricsDate int64
lastPrometheusMetricsReport string
config ExporterConfig
browserlessAddress string
)
func prefix() string {
if len(*config.Prefix) > 0 {
return *config.Prefix + "_"
}
return ""
}
func buildPrometheusMetricsReport(metricsObject MetricsObject, count int) string {
report := ""
report += fmt.Sprintf("%sbrowserless_cpu_sum %4.2f\n", prefix(), metricsObject.Cpu)
report += fmt.Sprintf("%sbrowserless_cpu_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_memory_sum %4.2f\n", prefix(), metricsObject.Memory)
report += fmt.Sprintf("%sbrowserless_memory_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_successful_sum %d\n", prefix(), metricsObject.Successful)
report += fmt.Sprintf("%sbrowserless_successful_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_queued_sum %d\n", prefix(), metricsObject.Queued)
report += fmt.Sprintf("%sbrowserless_queued_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_rejected_sum %d\n", prefix(), metricsObject.Rejected)
report += fmt.Sprintf("%sbrowserless_rejected_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_timedout_sum %d\n", prefix(), metricsObject.Timedout)
report += fmt.Sprintf("%sbrowserless_timedout_count %d\n", prefix(), count)
report += fmt.Sprintf("%sbrowserless_error_sum %d\n", prefix(), metricsObject.Error)
report += fmt.Sprintf("%sbrowserless_error_count %d\n", prefix(), count)
return report
}
func handleMetrics(w http.ResponseWriter, r *http.Request) {
res, err := http.Get(fmt.Sprintf("http://%s/metrics", browserlessAddress))
if err != nil {
log.Fatal(err)
w.Write(make([]byte, 0))
return
}
var buffer bytes.Buffer
io.Copy(&buffer, res.Body)
metricsObjects := make([]MetricsObject, 0)
json.Unmarshal(buffer.Bytes(), &metricsObjects)
lastMetricsObject := metricsObjects[len(metricsObjects) - 1]
if lastMetricsObject.Date != lastMetricsDate {
var sumMetricsObject MetricsObject
for _, metricsObject := range metricsObjects {
sumMetricsObject.Cpu += metricsObject.Cpu
sumMetricsObject.Error += metricsObject.Error
sumMetricsObject.Memory += metricsObject.Memory
sumMetricsObject.Queued += metricsObject.Queued
sumMetricsObject.Rejected += metricsObject.Rejected
sumMetricsObject.Successful += metricsObject.Successful
sumMetricsObject.Timedout += metricsObject.Timedout
}
lastPrometheusMetricsReport = buildPrometheusMetricsReport(sumMetricsObject, len(metricsObjects))
}
w.Write([]byte(lastPrometheusMetricsReport))
}
func getBrowserlessAddress(config ExporterConfig) string {
host := *config.BrowserlessHost
port := *config.BrowserlessPort
return fmt.Sprintf("%s:%d", host, port)
}
func getExporterAddress(config ExporterConfig) string {
host := *config.ExporterHost
port := *config.ExporterPort
return fmt.Sprintf("%s:%d", host, port)
}
func main() {
lastMetricsDate = 0
lastPrometheusMetricsReport = ""
config.Prefix = flag.String("prefix", "", "Prefix for metrics names")
config.BrowserlessHost = flag.String("browserless.host", "localhost", "Browserless host")
config.BrowserlessPort = flag.Int("browserless.port", 3000, "Browserless port")
config.ExporterHost = flag.String("exporter.host", "localhost", "Exporter host")
config.ExporterPort = flag.Int("exporter.port", 3002, "Exporter port")
flag.Parse()
browserlessAddress = getBrowserlessAddress(config)
log.Printf("Browserless address %s\n", browserlessAddress)
exporterAddress := getExporterAddress(config)
log.Printf("Starting exporter on %s\n", exporterAddress)
http.HandleFunc("/metrics", handleMetrics)
http.ListenAndServe(exporterAddress, nil)
}