2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-12-05 03:08:30 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
|
|
"github.com/mainflux/mainflux/lora"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ lora.Service = (*metricsMiddleware)(nil)
|
|
|
|
|
|
|
|
type metricsMiddleware struct {
|
|
|
|
counter metrics.Counter
|
|
|
|
latency metrics.Histogram
|
|
|
|
svc lora.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricsMiddleware instruments core service by tracking request count and latency.
|
|
|
|
func MetricsMiddleware(svc lora.Service, counter metrics.Counter, latency metrics.Histogram) lora.Service {
|
|
|
|
return &metricsMiddleware{
|
|
|
|
counter: counter,
|
|
|
|
latency: latency,
|
|
|
|
svc: svc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) CreateThing(thingID string, loraDevEUI string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "create_thing").Add(1)
|
|
|
|
mm.latency.With("method", "create_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.CreateThing(thingID, loraDevEUI)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) UpdateThing(thingID string, loraDevEUI string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "update_thing").Add(1)
|
|
|
|
mm.latency.With("method", "update_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.UpdateThing(thingID, loraDevEUI)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) RemoveThing(thingID string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "remove_thing").Add(1)
|
|
|
|
mm.latency.With("method", "remove_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.RemoveThing(thingID)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) CreateChannel(chanID string, loraApp string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "create_channel").Add(1)
|
|
|
|
mm.latency.With("method", "create_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.CreateChannel(chanID, loraApp)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) UpdateChannel(chanID string, loraApp string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "update_channel").Add(1)
|
|
|
|
mm.latency.With("method", "update_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.UpdateChannel(chanID, loraApp)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) RemoveChannel(chanID string) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "remove_channel").Add(1)
|
|
|
|
mm.latency.With("method", "remove_channel").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.RemoveChannel(chanID)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
func (mm *metricsMiddleware) ConnectThing(chanID, thingID string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "connect_thing").Add(1)
|
|
|
|
mm.latency.With("method", "connect_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return mm.svc.ConnectThing(chanID, thingID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *metricsMiddleware) DisconnectThing(chanID, thingID string) error {
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
mm.counter.With("method", "disconnect_thing").Add(1)
|
|
|
|
mm.latency.With("method", "disconnect_thing").Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
return mm.svc.DisconnectThing(chanID, thingID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *metricsMiddleware) Publish(m lora.Message) error {
|
2018-12-05 03:08:30 +08:00
|
|
|
defer func(begin time.Time) {
|
2019-10-22 23:44:19 +08:00
|
|
|
mm.counter.With("method", "publish").Add(1)
|
|
|
|
mm.latency.With("method", "publish").Observe(time.Since(begin).Seconds())
|
2018-12-05 03:08:30 +08:00
|
|
|
}(time.Now())
|
|
|
|
|
2021-04-15 04:26:49 +08:00
|
|
|
return mm.svc.Publish(m)
|
2018-12-05 03:08:30 +08:00
|
|
|
}
|