This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
instrumentation.go
216 lines (199 loc) · 7.04 KB
/
instrumentation.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package instrumentation
import (
"fmt"
"strings"
"github.com/short-d/app/fw/analytics"
"github.com/short-d/app/fw/ctx"
"github.com/short-d/app/fw/logger"
"github.com/short-d/app/fw/metrics"
"github.com/short-d/app/fw/timer"
"github.com/short-d/short/backend/app/entity"
)
// Instrumentation measures the internal operation of the system.
type Instrumentation struct {
logger logger.Logger
timer timer.Timer
metrics metrics.Metrics
analytics analytics.Analytics
ctxCh chan ctx.ExecutionContext
redirectingAliasToLongLinkCh chan ctx.ExecutionContext
redirectedAliasToLongLinkCh chan ctx.ExecutionContext
longLinkRetrievalSucceedCh chan ctx.ExecutionContext
longLinkRetrievalFailedCh chan ctx.ExecutionContext
featureToggleRetrievalSucceedCh chan ctx.ExecutionContext
featureToggleRetrievalFailedCh chan ctx.ExecutionContext
searchSucceedCh chan ctx.ExecutionContext
searchFailedCh chan ctx.ExecutionContext
madeFeatureDecisionCh chan ctx.ExecutionContext
trackCh chan ctx.ExecutionContext
}
// RedirectingAliasToLongLink tracks RedirectingAliasToLongLink event.
func (i Instrumentation) RedirectingAliasToLongLink(alias string) {
go func() {
c := <-i.redirectingAliasToLongLinkCh
userID := i.getUserID(nil)
props := map[string]string{
"request-id": c.RequestID,
"alias": alias,
}
i.analytics.Track("RedirectingAliasToLongLink", props, userID, c)
}()
}
// RedirectedAliasToLongLink tracks RedirectedAliasToLongLink event.
func (i Instrumentation) RedirectedAliasToLongLink(shortLink entity.ShortLink) {
go func() {
c := <-i.redirectedAliasToLongLinkCh
userID := i.getUserID(nil)
props := map[string]string{
"request-id": c.RequestID,
"alias": shortLink.Alias,
"long-link": shortLink.LongLink,
}
i.analytics.Track("RedirectedAliasToLongLink", props, userID, c)
}()
}
// LongLinkRetrievalSucceed tracks the successes when retrieving long links.
func (i Instrumentation) LongLinkRetrievalSucceed() {
go func() {
c := <-i.longLinkRetrievalSucceedCh
i.metrics.Count("long-link-retrieval-succeed", 1, 1, c)
}()
}
// LongLinkRetrievalFailed tracks the failures when retrieving long links.
func (i Instrumentation) LongLinkRetrievalFailed(err error) {
go func() {
c := <-i.longLinkRetrievalFailedCh
i.logger.Error(err)
i.metrics.Count("long-link-retrieval-failed", 1, 1, c)
}()
}
// FeatureToggleRetrievalSucceed tracks the successes when retrieving the status
// of the feature toggle.
func (i Instrumentation) FeatureToggleRetrievalSucceed() {
go func() {
c := <-i.featureToggleRetrievalSucceedCh
i.metrics.Count("feature-toggle-retrieval-succeed", 1, 1, c)
}()
}
// FeatureToggleRetrievalFailed tracks the failures when retrieving the status
// of the feature toggle.
func (i Instrumentation) FeatureToggleRetrievalFailed(err error) {
go func() {
c := <-i.featureToggleRetrievalFailedCh
i.logger.Error(err)
i.metrics.Count("feature-toggle-retrieval-failed", 1, 1, c)
}()
}
// SearchSucceed tracks the successes when searching the short resources.
func (i Instrumentation) SearchSucceed(user *entity.User, keywords string, resources []string) {
go func() {
c := <-i.searchSucceedCh
i.metrics.Count("search-succeed", 1, 1, c)
userID := i.getUserID(user)
props := map[string]string{
"keywords": keywords,
"resources": strings.Join(resources, ","),
}
i.analytics.Track("Search", props, userID, c)
}()
}
// SearchFailed tracks the failures when searching the short resources.
func (i Instrumentation) SearchFailed(err error) {
go func() {
c := <-i.searchFailedCh
i.logger.Error(err)
i.metrics.Count("search-failed", 1, 1, c)
}()
}
// MadeFeatureDecision tracks MadeFeatureDecision event.
func (i Instrumentation) MadeFeatureDecision(
featureID string,
isEnabled bool,
) {
go func() {
c := <-i.madeFeatureDecisionCh
userID := i.getUserID(nil)
isEnabledStr := fmt.Sprintf("%v", isEnabled)
props := map[string]string{
"request-id": c.RequestID,
"feature-id": featureID,
"is-enabled": isEnabledStr,
}
i.analytics.Track("MadeFeatureDecision", props, userID, c)
}()
}
// Track records events happened in the system.
func (i Instrumentation) Track(event string) {
go func() {
c := <-i.trackCh
userID := i.getUserID(nil)
props := map[string]string{}
i.analytics.Track(event, props, userID, c)
}()
}
// Done closes all the channels to prevent memory leak.
func (i Instrumentation) Done() {
close(i.redirectingAliasToLongLinkCh)
close(i.redirectedAliasToLongLinkCh)
close(i.longLinkRetrievalSucceedCh)
close(i.longLinkRetrievalFailedCh)
close(i.featureToggleRetrievalSucceedCh)
close(i.featureToggleRetrievalFailedCh)
}
func (i Instrumentation) getUserID(user *entity.User) string {
if user == nil {
return "anonymous"
}
return user.ID
}
// NewInstrumentation initializes instrumentation code.
func NewInstrumentation(
logger logger.Logger,
timer timer.Timer,
metrics metrics.Metrics,
analytics analytics.Analytics,
ctxCh chan ctx.ExecutionContext,
) Instrumentation {
redirectingAliasToLongLinkCh := make(chan ctx.ExecutionContext)
redirectedAliasToLongLinkCh := make(chan ctx.ExecutionContext)
longLinkRetrievalSucceedCh := make(chan ctx.ExecutionContext)
longLinkRetrievalFailedCh := make(chan ctx.ExecutionContext)
featureToggleRetrievalSucceedCh := make(chan ctx.ExecutionContext)
featureToggleRetrievalFailedCh := make(chan ctx.ExecutionContext)
searchSucceedCh := make(chan ctx.ExecutionContext)
searchFailedCh := make(chan ctx.ExecutionContext)
madeFeatureDecisionCh := make(chan ctx.ExecutionContext)
trackCh := make(chan ctx.ExecutionContext)
ins := &Instrumentation{
logger: logger,
timer: timer,
metrics: metrics,
analytics: analytics,
ctxCh: ctxCh,
redirectingAliasToLongLinkCh: redirectingAliasToLongLinkCh,
redirectedAliasToLongLinkCh: redirectedAliasToLongLinkCh,
longLinkRetrievalSucceedCh: longLinkRetrievalSucceedCh,
longLinkRetrievalFailedCh: longLinkRetrievalFailedCh,
featureToggleRetrievalSucceedCh: featureToggleRetrievalSucceedCh,
featureToggleRetrievalFailedCh: featureToggleRetrievalFailedCh,
searchSucceedCh: searchSucceedCh,
searchFailedCh: searchFailedCh,
madeFeatureDecisionCh: madeFeatureDecisionCh,
trackCh: trackCh,
}
go func() {
c := <-ctxCh
go func() { redirectingAliasToLongLinkCh <- c }()
go func() { redirectedAliasToLongLinkCh <- c }()
go func() { longLinkRetrievalSucceedCh <- c }()
go func() { longLinkRetrievalFailedCh <- c }()
go func() { featureToggleRetrievalSucceedCh <- c }()
go func() { featureToggleRetrievalFailedCh <- c }()
go func() { searchSucceedCh <- c }()
go func() { searchFailedCh <- c }()
go func() { madeFeatureDecisionCh <- c }()
go func() { trackCh <- c }()
close(ctxCh)
}()
return *ins
}