mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* Add both an ID and an Email to API key requests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use return UserIdentity response Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace GetValue with GetEmail Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Refactor Mainflux Key Add `Subject` field and reorganize Key manipulation. **Remove backward compatibility** Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix service test Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix DB tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix JWT tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Uncomment and fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix SQL statements alignment Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Issue method docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Retrieve API and API docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
"github.com/mainflux/mainflux/authn"
|
|
)
|
|
|
|
var _ authn.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc authn.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments core service by tracking request count and
|
|
// latency.
|
|
func MetricsMiddleware(svc authn.Service, counter metrics.Counter, latency metrics.Histogram) authn.Service {
|
|
return &metricsMiddleware{
|
|
counter: counter,
|
|
latency: latency,
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Issue(ctx context.Context, token string, key authn.Key) (authn.Key, string, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "issue").Add(1)
|
|
ms.latency.With("method", "issue").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Issue(ctx, token, key)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Revoke(ctx context.Context, token, id string) error {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "revoke").Add(1)
|
|
ms.latency.With("method", "revoke").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Revoke(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Retrieve(ctx context.Context, token, id string) (authn.Key, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "retrieve").Add(1)
|
|
ms.latency.With("method", "retrieve").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Retrieve(ctx, token, id)
|
|
}
|
|
|
|
func (ms *metricsMiddleware) Identify(ctx context.Context, token string) (authn.Identity, error) {
|
|
defer func(begin time.Time) {
|
|
ms.counter.With("method", "identify").Add(1)
|
|
ms.latency.With("method", "identify").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return ms.svc.Identify(ctx, token)
|
|
}
|