-
Notifications
You must be signed in to change notification settings - Fork 13
/
metrics.go
60 lines (47 loc) · 1.69 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
59
60
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
//go:build !test
// +build !test
package api
import (
"context"
"time"
"github.com/MainfluxLabs/mainflux/webhooks"
"github.com/go-kit/kit/metrics"
)
var _ webhooks.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc webhooks.Service
}
// MetricsMiddleware instruments core service by tracking request count and
// latency.
func MetricsMiddleware(svc webhooks.Service, counter metrics.Counter, latency metrics.Histogram) webhooks.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (ms *metricsMiddleware) CreateWebhooks(ctx context.Context, token string, webhooks ...webhooks.Webhook) (response []webhooks.Webhook, err error) {
defer func(begin time.Time) {
ms.counter.With("method", "create_webhooks").Add(1)
ms.latency.With("method", "create_webhooks").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.CreateWebhooks(ctx, token, webhooks...)
}
func (ms *metricsMiddleware) ListWebhooksByThing(ctx context.Context, token string, thingID string) ([]webhooks.Webhook, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_webhooks_by_thing").Add(1)
ms.latency.With("method", "list_webhooks_by_thing").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.ListWebhooksByThing(ctx, token, thingID)
}
func (ms *metricsMiddleware) Consume(message interface{}) error {
defer func(begin time.Time) {
ms.counter.With("method", "consume").Add(1)
ms.latency.With("method", "consume").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.Consume(message)
}