2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-04-19 13:40:22 +02:00
|
|
|
// +build !test
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/metrics"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2017-09-23 01:03:27 +02:00
|
|
|
)
|
|
|
|
|
2018-05-15 17:13:09 +02:00
|
|
|
var _ things.Service = (*metricsMiddleware)(nil)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
type metricsMiddleware struct {
|
2017-09-23 01:03:27 +02:00
|
|
|
counter metrics.Counter
|
|
|
|
latency metrics.Histogram
|
2018-05-15 17:13:09 +02:00
|
|
|
svc things.Service
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
// MetricsMiddleware instruments core service by tracking request count and
|
2017-09-23 01:03:27 +02:00
|
|
|
// latency.
|
2018-05-15 17:13:09 +02:00
|
|
|
func MetricsMiddleware(svc things.Service, counter metrics.Counter, latency metrics.Histogram) things.Service {
|
2018-03-11 18:06:01 +01:00
|
|
|
return &metricsMiddleware{
|
2017-09-23 01:03:27 +02:00
|
|
|
counter: counter,
|
|
|
|
latency: latency,
|
2018-03-11 18:06:01 +01:00
|
|
|
svc: svc,
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) AddThing(token string, thing things.Thing) (things.Thing, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-05-15 17:13:09 +02:00
|
|
|
ms.counter.With("method", "add_thing").Add(1)
|
|
|
|
ms.latency.With("method", "add_thing").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.AddThing(token, thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateThing(token string, thing things.Thing) error {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-05-15 17:13:09 +02:00
|
|
|
ms.counter.With("method", "update_thing").Add(1)
|
|
|
|
ms.latency.With("method", "update_thing").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.UpdateThing(token, thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateKey(token, id, key string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "update_key").Add(1)
|
|
|
|
ms.latency.With("method", "update_key").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return ms.svc.UpdateKey(token, id, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *metricsMiddleware) ViewThing(token, id string) (things.Thing, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-05-15 17:13:09 +02:00
|
|
|
ms.counter.With("method", "view_thing").Add(1)
|
|
|
|
ms.latency.With("method", "view_thing").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.ViewThing(token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 15:33:49 +02:00
|
|
|
func (ms *metricsMiddleware) ListThings(token string, offset, limit uint64, name string) (things.ThingsPage, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-05-15 17:13:09 +02:00
|
|
|
ms.counter.With("method", "list_things").Add(1)
|
|
|
|
ms.latency.With("method", "list_things").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-05-30 15:33:49 +02:00
|
|
|
return ms.svc.ListThings(token, offset, limit, name)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) ListThingsByChannel(token, id string, offset, limit uint64) (things.ThingsPage, error) {
|
2019-01-08 11:53:24 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "list_things_by_channel").Add(1)
|
|
|
|
ms.latency.With("method", "list_things_by_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.ListThingsByChannel(token, id, offset, limit)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) RemoveThing(token, id string) error {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2018-05-15 17:13:09 +02:00
|
|
|
ms.counter.With("method", "remove_thing").Add(1)
|
|
|
|
ms.latency.With("method", "remove_thing").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.RemoveThing(token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) CreateChannel(token string, channel things.Channel) (things.Channel, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "create_channel").Add(1)
|
|
|
|
ms.latency.With("method", "create_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.CreateChannel(token, channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateChannel(token string, channel things.Channel) error {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "update_channel").Add(1)
|
|
|
|
ms.latency.With("method", "update_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.UpdateChannel(token, channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) ViewChannel(token, id string) (things.Channel, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "view_channel").Add(1)
|
|
|
|
ms.latency.With("method", "view_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.ViewChannel(token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 10:37:25 +02:00
|
|
|
func (ms *metricsMiddleware) ListChannels(token string, offset, limit uint64, name string) (things.ChannelsPage, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "list_channels").Add(1)
|
|
|
|
ms.latency.With("method", "list_channels").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-06-11 10:37:25 +02:00
|
|
|
return ms.svc.ListChannels(token, offset, limit, name)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) ListChannelsByThing(token, id string, offset, limit uint64) (things.ChannelsPage, error) {
|
2019-01-08 11:53:24 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "list_channels_by_thing").Add(1)
|
|
|
|
ms.latency.With("method", "list_channels_by_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.ListChannelsByThing(token, id, offset, limit)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) RemoveChannel(token, id string) error {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "remove_channel").Add(1)
|
|
|
|
ms.latency.With("method", "remove_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.RemoveChannel(token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) Connect(token, chanID, thingID string) error {
|
2018-03-11 18:06:01 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "connect").Add(1)
|
|
|
|
ms.latency.With("method", "connect").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.Connect(token, chanID, thingID)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
func (ms *metricsMiddleware) Disconnect(token, chanID, thingID string) error {
|
2018-03-11 18:06:01 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "disconnect").Add(1)
|
|
|
|
ms.latency.With("method", "disconnect").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-04-25 14:37:51 +02:00
|
|
|
return ms.svc.Disconnect(token, chanID, thingID)
|
2017-12-29 10:47:43 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
func (ms *metricsMiddleware) CanAccess(id, key string) (string, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "can_access").Add(1)
|
|
|
|
ms.latency.With("method", "can_access").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
return ms.svc.CanAccess(id, key)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2019-07-15 18:28:15 +02:00
|
|
|
func (ms *metricsMiddleware) CanAccessByID(chanID, thingID string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "can_access_by_id").Add(1)
|
|
|
|
ms.latency.With("method", "can_access_by_id").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return ms.svc.CanAccessByID(chanID, thingID)
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:09:25 +01:00
|
|
|
func (ms *metricsMiddleware) Identify(key string) (string, error) {
|
2018-05-17 20:17:02 +02:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "identify").Add(1)
|
|
|
|
ms.latency.With("method", "identify").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return ms.svc.Identify(key)
|
|
|
|
}
|