2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-01-24 21:18:53 +01:00
|
|
|
//go:build !test
|
2018-04-19 13:40:22 +02:00
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2017-09-23 01:03:27 +02:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2020-12-29 23:02:35 +01:00
|
|
|
// MetricsMiddleware instruments core service by tracking request count and 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-11-04 13:14:17 -07:00
|
|
|
func (ms *metricsMiddleware) CreateThings(ctx context.Context, token string, ths ...things.Thing) (saved []things.Thing, err error) {
|
2019-10-29 05:59:54 -06:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "create_things").Add(1)
|
|
|
|
ms.latency.With("method", "create_things").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
return ms.svc.CreateThings(ctx, token, ths...)
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateThing(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.UpdateThing(ctx, token, thing)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:38:28 +02:00
|
|
|
func (ms *metricsMiddleware) ShareThing(ctx context.Context, token, thingID string, actions, userIDs []string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "share_thing").Add(1)
|
|
|
|
ms.latency.With("method", "share_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return ms.svc.ShareThing(ctx, token, thingID, actions, userIDs)
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateKey(ctx context.Context, token, id, key string) error {
|
2019-04-25 14:37:51 +02:00
|
|
|
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())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return ms.svc.UpdateKey(ctx, token, id, key)
|
2019-04-25 14:37:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) ViewThing(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.ViewThing(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (ms *metricsMiddleware) ListThings(ctx context.Context, token string, pm things.PageMetadata) (things.Page, 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())
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
return ms.svc.ListThings(ctx, token, pm)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (ms *metricsMiddleware) ListThingsByChannel(ctx context.Context, token, chID string, pm things.PageMetadata) (things.Page, 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())
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
return ms.svc.ListThingsByChannel(ctx, token, chID, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) RemoveThing(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.RemoveThing(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
func (ms *metricsMiddleware) CreateChannels(ctx context.Context, token string, channels ...things.Channel) (saved []things.Channel, err error) {
|
2019-10-29 05:59:54 -06:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "create_channels").Add(1)
|
|
|
|
ms.latency.With("method", "create_channels").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2019-11-04 13:14:17 -07:00
|
|
|
return ms.svc.CreateChannels(ctx, token, channels...)
|
2019-10-29 05:59:54 -06:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) UpdateChannel(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.UpdateChannel(ctx, token, channel)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) ViewChannel(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.ViewChannel(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
func (ms *metricsMiddleware) ListChannels(ctx context.Context, token string, pm things.PageMetadata) (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())
|
|
|
|
|
2020-12-08 21:30:47 +01:00
|
|
|
return ms.svc.ListChannels(ctx, token, pm)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
func (ms *metricsMiddleware) ListChannelsByThing(ctx context.Context, token, thID string, pm things.PageMetadata) (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())
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
return ms.svc.ListChannelsByThing(ctx, token, thID, pm)
|
2019-01-08 11:53:24 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) RemoveChannel(ctx context.Context, 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-07-18 15:01:09 +02:00
|
|
|
return ms.svc.RemoveChannel(ctx, token, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:44:31 -07:00
|
|
|
func (ms *metricsMiddleware) Connect(ctx context.Context, token string, chIDs, thIDs []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-11-15 10:44:31 -07:00
|
|
|
return ms.svc.Connect(ctx, token, chIDs, thIDs)
|
2018-03-11 18:06:01 +01:00
|
|
|
}
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
func (ms *metricsMiddleware) Disconnect(ctx context.Context, token string, chIDs, thIDs []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())
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
return ms.svc.Disconnect(ctx, token, chIDs, thIDs)
|
2017-12-29 10:47:43 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
func (ms *metricsMiddleware) CanAccessByKey(ctx context.Context, id, key string) (string, error) {
|
2017-09-23 01:03:27 +02:00
|
|
|
defer func(begin time.Time) {
|
2019-10-21 15:24:45 -06:00
|
|
|
ms.counter.With("method", "can_access_by_key").Add(1)
|
|
|
|
ms.latency.With("method", "can_access_by_key").Observe(time.Since(begin).Seconds())
|
2017-09-23 01:03:27 +02:00
|
|
|
}(time.Now())
|
|
|
|
|
2019-10-21 15:24:45 -06:00
|
|
|
return ms.svc.CanAccessByKey(ctx, id, key)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
2018-05-17 20:17:02 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) CanAccessByID(ctx context.Context, chanID, thingID string) error {
|
2019-07-15 18:28:15 +02:00
|
|
|
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())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return ms.svc.CanAccessByID(ctx, chanID, thingID)
|
2019-07-15 18:28:15 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:41:59 +01:00
|
|
|
func (ms *metricsMiddleware) IsChannelOwner(ctx context.Context, owner, chanID string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "is_channel_owner").Add(1)
|
|
|
|
ms.latency.With("method", "is_channel_owner").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-02-24 13:23:50 +01:00
|
|
|
return ms.svc.IsChannelOwner(ctx, owner, chanID)
|
2021-02-22 19:41:59 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (ms *metricsMiddleware) Identify(ctx context.Context, 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())
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return ms.svc.Identify(ctx, key)
|
2018-05-17 20:17:02 +02:00
|
|
|
}
|
2020-11-23 11:34:29 +01:00
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (ms *metricsMiddleware) ListMembers(ctx context.Context, token, groupID string, pm things.PageMetadata) (tp things.Page, err error) {
|
2020-11-23 11:34:29 +01:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
ms.counter.With("method", "list_members").Add(1)
|
|
|
|
ms.latency.With("method", "list_members").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-07-10 01:59:12 +03:00
|
|
|
return ms.svc.ListMembers(ctx, token, groupID, pm)
|
2020-11-23 11:34:29 +01:00
|
|
|
}
|