Просмотр исходного кода

Merge pull request #1775 from porter-dev/belanger/add-provisioning-analytics

Add provisioning analytics to provisioner service
abelanger5 4 лет назад
Родитель
Сommit
79d79c5c07

+ 9 - 0
provisioner/server/config/config.go

@@ -11,6 +11,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/apierrors/alerter"
 	"github.com/porter-dev/porter/api/server/shared/config/env"
 	"github.com/porter-dev/porter/internal/adapter"
+	"github.com/porter-dev/porter/internal/analytics"
 	"github.com/porter-dev/porter/internal/kubernetes"
 	klocal "github.com/porter-dev/porter/internal/kubernetes/local"
 	"github.com/porter-dev/porter/internal/oauth"
@@ -68,6 +69,9 @@ type Config struct {
 	RedisClient *redis.Client
 
 	Provisioner provisioner.Provisioner
+
+	// AnalyticsClient if Segment analytics reporting is enabled on the API instance
+	AnalyticsClient analytics.AnalyticsSegmentClient
 }
 
 // ProvisionerConf is the env var configuration for the provisioner server
@@ -110,6 +114,9 @@ type ProvisionerConf struct {
 
 	// Options to configure for the "local" provisioner method
 	LocalTerraformDirectory string `env:"LOCAL_TERRAFORM_DIRECTORY"`
+
+	// Client key for segment to report provisioning events
+	SegmentClientKey string `env:"SEGMENT_CLIENT_KEY"`
 }
 
 type EnvConf struct {
@@ -221,6 +228,8 @@ func GetConfig(envConf *EnvConf) (*Config, error) {
 		})
 	}
 
+	res.AnalyticsClient = analytics.InitializeAnalyticsSegmentClient(envConf.ProvisionerConf.SegmentClientKey, res.Logger)
+
 	return res, nil
 }
 

+ 21 - 0
provisioner/server/handlers/provision/apply.go

@@ -9,6 +9,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/analytics"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/random"
 	"github.com/porter-dev/porter/provisioner/integrations/provisioner"
@@ -141,6 +142,26 @@ func (c *ProvisionApplyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
 
 	// return the operation response type to the server
 	c.resultWriter.WriteResult(w, r, op)
+
+	// if this is a cluster or registry infra type, send to analytics client
+	switch infra.Kind {
+	case types.InfraDOKS, types.InfraEKS, types.InfraGKE:
+		c.Config.AnalyticsClient.Track(analytics.ClusterProvisioningStartTrack(
+			&analytics.ClusterProvisioningStartTrackOpts{
+				ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
+				ClusterType:            infra.Kind,
+				InfraID:                infra.ID,
+			},
+		))
+	case types.InfraDOCR, types.InfraECR, types.InfraGCR:
+		c.Config.AnalyticsClient.Track(analytics.RegistryProvisioningStartTrack(
+			&analytics.RegistryProvisioningStartTrackOpts{
+				ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
+				RegistryType:           infra.Kind,
+				InfraID:                infra.ID,
+			},
+		))
+	}
 }
 
 func createCredentialsExchangeToken(conf *config.Config, infra *models.Infra) (*models.CredentialsExchangeToken, string, error) {

+ 14 - 1
provisioner/server/handlers/state/create_resource.go

@@ -13,6 +13,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/analytics"
 	"github.com/porter-dev/porter/internal/kubernetes"
 	"github.com/porter-dev/porter/internal/kubernetes/envgroup"
 	"github.com/porter-dev/porter/internal/models"
@@ -86,7 +87,19 @@ func (c *CreateResourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
 	// switch on the kind of resource and write the corresponding objects to the database
 	switch req.Kind {
 	case string(types.InfraEKS), string(types.InfraDOKS), string(types.InfraGKE):
-		_, err = createCluster(c.Config, infra, operation, req.Output)
+		var cluster *models.Cluster
+
+		cluster, err = createCluster(c.Config, infra, operation, req.Output)
+
+		if cluster != nil {
+			c.Config.AnalyticsClient.Track(analytics.ClusterProvisioningSuccessTrack(
+				&analytics.ClusterProvisioningSuccessTrackOpts{
+					ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(0, infra.ProjectID, cluster.ID),
+					ClusterType:            infra.Kind,
+					InfraID:                infra.ID,
+				},
+			))
+		}
 	case string(types.InfraECR):
 		_, err = createECRRegistry(c.Config, infra, operation, req.Output)
 	case string(types.InfraRDS):

+ 16 - 0
provisioner/server/handlers/state/report_error.go

@@ -7,6 +7,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/analytics"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/provisioner/integrations/redis_stream"
 	"github.com/porter-dev/porter/provisioner/server/config"
@@ -80,4 +81,19 @@ func (c *ReportErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(
 		fmt.Errorf(req.Error),
 	), false)
+
+	switch infra.Kind {
+	case types.InfraEKS, types.InfraDOKS, types.InfraGKE:
+		var cluster *models.Cluster
+
+		if cluster != nil {
+			c.Config.AnalyticsClient.Track(analytics.ClusterProvisioningErrorTrack(
+				&analytics.ClusterProvisioningErrorTrackOpts{
+					ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
+					ClusterType:            infra.Kind,
+					InfraID:                infra.ID,
+				},
+			))
+		}
+	}
 }