149 lines
4.9 KiB
Go
149 lines
4.9 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/users"
|
|
)
|
|
|
|
var _ users.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc users.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments core service by tracking request count and latency.
|
|
func MetricsMiddleware(svc users.Service, counter metrics.Counter, latency metrics.Histogram) users.Service {
|
|
return &metricsMiddleware{
|
|
counter: counter,
|
|
latency: latency,
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Register(ctx context.Context, token string, user users.User) (string, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "register").Add(1)
|
|
ms.latency.With("method", "register").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Register(ctx, token, user)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Login(ctx context.Context, user users.User) (string, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "login").Add(1)
|
|
ms.latency.With("method", "login").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Login(ctx, user)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ViewUser(ctx context.Context, token, id string) (users.User, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "view_user").Add(1)
|
|
ms.latency.With("method", "view_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ViewUser(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ViewProfile(ctx context.Context, token string) (users.User, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "view_profile").Add(1)
|
|
ms.latency.With("method", "view_profile").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ViewProfile(ctx, token)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ListUsers(ctx context.Context, token string, pm users.PageMetadata) (users.UserPage, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "list_users").Add(1)
|
|
ms.latency.With("method", "list_users").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ListUsers(ctx, token, pm)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) UpdateUser(ctx context.Context, token string, u users.User) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "update_user").Add(1)
|
|
ms.latency.With("method", "update_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.UpdateUser(ctx, token, u)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) GenerateResetToken(ctx context.Context, email, host string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "generate_reset_token").Add(1)
|
|
ms.latency.With("method", "generate_reset_token").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.GenerateResetToken(ctx, email, host)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ChangePassword(ctx context.Context, email, password, oldPassword string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "change_password").Add(1)
|
|
ms.latency.With("method", "change_password").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ChangePassword(ctx, email, password, oldPassword)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ResetPassword(ctx context.Context, email, password string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "reset_password").Add(1)
|
|
ms.latency.With("method", "reset_password").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ResetPassword(ctx, email, password)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) SendPasswordReset(ctx context.Context, host, email, token string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "send_password_reset").Add(1)
|
|
ms.latency.With("method", "send_password_reset").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.SendPasswordReset(ctx, host, email, token)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) ListMembers(ctx context.Context, token, groupID string, pm users.PageMetadata) (users.UserPage, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "list_members").Add(1)
|
|
ms.latency.With("method", "list_members").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.ListMembers(ctx, token, groupID, pm)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) EnableUser(ctx context.Context, token string, id string) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "enable_user").Add(1)
|
|
ms.latency.With("method", "enable_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.EnableUser(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) DisableUser(ctx context.Context, token string, id string) (err error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "disable_user").Add(1)
|
|
ms.latency.With("method", "disable_user").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.DisableUser(ctx, token, id)
|
|
}
|