Skip to content

Commit 0404234

Browse files
authored
Merge pull request #6618 from devtron-labs/posthog-dev
feat: Posthog events
2 parents 23570cc + 3051c3d commit 0404234

16 files changed

+374
-22
lines changed

client/telemetry/TelemetryEventClient.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ func (impl *TelemetryEventClientImpl) SummaryDetailsForTelemetry() (cluster []be
263263
return clusters, users, k8sServerVersion, hostURL, ssoSetup, HelmAppAccessCount, ChartStoreVisitCount, SkippedOnboarding, HelmAppUpdateCounter, helmChartSuccessfulDeploymentCount, ExternalHelmAppClusterCount
264264
}
265265

266+
// New methods for collecting additional telemetry metrics
267+
266268
func (impl *TelemetryEventClientImpl) SummaryEventForTelemetryEA() {
267269
err := impl.SendSummaryEvent(string(Summary))
268270
if err != nil {
@@ -310,6 +312,11 @@ func (impl *TelemetryEventClientImpl) SendSummaryEvent(eventType string) error {
310312
payload.HelmChartSuccessfulDeploymentCount = helmChartSuccessfulDeploymentCount
311313
payload.ExternalHelmAppClusterCount = ExternalHelmAppClusterCount
312314

315+
// Collect EA-mode compatible telemetry metrics
316+
payload.HelmAppCount = impl.getHelmAppCount()
317+
payload.PhysicalClusterCount, payload.IsolatedClusterCount = impl.getClusterCounts()
318+
payload.ActiveUsersLast30Days = impl.getActiveUsersLast30Days()
319+
313320
payload.ClusterProvider, err = impl.GetCloudProvider()
314321
if err != nil {
315322
impl.logger.Errorw("error while getting cluster provider", "error", err)

client/telemetry/TelemetryEventClientExtended.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
installedAppReader "github.com/devtron-labs/devtron/pkg/appStore/installedApp/read"
2727
"github.com/devtron-labs/devtron/pkg/auth/sso"
2828
user2 "github.com/devtron-labs/devtron/pkg/auth/user"
29+
authPolicyRepository "github.com/devtron-labs/devtron/pkg/auth/user/repository"
2930
"github.com/devtron-labs/devtron/pkg/build/git/gitMaterial/read"
3031
repository3 "github.com/devtron-labs/devtron/pkg/build/git/gitProvider/repository"
3132
"github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
@@ -34,6 +35,8 @@ import (
3435
"github.com/devtron-labs/devtron/pkg/cluster/environment"
3536
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
3637
moduleRepo "github.com/devtron-labs/devtron/pkg/module/repo"
38+
pluginRepository "github.com/devtron-labs/devtron/pkg/plugin/repository"
39+
cvePolicyRepository "github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning/repository"
3740
serverDataStore "github.com/devtron-labs/devtron/pkg/server/store"
3841
ucidService "github.com/devtron-labs/devtron/pkg/ucid"
3942
util3 "github.com/devtron-labs/devtron/pkg/util"
@@ -68,6 +71,11 @@ type TelemetryEventClientImplExtended struct {
6871
chartRepository chartRepoRepository.ChartRepository
6972
ciBuildConfigService pipeline.CiBuildConfigService
7073
gitOpsConfigReadService config.GitOpsConfigReadService
74+
// Additional repositories for FULL-mode telemetry metrics
75+
pluginRepository pluginRepository.GlobalPluginRepository
76+
cvePolicyRepository cvePolicyRepository.CvePolicyRepository
77+
defaultAuthPolicyRepository authPolicyRepository.DefaultAuthPolicyRepository
78+
rbacPolicyRepository authPolicyRepository.RbacPolicyDataRepository
7179
*TelemetryEventClientImpl
7280
}
7381

@@ -85,7 +93,12 @@ func NewTelemetryEventClientImplExtended(logger *zap.SugaredLogger, client *http
8593
ciBuildConfigService pipeline.CiBuildConfigService, moduleRepository moduleRepo.ModuleRepository, serverDataStore *serverDataStore.ServerDataStore,
8694
helmAppClient client.HelmAppClient, installedAppReadService installedAppReader.InstalledAppReadService, userAttributesRepository repository.UserAttributesRepository,
8795
cloudProviderIdentifierService cloudProviderIdentifier.ProviderIdentifierService, cronLogger *cron3.CronLoggerImpl,
88-
gitOpsConfigReadService config.GitOpsConfigReadService, envVariables *util.EnvironmentVariables) (*TelemetryEventClientImplExtended, error) {
96+
gitOpsConfigReadService config.GitOpsConfigReadService, envVariables *util.EnvironmentVariables,
97+
// Optional repositories for additional telemetry metrics
98+
pluginRepository pluginRepository.GlobalPluginRepository,
99+
cvePolicyRepository cvePolicyRepository.CvePolicyRepository,
100+
defaultAuthPolicyRepository authPolicyRepository.DefaultAuthPolicyRepository,
101+
rbacPolicyRepository authPolicyRepository.RbacPolicyDataRepository) (*TelemetryEventClientImplExtended, error) {
89102

90103
cron := cron.New(
91104
cron.WithChain(cron.Recover(cronLogger)))
@@ -105,6 +118,11 @@ func NewTelemetryEventClientImplExtended(logger *zap.SugaredLogger, client *http
105118
chartRepository: chartRepository,
106119
ciBuildConfigService: ciBuildConfigService,
107120
gitOpsConfigReadService: gitOpsConfigReadService,
121+
// Initialize FULL-mode specific repositories
122+
pluginRepository: pluginRepository,
123+
cvePolicyRepository: cvePolicyRepository,
124+
defaultAuthPolicyRepository: defaultAuthPolicyRepository,
125+
rbacPolicyRepository: rbacPolicyRepository,
108126
TelemetryEventClientImpl: &TelemetryEventClientImpl{
109127
cron: cron,
110128
logger: logger,
@@ -319,6 +337,21 @@ func (impl *TelemetryEventClientImplExtended) SendSummaryEvent(eventType string)
319337
payload.HelmChartSuccessfulDeploymentCount = HelmChartSuccessfulDeploymentCount
320338
payload.ExternalHelmAppClusterCount = ExternalHelmAppClusterCount
321339

340+
// Collect new telemetry metrics
341+
payload.HelmAppCount = impl.getHelmAppCount()
342+
payload.DevtronAppCount = impl.getDevtronAppCount()
343+
payload.JobCount = impl.getJobCount()
344+
payload.JobPipelineCount = impl.getJobPipelineCount()
345+
payload.JobPipelineTriggeredLast24h = impl.getJobPipelineTriggeredLast24h()
346+
payload.JobPipelineSucceededLast24h = impl.getJobPipelineSucceededLast24h()
347+
payload.UserCreatedPluginCount = impl.getUserCreatedPluginCount()
348+
payload.PolicyCount = impl.getPolicyCount()
349+
payload.AppliedPolicyRowCount = impl.getAppliedPolicyRowCount()
350+
payload.PhysicalClusterCount, payload.IsolatedClusterCount = impl.getClusterCounts()
351+
payload.ActiveUsersLast30Days = impl.getActiveUsersLast30Days()
352+
payload.GitOpsPipelineCount = impl.getGitOpsPipelineCount()
353+
payload.HelmPipelineCount = impl.helmPipelineCount()
354+
322355
payload.ClusterProvider, err = impl.GetCloudProvider()
323356
if err != nil {
324357
impl.logger.Errorw("error while getting cluster provider", "error", err)
@@ -374,7 +407,11 @@ func (impl *TelemetryEventClientImplExtended) getCiBuildTypeData() (int, int, in
374407
func (impl *TelemetryEventClientImplExtended) getCiBuildTypeVsStatusVsCount() (successCount map[bean.CiBuildType]int, failureCount map[bean.CiBuildType]int) {
375408
successCount = make(map[bean.CiBuildType]int)
376409
failureCount = make(map[bean.CiBuildType]int)
377-
buildTypeAndStatusVsCount := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
410+
buildTypeAndStatusVsCount, err := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
411+
if err != nil {
412+
impl.logger.Errorw("error getting build type vs status vs count data", "err", err)
413+
return successCount, failureCount
414+
}
378415
for _, buildTypeCount := range buildTypeAndStatusVsCount {
379416
if buildTypeCount == nil {
380417
continue

client/telemetry/bean.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ type TelemetryEventEA struct {
7575
HelmChartSuccessfulDeploymentCount int `json:"helmChartSuccessfulDeploymentCount,omitempty"`
7676
ExternalHelmAppClusterCount map[int32]int `json:"ExternalHelmAppClusterCount,omitempty"`
7777
ClusterProvider string `json:"clusterProvider,omitempty"`
78+
// New telemetry fields
79+
HelmAppCount int `json:"helmAppCount,omitempty"`
80+
PhysicalClusterCount int `json:"physicalClusterCount,omitempty"`
81+
IsolatedClusterCount int `json:"isolatedClusterCount,omitempty"`
82+
ActiveUsersLast30Days int `json:"activeUsersLast30Days,omitempty"`
7883
}
7984

8085
const AppsCount int = 50
@@ -136,4 +141,19 @@ type TelemetryEventDto struct {
136141
HelmChartSuccessfulDeploymentCount int `json:"helmChartSuccessfulDeploymentCount,omitempty"`
137142
ExternalHelmAppClusterCount map[int32]int `json:"ExternalHelmAppClusterCount"`
138143
ClusterProvider string `json:"clusterProvider,omitempty"`
144+
// New telemetry fields
145+
HelmAppCount int `json:"helmAppCount,omitempty"`
146+
DevtronAppCount int `json:"devtronAppCount,omitempty"`
147+
JobCount int `json:"jobCount,omitempty"`
148+
JobPipelineCount int `json:"jobPipelineCount,omitempty"`
149+
JobPipelineTriggeredLast24h int `json:"jobPipelineTriggeredLast24h,omitempty"`
150+
JobPipelineSucceededLast24h int `json:"jobPipelineSucceededLast24h,omitempty"`
151+
UserCreatedPluginCount int `json:"userCreatedPluginCount,omitempty"`
152+
PolicyCount int `json:"policyCount,omitempty"`
153+
AppliedPolicyRowCount int `json:"appliedPolicyRowCount,omitempty"`
154+
PhysicalClusterCount int `json:"physicalClusterCount,omitempty"`
155+
IsolatedClusterCount int `json:"isolatedClusterCount,omitempty"`
156+
ActiveUsersLast30Days int `json:"activeUsersLast30Days,omitempty"`
157+
GitOpsPipelineCount int `json:"gitOpsPipelineCount,omitempty"`
158+
HelmPipelineCount int `json:"helmPipelineCount,omitempty"`
139159
}

client/telemetry/telemetryQueries.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package telemetry
2+
3+
// EA-mode compatible telemetry queries - no imports needed for current methods
4+
5+
func (impl *TelemetryEventClientImpl) getHelmAppCount() int {
6+
if impl.installedAppReadService == nil {
7+
impl.logger.Warnw("installedAppReadService not available for helm app count")
8+
return -1
9+
}
10+
count, err := impl.installedAppReadService.GetActiveInstalledAppCount()
11+
if err != nil {
12+
impl.logger.Errorw("error getting helm app count", "err", err)
13+
return -1
14+
}
15+
return count
16+
}
17+
18+
// EA-mode compatible telemetry methods
19+
20+
func (impl *TelemetryEventClientImpl) getClusterCounts() (physicalCount int, isolatedCount int) {
21+
clusters, err := impl.clusterService.FindAllActive()
22+
if err != nil {
23+
impl.logger.Errorw("error getting cluster counts", "err", err)
24+
return -1, -1
25+
}
26+
27+
physicalCount = 0
28+
isolatedCount = 0
29+
30+
for _, cluster := range clusters {
31+
if cluster.IsVirtualCluster {
32+
isolatedCount++
33+
} else {
34+
physicalCount++
35+
}
36+
}
37+
38+
return physicalCount, isolatedCount
39+
}
40+
41+
// Note: FULL-mode specific methods like getDevtronAppCount, getJobCount, etc.
42+
// are now implemented in TelemetryEventClientImplExtended in telemetryQueriesExtended.go
43+
44+
func (impl *TelemetryEventClientImpl) getActiveUsersLast30Days() int {
45+
if impl.userAuditService == nil {
46+
impl.logger.Warnw("userAuditService not available for active users count")
47+
return -1
48+
}
49+
50+
count, err := impl.userAuditService.GetActiveUsersCountInLast30Days()
51+
if err != nil {
52+
impl.logger.Errorw("error getting active users count in last 30 days", "err", err)
53+
return -1
54+
}
55+
56+
impl.logger.Debugw("counted active users in last 30 days", "count", count)
57+
return count
58+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package telemetry
2+
3+
import (
4+
"github.com/devtron-labs/devtron/pkg/build/pipeline/bean/common"
5+
pluginRepository "github.com/devtron-labs/devtron/pkg/plugin/repository"
6+
)
7+
8+
// FULL-mode specific telemetry methods for TelemetryEventClientImplExtended
9+
10+
func (impl *TelemetryEventClientImplExtended) getDevtronAppCount() int {
11+
devtronAppCount, err := impl.appRepository.FindDevtronAppCount()
12+
if err != nil {
13+
impl.logger.Errorw("error getting all apps for devtron app count", "err", err)
14+
return -1
15+
}
16+
return devtronAppCount
17+
}
18+
19+
func (impl *TelemetryEventClientImplExtended) getJobCount() int {
20+
jobCount, err := impl.appRepository.FindJobCount()
21+
if err != nil {
22+
impl.logger.Errorw("error getting all apps for job count", "err", err)
23+
return -1
24+
}
25+
return jobCount
26+
}
27+
28+
func (impl *TelemetryEventClientImplExtended) getJobPipelineTriggeredLast24h() int {
29+
// Get build type and status data for the last 24 hours
30+
buildTypeStatusData, err := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
31+
if err != nil {
32+
impl.logger.Warnw("no build type status data available for last 24 hours")
33+
return -1
34+
}
35+
36+
// Count job pipeline triggers
37+
// Job pipelines have build type "CI_JOB"
38+
jobTriggeredCount := 0
39+
for _, data := range buildTypeStatusData {
40+
if data.Type == string(common.CI_JOB) {
41+
jobTriggeredCount += data.Count
42+
}
43+
}
44+
45+
return jobTriggeredCount
46+
}
47+
48+
func (impl *TelemetryEventClientImplExtended) getJobPipelineSucceededLast24h() int {
49+
// Get build type and status data for the last 24 hours
50+
buildTypeStatusData, err := impl.ciWorkflowRepository.FindBuildTypeAndStatusDataOfLast1Day()
51+
if err != nil {
52+
impl.logger.Warnw("no build type status data available for last 24 hours")
53+
return -1
54+
}
55+
56+
// Count successful job pipeline runs
57+
// Job pipelines have build type "CI_JOB"
58+
successfulJobCount := 0
59+
for _, data := range buildTypeStatusData {
60+
if data.Type == "CI_JOB" && data.Status == "Succeeded" {
61+
successfulJobCount += data.Count
62+
}
63+
}
64+
65+
impl.logger.Debugw("counted successful job pipeline runs in last 24h", "count", successfulJobCount)
66+
return successfulJobCount
67+
}
68+
69+
func (impl *TelemetryEventClientImplExtended) getUserCreatedPluginCount() int {
70+
// Get all user-created plugins (SHARED type)
71+
plugins, err := impl.pluginRepository.GetAllPluginMinDataByType(string(pluginRepository.PLUGIN_TYPE_SHARED))
72+
if err != nil {
73+
impl.logger.Errorw("error getting user created plugin count", "err", err)
74+
return -1
75+
}
76+
77+
return len(plugins)
78+
}
79+
80+
func (impl *TelemetryEventClientImplExtended) getPolicyCount() int {
81+
// Get global policies
82+
globalPolicies, err := impl.cvePolicyRepository.GetGlobalPolicies()
83+
if err != nil {
84+
impl.logger.Errorw("error getting global CVE policies", "err", err)
85+
return -1
86+
}
87+
return len(globalPolicies)
88+
}
89+
90+
func (impl *TelemetryEventClientImplExtended) getGitOpsPipelineCount() int {
91+
var count int
92+
query := `
93+
SELECT COUNT(DISTINCT p.id)
94+
FROM pipeline p
95+
WHERE p.deleted = false AND p.deployment_app_type = 'argo_cd'
96+
`
97+
98+
dbConnection := impl.cdWorkflowRepository.GetConnection()
99+
_, err := dbConnection.Query(&count, query)
100+
if err != nil {
101+
impl.logger.Errorw("error getting GitOps pipeline count", "err", err)
102+
return -1
103+
}
104+
105+
impl.logger.Debugw("counted GitOps pipelines", "count", count)
106+
return count
107+
}
108+
109+
func (impl *TelemetryEventClientImplExtended) helmPipelineCount() int {
110+
// Get the pipeline repository from cdWorkflowRepository connection
111+
var count int
112+
query := `
113+
SELECT COUNT(DISTINCT p.id)
114+
FROM pipeline p
115+
WHERE p.deleted = false AND p.deployment_app_type = 'helm'
116+
`
117+
118+
dbConnection := impl.cdWorkflowRepository.GetConnection()
119+
_, err := dbConnection.Query(&count, query)
120+
if err != nil {
121+
impl.logger.Errorw("error getting No-GitOps pipeline count", "err", err)
122+
return -1
123+
}
124+
125+
impl.logger.Debugw("counted No-GitOps pipelines", "count", count)
126+
return count
127+
}
128+
129+
// getJobPipelineCount returns 0 for now as implementation is not yet available
130+
func (impl *TelemetryEventClientImplExtended) getJobPipelineCount() int {
131+
return -1
132+
}
133+
134+
// getAppliedPolicyRowCount returns 0 for now as implementation is not yet available
135+
func (impl *TelemetryEventClientImplExtended) getAppliedPolicyRowCount() int {
136+
return -1
137+
}

env_gen.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

env_gen.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@
205205
| K8s_TCP_KEEPALIVE | int |30 | | | false |
206206
| K8s_TCP_TIMEOUT | int |30 | | | false |
207207
| K8s_TLS_HANDSHAKE_TIMEOUT | int |10 | | | false |
208-
| KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE | int |20 | | | false |
209-
| KUBELINK_GRPC_MAX_SEND_MSG_SIZE | int |4 | | | false |
210208
| LENS_TIMEOUT | int |0 | Lens microservice timeout. | | false |
211209
| LENS_URL | string |http://lens-milandevtron-service:80 | Lens micro-service URL | | false |
212210
| LIMIT_CI_CPU | string |0.5 | | | false |
@@ -287,9 +285,13 @@
287285
| DEX_HOST | string |http://localhost | | | false |
288286
| DEX_PORT | string |5556 | | | false |
289287
| GIT_SENSOR_PROTOCOL | string |REST | Protocol to connect with git-sensor micro-service | | false |
288+
| GIT_SENSOR_SERVICE_CONFIG | string |{"loadBalancingPolicy":"pick_first"} | git-sensor grpc service config | | false |
290289
| GIT_SENSOR_TIMEOUT | int |0 | Timeout for getting response from the git-sensor | | false |
291290
| GIT_SENSOR_URL | string |127.0.0.1:7070 | git-sensor micro-service url | | false |
292291
| HELM_CLIENT_URL | string |127.0.0.1:50051 | Kubelink micro-service url | | false |
292+
| KUBELINK_GRPC_MAX_RECEIVE_MSG_SIZE | int |20 | | | false |
293+
| KUBELINK_GRPC_MAX_SEND_MSG_SIZE | int |4 | | | false |
294+
| KUBELINK_GRPC_SERVICE_CONFIG | string |{"loadBalancingPolicy":"round_robin"} | kubelink grpc service config | | false |
293295

294296

295297
## POSTGRES Related Environment Variables

internal/sql/repository/app/AppRepository.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ type AppRepository interface {
9292
FindAppAndProjectByIdsIn(ids []int) ([]*App, error)
9393
FetchAppIdsByDisplayNamesForJobs(names []string) (map[int]string, []int, error)
9494
GetActiveCiCdAppsCount() (int, error)
95+
FindDevtronAppCount() (int, error)
96+
FindJobCount() (int, error)
9597

9698
UpdateAppOfferingModeForAppIds(successAppIds []*int, appOfferingMode string, userId int32) error
9799
}
@@ -520,6 +522,20 @@ func (repo AppRepositoryImpl) GetActiveCiCdAppsCount() (int, error) {
520522
Count()
521523
}
522524

525+
func (repo AppRepositoryImpl) FindDevtronAppCount() (int, error) {
526+
return repo.dbConnection.Model(&App{}).
527+
Where("active=?", true).
528+
Where("app_type=?", helper.CustomApp).
529+
Count()
530+
}
531+
532+
func (repo AppRepositoryImpl) FindJobCount() (int, error) {
533+
return repo.dbConnection.Model(&App{}).
534+
Where("active=?", true).
535+
Where("app_type=?", helper.Job).
536+
Count()
537+
}
538+
523539
func (repo AppRepositoryImpl) UpdateAppOfferingModeForAppIds(successAppIds []*int, appOfferingMode string, userId int32) error {
524540
query := "update app set app_offering_mode = ?,updated_by = ?, updated_on = ? where id in (?);"
525541
var app *App

0 commit comments

Comments
 (0)