2017-09-23 07:55:29 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
|
|
"github.com/mainflux/mainflux/http"
|
|
|
|
"github.com/mainflux/mainflux/writer"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ http.Service = (*metricService)(nil)
|
|
|
|
|
|
|
|
type metricService struct {
|
|
|
|
counter metrics.Counter
|
|
|
|
latency metrics.Histogram
|
|
|
|
http.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetricService instruments adapter by tracking request count and latency.
|
|
|
|
func NewMetricService(counter metrics.Counter, latency metrics.Histogram, s http.Service) http.Service {
|
|
|
|
return &metricService{
|
|
|
|
counter: counter,
|
|
|
|
latency: latency,
|
|
|
|
Service: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:03:24 +08:00
|
|
|
func (ms *metricService) Publish(msg writer.RawMessage) error {
|
2017-09-23 07:55:29 +08:00
|
|
|
defer func(begin time.Time) {
|
2017-10-07 05:03:24 +08:00
|
|
|
ms.counter.With("method", "publish").Add(1)
|
|
|
|
ms.latency.With("method", "publish").Observe(time.Since(begin).Seconds())
|
2017-09-23 07:55:29 +08:00
|
|
|
}(time.Now())
|
|
|
|
|
2017-10-07 05:03:24 +08:00
|
|
|
return ms.Service.Publish(msg)
|
2017-09-23 07:55:29 +08:00
|
|
|
}
|