Skip to content

Commit ac67256

Browse files
moved otel to ext
1 parent 8f3a6a1 commit ac67256

File tree

5 files changed

+130
-43
lines changed

5 files changed

+130
-43
lines changed

api/services/sales/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ardanlabs/service/business/domain/productbus/stores/productdb"
2929
"github.com/ardanlabs/service/business/domain/userbus"
3030
"github.com/ardanlabs/service/business/domain/userbus/extensions/useraudit"
31+
"github.com/ardanlabs/service/business/domain/userbus/extensions/userotel"
3132
"github.com/ardanlabs/service/business/domain/userbus/stores/usercache"
3233
"github.com/ardanlabs/service/business/domain/userbus/stores/userdb"
3334
"github.com/ardanlabs/service/business/domain/vproductbus"
@@ -170,12 +171,13 @@ func run(ctx context.Context, log *logger.Logger) error {
170171
// -------------------------------------------------------------------------
171172
// Create Business Packages
172173

173-
userAuditExt := useraudit.NewExtension(log, auditbus.NewBusiness(log, auditdb.NewStore(log, db)))
174+
userOtelExt := userotel.NewExtension()
175+
userAuditExt := useraudit.NewExtension(auditbus.NewBusiness(log, auditdb.NewStore(log, db)))
174176
userStorage := usercache.NewStore(log, userdb.NewStore(log, db), time.Minute)
175177

176178
delegate := delegate.New(log)
177179
auditBus := auditbus.NewBusiness(log, auditdb.NewStore(log, db))
178-
userBus := userbus.NewBusiness(log, delegate, userStorage, userAuditExt)
180+
userBus := userbus.NewBusiness(log, delegate, userStorage, userOtelExt, userAuditExt)
179181
productBus := productbus.NewBusiness(log, userBus, delegate, productdb.NewStore(log, db))
180182
homeBus := homebus.NewBusiness(log, userBus, delegate, homedb.NewStore(log, db))
181183
vproductBus := vproductbus.NewBusiness(vproductdb.NewStore(log, db))

business/domain/userbus/extensions/useraudit/useraudit.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/ardanlabs/service/business/sdk/page"
1313
"github.com/ardanlabs/service/business/sdk/sqldb"
1414
"github.com/ardanlabs/service/business/types/domain"
15-
"github.com/ardanlabs/service/foundation/logger"
1615
"github.com/google/uuid"
1716
)
1817

@@ -23,7 +22,7 @@ type Extension struct {
2322
}
2423

