-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
58 lines (45 loc) · 1.52 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
55
56
57
58
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// +build !test
package api
import (
"context"
"time"
"github.com/drasko/gui/gui"
"github.com/go-kit/kit/metrics"
)
var _ gui.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc gui.Service
}
// MetricsMiddleware instruments adapter by tracking request count and latency.
func MetricsMiddleware(svc gui.Service, counter metrics.Counter, latency metrics.Histogram) gui.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (mm *metricsMiddleware) Index(ctx context.Context, token string) (td gui.TemplateData, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "index").Add(1)
mm.latency.With("method", "index").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.Index(ctx, token)
}
func (mm *metricsMiddleware) Things(ctx context.Context, token string) (td gui.TemplateData, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "things").Add(1)
mm.latency.With("method", "things").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.Things(ctx, token)
}
func (mm *metricsMiddleware) Channels(ctx context.Context, token string) (td gui.TemplateData, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "channels").Add(1)
mm.latency.With("method", "channels").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.Channels(ctx, token)
}