9 "github.com/bytom/metrics"
10 "github.com/bytom/net/http/reqid"
15 latencies = map[string]*metrics.RotatingLatency{}
17 latencyRange = map[string]time.Duration{
18 crosscoreRPCPrefix + "get-block": 20 * time.Second,
19 crosscoreRPCPrefix + "signer/sign-block": 5 * time.Second,
20 crosscoreRPCPrefix + "get-snapshot": 30 * time.Second,
21 // the rest have a default range
25 // latency returns a rotating latency histogram for the given request.
26 func latency(tab *http.ServeMux, req *http.Request) *metrics.RotatingLatency {
28 defer latencyMu.Unlock()
29 if l := latencies[req.URL.Path]; l != nil {
32 // Create a histogram only if the path is legit.
33 if _, pat := tab.Handler(req); pat == req.URL.Path {
34 d, ok := latencyRange[req.URL.Path]
36 d = 100 * time.Millisecond
38 l := metrics.NewRotatingLatency(5, d)
39 latencies[req.URL.Path] = l
40 metrics.PublishLatency(req.URL.Path, l)
48 ncore = expvar.NewInt("ncore")
50 coresSeen map[string]bool
53 func coreCounter(h http.Handler) http.Handler {
54 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
55 countCore(reqid.CoreIDFromContext(req.Context()))
60 func countCore(id string) {
63 defer ncoreMu.Unlock()
64 if t.Sub(ncoreTime) > time.Minute {
65 ncore.Set(int64(len(coresSeen)))
67 coresSeen = make(map[string]bool)