mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* Update copyright comment for go files Signed-off-by: nwneisen <nwneisen@gmail.com> * Update copyright in assortment of file types Signed-off-by: nwneisen <nwneisen@gmail.com> * Remove missed copyright date Signed-off-by: nwneisen <nwneisen@gmail.com>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// +build !test
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/coap"
|
|
)
|
|
|
|
var _ coap.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc coap.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments adapter by tracking request count and latency.
|
|
func MetricsMiddleware(svc coap.Service, counter metrics.Counter, latency metrics.Histogram) coap.Service {
|
|
return &metricsMiddleware{
|
|
counter: counter,
|
|
latency: latency,
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
func (mm *metricsMiddleware) Publish(ctx context.Context, token string, msg mainflux.RawMessage) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "publish").Add(1)
|
|
mm.latency.With("method", "publish").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.Publish(ctx, token, msg)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) Subscribe(chanID, subtopic, clientID string, o *coap.Observer) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "subscribe").Add(1)
|
|
mm.latency.With("method", "subscribe").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.Subscribe(chanID, subtopic, clientID, o)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) Unsubscribe(clientID string) {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "unsubscribe").Add(1)
|
|
mm.latency.With("method", "unsubscribe").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
mm.svc.Unsubscribe(clientID)
|
|
}
|