1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/ws/api/metrics.go
b1ackd0t ada5813f47
MF-1455 - Update Versions of Protobuf (#1704)
* initial commit
* add protoc-gen-gofast
* update generated files
* fix linting
* fix consumers error on message conversion
* fix copying values on transformers
* initial commit
* initial commit
* add protoc-gen-gofast
* update generated files
* fix linting
* fix consumers error on message conversion
* fix copying values on transformers
* embedded for forward compatible.
* remove gogo
* embedded for forward compatible.
* update protoc compiler
* fix linting
* remove hex comment

Signed-off-by: rodneyosodo <socials@rodneyosodo.com>
2023-02-02 18:28:32 +01:00

60 lines
1.6 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
//go:build !test
package api
import (
"context"
"time"
"github.com/go-kit/kit/metrics"
"github.com/mainflux/mainflux/pkg/messaging"
"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, thingKey string, msg *messaging.Message) 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, thingKey, msg)
}
func (mm *metricsMiddleware) Subscribe(ctx context.Context, thingKey, chanID, subtopic string, c *ws.Client) 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(ctx, thingKey, chanID, subtopic, c)
}
func (mm *metricsMiddleware) Unsubscribe(ctx context.Context, thingKey, chanID, subtopic string) error {
defer func(begin time.Time) {
mm.counter.With("method", "unsubscribe").Add(1)
mm.latency.With("method", "unsubscribe").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.Unsubscribe(ctx, thingKey, chanID, subtopic)
}