-
Notifications
You must be signed in to change notification settings - Fork 18
/
handler.go
122 lines (111 loc) · 2.68 KB
/
handler.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
package expvar2
import (
"bytes"
"encoding/json"
"expvar"
"fmt"
"github.com/signalfx/golib/log"
"io"
"net/http"
"os"
"strconv"
"strings"
)
// Handler can serve via HTTP expvar variables as well as custom variables inside
// Exported
type Handler struct {
Exported map[string]expvar.Var
Logger log.Logger
}
// New creates and returns a new handler
func New() *Handler {
e := Handler{
Exported: make(map[string]expvar.Var),
Logger: log.Discard,
}
return &e
}
// EnviromentalVariables returns an expvar that also shows env variables
func EnviromentalVariables() expvar.Var {
return enviromentalVariables(os.Environ)
}
func enviromentalVariables(osEnviron func() []string) expvar.Var {
return expvar.Func(func() interface{} {
ret := make(map[string]string)
for _, env := range osEnviron() {
parts := strings.SplitN(env, "=", 2)
if len(parts) <= 1 {
continue
}
ret[parts[0]] = parts[1]
}
return ret
})
}
var _ http.Handler = &Handler{}
type filterSet map[string]struct{}
func (s filterSet) shouldFilter(search string) bool {
if len(s) == 0 {
return false
}
_, exists := s[search]
return !exists
}
func (e *Handler) initialDump(w io.Writer, onlyFetch filterSet) {
_, err := fmt.Fprintf(w, "{")
log.IfErr(e.Logger, err)
first := true
usedKeys := map[string]struct{}{}
f := func(kv expvar.KeyValue) {
if _, exists := usedKeys[kv.Key]; exists {
return
}
if onlyFetch.shouldFilter(kv.Key) {
return
}
if !first {
_, err = fmt.Fprintf(w, ",")
log.IfErr(e.Logger, err)
}
first = false
_, err = fmt.Fprintf(w, "%q:%s", kv.Key, kv.Value)
log.IfErr(e.Logger, err)
}
for k, v := range e.Exported {
f(expvar.KeyValue{
Key: k,
Value: v,
})
usedKeys[k] = struct{}{}
}
expvar.Do(f)
_, err = fmt.Fprintf(w, "}")
log.IfErr(e.Logger, err)
}
func asSet(items []string) filterSet {
r := make(map[string]struct{}, len(items))
for _, item := range items {
if item != "" {
r[item] = struct{}{}
}
}
return r
}
// ServeHTTP is a copy/past of the private expvar.expvarHandler that I sometimes want to
// register to my own handler.
func (e *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
prettyPrint, _ := strconv.ParseBool(r.URL.Query().Get("pretty"))
onlyFetch := asSet(strings.Split(r.URL.Query().Get("filter"), ","))
if prettyPrint {
tmp := &bytes.Buffer{}
e.initialDump(tmp, onlyFetch)
buf := &bytes.Buffer{}
log.IfErr(log.Panic, json.Indent(buf, tmp.Bytes(), "", "\t"))
w.Header().Set("Content-Length", strconv.FormatInt(int64(buf.Len()), 10))
_, err := w.Write(buf.Bytes())
log.IfErr(e.Logger, err)
return
}
e.initialDump(w, onlyFetch)
}