mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +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>
46 lines
1.1 KiB
Go
46 lines
1.1 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/ws"
|
|
)
|
|
|
|
var _ ws.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc ws.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments adapter by tracking request count and latency.
|
|
func MetricsMiddleware(svc ws.Service, counter metrics.Counter, latency metrics.Histogram) ws.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 string, channel *ws.Channel) error {
|
|
return mm.svc.Subscribe(chanID, subtopic, channel)
|
|
}
|