-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelemetryEventClientExtended.go
253 lines (227 loc) · 11.4 KB
/
TelemetryEventClientExtended.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
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
package telemetry
import (
"encoding/json"
"github.com/devtron-labs/template-cron-job/internal/sql/repository"
"github.com/devtron-labs/template-cron-job/internal/sql/repository/app"
"github.com/devtron-labs/template-cron-job/internal/sql/repository/pipelineConfig"
util2 "github.com/devtron-labs/template-cron-job/internal/util"
chartRepoRepository "github.com/devtron-labs/template-cron-job/pkg/chartRepo/repository"
"github.com/devtron-labs/template-cron-job/pkg/cluster"
"github.com/devtron-labs/template-cron-job/pkg/sso"
"github.com/devtron-labs/template-cron-job/pkg/user"
util3 "github.com/devtron-labs/template-cron-job/pkg/util"
"github.com/devtron-labs/template-cron-job/util"
"github.com/go-pg/pg"
"go.uber.org/zap"
"net/http"
"time"
)
const AppsCount int = 50
type TelemetryEventClientImplExtended struct {
environmentService cluster.EnvironmentService
appListingRepository repository.AppListingRepository
ciPipelineRepository pipelineConfig.CiPipelineRepository
pipelineRepository pipelineConfig.PipelineRepository
gitOpsConfigRepository repository.GitOpsConfigRepository
gitProviderRepository repository.GitProviderRepository
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository
appRepository app.AppRepository
ciWorkflowRepository pipelineConfig.CiWorkflowRepository
cdWorkflowRepository pipelineConfig.CdWorkflowRepository
materialRepository pipelineConfig.MaterialRepository
ciTemplateRepository pipelineConfig.CiTemplateRepository
chartRepository chartRepoRepository.ChartRepository
*TelemetryEventClientImpl
}
func NewTelemetryEventClientImplExtended(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService,
K8sUtil *util2.K8sUtil, aCDAuthConfig *util3.ACDAuthConfig,
environmentService cluster.EnvironmentService, userService user.UserService,
appListingRepository repository.AppListingRepository, PosthogClient *PosthogClient,
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
gitOpsConfigRepository repository.GitOpsConfigRepository, gitProviderRepository repository.GitProviderRepository,
attributeRepo repository.AttributesRepository, ssoLoginService sso.SSOLoginService, appRepository app.AppRepository,
ciWorkflowRepository pipelineConfig.CiWorkflowRepository, cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository,
materialRepository pipelineConfig.MaterialRepository, ciTemplateRepository pipelineConfig.CiTemplateRepository,
chartRepository chartRepoRepository.ChartRepository) (*TelemetryEventClientImplExtended, error) {
watcher := &TelemetryEventClientImplExtended{
environmentService: environmentService,
appListingRepository: appListingRepository,
ciPipelineRepository: ciPipelineRepository,
pipelineRepository: pipelineRepository,
gitOpsConfigRepository: gitOpsConfigRepository,
gitProviderRepository: gitProviderRepository,
dockerArtifactStoreRepository: dockerArtifactStoreRepository,
appRepository: appRepository,
cdWorkflowRepository: cdWorkflowRepository,
ciWorkflowRepository: ciWorkflowRepository,
materialRepository: materialRepository,
ciTemplateRepository: ciTemplateRepository,
chartRepository: chartRepository,
TelemetryEventClientImpl: &TelemetryEventClientImpl{
logger: logger,
client: client,
clusterService: clusterService,
K8sUtil: K8sUtil,
aCDAuthConfig: aCDAuthConfig,
userService: userService,
attributeRepo: attributeRepo,
ssoLoginService: ssoLoginService,
PosthogClient: PosthogClient,
},
}
return watcher, nil
}
type TelemetryEventDto struct {
UCID string `json:"ucid"` //unique client id
Timestamp time.Time `json:"timestamp"`
EventMessage string `json:"eventMessage,omitempty"`
EventType TelemetryEventType `json:"eventType"`
ProdAppCount int `json:"prodAppCount,omitempty"`
NonProdAppCount int `json:"nonProdAppCount,omitempty"`
UserCount int `json:"userCount,omitempty"`
EnvironmentCount int `json:"environmentCount,omitempty"`
ClusterCount int `json:"clusterCount,omitempty"`
CiCountPerDay int `json:"ciCountPerDay,omitempty"`
CdCountPerDay int `json:"cdCountPerDay,omitempty"`
HelmChartCount int `json:"helmChartCount,omitempty"`
SecurityScanCountPerDay int `json:"securityScanCountPerDay,omitempty"`
GitAccountsCount int `json:"gitAccountsCount,omitempty"`
GitOpsCount int `json:"gitOpsCount,omitempty"`
RegistryCount int `json:"registryCount,omitempty"`
HostURL bool `json:"hostURL,omitempty"`
SSOLogin bool `json:"ssoLogin,omitempty"`
AppCount int `json:"appCount,omitempty"`
AppsWithGitRepoConfigured int `json:"appsWithGitRepoConfigured,omitempty"`
AppsWithDockerConfigured int `json:"appsWithDockerConfigured,omitempty"`
AppsWithDeploymentTemplateConfigured int `json:"appsWithDeploymentTemplateConfigured,omitempty"`
AppsWithCiPipelineConfigured int `json:"appsWithCiPipelineConfigured,omitempty"`
AppsWithCdPipelineConfigured int `json:"appsWithCdPipelineConfigured,omitempty"`
Build bool `json:"build,omitempty"`
Deployment bool `json:"deployment,omitempty"`
ServerVersion string `json:"serverVersion,omitempty"`
DevtronGitVersion string `json:"devtronGitVersion,omitempty"`
DevtronVersion string `json:"devtronVersion,omitempty"`
DevtronMode string `json:"devtronMode,omitempty"`
}
func (impl *TelemetryEventClientImplExtended) SummaryEventForTelemetry() {
ucid, err := impl.getUCID()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
if IsOptOut {
impl.logger.Warnw("client is opt-out for telemetry, there will be no events capture", "ucid", ucid)
return
}
clusters, users, k8sServerVersion, hostURL, ssoSetup := impl.SummaryDetailsForTelemetry()
payload := &TelemetryEventDto{UCID: ucid, Timestamp: time.Now(), EventType: Summary, DevtronVersion: "v1"}
payload.ServerVersion = k8sServerVersion.String()
environments, err := impl.environmentService.GetAllActive()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
prodApps, err := impl.appListingRepository.FindAppCount(true)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
nonProdApps, err := impl.appListingRepository.FindAppCount(false)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
ciPipeline, err := impl.ciPipelineRepository.FindAllPipelineInLast24Hour()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
cdPipeline, err := impl.pipelineRepository.FindAllPipelineInLast24Hour()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
gitAccounts, err := impl.gitProviderRepository.FindAll()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
gitOps, err := impl.gitOpsConfigRepository.GetAllGitOpsConfig()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
containerRegistry, err := impl.dockerArtifactStoreRepository.FindAll()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
//appSetup := false
apps, err := impl.appRepository.FindAll()
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
return
}
var appIds []int
for _, appInfo := range apps {
appIds = append(appIds, appInfo.Id)
}
payload.AppCount = len(appIds)
if len(appIds) < AppsCount {
payload.AppsWithGitRepoConfigured, err = impl.materialRepository.FindNumberOfAppsWithGitRepo(appIds)
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
}
payload.AppsWithDockerConfigured, err = impl.ciTemplateRepository.FindNumberOfAppsWithDockerConfigured(appIds)
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
}
payload.AppsWithDeploymentTemplateConfigured, err = impl.chartRepository.FindNumberOfAppsWithDeploymentTemplate(appIds)
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
}
payload.AppsWithCiPipelineConfigured, err = impl.ciPipelineRepository.FindNumberOfAppsWithCiPipeline(appIds)
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
}
payload.AppsWithCdPipelineConfigured, err = impl.pipelineRepository.FindNumberOfAppsWithCdPipeline(appIds)
if err != nil {
impl.logger.Errorw("exception caught inside telemetry summary event", "err", err)
}
}
build, err := impl.ciWorkflowRepository.ExistsByStatus("Succeeded")
deployment, err := impl.cdWorkflowRepository.ExistsByStatus("Healthy")
devtronVersion := util.GetDevtronVersion()
payload.ProdAppCount = prodApps
payload.NonProdAppCount = nonProdApps
payload.RegistryCount = len(containerRegistry)
payload.SSOLogin = ssoSetup
payload.UserCount = len(users)
payload.EnvironmentCount = len(environments)
payload.ClusterCount = len(clusters)
payload.CiCountPerDay = len(ciPipeline)
payload.CdCountPerDay = len(cdPipeline)
payload.GitAccountsCount = len(gitAccounts)
payload.GitOpsCount = len(gitOps)
payload.HostURL = hostURL
payload.DevtronGitVersion = devtronVersion.GitCommit
payload.Build = build
payload.Deployment = deployment
payload.DevtronMode = devtronVersion.ServerMode
reqBody, err := json.Marshal(payload)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, payload marshal error", "error", err)
return
}
prop := make(map[string]interface{})
err = json.Unmarshal(reqBody, &prop)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, payload unmarshal error", "error", err)
return
}
err = impl.EnqueuePostHog(ucid, Summary, prop)
if err != nil {
impl.logger.Errorw("SummaryEventForTelemetry, failed to push event", "ucid", ucid, "error", err)
}
}