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

* NOISSUE - Add admin method in users service to return users list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix loggings and metrics Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add email and metadata filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Retrieve User infos by ID if Admin Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Remove admin checks and fix comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix missing query Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use generic funccs to create email and metadata queries Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add /users/profile endpoint Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Simplify db helpers Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix View, List, Retrieve prefix methods naming Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix tracer endpoints naming Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add tests and remove TODO comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
111 lines
3.5 KiB
Go
111 lines
3.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package tracing contains middlewares that will add spans
|
|
// to existing traces.
|
|
package tracing
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mainflux/mainflux/users"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
const (
|
|
assignUser = "assign_user"
|
|
saveGroup = "save_group"
|
|
deleteGroup = "delete_group"
|
|
updateGroup = "update_group"
|
|
retrieveGroupByID = "retrieve_group_by_id"
|
|
retrieveAll = "retrieve_all_groups"
|
|
retrieveByName = "retrieve_by_name"
|
|
memberships = "memberships"
|
|
unassignUser = "unassign_user"
|
|
)
|
|
|
|
var _ users.GroupRepository = (*groupRepositoryMiddleware)(nil)
|
|
|
|
type groupRepositoryMiddleware struct {
|
|
tracer opentracing.Tracer
|
|
repo users.GroupRepository
|
|
}
|
|
|
|
// GroupRepositoryMiddleware tracks request and their latency, and adds spans to context.
|
|
func GroupRepositoryMiddleware(repo users.GroupRepository, tracer opentracing.Tracer) users.GroupRepository {
|
|
return groupRepositoryMiddleware{
|
|
tracer: tracer,
|
|
repo: repo,
|
|
}
|
|
}
|
|
func (grm groupRepositoryMiddleware) Save(ctx context.Context, group users.Group) (users.Group, error) {
|
|
span := createSpan(ctx, grm.tracer, saveGroup)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.Save(ctx, group)
|
|
}
|
|
func (grm groupRepositoryMiddleware) Update(ctx context.Context, group users.Group) error {
|
|
span := createSpan(ctx, grm.tracer, updateGroup)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.Update(ctx, group)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) Delete(ctx context.Context, groupID string) error {
|
|
span := createSpan(ctx, grm.tracer, deleteGroup)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.Delete(ctx, groupID)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) RetrieveByID(ctx context.Context, id string) (users.Group, error) {
|
|
span := createSpan(ctx, grm.tracer, retrieveGroupByID)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.RetrieveByID(ctx, id)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) RetrieveByName(ctx context.Context, name string) (users.Group, error) {
|
|
span := createSpan(ctx, grm.tracer, retrieveByName)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.RetrieveByName(ctx, name)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) RetrieveAllWithAncestors(ctx context.Context, groupID string, offset, limit uint64, um users.Metadata) (users.GroupPage, error) {
|
|
span := createSpan(ctx, grm.tracer, retrieveAll)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.RetrieveAllWithAncestors(ctx, groupID, offset, limit, um)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) RetrieveMemberships(ctx context.Context, userID string, offset, limit uint64, um users.Metadata) (users.GroupPage, error) {
|
|
span := createSpan(ctx, grm.tracer, memberships)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.RetrieveMemberships(ctx, userID, offset, limit, um)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) Unassign(ctx context.Context, userID, groupID string) error {
|
|
span := createSpan(ctx, grm.tracer, unassignUser)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.Unassign(ctx, userID, groupID)
|
|
}
|
|
|
|
func (grm groupRepositoryMiddleware) Assign(ctx context.Context, userID, groupID string) error {
|
|
span := createSpan(ctx, grm.tracer, assignUser)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return grm.repo.Assign(ctx, userID, groupID)
|
|
}
|