-
Notifications
You must be signed in to change notification settings - Fork 16
/
users.go
114 lines (90 loc) · 3.38 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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/MainfluxLabs/mainflux/users"
opentracing "github.com/opentracing/opentracing-go"
)
const (
saveOp = "save"
updateOp = "update"
retrieveByEmailOp = "retrieve_by_email"
retrieveByIDOp = "retrieve_by_id"
retrieveByIDsOp = "retrieve_by_ids"
retrieveAllOp = "retrieve_all"
updatePasswordOp = "update_password"
changeStatusOp = "change_status"
)
var _ users.UserRepository = (*userRepositoryMiddleware)(nil)
type userRepositoryMiddleware struct {
tracer opentracing.Tracer
repo users.UserRepository
}
// UserRepositoryMiddleware tracks request and their latency, and adds spans
// to context.
func UserRepositoryMiddleware(repo users.UserRepository, tracer opentracing.Tracer) users.UserRepository {
return userRepositoryMiddleware{
tracer: tracer,
repo: repo,
}
}
func (urm userRepositoryMiddleware) Save(ctx context.Context, user users.User) (string, error) {
span := createSpan(ctx, urm.tracer, saveOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.Save(ctx, user)
}
func (urm userRepositoryMiddleware) UpdateUser(ctx context.Context, user users.User) error {
span := createSpan(ctx, urm.tracer, updateOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.UpdateUser(ctx, user)
}
func (urm userRepositoryMiddleware) RetrieveByEmail(ctx context.Context, email string) (users.User, error) {
span := createSpan(ctx, urm.tracer, retrieveByEmailOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.RetrieveByEmail(ctx, email)
}
func (urm userRepositoryMiddleware) RetrieveByID(ctx context.Context, id string) (users.User, error) {
span := createSpan(ctx, urm.tracer, retrieveByIDOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.RetrieveByID(ctx, id)
}
func (urm userRepositoryMiddleware) UpdatePassword(ctx context.Context, email, password string) error {
span := createSpan(ctx, urm.tracer, updatePasswordOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.UpdatePassword(ctx, email, password)
}
func (urm userRepositoryMiddleware) RetrieveByIDs(ctx context.Context, ids []string, pm users.PageMetadata) (users.UserPage, error) {
span := createSpan(ctx, urm.tracer, retrieveByIDsOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.RetrieveByIDs(ctx, ids, pm)
}
func (urm userRepositoryMiddleware) RetrieveAll(ctx context.Context) ([]users.User, error) {
span := createSpan(ctx, urm.tracer, retrieveAllOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.RetrieveAll(ctx)
}
func (urm userRepositoryMiddleware) ChangeStatus(ctx context.Context, id, status string) error {
span := createSpan(ctx, urm.tracer, changeStatusOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return urm.repo.ChangeStatus(ctx, id, status)
}
func createSpan(ctx context.Context, tracer opentracing.Tracer, opName string) opentracing.Span {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
return tracer.StartSpan(
opName,
opentracing.ChildOf(parentSpan.Context()),
)
}
return tracer.StartSpan(opName)
}