-
Notifications
You must be signed in to change notification settings - Fork 19
/
http_metrics.go
87 lines (77 loc) · 2.29 KB
/
http_metrics.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
package main
import (
"fmt"
"github.com/espebra/filebin2/ds"
"io"
"net/http"
"time"
)
func (h *HTTP) viewMetrics(w http.ResponseWriter, r *http.Request) {
// If metrics are not enabled, exit early
if !h.config.Metrics {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// Check that authentication is enabled
if h.config.MetricsAuth != "" {
if h.config.MetricsAuth == "basic" {
username, password, ok := r.BasicAuth()
if ok == false || username != h.config.MetricsUsername || password != h.config.MetricsPassword {
time.Sleep(3 * time.Second)
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
} else {
// If an unknown authentication mechanism is specified,
// reject the request.
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
// Get metrics from the database
err := h.dao.Metrics().UpdateMetrics(h.metrics)
if err != nil {
fmt.Printf("Unable to UpdateMetrics(): %s\n", err.Error())
http.Error(w, "Errno 328", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
w.Header().Set("Cache-Control", "max-age=1")
type Data struct {
Metrics ds.Metrics
Proxy string
ProxyURL string
}
h.metrics.Lock()
defer h.metrics.Unlock()
data := Data{}
data.Metrics = *h.metrics
data.ProxyURL = h.config.MetricsProxyURL
// Fetch metrics from another URL
if h.config.MetricsProxyURL != "" {
// XXX: Add support for timeout
resp, err := http.Get(h.config.MetricsProxyURL)
if err != nil {
fmt.Printf("Metrics proxy: Unable to GET %s: %s\n", h.config.MetricsProxyURL, err.Error())
}
defer resp.Body.Close()
if err == nil {
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Metrics proxy: Unable to read body %s: %s\n", h.config.MetricsProxyURL, err.Error())
}
if resp.StatusCode == 200 {
data.Proxy = string(body[:])
} else {
fmt.Printf("Metrics proxy: Got status code %d\n", resp.StatusCode)
}
}
}
if err := h.templates.ExecuteTemplate(w, "metrics", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 302", http.StatusInternalServerError)
return
}
return
}