-
Notifications
You must be signed in to change notification settings - Fork 1
/
prom_probe.go
66 lines (56 loc) · 1.96 KB
/
prom_probe.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
package user_adapter
import (
"app/internal/mill"
"app/user"
"context"
"github.com/ThreeDotsLabs/watermill/components/cqrs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/zap"
)
type PromProbe struct {
logger *zap.Logger
usersCreated prometheus.Counter
usersCreateFailed prometheus.Counter
}
func NewPromProbe(logger *zap.Logger, prom promauto.Factory) *PromProbe {
return &PromProbe{
logger: logger,
usersCreated: prom.NewCounter(prometheus.CounterOpts{Name: "users_created"}),
usersCreateFailed: prom.NewCounter(prometheus.CounterOpts{Name: "users_create_fail"}),
}
}
func (probe *PromProbe) RegisterEventHandlers(processor *cqrs.EventProcessor) error {
return processor.AddHandlers(
mill.NewEventHandler(probe.UserCreated),
mill.NewEventHandler(probe.FailedToCreateUser),
mill.NewEventHandler(probe.FailedToDeleteAll),
mill.NewEventHandler(probe.FailedToFindByName),
mill.NewEventHandler(probe.FailedToRemoveUser),
)
}
func (probe *PromProbe) UserCreated(context.Context, *user.UserCreated) error {
probe.usersCreated.Inc()
return nil
}
func (probe *PromProbe) FailedToCreateUser(_ context.Context, event *user.FailedToCreateUser) error {
probe.logger.Error("failed to create user", zap.Error(event.Err))
probe.usersCreateFailed.Inc()
return nil
}
func (probe *PromProbe) FailedToDeleteAll(_ context.Context, event *user.FailedToDeleteAll) error {
probe.logger.Error("failed to delete all", zap.Error(event.Err))
return nil
}
func (probe *PromProbe) FailedToFindByName(_ context.Context, event *user.FailedToFindByName) error {
probe.logger.Error("failed to find user by name", zap.Error(event.Err))
return nil
}
func (probe *PromProbe) FailedToRemoveUser(ctx context.Context, event *user.FailedToRemoveUser) error {
probe.logger.Error(
"failed to remove user",
zap.Error(event.Err),
zap.String("name", event.User.Email),
)
return nil
}