Skip to content

Commit

Permalink
fix build/predeploy event reporting and add more analytics (#3654)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored Sep 26, 2023
1 parent a615920 commit 80b832d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
10 changes: 10 additions & 0 deletions api/server/handlers/porter_app/analytics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package porter_app

import (
"context"
"net/http"

"github.com/porter-dev/porter/api/server/handlers"
Expand All @@ -9,6 +10,7 @@ import (
"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/telemetry"
)

type PorterAppAnalyticsHandler struct {
Expand Down Expand Up @@ -101,13 +103,21 @@ func (v *PorterAppAnalyticsHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
}

func TrackStackBuildStatus(
ctx context.Context,
config *config.Config,
user *models.User,
project *models.Project,
stackName string,
errorMessage string,
status types.PorterAppEventStatus,
) error {
_, span := telemetry.NewSpan(ctx, "track-build-status")
defer span.End()

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "porter-app-build-status", Value: string(status)})
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "porter-app-name", Value: stackName})
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "porter-app-error-message", Value: errorMessage})

if status == types.PorterAppEventStatus_Progressing {
return config.AnalyticsClient.Track(analytics.StackBuildProgressingTrack(&analytics.StackBuildOpts{
ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, project.ID),
Expand Down
6 changes: 3 additions & 3 deletions api/server/handlers/porter_app/create_and_update_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (p *CreateUpdatePorterAppEventHandler) ServeHTTP(w http.ResponseWriter, r *
telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "porter-app-name", Value: appName},
telemetry.AttributeKV{Key: "porter-app-event-type", Value: string(request.Type)},
telemetry.AttributeKV{Key: "porter-app-event-status", Value: request.Status},
telemetry.AttributeKV{Key: "porter-app-event-status", Value: string(request.Status)},
telemetry.AttributeKV{Key: "porter-app-event-external-source", Value: request.TypeExternalSource},
telemetry.AttributeKV{Key: "porter-app-event-id", Value: request.ID},
telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
Expand Down Expand Up @@ -92,7 +92,7 @@ func reportBuildStatus(ctx context.Context, request *types.CreateOrUpdatePorterA
ctx, span := telemetry.NewSpan(ctx, "report-build-status")
defer span.End()

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "porter-app-build-status", Value: request.Status})
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "porter-app-build-status", Value: string(request.Status)})

var errStr string
if errors, ok := request.Metadata["errors"]; ok {
Expand All @@ -113,7 +113,7 @@ func reportBuildStatus(ctx context.Context, request *types.CreateOrUpdatePorterA
}
}

_ = TrackStackBuildStatus(config, user, project, stackName, errStr, request.Status)
_ = TrackStackBuildStatus(ctx, config, user, project, stackName, errStr, request.Status)
}

// createNewAppEvent will create a new app event for the given porter app name. If the app event is an agent event, then it will be created only if there is no existing event which has the agent ID. In the case that an existing event is found, that will be returned instead
Expand Down
4 changes: 2 additions & 2 deletions api/server/handlers/porter_app/list_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ func (p *PorterAppEventListHandler) updateBuildEvent_Github(
if *actionRun.Status == "completed" {
if *actionRun.Conclusion == "success" {
event.Status = string(types.PorterAppEventStatus_Success)
_ = TrackStackBuildStatus(p.Config(), user, project, stackName, "", types.PorterAppEventStatus_Success)
_ = TrackStackBuildStatus(ctx, p.Config(), user, project, stackName, "", types.PorterAppEventStatus_Success)
} else {
event.Status = string(types.PorterAppEventStatus_Failed)
_ = TrackStackBuildStatus(p.Config(), user, project, stackName, "", types.PorterAppEventStatus_Failed)
_ = TrackStackBuildStatus(ctx, p.Config(), user, project, stackName, "", types.PorterAppEventStatus_Failed)
}
event.Metadata["end_time"] = actionRun.GetUpdatedAt().Time
}
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/v2/app_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func createPredeployEvent(ctx context.Context, client api.Client, applicationNam
return event.ID, nil
}

func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
func updateExistingEvent(ctx context.Context, client api.Client, applicationName string, projectId, clusterId uint, deploymentTargetID string, eventType types.PorterAppEventType, eventID string, status types.PorterAppEventStatus, metadata map[string]interface{}) error {
ctx, span := telemetry.NewSpan(ctx, "update-existing-event")
defer span.End()

Expand All @@ -92,6 +92,7 @@ func updateExistingEvent(ctx context.Context, client api.Client, applicationName
Status: status,
Metadata: metadata,
DeploymentTargetID: deploymentTargetID,
Type: eventType,
}

_, err := client.CreateOrUpdatePorterAppEvent(ctx, projectId, clusterId, applicationName, req)
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/v2/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func Apply(ctx context.Context, inp ApplyInput) error {

buildMetadata := make(map[string]interface{})
buildMetadata["end_time"] = time.Now().UTC()
_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, eventID, types.PorterAppEventStatus_Success, buildMetadata)
_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_Build, eventID, types.PorterAppEventStatus_Success, buildMetadata)

applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
if err != nil {
Expand Down Expand Up @@ -243,7 +243,7 @@ func Apply(ctx context.Context, inp ApplyInput) error {

metadata := make(map[string]interface{})
metadata["end_time"] = time.Now().UTC()
_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, eventID, eventStatus, metadata)
_ = updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_PreDeploy, eventID, eventStatus, metadata)

applyResp, err = client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, "", "", applyResp.AppRevisionId, !forceBuild)
if err != nil {
Expand Down Expand Up @@ -474,7 +474,7 @@ func updateEnvGroupsInProto(ctx context.Context, base64AppProto string, envGroup
func reportBuildFailure(ctx context.Context, client api.Client, appName string, cliConf config.CLIConfig, deploymentTargetID string, appRevisionID string, eventID string) error {
buildMetadata := make(map[string]interface{})
buildMetadata["end_time"] = time.Now().UTC()
err := updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
err := updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_Build, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
if err != nil {
return err
}
Expand Down

0 comments on commit 80b832d

Please sign in to comment.