forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
54 lines (43 loc) · 1.25 KB
/
metrics.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
package servicecontext
import (
"context"
"net/http"
"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
"github.com/pydio/cells/common/service/metrics"
"github.com/uber-go/tally"
)
func NewMetricsWrapper(service micro.Service) {
var options []micro.Option
ctx := service.Options().Context
name := GetServiceName(ctx)
options = append(options, micro.WrapHandler(wrapperByName(name)))
service.Init(options...)
}
func wrapperByName(name string) server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
scope := metrics.GetMetricsForService(name)
if scope == tally.NoopScope {
return fn(ctx, req, rsp)
}
scope.Counter("grpc_calls").Inc(1)
tsw := scope.Timer("grpc_time").Start()
defer tsw.Stop()
return fn(ctx, req, rsp)
}
}
}
func NewMetricsHttpWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scope := metrics.GetMetricsForService(GetServiceName(r.Context()))
if scope == tally.NoopScope {
h.ServeHTTP(w, r)
return
}
scope.Counter("rest_calls").Inc(1)
tsw := scope.Timer("rest_time").Start()
defer tsw.Stop()
h.ServeHTTP(w, r)
})
}