2020-12-29 23:02:35 +01:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package tracing contains middlewares that will add spans to existing traces.
|
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
"github.com/mainflux/mainflux/auth"
|
2020-12-29 23:02:35 +01:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-04 10:29:03 +01:00
|
|
|
assign = "assign"
|
|
|
|
saveGroup = "save_group"
|
|
|
|
deleteGroup = "delete_group"
|
|
|
|
updateGroup = "update_group"
|
|
|
|
retrieveByID = "retrieve_by_id"
|
|
|
|
retrieveAllParents = "retrieve_all_parents"
|
|
|
|
retrieveAllChildren = "retrieve_all_children"
|
|
|
|
retrieveAll = "retrieve_all_groups"
|
|
|
|
memberships = "memberships"
|
|
|
|
members = "members"
|
|
|
|
unassign = "unassign"
|
2020-12-29 23:02:35 +01:00
|
|
|
)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
var _ auth.GroupRepository = (*groupRepositoryMiddleware)(nil)
|
2020-12-29 23:02:35 +01:00
|
|
|
|
|
|
|
type groupRepositoryMiddleware struct {
|
|
|
|
tracer opentracing.Tracer
|
2021-03-04 10:29:03 +01:00
|
|
|
repo auth.GroupRepository
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GroupRepositoryMiddleware tracks request and their latency, and adds spans to context.
|
2021-03-04 10:29:03 +01:00
|
|
|
func GroupRepositoryMiddleware(tracer opentracing.Tracer, gr auth.GroupRepository) auth.GroupRepository {
|
2020-12-29 23:02:35 +01:00
|
|
|
return groupRepositoryMiddleware{
|
|
|
|
tracer: tracer,
|
|
|
|
repo: gr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Save(ctx context.Context, g auth.Group) (auth.Group, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, saveGroup)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
|
|
|
return grm.repo.Save(ctx, g)
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Update(ctx context.Context, g auth.Group) (auth.Group, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, updateGroup)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
|
|
|
return grm.repo.Update(ctx, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) RetrieveByID(ctx context.Context, id string) (auth.Group, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, retrieveByID)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
|
|
|
return grm.repo.RetrieveByID(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) RetrieveAllParents(ctx context.Context, groupID string, pm auth.PageMetadata) (auth.GroupPage, error) {
|
|
|
|
span := createSpan(ctx, grm.tracer, retrieveAllParents)
|
2020-12-29 23:02:35 +01:00
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.RetrieveAllParents(ctx, groupID, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) RetrieveAllChildren(ctx context.Context, groupID string, pm auth.PageMetadata) (auth.GroupPage, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, retrieveAllChildren)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.RetrieveAllChildren(ctx, groupID, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) RetrieveAll(ctx context.Context, pm auth.PageMetadata) (auth.GroupPage, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, retrieveAll)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.RetrieveAll(ctx, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Memberships(ctx context.Context, memberID string, pm auth.PageMetadata) (auth.GroupPage, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, memberships)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.Memberships(ctx, memberID, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Members(ctx context.Context, groupID, groupType string, pm auth.PageMetadata) (auth.MemberPage, error) {
|
2020-12-29 23:02:35 +01:00
|
|
|
span := createSpan(ctx, grm.tracer, members)
|
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.Members(ctx, groupID, groupType, pm)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Assign(ctx context.Context, groupID, groupType string, memberIDs ...string) error {
|
|
|
|
span := createSpan(ctx, grm.tracer, assign)
|
2020-12-29 23:02:35 +01:00
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.Assign(ctx, groupID, groupType, memberIDs...)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
func (grm groupRepositoryMiddleware) Unassign(ctx context.Context, groupID string, memberIDs ...string) error {
|
|
|
|
span := createSpan(ctx, grm.tracer, unassign)
|
2020-12-29 23:02:35 +01:00
|
|
|
defer span.Finish()
|
|
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
|
2021-03-04 10:29:03 +01:00
|
|
|
return grm.repo.Unassign(ctx, groupID, memberIDs...)
|
2020-12-29 23:02:35 +01:00
|
|
|
}
|