-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathdebug.go
101 lines (83 loc) · 2.51 KB
/
debug.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
/*
Copyright 2016-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package rest
import (
"expvar"
"net/http"
"strings"
"sync"
"time"
_ "github.com/couchbase/gomemcached/debug"
"github.com/couchbase/sync_gateway/base"
"github.com/samuel/go-metrics/metrics"
)
const (
kDebugURLPathPrefix = "/_expvar"
)
var (
poolhistos = map[string]metrics.Histogram{}
opshistos = map[string]metrics.Histogram{}
histosMu = sync.Mutex{}
expPoolHistos *expvar.Map
expOpsHistos *expvar.Map
)
func init() {
expCb := expvar.NewMap("cb")
expPoolHistos = &expvar.Map{}
expPoolHistos.Init()
expCb.Set("pools", expPoolHistos)
expOpsHistos = &expvar.Map{}
expOpsHistos.Init()
expCb.Set("ops", expOpsHistos)
}
func connPoolHisto(name string) metrics.Histogram {
histosMu.Lock()
defer histosMu.Unlock()
rv, ok := poolhistos[name]
if !ok {
rv = metrics.NewBiasedHistogram()
poolhistos[name] = rv
expPoolHistos.Set(name, &metrics.HistogramExport{
Histogram: rv,
Percentiles: []float64{0.25, 0.5, 0.75, 0.90, 0.99},
PercentileNames: []string{"p25", "p50", "p75", "p90", "p99"}})
}
return rv
}
func recordConnPoolStat(host string, source string, start time.Time, err error) {
duration := time.Since(start)
histo := connPoolHisto(host)
histo.Update(int64(duration))
}
func clientCBHisto(name string) metrics.Histogram {
histosMu.Lock()
defer histosMu.Unlock()
rv, ok := opshistos[name]
if !ok {
rv = metrics.NewBiasedHistogram()
opshistos[name] = rv
expOpsHistos.Set(name, &metrics.HistogramExport{
Histogram: rv,
Percentiles: []float64{0.25, 0.5, 0.75, 0.90, 0.99},
PercentileNames: []string{"p25", "p50", "p75", "p90", "p99"}})
}
return rv
}
func recordCBClientStat(opname, k string, start time.Time, err error) {
duration := time.Since(start)
histo := clientCBHisto(opname)
histo.Update(int64(duration))
}
func (h *handler) handleExpvar() error {
base.DebugfCtx(h.ctx(), base.KeyHTTP, "Recording snapshot of current debug variables.")
h.rq.URL.Path = strings.Replace(h.rq.URL.Path, kDebugURLPathPrefix, "/debug/vars", 1)
http.DefaultServeMux.ServeHTTP(h.response, h.rq)
base.Audit(h.ctx(), base.AuditIDSyncGatewayStats, base.AuditFields{base.AuditFieldStatsFormat: "expvar"})
return nil
}