forked from Onyx-Protocol/Onyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugvars.go
78 lines (64 loc) · 1.28 KB
/
debugvars.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"sync"
"github.com/codahale/hdrhistogram"
)
type debugVars struct {
Raw *json.RawMessage
Latency map[string]struct {
Buckets []struct {
Over int
Max int64
Histogram hdrhistogram.Snapshot
}
}
}
var (
debugVarMu sync.Mutex
debugVarData = make(map[int]*debugVars)
debugVarNext int
)
func getDebugVars(i int) *debugVars {
debugVarMu.Lock()
defer debugVarMu.Unlock()
return debugVarData[i]
}
var errAuth = errors.New("need auth")
func fetchDebugVars(baseURL, user, pass string) (int, *debugVars, error) {
v := new(debugVars)
req, err := http.NewRequest("GET", strings.TrimRight(baseURL, "/")+"/debug/vars", nil)
if err != nil {
return 0, nil, err
}
if user != "" && pass != "" {
req.SetBasicAuth(user, pass)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
if resp.StatusCode == 401 {
return 0, nil, errAuth
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
err = json.Unmarshal(b, v)
if err != nil {
return 0, nil, err
}
v.Raw = (*json.RawMessage)(&b)
debugVarMu.Lock()
n := debugVarNext
debugVarNext++
debugVarData[n] = v
debugVarMu.Unlock()
return n, v, err
}