From 7962ee598e512bcb7fe75cd7fc85af528c08c2fd Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Tue, 22 Jun 2021 10:52:39 +0530 Subject: [PATCH 1/5] telemetry api added for dashboard to get ucid --- Wire.go | 5 +++ api/restHandler/TelemetryRouter.go | 60 ++++++++++++++++++++++++++++++ api/router/TelemetryRouter.go | 45 ++++++++++++++++++++++ api/router/router.go | 7 +++- internal/util/K8sUtil.go | 9 +++++ pkg/sso/SSOLoginService.go | 50 +++++++++++++++++++++++++ wire_gen.go | 4 +- 7 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 api/restHandler/TelemetryRouter.go create mode 100644 api/router/TelemetryRouter.go diff --git a/Wire.go b/Wire.go index e23dfed12a..4aa7a9d00b 100644 --- a/Wire.go +++ b/Wire.go @@ -651,6 +651,11 @@ func InitializeApp() (*App, error) { wire.Bind(new(router.SsoLoginRouter), new(*router.SsoLoginRouterImpl)), restHandler.NewSsoLoginRestHandlerImpl, wire.Bind(new(restHandler.SsoLoginRestHandler), new(*restHandler.SsoLoginRestHandlerImpl)), + + router.NewTelemetryRouterImpl, + wire.Bind(new(router.TelemetryRouter), new(*router.TelemetryRouterImpl)), + restHandler.NewTelemetryRestHandlerImpl, + wire.Bind(new(restHandler.TelemetryRestHandler), new(*restHandler.TelemetryRestHandlerImpl)), ) return &App{}, nil } diff --git a/api/restHandler/TelemetryRouter.go b/api/restHandler/TelemetryRouter.go new file mode 100644 index 0000000000..3426e64f89 --- /dev/null +++ b/api/restHandler/TelemetryRouter.go @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 Devtron Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package restHandler + +import ( + "github.com/devtron-labs/devtron/client/pubsub" + "github.com/devtron-labs/devtron/pkg/sso" + "github.com/devtron-labs/devtron/pkg/user" + "github.com/devtron-labs/devtron/util/rbac" + "go.uber.org/zap" + "gopkg.in/go-playground/validator.v9" + "net/http" +) + +type TelemetryRestHandler interface { + GetUCID(w http.ResponseWriter, r *http.Request) +} + +type TelemetryRestHandlerImpl struct { + userAuthService user.UserAuthService + validator *validator.Validate + logger *zap.SugaredLogger + enforcer rbac.Enforcer + natsClient *pubsub.PubSubClient + userService user.UserService + ssoLoginService sso.SSOLoginService +} + +func NewTelemetryRestHandlerImpl(userAuthService user.UserAuthService, validator *validator.Validate, + logger *zap.SugaredLogger, enforcer rbac.Enforcer, natsClient *pubsub.PubSubClient, userService user.UserService, + ssoLoginService sso.SSOLoginService) *TelemetryRestHandlerImpl { + handler := &TelemetryRestHandlerImpl{userAuthService: userAuthService, validator: validator, logger: logger, + enforcer: enforcer, natsClient: natsClient, userService: userService, ssoLoginService: ssoLoginService} + return handler +} + +func (handler TelemetryRestHandlerImpl) GetUCID(w http.ResponseWriter, r *http.Request) { + res, err := handler.ssoLoginService.GetUCID() + if err != nil { + handler.logger.Errorw("service err, GetUCID", "err", err) + writeJsonResp(w, err, nil, http.StatusInternalServerError) + return + } + writeJsonResp(w, nil, res, http.StatusOK) +} diff --git a/api/router/TelemetryRouter.go b/api/router/TelemetryRouter.go new file mode 100644 index 0000000000..fa745c1be1 --- /dev/null +++ b/api/router/TelemetryRouter.go @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Devtron Labs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package router + +import ( + "github.com/devtron-labs/devtron/api/restHandler" + "github.com/gorilla/mux" + "go.uber.org/zap" +) + +type TelemetryRouter interface { + initTelemetryRouter(router *mux.Router) +} + +type TelemetryRouterImpl struct { + logger *zap.SugaredLogger + handler restHandler.TelemetryRestHandler +} + +func NewTelemetryRouterImpl(logger *zap.SugaredLogger, handler restHandler.TelemetryRestHandler) *TelemetryRouterImpl { + router := &TelemetryRouterImpl{ + handler: handler, + } + return router +} + +func (router TelemetryRouterImpl) initTelemetryRouter(telemetryRouter *mux.Router) { + telemetryRouter.Path("/telemetry/ucid"). + HandlerFunc(router.handler.GetUCID).Methods("GET") +} diff --git a/api/router/router.go b/api/router/router.go index 8f9471e5bf..ac3a103492 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -72,6 +72,7 @@ type MuxRouter struct { commonRouter CommonRouter grafanaRouter GrafanaRouter ssoLoginRouter SsoLoginRouter + telemetryRouter TelemetryRouter } func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter HelmRouter, PipelineConfigRouter PipelineConfigRouter, @@ -90,7 +91,7 @@ func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter HelmRouter, PipelineConf ReleaseMetricsRouter ReleaseMetricsRouter, deploymentGroupRouter DeploymentGroupRouter, batchOperationRouter BatchOperationRouter, chartGroupRouter ChartGroupRouter, testSuitRouter TestSuitRouter, imageScanRouter ImageScanRouter, policyRouter PolicyRouter, gitOpsConfigRouter GitOpsConfigRouter, dashboardRouter DashboardRouter, attributesRouter AttributesRouter, - commonRouter CommonRouter, grafanaRouter GrafanaRouter, ssoLoginRouter SsoLoginRouter) *MuxRouter { + commonRouter CommonRouter, grafanaRouter GrafanaRouter, ssoLoginRouter SsoLoginRouter, telemetryRouter TelemetryRouter) *MuxRouter { r := &MuxRouter{ Router: mux.NewRouter(), HelmRouter: HelmRouter, @@ -134,6 +135,7 @@ func NewMuxRouter(logger *zap.SugaredLogger, HelmRouter HelmRouter, PipelineConf commonRouter: commonRouter, grafanaRouter: grafanaRouter, ssoLoginRouter: ssoLoginRouter, + telemetryRouter: telemetryRouter, } return r } @@ -256,4 +258,7 @@ func (r MuxRouter) Init() { ssoLoginRouter := r.Router.PathPrefix("/orchestrator/sso").Subrouter() r.ssoLoginRouter.initSsoLoginRouter(ssoLoginRouter) + + telemetryRouter := r.Router.PathPrefix("/orchestrator/telemetry").Subrouter() + r.telemetryRouter.initTelemetryRouter(telemetryRouter) } diff --git a/internal/util/K8sUtil.go b/internal/util/K8sUtil.go index 5849a6ec8a..c11e7e091b 100644 --- a/internal/util/K8sUtil.go +++ b/internal/util/K8sUtil.go @@ -124,6 +124,15 @@ func (impl K8sUtil) GetConfigMap(namespace string, name string, clusterConfig *C } } +func (impl K8sUtil) CreateConfigMap(namespace string, cm *v1.ConfigMap, client *v12.CoreV1Client) (*v1.ConfigMap, error) { + cm, err := client.ConfigMaps(namespace).Create(cm) + if err != nil { + return nil, err + } else { + return cm, nil + } +} + func (impl K8sUtil) GetConfigMapFast(namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error) { cm, err := client.ConfigMaps(namespace).Get(name, metav1.GetOptions{}) if err != nil { diff --git a/pkg/sso/SSOLoginService.go b/pkg/sso/SSOLoginService.go index 0e77af0a86..42d51953fd 100644 --- a/pkg/sso/SSOLoginService.go +++ b/pkg/sso/SSOLoginService.go @@ -27,9 +27,13 @@ import ( "github.com/devtron-labs/devtron/internal/util" "github.com/devtron-labs/devtron/pkg/cluster" "github.com/devtron-labs/devtron/pkg/user" + util2 "github.com/devtron-labs/devtron/util" "github.com/ghodss/yaml" "github.com/go-pg/pg" "go.uber.org/zap" + "k8s.io/api/core/v1" + v12 "k8s.io/apimachinery/pkg/apis/meta/v1" + "strings" "time" ) @@ -39,6 +43,7 @@ type SSOLoginService interface { GetById(id int32) (*bean.SSOLoginDto, error) GetAll() ([]*bean.SSOLoginDto, error) GetByName(name string) (*bean.SSOLoginDto, error) + GetUCID() (*PosthogData, error) } type SSOLoginServiceImpl struct { @@ -349,3 +354,48 @@ func (impl SSOLoginServiceImpl) GetByName(name string) (*bean.SSOLoginDto, error } return ssoLoginDto, nil } + +func (impl SSOLoginServiceImpl) GetUCID() (*PosthogData, error) { + clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName) + if err != nil { + return nil, err + } + cfg, err := impl.clusterService.GetClusterConfig(clusterBean) + if err != nil { + return nil, err + } + client, err := impl.K8sUtil.GetClient(cfg) + if err != nil { + return nil, err + } + + cm, err := impl.K8sUtil.GetConfigMapFast(impl.aCDAuthConfig.ACDConfigMapNamespace, "devtron-ucid", client) + if err != nil && strings.Contains(err.Error(), "not found") { + // if not found, create new cm + //cm = &v12.ConfigMap{ObjectMeta: v13.ObjectMeta{Name: "devtron-upid"}} + cm = &v1.ConfigMap{ObjectMeta: v12.ObjectMeta{Name: "devtron-ucid"}} + data := map[string]string{} + data["UCID"] = util2.Generate(16) // generate unique random number + cm.Data = data + _, err = impl.K8sUtil.CreateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client) + if err != nil { + return nil, err + } + } + + if cm == nil { + return nil, err + } + dataMap := cm.Data + ucid := dataMap["UCID"] + data := &PosthogData{ + Url: "https://posthog.com", + UCID: ucid, + } + return data, err +} + +type PosthogData struct { + Url string `json:"url,omitempty"` + UCID string `json:"ucid,omitempty"` +} diff --git a/wire_gen.go b/wire_gen.go index 116a4246fa..54c2f3c91a 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -389,7 +389,9 @@ func InitializeApp() (*App, error) { grafanaRouterImpl := router.NewGrafanaRouterImpl(sugaredLogger, grafanaConfig) ssoLoginRestHandlerImpl := restHandler.NewSsoLoginRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, ssoLoginServiceImpl) ssoLoginRouterImpl := router.NewSsoLoginRouterImpl(sugaredLogger, ssoLoginRestHandlerImpl, argocdServerConfig, dexConfig, argoCDSettings, userServiceImpl) - muxRouter := router.NewMuxRouter(sugaredLogger, helmRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, clusterAccountsRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, clusterHelmConfigRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, cronBasedEventReceiverImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl) + telemetryRestHandlerImpl := restHandler.NewTelemetryRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, ssoLoginServiceImpl) + telemetryRouterImpl := router.NewTelemetryRouterImpl(sugaredLogger, telemetryRestHandlerImpl) + muxRouter := router.NewMuxRouter(sugaredLogger, helmRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, clusterAccountsRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, clusterHelmConfigRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, cronBasedEventReceiverImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl, telemetryRouterImpl) mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, sessionManager, versionServiceImpl, enforcer, db, pubSubClient) return mainApp, nil } From 3eee77271e56569af42e45c46af7d8551956931a Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Tue, 22 Jun 2021 11:22:32 +0530 Subject: [PATCH 2/5] code refactored --- api/router/TelemetryRouter.go | 2 +- pkg/sso/SSOLoginService.go | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/api/router/TelemetryRouter.go b/api/router/TelemetryRouter.go index fa745c1be1..d77f7cdd73 100644 --- a/api/router/TelemetryRouter.go +++ b/api/router/TelemetryRouter.go @@ -40,6 +40,6 @@ func NewTelemetryRouterImpl(logger *zap.SugaredLogger, handler restHandler.Telem } func (router TelemetryRouterImpl) initTelemetryRouter(telemetryRouter *mux.Router) { - telemetryRouter.Path("/telemetry/ucid"). + telemetryRouter.Path("/ucid"). HandlerFunc(router.handler.GetUCID).Methods("GET") } diff --git a/pkg/sso/SSOLoginService.go b/pkg/sso/SSOLoginService.go index 42d51953fd..54ec0f737e 100644 --- a/pkg/sso/SSOLoginService.go +++ b/pkg/sso/SSOLoginService.go @@ -355,41 +355,49 @@ func (impl SSOLoginServiceImpl) GetByName(name string) (*bean.SSOLoginDto, error return ssoLoginDto, nil } +const DevtronUniqueClientIdConfigMap = "devtron-ucid" +const DevtronUniqueClientIdConfigMapKey = "UCID" + func (impl SSOLoginServiceImpl) GetUCID() (*PosthogData, error) { clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName) if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err } cfg, err := impl.clusterService.GetClusterConfig(clusterBean) if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err } client, err := impl.K8sUtil.GetClient(cfg) if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err } - cm, err := impl.K8sUtil.GetConfigMapFast(impl.aCDAuthConfig.ACDConfigMapNamespace, "devtron-ucid", client) + cm, err := impl.K8sUtil.GetConfigMapFast(impl.aCDAuthConfig.ACDConfigMapNamespace, DevtronUniqueClientIdConfigMap, client) if err != nil && strings.Contains(err.Error(), "not found") { // if not found, create new cm //cm = &v12.ConfigMap{ObjectMeta: v13.ObjectMeta{Name: "devtron-upid"}} - cm = &v1.ConfigMap{ObjectMeta: v12.ObjectMeta{Name: "devtron-ucid"}} + cm = &v1.ConfigMap{ObjectMeta: v12.ObjectMeta{Name: DevtronUniqueClientIdConfigMap}} data := map[string]string{} - data["UCID"] = util2.Generate(16) // generate unique random number + data[DevtronUniqueClientIdConfigMapKey] = util2.Generate(16) // generate unique random number 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 nil, err } } if cm == nil { + impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) return nil, err } dataMap := cm.Data - ucid := dataMap["UCID"] + ucid := dataMap[DevtronUniqueClientIdConfigMapKey] data := &PosthogData{ - Url: "https://posthog.com", + Url: "https://app.posthog.com/", UCID: ucid, } return data, err From 8a26a4c29d8454663b129ae3b94d206b2ae3114f Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Wed, 23 Jun 2021 19:22:35 +0530 Subject: [PATCH 3/5] code refacored for client id api for posthog --- ...metryRouter.go => TelemetryRestHandler.go} | 23 ++++---- client/telemetry/TelemetryEventClient.go | 51 +++++++++++++++++ pkg/sso/SSOLoginService.go | 55 ------------------- wire_gen.go | 14 ++--- 4 files changed, 70 insertions(+), 73 deletions(-) rename api/restHandler/{TelemetryRouter.go => TelemetryRestHandler.go} (75%) diff --git a/api/restHandler/TelemetryRouter.go b/api/restHandler/TelemetryRestHandler.go similarity index 75% rename from api/restHandler/TelemetryRouter.go rename to api/restHandler/TelemetryRestHandler.go index 3426e64f89..df2b1b87c9 100644 --- a/api/restHandler/TelemetryRouter.go +++ b/api/restHandler/TelemetryRestHandler.go @@ -19,7 +19,7 @@ package restHandler import ( "github.com/devtron-labs/devtron/client/pubsub" - "github.com/devtron-labs/devtron/pkg/sso" + "github.com/devtron-labs/devtron/client/telemetry" "github.com/devtron-labs/devtron/pkg/user" "github.com/devtron-labs/devtron/util/rbac" "go.uber.org/zap" @@ -32,25 +32,26 @@ type TelemetryRestHandler interface { } type TelemetryRestHandlerImpl struct { - userAuthService user.UserAuthService - validator *validator.Validate - logger *zap.SugaredLogger - enforcer rbac.Enforcer - natsClient *pubsub.PubSubClient - userService user.UserService - ssoLoginService sso.SSOLoginService + userAuthService user.UserAuthService + validator *validator.Validate + logger *zap.SugaredLogger + enforcer rbac.Enforcer + natsClient *pubsub.PubSubClient + userService user.UserService + telemetryEventClient telemetry.TelemetryEventClient } func NewTelemetryRestHandlerImpl(userAuthService user.UserAuthService, validator *validator.Validate, logger *zap.SugaredLogger, enforcer rbac.Enforcer, natsClient *pubsub.PubSubClient, userService user.UserService, - ssoLoginService sso.SSOLoginService) *TelemetryRestHandlerImpl { + telemetryEventClient telemetry.TelemetryEventClient) *TelemetryRestHandlerImpl { handler := &TelemetryRestHandlerImpl{userAuthService: userAuthService, validator: validator, logger: logger, - enforcer: enforcer, natsClient: natsClient, userService: userService, ssoLoginService: ssoLoginService} + enforcer: enforcer, natsClient: natsClient, userService: userService, + telemetryEventClient: telemetryEventClient} return handler } func (handler TelemetryRestHandlerImpl) GetUCID(w http.ResponseWriter, r *http.Request) { - res, err := handler.ssoLoginService.GetUCID() + res, err := handler.telemetryEventClient.GetUCID() if err != nil { handler.logger.Errorw("service err, GetUCID", "err", err) writeJsonResp(w, err, nil, http.StatusInternalServerError) diff --git a/client/telemetry/TelemetryEventClient.go b/client/telemetry/TelemetryEventClient.go index cb9c98742b..bd4165218a 100644 --- a/client/telemetry/TelemetryEventClient.go +++ b/client/telemetry/TelemetryEventClient.go @@ -40,6 +40,7 @@ type TelemetryEventClientImpl struct { } type TelemetryEventClient interface { + GetUCID() (*PosthogData, error) } func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService, @@ -283,3 +284,53 @@ func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() { Properties: prop, }) } + + +func (impl *TelemetryEventClientImpl) GetUCID() (*PosthogData, error) { + clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName) + if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) + return nil, err + } + cfg, err := impl.clusterService.GetClusterConfig(clusterBean) + if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) + return nil, err + } + client, err := impl.K8sUtil.GetClient(cfg) + if err != nil { + impl.logger.Errorw("exception while getting unique client id", "error", err) + return nil, 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 + 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 nil, err + } + } + + if cm == nil { + impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) + return nil, err + } + dataMap := cm.Data + ucid := dataMap[DevtronUniqueClientIdConfigMapKey] + data := &PosthogData{ + Url: impl.posthogConfig.PosthogEndpoint, + UCID: ucid, + } + return data, err +} + +type PosthogData struct { + Url string `json:"url,omitempty"` + UCID string `json:"ucid,omitempty"` +} diff --git a/pkg/sso/SSOLoginService.go b/pkg/sso/SSOLoginService.go index 2d67169861..4264bc0c28 100644 --- a/pkg/sso/SSOLoginService.go +++ b/pkg/sso/SSOLoginService.go @@ -28,14 +28,9 @@ import ( "github.com/devtron-labs/devtron/internal/util" "github.com/devtron-labs/devtron/pkg/cluster" "github.com/devtron-labs/devtron/pkg/user" - util2 "github.com/devtron-labs/devtron/util" "github.com/ghodss/yaml" "github.com/go-pg/pg" "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" "time" ) @@ -45,7 +40,6 @@ type SSOLoginService interface { GetById(id int32) (*bean.SSOLoginDto, error) GetAll() ([]*bean.SSOLoginDto, error) GetByName(name string) (*bean.SSOLoginDto, error) - GetUCID() (*PosthogData, error) } type SSOLoginServiceImpl struct { @@ -358,52 +352,3 @@ func (impl SSOLoginServiceImpl) GetByName(name string) (*bean.SSOLoginDto, error } return ssoLoginDto, nil } - -func (impl SSOLoginServiceImpl) GetUCID() (*PosthogData, error) { - clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName) - if err != nil { - impl.logger.Errorw("exception while getting unique client id", "error", err) - return nil, err - } - cfg, err := impl.clusterService.GetClusterConfig(clusterBean) - if err != nil { - impl.logger.Errorw("exception while getting unique client id", "error", err) - return nil, err - } - client, err := impl.K8sUtil.GetClient(cfg) - if err != nil { - impl.logger.Errorw("exception while getting unique client id", "error", err) - return nil, err - } - - cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, telemetry.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: telemetry.DevtronUniqueClientIdConfigMap}} - data := map[string]string{} - data[telemetry.DevtronUniqueClientIdConfigMapKey] = util2.Generate(16) // generate unique random number - 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 nil, err - } - } - - if cm == nil { - impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) - return nil, err - } - dataMap := cm.Data - ucid := dataMap[telemetry.DevtronUniqueClientIdConfigMapKey] - data := &PosthogData{ - Url: impl.posthogConfig.PosthogEndpoint, - UCID: ucid, - } - return data, err -} - -type PosthogData struct { - Url string `json:"url,omitempty"` - UCID string `json:"ucid,omitempty"` -} diff --git a/wire_gen.go b/wire_gen.go index 392cc3e331..d03dc6e03b 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -279,7 +279,11 @@ func InitializeApp() (*App, error) { pubSubClientRestHandlerImpl := restHandler.NewPubSubClientRestHandlerImpl(natsPublishClientImpl, sugaredLogger, cdConfig) webhookRouterImpl := router.NewWebhookRouterImpl(gitWebhookRestHandlerImpl, pipelineConfigRestHandlerImpl, externalCiRestHandlerImpl, pubSubClientRestHandlerImpl) ssoLoginRepositoryImpl := repository.NewSSOLoginRepositoryImpl(db) - ssoLoginServiceImpl := sso.NewSSOLoginServiceImpl(userAuthRepositoryImpl, sessionManager, sessionServiceClientImpl, sugaredLogger, userRepositoryImpl, roleGroupRepositoryImpl, ssoLoginRepositoryImpl, k8sUtil, clusterServiceImpl, environmentServiceImpl, acdAuthConfig) + posthogConfig, err := telemetry.GetPosthogConfig() + if err != nil { + return nil, err + } + ssoLoginServiceImpl := sso.NewSSOLoginServiceImpl(userAuthRepositoryImpl, sessionManager, sessionServiceClientImpl, sugaredLogger, userRepositoryImpl, roleGroupRepositoryImpl, ssoLoginRepositoryImpl, k8sUtil, clusterServiceImpl, environmentServiceImpl, acdAuthConfig, posthogConfig) userAuthHandlerImpl := restHandler.NewUserAuthHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, ssoLoginServiceImpl) userAuthRouterImpl := router.NewUserAuthRouterImpl(sugaredLogger, userAuthHandlerImpl, argocdServerConfig, dexConfig, argoCDSettings, userServiceImpl) pumpImpl := connector.NewPumpImpl(sugaredLogger) @@ -390,20 +394,16 @@ func InitializeApp() (*App, error) { grafanaRouterImpl := router.NewGrafanaRouterImpl(sugaredLogger, grafanaConfig) ssoLoginRestHandlerImpl := restHandler.NewSsoLoginRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, ssoLoginServiceImpl) ssoLoginRouterImpl := router.NewSsoLoginRouterImpl(sugaredLogger, ssoLoginRestHandlerImpl, argocdServerConfig, dexConfig, argoCDSettings, userServiceImpl) - telemetryRestHandlerImpl := restHandler.NewTelemetryRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, ssoLoginServiceImpl) - telemetryRouterImpl := router.NewTelemetryRouterImpl(sugaredLogger, telemetryRestHandlerImpl) posthogClient, err := telemetry.NewPosthogClient(sugaredLogger) if err != nil { return nil, err } - posthogConfig, err := telemetry.GetPosthogConfig() - if err != nil { - return nil, err - } telemetryEventClientImpl, err := telemetry.NewTelemetryEventClientImpl(sugaredLogger, httpClient, clusterServiceImpl, k8sUtil, acdAuthConfig, eventClientConfig, environmentServiceImpl, userServiceImpl, appListingRepositoryImpl, posthogClient, ciPipelineRepositoryImpl, pipelineRepositoryImpl, posthogConfig) if err != nil { return nil, err } + telemetryRestHandlerImpl := restHandler.NewTelemetryRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, telemetryEventClientImpl) + telemetryRouterImpl := router.NewTelemetryRouterImpl(sugaredLogger, telemetryRestHandlerImpl) muxRouter := router.NewMuxRouter(sugaredLogger, helmRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, clusterAccountsRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, clusterHelmConfigRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, cronBasedEventReceiverImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl, telemetryRouterImpl, telemetryEventClientImpl) mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, sessionManager, versionServiceImpl, enforcer, db, pubSubClient) return mainApp, nil From 49a4acdde42e36effa9d7a62cf1bb4ffb27a9031 Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Thu, 24 Jun 2021 08:06:09 +0530 Subject: [PATCH 4/5] review changes on posthog client id api for dashboard --- api/restHandler/TelemetryRestHandler.go | 24 ++----- api/router/TelemetryRouter.go | 2 +- client/telemetry/TelemetryEventClient.go | 87 ++++++++---------------- wire_gen.go | 4 +- 4 files changed, 36 insertions(+), 81 deletions(-) diff --git a/api/restHandler/TelemetryRestHandler.go b/api/restHandler/TelemetryRestHandler.go index df2b1b87c9..578407da6a 100644 --- a/api/restHandler/TelemetryRestHandler.go +++ b/api/restHandler/TelemetryRestHandler.go @@ -18,42 +18,30 @@ package restHandler import ( - "github.com/devtron-labs/devtron/client/pubsub" "github.com/devtron-labs/devtron/client/telemetry" - "github.com/devtron-labs/devtron/pkg/user" - "github.com/devtron-labs/devtron/util/rbac" "go.uber.org/zap" - "gopkg.in/go-playground/validator.v9" "net/http" ) type TelemetryRestHandler interface { - GetUCID(w http.ResponseWriter, r *http.Request) + GetClientPlatformIdAndTelemetryUrl(w http.ResponseWriter, r *http.Request) } type TelemetryRestHandlerImpl struct { - userAuthService user.UserAuthService - validator *validator.Validate logger *zap.SugaredLogger - enforcer rbac.Enforcer - natsClient *pubsub.PubSubClient - userService user.UserService telemetryEventClient telemetry.TelemetryEventClient } -func NewTelemetryRestHandlerImpl(userAuthService user.UserAuthService, validator *validator.Validate, - logger *zap.SugaredLogger, enforcer rbac.Enforcer, natsClient *pubsub.PubSubClient, userService user.UserService, +func NewTelemetryRestHandlerImpl(logger *zap.SugaredLogger, telemetryEventClient telemetry.TelemetryEventClient) *TelemetryRestHandlerImpl { - handler := &TelemetryRestHandlerImpl{userAuthService: userAuthService, validator: validator, logger: logger, - enforcer: enforcer, natsClient: natsClient, userService: userService, - telemetryEventClient: telemetryEventClient} + handler := &TelemetryRestHandlerImpl{logger: logger, telemetryEventClient: telemetryEventClient} return handler } -func (handler TelemetryRestHandlerImpl) GetUCID(w http.ResponseWriter, r *http.Request) { - res, err := handler.telemetryEventClient.GetUCID() +func (handler TelemetryRestHandlerImpl) GetClientPlatformIdAndTelemetryUrl(w http.ResponseWriter, r *http.Request) { + res, err := handler.telemetryEventClient.GetClientPlatformIdAndTelemetryUrl() if err != nil { - handler.logger.Errorw("service err, GetUCID", "err", err) + handler.logger.Errorw("service err, GetClientPlatformIdAndTelemetryUrl", "err", err) writeJsonResp(w, err, nil, http.StatusInternalServerError) return } diff --git a/api/router/TelemetryRouter.go b/api/router/TelemetryRouter.go index d77f7cdd73..691b7ea082 100644 --- a/api/router/TelemetryRouter.go +++ b/api/router/TelemetryRouter.go @@ -41,5 +41,5 @@ func NewTelemetryRouterImpl(logger *zap.SugaredLogger, handler restHandler.Telem func (router TelemetryRouterImpl) initTelemetryRouter(telemetryRouter *mux.Router) { telemetryRouter.Path("/ucid"). - HandlerFunc(router.handler.GetUCID).Methods("GET") + HandlerFunc(router.handler.GetClientPlatformIdAndTelemetryUrl).Methods("GET") } diff --git a/client/telemetry/TelemetryEventClient.go b/client/telemetry/TelemetryEventClient.go index bd4165218a..706be1179c 100644 --- a/client/telemetry/TelemetryEventClient.go +++ b/client/telemetry/TelemetryEventClient.go @@ -3,7 +3,6 @@ package telemetry import ( "encoding/json" "fmt" - client "github.com/devtron-labs/devtron/client/events" "github.com/devtron-labs/devtron/internal/sql/repository" "github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig" util2 "github.com/devtron-labs/devtron/internal/util" @@ -29,7 +28,6 @@ type TelemetryEventClientImpl struct { clusterService cluster.ClusterService K8sUtil *util2.K8sUtil aCDAuthConfig *user.ACDAuthConfig - config *client.EventClientConfig environmentService cluster.EnvironmentService userService user.UserService appListingRepository repository.AppListingRepository @@ -40,11 +38,11 @@ type TelemetryEventClientImpl struct { } type TelemetryEventClient interface { - GetUCID() (*PosthogData, error) + GetClientPlatformIdAndTelemetryUrl() (*PosthogData, error) } func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService, - K8sUtil *util2.K8sUtil, aCDAuthConfig *user.ACDAuthConfig, config *client.EventClientConfig, + K8sUtil *util2.K8sUtil, aCDAuthConfig *user.ACDAuthConfig, environmentService cluster.EnvironmentService, userService user.UserService, appListingRepository repository.AppListingRepository, PosthogClient *PosthogClient, ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository, @@ -56,7 +54,7 @@ func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, cron: cron, logger: logger, client: client, clusterService: clusterService, - K8sUtil: K8sUtil, aCDAuthConfig: aCDAuthConfig, config: config, + K8sUtil: K8sUtil, aCDAuthConfig: aCDAuthConfig, environmentService: environmentService, userService: userService, appListingRepository: appListingRepository, PosthogClient: PosthogClient, ciPipelineRepository: ciPipelineRepository, pipelineRepository: pipelineRepository, @@ -123,27 +121,11 @@ func (d TelemetryEventType) String() string { } func (impl *TelemetryEventClientImpl) SummaryEventForTelemetry() { - client, err := impl.K8sUtil.GetClientForInCluster() + cm, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception caught inside telemetry summary event", "err", err) return } - 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 - cm.Data = data - _, err = impl.K8sUtil.CreateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client) - if err != nil { - return - } - } - if cm == nil { - impl.logger.Errorw("cm found nil inside telemetry summary event", "cm", cm) - return - } dataMap := cm.Data ucid := dataMap[DevtronUniqueClientIdConfigMapKey] @@ -230,28 +212,11 @@ func (impl *TelemetryEventClientImpl) SummaryEventForTelemetry() { } func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() { - client, err := impl.K8sUtil.GetClientForInCluster() + cm, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err) return } - 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 - cm.Data = data - _, err = impl.K8sUtil.CreateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client) - if err != nil { - impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err) - return - } - } - if cm == nil { - impl.logger.Errorw("configmap found nil for telemetry heartbeat event", "cm", cm) - return - } dataMap := cm.Data ucid := dataMap[DevtronUniqueClientIdConfigMapKey] @@ -285,19 +250,33 @@ func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() { }) } - -func (impl *TelemetryEventClientImpl) GetUCID() (*PosthogData, error) { - clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName) +func (impl *TelemetryEventClientImpl) GetClientPlatformIdAndTelemetryUrl() (*PosthogData, error) { + cm, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err } - cfg, err := impl.clusterService.GetClusterConfig(clusterBean) - if err != nil { - impl.logger.Errorw("exception while getting unique client id", "error", err) + + if cm == nil { + impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) return nil, err } - client, err := impl.K8sUtil.GetClient(cfg) + dataMap := cm.Data + ucid := dataMap[DevtronUniqueClientIdConfigMapKey] + data := &PosthogData{ + Url: impl.posthogConfig.PosthogEndpoint, + UCID: ucid, + } + return data, err +} + +type PosthogData struct { + Url string `json:"url,omitempty"` + UCID string `json:"ucid,omitempty"` +} + +func (impl *TelemetryEventClientImpl) getUCID() (*v1.ConfigMap, error) { + client, err := impl.K8sUtil.GetClientForInCluster() if err != nil { impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err @@ -316,21 +295,9 @@ func (impl *TelemetryEventClientImpl) GetUCID() (*PosthogData, error) { return nil, err } } - if cm == nil { impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) return nil, err } - dataMap := cm.Data - ucid := dataMap[DevtronUniqueClientIdConfigMapKey] - data := &PosthogData{ - Url: impl.posthogConfig.PosthogEndpoint, - UCID: ucid, - } - return data, err -} - -type PosthogData struct { - Url string `json:"url,omitempty"` - UCID string `json:"ucid,omitempty"` + return cm, nil } diff --git a/wire_gen.go b/wire_gen.go index d03dc6e03b..1d1efdc7a4 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -398,11 +398,11 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - telemetryEventClientImpl, err := telemetry.NewTelemetryEventClientImpl(sugaredLogger, httpClient, clusterServiceImpl, k8sUtil, acdAuthConfig, eventClientConfig, environmentServiceImpl, userServiceImpl, appListingRepositoryImpl, posthogClient, ciPipelineRepositoryImpl, pipelineRepositoryImpl, posthogConfig) + telemetryEventClientImpl, err := telemetry.NewTelemetryEventClientImpl(sugaredLogger, httpClient, clusterServiceImpl, k8sUtil, acdAuthConfig, environmentServiceImpl, userServiceImpl, appListingRepositoryImpl, posthogClient, ciPipelineRepositoryImpl, pipelineRepositoryImpl, posthogConfig) if err != nil { return nil, err } - telemetryRestHandlerImpl := restHandler.NewTelemetryRestHandlerImpl(userAuthServiceImpl, validate, sugaredLogger, enforcerImpl, pubSubClient, userServiceImpl, telemetryEventClientImpl) + telemetryRestHandlerImpl := restHandler.NewTelemetryRestHandlerImpl(sugaredLogger, telemetryEventClientImpl) telemetryRouterImpl := router.NewTelemetryRouterImpl(sugaredLogger, telemetryRestHandlerImpl) muxRouter := router.NewMuxRouter(sugaredLogger, helmRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, clusterAccountsRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, clusterHelmConfigRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, cronBasedEventReceiverImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl, telemetryRouterImpl, telemetryEventClientImpl) mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, sessionManager, versionServiceImpl, enforcer, db, pubSubClient) From 4fece972be2cb7e65a92726848034b08f6d96ae0 Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Thu, 24 Jun 2021 09:17:35 +0530 Subject: [PATCH 5/5] cache implemented for configmap value for cient id --- client/telemetry/PosthogClient.go | 7 +++ client/telemetry/TelemetryEventClient.go | 71 ++++++++++++------------ 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/client/telemetry/PosthogClient.go b/client/telemetry/PosthogClient.go index 3258ac43d2..74abd9c148 100644 --- a/client/telemetry/PosthogClient.go +++ b/client/telemetry/PosthogClient.go @@ -19,12 +19,15 @@ package telemetry import ( "github.com/caarlos0/env" + "github.com/patrickmn/go-cache" "github.com/posthog/posthog-go" "go.uber.org/zap" + "time" ) type PosthogClient struct { Client posthog.Client + cache *cache.Cache } type PosthogConfig struct { @@ -32,6 +35,7 @@ type PosthogConfig struct { PosthogEndpoint string `env:"POSTHOG_ENDPOINT" envDefault:"https://app.posthog.com"` SummaryInterval int `env:"SUMMARY_INTERVAL" envDefault:"24"` HeartbeatInterval int `env:"HEARTBEAT_INTERVAL" envDefault:"3"` + CacheExpiry int `env:"CACHE_EXPIRY" envDefault:"120"` } func GetPosthogConfig() (*PosthogConfig, error) { @@ -52,8 +56,11 @@ func NewPosthogClient(logger *zap.SugaredLogger) (*PosthogClient, error) { } client, _ := posthog.NewWithConfig(cfg.ApiKey, posthog.Config{Endpoint: cfg.PosthogEndpoint}) //defer client.Close() + d := time.Duration(cfg.CacheExpiry) + c := cache.New(d*time.Minute, 240*time.Minute) pgClient := &PosthogClient{ Client: client, + cache: c, } return pgClient, nil } diff --git a/client/telemetry/TelemetryEventClient.go b/client/telemetry/TelemetryEventClient.go index 706be1179c..d415a9be7c 100644 --- a/client/telemetry/TelemetryEventClient.go +++ b/client/telemetry/TelemetryEventClient.go @@ -10,6 +10,7 @@ import ( "github.com/devtron-labs/devtron/pkg/user" "github.com/devtron-labs/devtron/util" "github.com/go-pg/pg" + "github.com/patrickmn/go-cache" "github.com/posthog/posthog-go" "go.uber.org/zap" "google.golang.org/grpc/codes" @@ -60,6 +61,7 @@ func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, ciPipelineRepository: ciPipelineRepository, pipelineRepository: pipelineRepository, posthogConfig: posthogConfig, } + watcher.HeartbeatEventForTelemetry() _, err := cron.AddFunc(fmt.Sprintf("@every %dm", watcher.posthogConfig.SummaryInterval), watcher.SummaryEventForTelemetry) if err != nil { @@ -121,14 +123,11 @@ func (d TelemetryEventType) String() string { } func (impl *TelemetryEventClientImpl) SummaryEventForTelemetry() { - cm, err := impl.getUCID() + ucid, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception caught inside telemetry summary event", "err", err) return } - dataMap := cm.Data - ucid := dataMap[DevtronUniqueClientIdConfigMapKey] - discoveryClient, err := impl.K8sUtil.GetK8sDiscoveryClientInCluster() if err != nil { impl.logger.Errorw("exception caught inside telemetry summary event", "err", err) @@ -212,14 +211,11 @@ func (impl *TelemetryEventClientImpl) SummaryEventForTelemetry() { } func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() { - cm, err := impl.getUCID() + ucid, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err) return } - dataMap := cm.Data - ucid := dataMap[DevtronUniqueClientIdConfigMapKey] - discoveryClient, err := impl.K8sUtil.GetK8sDiscoveryClientInCluster() if err != nil { impl.logger.Errorw("exception caught inside telemetry heartbeat event", "err", err) @@ -251,18 +247,11 @@ func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() { } func (impl *TelemetryEventClientImpl) GetClientPlatformIdAndTelemetryUrl() (*PosthogData, error) { - cm, err := impl.getUCID() + ucid, err := impl.getUCID() if err != nil { impl.logger.Errorw("exception while getting unique client id", "error", err) return nil, err } - - if cm == nil { - impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) - return nil, err - } - dataMap := cm.Data - ucid := dataMap[DevtronUniqueClientIdConfigMapKey] data := &PosthogData{ Url: impl.posthogConfig.PosthogEndpoint, UCID: ucid, @@ -275,29 +264,37 @@ type PosthogData struct { UCID string `json:"ucid,omitempty"` } -func (impl *TelemetryEventClientImpl) getUCID() (*v1.ConfigMap, error) { - client, err := impl.K8sUtil.GetClientForInCluster() - if err != nil { - impl.logger.Errorw("exception while getting unique client id", "error", err) - return nil, 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 - cm.Data = data - _, err = impl.K8sUtil.CreateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client) +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 nil, 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 + 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 } } - if cm == nil { - impl.logger.Errorw("configmap not found while getting unique client id", "cm", cm) - return nil, err - } - return cm, nil + return ucid.(string), nil }