// 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, } } // Publish instruments Publish method with metrics. 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) } // Subscribe instruments Subscribe method with metrics. 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) } // Unsubscribe instruments Unsubscribe method with metrics. 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) }