-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathTelemetryEventClient.go
More file actions
692 lines (603 loc) · 25.5 KB
/
TelemetryEventClient.go
File metadata and controls
692 lines (603 loc) · 25.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package telemetry
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/api/bean"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/internal/sql/repository"
util2 "github.com/devtron-labs/devtron/internal/util"
repository2 "github.com/devtron-labs/devtron/pkg/appStore/deployment/repository"
"github.com/devtron-labs/devtron/pkg/attributes"
"github.com/devtron-labs/devtron/pkg/cluster"
module2 "github.com/devtron-labs/devtron/pkg/module"
moduleRepo "github.com/devtron-labs/devtron/pkg/module/repo"
serverDataStore "github.com/devtron-labs/devtron/pkg/server/store"
"github.com/devtron-labs/devtron/pkg/sso"
"github.com/devtron-labs/devtron/pkg/user"
util3 "github.com/devtron-labs/devtron/pkg/util"
"github.com/devtron-labs/devtron/util"
"github.com/go-pg/pg"
"github.com/patrickmn/go-cache"
"github.com/posthog/posthog-go"
"github.com/robfig/cron/v3"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"net/http"
"time"
)
const LOGIN_COUNT_CONST = "login-count"
const SKIPPED_ONBOARDING_CONST = "SkippedOnboarding"
const ADMIN_EMAIL_ID_CONST = "admin"
type TelemetryEventClientImpl struct {
cron *cron.Cron
logger *zap.SugaredLogger
client *http.Client
clusterService cluster.ClusterService
K8sUtil *util2.K8sUtil
aCDAuthConfig *util3.ACDAuthConfig
userService user.UserService
attributeRepo repository.AttributesRepository
ssoLoginService sso.SSOLoginService
PosthogClient *PosthogClient
moduleRepository moduleRepo.ModuleRepository
serverDataStore *serverDataStore.ServerDataStore
userAuditService user.UserAuditService
helmAppClient client.HelmAppClient
InstalledAppRepository repository2.InstalledAppRepository
userAttributesRepository repository.UserAttributesRepository
}
type TelemetryEventClient interface {
GetTelemetryMetaInfo() (*TelemetryMetaInfo, error)
SendTelemetryInstallEventEA() (*TelemetryEventType, error)
SendTelemetryDashboardAccessEvent() error
SendTelemetryDashboardLoggedInEvent() error
SendGenericTelemetryEvent(eventType string, prop map[string]interface{}) error
SendSummaryEvent(eventType string) error
}
func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService,
K8sUtil *util2.K8sUtil, aCDAuthConfig *util3.ACDAuthConfig, userService user.UserService,
attributeRepo repository.AttributesRepository, ssoLoginService sso.SSOLoginService,
PosthogClient *PosthogClient, moduleRepository moduleRepo.ModuleRepository, serverDataStore *serverDataStore.ServerDataStore, userAuditService user.UserAuditService, helmAppClient client.HelmAppClient, InstalledAppRepository repository2.InstalledAppRepository) (*TelemetryEventClientImpl, error) {
cron := cron.New(
cron.WithChain())
cron.Start()
watcher := &TelemetryEventClientImpl{
cron: cron,
logger: logger,
client: client, clusterService: clusterService,
K8sUtil: K8sUtil, aCDAuthConfig: aCDAuthConfig,
userService: userService, attributeRepo: attributeRepo,
ssoLoginService: ssoLoginService,
PosthogClient: PosthogClient,
moduleRepository: moduleRepository,
serverDataStore: serverDataStore,
userAuditService: userAuditService,
helmAppClient: helmAppClient,
InstalledAppRepository: InstalledAppRepository,
}
watcher.HeartbeatEventForTelemetry()
_, err := cron.AddFunc(SummaryCronExpr, watcher.SummaryEventForTelemetryEA)
if err != nil {
logger.Errorw("error in starting summery event", "err", err)
return nil, err
}
_, err = cron.AddFunc(HeartbeatCronExpr, watcher.HeartbeatEventForTelemetry)
if err != nil {
logger.Errorw("error in starting heartbeat event", "err", err)
return nil, err
}
return watcher, err
}
func (impl *TelemetryEventClientImpl) StopCron() {
impl.cron.Stop()
}
type TelemetryEventEA struct {
UCID string `json:"ucid"` //unique client id
Timestamp time.Time `json:"timestamp"`
EventMessage string `json:"eventMessage,omitempty"`
EventType TelemetryEventType `json:"eventType"`
ServerVersion string `json:"serverVersion,omitempty"`
UserCount int `json:"userCount,omitempty"`
ClusterCount int `json:"clusterCount,omitempty"`
HostURL bool `json:"hostURL,omitempty"`
SSOLogin bool `json:"ssoLogin,omitempty"`
DevtronVersion string `json:"devtronVersion,omitempty"`
DevtronMode string `json:"devtronMode,omitempty"`
InstalledIntegrations []string `json:"installedIntegrations,omitempty"`
InstallFailedIntegrations []string `json:"installFailedIntegrations,omitempty"`
InstallTimedOutIntegrations []string `json:"installTimedOutIntegrations,omitempty"`
LastLoginTime time.Time `json:"LastLoginTime,omitempty"`
InstallingIntegrations []string `json:"installingIntegrations,omitempty"`
DevtronReleaseVersion string `json:"devtronReleaseVersion,omitempty"`
HelmAppAccessCounter string `json:"HelmAppAccessCounter,omitempty"`
HelmAppUpdateCounter string `json:"HelmAppUpdateCounter,omitempty"`
ChartStoreVisitCount string `json:"ChartStoreVisitCount,omitempty"`
SkippedOnboarding bool `json:"SkippedOnboarding"`
HelmChartSuccessfulDeploymentCount int `json:"helmChartSuccessfulDeploymentCount,omitempty"`
ExternalHelmAppClusterCount map[int32]int `json:"ExternalHelmAppClusterCount,omitempty"`
}
const DevtronUniqueClientIdConfigMap = "devtron-ucid"
const DevtronUniqueClientIdConfigMapKey = "UCID"
const InstallEventKey = "installEvent"
const UIEventKey = "uiEventKey"
type TelemetryEventType string
const (
Heartbeat TelemetryEventType = "Heartbeat"
InstallationStart TelemetryEventType = "InstallationStart"
InstallationInProgress TelemetryEventType = "InstallationInProgress"
InstallationInterrupt TelemetryEventType = "InstallationInterrupt"
InstallationSuccess TelemetryEventType = "InstallationSuccess"
InstallationFailure TelemetryEventType = "InstallationFailure"
UpgradeStart TelemetryEventType = "UpgradeStart"
UpgradeInProgress TelemetryEventType = "UpgradeInProgress"
UpgradeInterrupt TelemetryEventType = "UpgradeInterrupt"
UpgradeSuccess TelemetryEventType = "UpgradeSuccess"
UpgradeFailure TelemetryEventType = "UpgradeFailure"
Summary TelemetryEventType = "Summary"
InstallationApplicationError TelemetryEventType = "InstallationApplicationError"
DashboardAccessed TelemetryEventType = "DashboardAccessed"
DashboardLoggedIn TelemetryEventType = "DashboardLoggedIn"
SIG_TERM TelemetryEventType = "SIG_TERM"
)
func (impl *TelemetryEventClientImpl) SummaryDetailsForTelemetry() (cluster []cluster.ClusterBean, user []bean.UserInfo,
k8sServerVersion *version.Info, hostURL bool, ssoSetup bool, HelmAppAccessCount string, ChartStoreVisitCount string,
SkippedOnboarding bool, HelmAppUpdateCounter string, helmChartSuccessfulDeploymentCount int, ExternalHelmAppClusterCount map[int32]int) {
discoveryClient, err := impl.K8sUtil.GetK8sDiscoveryClientInCluster()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
k8sServerVersion, err = discoveryClient.ServerVersion()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
users, err := impl.userService.GetAll()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summery event", "err", err)
return
}
clusters, err := impl.clusterService.FindAllActive()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
hostURL = false
attribute, err := impl.attributeRepo.FindByKey(attributes.HostUrlKey)
if err == nil && attribute.Id > 0 {
hostURL = true
}
attribute, err = impl.attributeRepo.FindByKey("HelmAppAccessCounter")
if err == nil {
HelmAppAccessCount = attribute.Value
}
attribute, err = impl.attributeRepo.FindByKey("ChartStoreVisitCount")
if err == nil {
ChartStoreVisitCount = attribute.Value
}
attribute, err = impl.attributeRepo.FindByKey("HelmAppUpdateCounter")
if err == nil {
HelmAppUpdateCounter = attribute.Value
}
helmChartSuccessfulDeploymentCount, err = impl.InstalledAppRepository.GetDeploymentSuccessfulStatusCountForTelemetry()
//externalHelmCount := make(map[int32]int)
ExternalHelmAppClusterCount = make(map[int32]int)
for _, clusterDetail := range clusters {
req := &client.AppListRequest{}
config := &client.ClusterConfig{
ApiServerUrl: clusterDetail.ServerUrl,
Token: clusterDetail.Config["bearer_token"],
ClusterId: int32(clusterDetail.Id),
ClusterName: clusterDetail.ClusterName,
}
req.Clusters = append(req.Clusters, config)
applicatonStream, err := impl.helmAppClient.ListApplication(req)
if err == nil {
clusterList, _ := applicatonStream.Recv()
if !clusterList.Errored {
ExternalHelmAppClusterCount[clusterList.ClusterId] = len(clusterList.DeployedAppDetail)
}
}
}
//getting userData from emailId
userData, err := impl.userAttributesRepository.GetUserDataByEmailId(ADMIN_EMAIL_ID_CONST)
SkippedOnboardingValue := gjson.Get(userData, SKIPPED_ONBOARDING_CONST).Str
if SkippedOnboardingValue == "true" {
SkippedOnboarding = true
} else {
SkippedOnboarding = false
}
ssoSetup = false
ssoConfig, err := impl.ssoLoginService.GetAll()
if err == nil && len(ssoConfig) > 0 {
ssoSetup = true
}
return clusters, users, k8sServerVersion, hostURL, ssoSetup, HelmAppAccessCount, ChartStoreVisitCount, SkippedOnboarding, HelmAppUpdateCounter, helmChartSuccessfulDeploymentCount, ExternalHelmAppClusterCount
}
func (impl *TelemetryEventClientImpl) SummaryEventForTelemetryEA() {
err := impl.SendSummaryEvent(string(Summary))
if err != nil {
impl.logger.Errorw("error occurred in SummaryEventForTelemetryEA", "err", err)
}
}
func (impl *TelemetryEventClientImpl) SendSummaryEvent(eventType string) error {
impl.logger.Infow("sending summary event", "eventType", eventType)
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return err
}
if IsOptOut {
impl.logger.Warnw("client is opt-out for telemetry, there will be no events capture", "ucid", ucid)
return err
}
// build integrations data
installedIntegrations, installFailedIntegrations, installTimedOutIntegrations, installingIntegrations, err := impl.buildIntegrationsList()
if err != nil {
return err
}
clusters, users, k8sServerVersion, hostURL, ssoSetup, HelmAppAccessCount, ChartStoreVisitCount, SkippedOnboarding, HelmAppUpdateCounter, helmChartSuccessfulDeploymentCount, ExternalHelmAppClusterCount := impl.SummaryDetailsForTelemetry()
payload := &TelemetryEventEA{UCID: ucid, Timestamp: time.Now(), EventType: TelemetryEventType(eventType), DevtronVersion: "v1"}
payload.ServerVersion = k8sServerVersion.String()
payload.DevtronMode = util.GetDevtronVersion().ServerMode
payload.HostURL = hostURL
payload.SSOLogin = ssoSetup
payload.UserCount = len(users)
payload.ClusterCount = len(clusters)
payload.InstalledIntegrations = installedIntegrations
payload.InstallFailedIntegrations = installFailedIntegrations
payload.InstallTimedOutIntegrations = installTimedOutIntegrations
payload.InstallingIntegrations = installingIntegrations
payload.DevtronReleaseVersion = impl.serverDataStore.CurrentVersion
payload.HelmAppAccessCounter = HelmAppAccessCount
payload.ChartStoreVisitCount = ChartStoreVisitCount
payload.SkippedOnboarding = SkippedOnboarding
payload.HelmAppUpdateCounter = HelmAppUpdateCounter
payload.HelmChartSuccessfulDeploymentCount = helmChartSuccessfulDeploymentCount
payload.ExternalHelmAppClusterCount = ExternalHelmAppClusterCount
latestUser, err := impl.userAuditService.GetLatestUser()
if err == nil {
loginTime := latestUser.UpdatedOn
if loginTime.IsZero() {
loginTime = latestUser.CreatedOn
}
payload.LastLoginTime = loginTime
}
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, payload marshal error", "error", err)
return err
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, payload unmarshal error", "error", err)
return err
}
err = impl.EnqueuePostHog(ucid, TelemetryEventType(eventType), prop)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, failed to push event", "ucid", ucid, "error", err)
return err
}
return nil
}
func (impl *TelemetryEventClientImpl) EnqueuePostHog(ucid string, eventType TelemetryEventType, prop map[string]interface{}) error {
return impl.EnqueueGenericPostHogEvent(ucid, string(eventType), prop)
}
func (impl *TelemetryEventClientImpl) SendGenericTelemetryEvent(eventType string, prop map[string]interface{}) error {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry generic event", "err", err)
return nil
}
if IsOptOut {
impl.logger.Warnw("client is opt-out for telemetry, there will be no events capture", "ucid", ucid)
return nil
}
return impl.EnqueueGenericPostHogEvent(ucid, eventType, prop)
}
func (impl *TelemetryEventClientImpl) EnqueueGenericPostHogEvent(ucid string, eventType string, prop map[string]interface{}) error {
if impl.PosthogClient.Client == nil {
impl.logger.Warn("no posthog client found, creating new")
client, err := impl.retryPosthogClient(PosthogApiKey, PosthogEndpoint)
if err == nil {
impl.PosthogClient.Client = client
}
}
if impl.PosthogClient.Client != nil {
err := impl.PosthogClient.Client.Enqueue(posthog.Capture{
DistinctId: ucid,
Event: eventType,
Properties: prop,
})
if err != nil {
impl.logger.Errorw("EnqueueGenericPostHogEvent, failed to push event", "error", err)
return err
}
}
return nil
}
func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err)
return
}
if IsOptOut {
impl.logger.Warnw("client is opt-out for telemetry, there will be no events capture", "ucid", ucid)
return
}
discoveryClient, err := impl.K8sUtil.GetK8sDiscoveryClientInCluster()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err)
return
}
k8sServerVersion, err := discoveryClient.ServerVersion()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err)
return
}
payload := &TelemetryEventEA{UCID: ucid, Timestamp: time.Now(), EventType: Heartbeat, DevtronVersion: "v1"}
payload.ServerVersion = k8sServerVersion.String()
payload.DevtronMode = util.GetDevtronVersion().ServerMode
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("HeartbeatEventForTelemetry, payload marshal error", "error", err)
return
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("HeartbeatEventForTelemetry, payload unmarshal error", "error", err)
return
}
err = impl.EnqueuePostHog(ucid, Heartbeat, prop)
if err != nil {
impl.logger.Warnw("HeartbeatEventForTelemetry, failed to push event", "error", err)
return
}
impl.logger.Debugw("HeartbeatEventForTelemetry success")
return
}
func (impl *TelemetryEventClientImpl) GetTelemetryMetaInfo() (*TelemetryMetaInfo, error) {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return nil, err
}
data := &TelemetryMetaInfo{
Url: PosthogEndpoint,
UCID: ucid,
ApiKey: PosthogEncodedApiKey,
}
return data, err
}
func (impl *TelemetryEventClientImpl) SendTelemetryInstallEventEA() (*TelemetryEventType, error) {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return nil, err
}
client, err := impl.K8sUtil.GetClientForInCluster()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return nil, err
}
payload := &TelemetryEventEA{UCID: ucid, Timestamp: time.Now(), EventType: InstallationSuccess, DevtronVersion: "v1"}
payload.DevtronMode = util.GetDevtronVersion().ServerMode
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("Installation EventForTelemetry EA Mode, payload marshal error", "error", err)
return nil, nil
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("Installation EventForTelemetry EA Mode, payload unmarshal error", "error", err)
return nil, nil
}
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, DevtronUniqueClientIdConfigMap, client)
datamap := cm.Data
installEventValue, installEventKeyExists := datamap[InstallEventKey]
if installEventKeyExists == false || installEventValue == "1" {
err = impl.EnqueuePostHog(ucid, InstallationSuccess, prop)
if err == nil {
datamap[InstallEventKey] = "2"
cm.Data = datamap
_, err = impl.K8sUtil.UpdateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
impl.logger.Warnw("config map update failed for install event", "err", err)
} else {
impl.logger.Debugw("config map apply succeeded for install event")
}
}
}
return nil, nil
}
func (impl *TelemetryEventClientImpl) SendTelemetryDashboardAccessEvent() error {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return err
}
client, err := impl.K8sUtil.GetClientForInCluster()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return err
}
payload := &TelemetryEventEA{UCID: ucid, Timestamp: time.Now(), EventType: DashboardAccessed, DevtronVersion: "v1"}
payload.DevtronMode = util.GetDevtronVersion().ServerMode
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("DashboardAccessed EventForTelemetry, payload marshal error", "error", err)
return err
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("DashboardAccessed EventForTelemetry, payload unmarshal error", "error", err)
return err
}
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, DevtronUniqueClientIdConfigMap, client)
datamap := cm.Data
accessEventValue, installEventKeyExists := datamap[UIEventKey]
if installEventKeyExists == false || accessEventValue <= "1" {
err = impl.EnqueuePostHog(ucid, DashboardAccessed, prop)
if err == nil {
datamap[UIEventKey] = "2"
cm.Data = datamap
_, err = impl.K8sUtil.UpdateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
impl.logger.Warnw("config map update failed for install event", "err", err)
} else {
impl.logger.Debugw("config map apply succeeded for install event")
}
}
}
return nil
}
func (impl *TelemetryEventClientImpl) SendTelemetryDashboardLoggedInEvent() error {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return err
}
client, err := impl.K8sUtil.GetClientForInCluster()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return err
}
payload := &TelemetryEventEA{UCID: ucid, Timestamp: time.Now(), EventType: DashboardLoggedIn, DevtronVersion: "v1"}
payload.DevtronMode = util.GetDevtronVersion().ServerMode
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("DashboardLoggedIn EventForTelemetry, payload marshal error", "error", err)
return err
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("DashboardLoggedIn EventForTelemetry, payload unmarshal error", "error", err)
return err
}
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, DevtronUniqueClientIdConfigMap, client)
datamap := cm.Data
accessEventValue, installEventKeyExists := datamap[UIEventKey]
if installEventKeyExists == false || accessEventValue != "3" {
err = impl.EnqueuePostHog(ucid, DashboardLoggedIn, prop)
if err == nil {
datamap[UIEventKey] = "3"
cm.Data = datamap
_, err = impl.K8sUtil.UpdateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
impl.logger.Warnw("config map update failed for install event", "err", err)
} else {
impl.logger.Debugw("config map apply succeeded for install event")
}
}
}
return nil
}
type TelemetryMetaInfo struct {
Url string `json:"url,omitempty"`
UCID string `json:"ucid,omitempty"`
ApiKey string `json:"apiKey,omitempty"`
}
func (impl *TelemetryEventClientImpl) getUCID() (string, error) {
ucid, found := impl.PosthogClient.cache.Get(DevtronUniqueClientIdConfigMapKey)
if found {
return ucid.(string), nil
} else {
client, err := impl.K8sUtil.GetClientForInCluster()
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return "", err
}
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, DevtronUniqueClientIdConfigMap, client)
if errStatus, ok := status.FromError(err); !ok || errStatus.Code() == codes.NotFound || errStatus.Code() == codes.Unknown {
// if not found, create new cm
cm = &v1.ConfigMap{ObjectMeta: v12.ObjectMeta{Name: DevtronUniqueClientIdConfigMap}}
data := map[string]string{}
data[DevtronUniqueClientIdConfigMapKey] = util.Generate(16) // generate unique random number
data[InstallEventKey] = "1" // used in operator to detect event is install or upgrade
data[UIEventKey] = "1"
cm.Data = data
_, err = impl.K8sUtil.CreateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
impl.logger.Errorw("exception while getting unique client id", "error", err)
return "", err
}
}
dataMap := cm.Data
ucid = dataMap[DevtronUniqueClientIdConfigMapKey]
impl.PosthogClient.cache.Set(DevtronUniqueClientIdConfigMapKey, ucid, cache.DefaultExpiration)
if cm == nil {
impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm)
return ucid.(string), err
}
flag, err := impl.checkForOptOut(ucid.(string))
if err != nil {
impl.logger.Errorw("error sending event to posthog, failed check for opt-out", "err", err)
return "", err
}
IsOptOut = flag
}
return ucid.(string), nil
}
func (impl *TelemetryEventClientImpl) checkForOptOut(UCID string) (bool, error) {
decodedUrl, err := base64.StdEncoding.DecodeString(TelemetryOptOutApiBaseUrl)
if err != nil {
impl.logger.Errorw("check opt-out list failed, decode error", "err", err)
return false, err
}
encodedUrl := string(decodedUrl)
url := fmt.Sprintf("%s/%s", encodedUrl, UCID)
response, err := util.HttpRequest(url)
if err != nil {
impl.logger.Errorw("check opt-out list failed, rest api error", "err", err)
return false, err
}
flag := response["result"].(bool)
return flag, err
}
func (impl *TelemetryEventClientImpl) retryPosthogClient(PosthogApiKey string, PosthogEndpoint string) (posthog.Client, error) {
client, err := posthog.NewWithConfig(PosthogApiKey, posthog.Config{Endpoint: PosthogEndpoint})
//defer client.Close()
if err != nil {
impl.logger.Errorw("exception caught while creating posthog client", "err", err)
}
return client, err
}
// returns installedIntegrations, installFailedIntegrations, installTimedOutIntegrations, installingIntegrations
func (impl *TelemetryEventClientImpl) buildIntegrationsList() ([]string, []string, []string, []string, error) {
impl.logger.Info("building integrations list for telemetry")
modules, err := impl.moduleRepository.FindAll()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error while getting integrations list", "err", err)
return nil, nil, nil, nil, err
}
var installedIntegrations []string
var installFailedIntegrations []string
var installTimedOutIntegrations []string
var installingIntegrations []string
for _, module := range modules {
integrationName := module.Name
switch module.Status {
case module2.ModuleStatusInstalled:
installedIntegrations = append(installedIntegrations, integrationName)
case module2.ModuleStatusInstallFailed:
installFailedIntegrations = append(installFailedIntegrations, integrationName)
case module2.ModuleStatusTimeout:
installTimedOutIntegrations = append(installTimedOutIntegrations, integrationName)
case module2.ModuleStatusInstalling:
installingIntegrations = append(installingIntegrations, integrationName)
}
}
return installedIntegrations, installFailedIntegrations, installTimedOutIntegrations, installingIntegrations, nil
}