2524
// NewExtension constructs a new extension that wraps the userbus with audit.
26-
func NewExtension(log *logger.Logger, auditBus *auditbus.Business) userbus.ExtFunc {
25+
func NewExtension(auditBus *auditbus.Business) userbus.ExtFunc {
2726
return func(bus userbus.ExtBusiness) userbus.ExtBusiness {
2827
return &Extension{
2928
bus: bus,
@@ -32,13 +31,12 @@ func NewExtension(log *logger.Logger, auditBus *auditbus.Business) userbus.ExtFu
3231
}
3332
}
3433

35-
// NewWithTx constructs a new business value that will use the
36-
// specified transaction in any store related calls.
34+
// NewWithTx does not apply auditing.
3735
func (ext *Extension) NewWithTx(tx sqldb.CommitRollbacker) (userbus.ExtBusiness, error) {
3836
return ext.bus.NewWithTx(tx)
3937
}
4038

41-
// Create adds a new user to the system.
39+
// Create applies auditing to the user creation process.
4240
func (ext *Extension) Create(ctx context.Context, actorID uuid.UUID, nu userbus.NewUser) (userbus.User, error) {
4341
usr, err := ext.bus.Create(ctx, actorID, nu)
4442
if err != nil {
@@ -62,7 +60,7 @@ func (ext *Extension) Create(ctx context.Context, actorID uuid.UUID, nu userbus.
6260
return usr, nil
6361
}
6462

65-
// Update modifies information about a user.
63+
// Update applies auditing to the user update process.
6664
func (ext *Extension) Update(ctx context.Context, actorID uuid.UUID, usr userbus.User, uu userbus.UpdateUser) (userbus.User, error) {
6765
usr, err := ext.bus.Update(ctx, actorID, usr, uu)
6866
if err != nil {
@@ -86,7 +84,7 @@ func (ext *Extension) Update(ctx context.Context, actorID uuid.UUID, usr userbus
8684
return usr, nil
8785
}
8886

89-
// Delete removes the specified user.
87+
// Delete applies auditing to the user deletion process.
9088
func (ext *Extension) Delete(ctx context.Context, actorID uuid.UUID, usr userbus.User) error {
9189
if err := ext.bus.Delete(ctx, actorID, usr); err != nil {
9290
return err
@@ -109,29 +107,27 @@ func (ext *Extension) Delete(ctx context.Context, actorID uuid.UUID, usr userbus
109107
return nil
110108
}
111109

112-
// Query retrieves a list of existing users.
110+
// Query does not apply auditing.
113111
func (ext *Extension) Query(ctx context.Context, filter userbus.QueryFilter, orderBy order.By, page page.Page) ([]userbus.User, error) {
114112
return ext.bus.Query(ctx, filter, orderBy, page)
115113
}
116114

117-
// Count returns the total number of users.
115+
// Count does not apply auditing.
118116
func (ext *Extension) Count(ctx context.Context, filter userbus.QueryFilter) (int, error) {
119117
return ext.bus.Count(ctx, filter)
120118
}
121119

122-
// QueryByID finds the user by the specified ID.
120+
// QueryByID does not apply auditing.
123121
func (ext *Extension) QueryByID(ctx context.Context, userID uuid.UUID) (userbus.User, error) {
124122
return ext.bus.QueryByID(ctx, userID)
125123
}
126124

127-
// QueryByEmail finds the user by a specified user email.
125+
// QueryByEmail does not apply auditing.
128126
func (ext *Extension) QueryByEmail(ctx context.Context, email mail.Address) (userbus.User, error) {
129127
return ext.bus.QueryByEmail(ctx, email)
130128
}
131129

132-
// Authenticate finds a user by their email and verifies their password. On
133-
// success it returns a Claims User representing this user. The claims can be
134-
// used to generate a token for future authentication.
130+
// Authenticate does not apply auditing.
135131
func (ext *Extension) Authenticate(ctx context.Context, email mail.Address, password string) (userbus.User, error) {
136132
return ext.bus.Authenticate(ctx, email, password)
137133
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Package userotel provides an extension for userbus that adds
2+
// otel tracking.
3+
package userotel
4+
5+
import (
6+
"context"
7+
"net/mail"
8+
9+
"github.com/ardanlabs/service/business/domain/userbus"
10+
"github.com/ardanlabs/service/business/sdk/order"
11+
"github.com/ardanlabs/service/business/sdk/page"
12+
"github.com/ardanlabs/service/business/sdk/sqldb"
13+
"github.com/ardanlabs/service/foundation/otel"
14+
"github.com/google/uuid"
15+
)
16+
17+
// Extension provides a wrapper for otel functionality around the userbus.
18+
type Extension struct {
19+
bus userbus.ExtBusiness
20+
}
21+
22+
// NewExtension constructs a new extension that wraps the userbus with otel.
23+
func NewExtension() userbus.ExtFunc {
24+
return func(bus userbus.ExtBusiness) userbus.ExtBusiness {
25+
return &Extension{
26+
bus: bus,
27+
}
28+
}
29+
}
30+
31+
// NewWithTx does not apply otel.
32+
func (ext *Extension) NewWithTx(tx sqldb.CommitRollbacker) (userbus.ExtBusiness, error) {
33+
return ext.bus.NewWithTx(tx)
34+
}
35+
36+
// Create applies otel to the user creation process.
37+
func (ext *Extension) Create(ctx context.Context, actorID uuid.UUID, nu userbus.NewUser) (userbus.User, error) {
38+
ctx, span := otel.AddSpan(ctx, "business.userbus.create")
39+
defer span.End()
40+
41+
usr, err := ext.bus.Create(ctx, actorID, nu)
42+
if err != nil {
43+
return userbus.User{}, err
44+
}
45+
46+
return usr, nil
47+
}
48+
49+
// Update applies otel to the user update process.
50+
func (ext *Extension) Update(ctx context.Context, actorID uuid.UUID, usr userbus.User, uu userbus.UpdateUser) (userbus.User, error) {
51+
ctx, span := otel.AddSpan(ctx, "business.userbus.update")
52+
defer span.End()
53+
54+
usr, err := ext.bus.Update(ctx, actorID, usr, uu)
55+
if err != nil {
56+
return userbus.User{}, err
57+
}
58+
59+
return usr, nil
60+
}
61+
62+
// Delete applies otel to the user deletion process.
63+
func (ext *Extension) Delete(ctx context.Context, actorID uuid.UUID, usr userbus.User) error {
64+
ctx, span := otel.AddSpan(ctx, "business.userbus.delete")
65+
defer span.End()
66+
67+
if err := ext.bus.Delete(ctx, actorID, usr); err != nil {
68+
return err
69+
}
70+
71+
return nil
72+
}
73+
74+
// Query applies otel to the user query process.
75+
func (ext *Extension) Query(ctx context.Context, filter userbus.QueryFilter, orderBy order.By, page page.Page) ([]userbus.User, error) {
76+
ctx, span := otel.AddSpan(ctx, "business.userbus.query")
77+
defer span.End()
78+
79+
return ext.bus.Query(ctx, filter, orderBy, page)
80+
}
81+
82+
// Count applies otel to the user count process.
83+
func (ext *Extension) Count(ctx context.Context, filter userbus.QueryFilter) (int, error) {
84+
ctx, span := otel.AddSpan(ctx, "business.userbus.count")
85+
defer span.End()
86+
87+
return ext.bus.Count(ctx, filter)
88+
}
89+
90+
// QueryByID applies otel to the user query by ID process.
91+
func (ext *Extension) QueryByID(ctx context.Context, userID uuid.UUID) (userbus.User, error) {
92+
ctx, span := otel.AddSpan(ctx, "business.userbus.querybyid")
93+
defer span.End()
94+
95+
return ext.bus.QueryByID(ctx, userID)
96+
}
97+
98+
// QueryByEmail applies otel to the user query by email process.
99+
func (ext *Extension) QueryByEmail(ctx context.Context, email mail.Address) (userbus.User, error) {
100+
ctx, span := otel.AddSpan(ctx, "business.userbus.querybyemail")
101+
defer span.End()
102+
103+
return ext.bus.QueryByEmail(ctx, email)
104+
}
105+
106+
// Authenticate applies otel to the user authentication process.
107+
func (ext *Extension) Authenticate(ctx context.Context, email mail.Address, password string) (userbus.User, error) {
108+
ctx, span := otel.AddSpan(ctx, "business.userbus.authenticate")
109+
defer span.End()
110+
111+
return ext.bus.Authenticate(ctx, email, password)
112+
}

business/domain/userbus/userbus.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/ardanlabs/service/business/sdk/page"
1414
"github.com/ardanlabs/service/business/sdk/sqldb"
1515
"github.com/ardanlabs/service/foundation/logger"
16-
"github.com/ardanlabs/service/foundation/otel"
1716
"github.com/google/uuid"
1817
"golang.org/x/crypto/bcrypt"
1918
)
@@ -100,9 +99,6 @@ func (b *Business) NewWithTx(tx sqldb.CommitRollbacker) (ExtBusiness, error) {
10099

101100
// Create adds a new user to the system.
102101
func (b *Business) Create(ctx context.Context, actorID uuid.UUID, nu NewUser) (User, error) {
103-
ctx, span := otel.AddSpan(ctx, "business.userbus.create")
104-
defer span.End()
105-
106102
hash, err := bcrypt.GenerateFromPassword([]byte(nu.Password), bcrypt.DefaultCost)
107103
if err != nil {
108104
return User{}, fmt.Errorf("generatefrompassword: %w", err)
@@ -131,9 +127,6 @@ func (b *Business) Create(ctx context.Context, actorID uuid.UUID, nu NewUser) (U
131127

132128
// Update modifies information about a user.
133129
func (b *Business) Update(ctx context.Context, actorID uuid.UUID, usr User, uu UpdateUser) (User, error) {
134-
ctx, span := otel.AddSpan(ctx, "business.userbus.update")
135-
defer span.End()
136-
137130
if uu.Name != nil {
138131
usr.Name = *uu.Name
139132
}
@@ -173,9 +166,6 @@ func (b *Business) Update(ctx context.Context, actorID uuid.UUID, usr User, uu U
173166

174167
// Delete removes the specified user.
175168
func (b *Business) Delete(ctx context.Context, actorID uuid.UUID, usr User) error {
176-
ctx, span := otel.AddSpan(ctx, "business.userbus.delete")
177-
defer span.End()
178-
179169
if err := b.storer.Delete(ctx, usr); err != nil {
180170
return fmt.Errorf("delete: %w", err)
181171
}
@@ -191,9 +181,6 @@ func (b *Business) Delete(ctx context.Context, actorID uuid.UUID, usr User) erro
191181

192182
// Query retrieves a list of existing users.
193183
func (b *Business) Query(ctx context.Context, filter QueryFilter, orderBy order.By, page page.Page) ([]User, error) {
194-
ctx, span := otel.AddSpan(ctx, "business.userbus.query")
195-
defer span.End()
196-
197184
users, err := b.storer.Query(ctx, filter, orderBy, page)
198185
if err != nil {
199186
return nil, fmt.Errorf("query: %w", err)
@@ -204,17 +191,11 @@ func (b *Business) Query(ctx context.Context, filter QueryFilter, orderBy order.
204191

205192
// Count returns the total number of users.
206193
func (b *Business) Count(ctx context.Context, filter QueryFilter) (int, error) {
207-
ctx, span := otel.AddSpan(ctx, "business.userbus.count")
208-
defer span.End()
209-
210194
return b.storer.Count(ctx, filter)
211195
}
212196

213197
// QueryByID finds the user by the specified ID.
214198
func (b *Business) QueryByID(ctx context.Context, userID uuid.UUID) (User, error) {
215-
ctx, span := otel.AddSpan(ctx, "business.userbus.querybyid")
216-
defer span.End()
217-
218199
user, err := b.storer.QueryByID(ctx, userID)
219200
if err != nil {
220201
return User{}, fmt.Errorf("query: userID[%s]: %w", userID, err)
@@ -225,9 +206,6 @@ func (b *Business) QueryByID(ctx context.Context, userID uuid.UUID) (User, error
225206

226207
// QueryByEmail finds the user by a specified user email.
227208
func (b *Business) QueryByEmail(ctx context.Context, email mail.Address) (User, error) {
228-
ctx, span := otel.AddSpan(ctx, "business.userbus.querybyemail")
229-
defer span.End()
230-
231209
user, err := b.storer.QueryByEmail(ctx, email)
232210
if err != nil {
233211
return User{}, fmt.Errorf("query: email[%s]: %w", email, err)
@@ -240,9 +218,6 @@ func (b *Business) QueryByEmail(ctx context.Context, email mail.Address) (User,
240218
// success it returns a Claims User representing this user. The claims can be
241219
// used to generate a token for future authentication.
242220
func (b *Business) Authenticate(ctx context.Context, email mail.Address, password string) (User, error) {
243-
ctx, span := otel.AddSpan(ctx, "business.userbus.authenticate")
244-
defer span.End()
245-
246221
usr, err := b.QueryByEmail(ctx, email)
247222
if err != nil {
248223
return User{}, fmt.Errorf("query: email[%s]: %w", email, err)

business/sdk/dbtest/business.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ardanlabs/service/business/domain/productbus/stores/productdb"
1212
"github.com/ardanlabs/service/business/domain/userbus"
1313
"github.com/ardanlabs/service/business/domain/userbus/extensions/useraudit"
14+
"github.com/ardanlabs/service/business/domain/userbus/extensions/userotel"
1415
"github.com/ardanlabs/service/business/domain/userbus/stores/usercache"
1516
"github.com/ardanlabs/service/business/domain/userbus/stores/userdb"
1617
"github.com/ardanlabs/service/business/domain/vproductbus"
@@ -31,12 +32,13 @@ type BusDomain struct {
3132
}
3233

3334
func newBusDomains(log *logger.Logger, db *sqlx.DB) BusDomain {
34-
userAuditExt := useraudit.NewExtension(log, auditbus.NewBusiness(log, auditdb.NewStore(log, db)))
35+
userOtelExt := userotel.NewExtension()
36+
userAuditExt := useraudit.NewExtension(auditbus.NewBusiness(log, auditdb.NewStore(log, db)))
3537
userStorage := usercache.NewStore(log, userdb.NewStore(log, db), time.Hour)
3638

3739
delegate := delegate.New(log)
3840
auditBus := auditbus.NewBusiness(log, auditdb.NewStore(log, db))
39-
userBus := userbus.NewBusiness(log, delegate, userStorage, userAuditExt)
41+
userBus := userbus.NewBusiness(log, delegate, userStorage, userOtelExt, userAuditExt)
4042
productBus := productbus.NewBusiness(log, userBus, delegate, productdb.NewStore(log, db))
4143
homeBus := homebus.NewBusiness(log, userBus, delegate, homedb.NewStore(log, db))
4244
vproductBus := vproductbus.NewBusiness(vproductdb.NewStore(log, db))

0 commit comments

Comments
 (0)