OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / api / metrics.go
1 package api
2
3 import (
4         "net/http"
5         "sync"
6         "time"
7
8         "github.com/vapor/metrics"
9 )
10
11 var (
12         latencyMu sync.Mutex
13         latencies = map[string]*metrics.RotatingLatency{}
14 )
15
16 // latency returns a rotating latency histogram for the given request.
17 func latency(tab *http.ServeMux, req *http.Request) *metrics.RotatingLatency {
18         latencyMu.Lock()
19         defer latencyMu.Unlock()
20         if l := latencies[req.URL.Path]; l != nil {
21                 return l
22         }
23         // Create a histogram only if the path is legit.
24         if _, pat := tab.Handler(req); pat == req.URL.Path {
25                 d := 100 * time.Millisecond
26                 l := metrics.NewRotatingLatency(5, d)
27                 latencies[req.URL.Path] = l
28                 metrics.PublishLatency(req.URL.Path, l)
29                 return l
30         }
31         return nil
32 }