Explorar el Código

add telemetry to re-run github workflow (#3803)

Feroze Mohideen hace 2 años
padre
commit
8894d08bec
Se han modificado 1 ficheros con 45 adiciones y 15 borrados
  1. 45 15
      api/server/handlers/gitinstallation/rerun_workflow.go

+ 45 - 15
api/server/handlers/gitinstallation/rerun_workflow.go

@@ -11,6 +11,9 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
 	"github.com/porter-dev/porter/api/server/shared/commonutils"
 	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/api/server/shared/requestutils"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/telemetry"
 )
 
 type RerunWorkflowHandler struct {
@@ -28,9 +31,20 @@ func NewRerunWorkflowHandler(
 }
 
 func (c *RerunWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
+	ctx, span := telemetry.NewSpan(r.Context(), "serve-rerun-github-workflow")
+	defer span.End()
 
-	if !ok {
+	owner, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoOwner)
+	if reqErr != nil {
+		err := telemetry.Error(ctx, span, nil, "repo owner not found in request")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
+		return
+	}
+
+	name, reqErr := requestutils.GetURLParamString(r, types.URLParamGitRepoName)
+	if reqErr != nil {
+		err := telemetry.Error(ctx, span, nil, "repo name not found in request")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
 		return
 	}
 
@@ -40,9 +54,17 @@ func (c *RerunWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	// only the very latest of the workflow runs will be rerun
 	branch := r.URL.Query().Get("branch")
 	releaseName := r.URL.Query().Get("release_name")
+	telemetry.WithAttributes(
+		span,
+		telemetry.AttributeKV{Key: "repo-owner", Value: owner},
+		telemetry.AttributeKV{Key: "repo-name", Value: name},
+		telemetry.AttributeKV{Key: "branch", Value: branch},
+		telemetry.AttributeKV{Key: "release-name", Value: releaseName},
+	)
 
 	if filename == "" && releaseName == "" {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("filename and release name are both empty")))
+		err := telemetry.Error(ctx, span, nil, "filename and release name are both empty")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
 		return
 	}
 
@@ -59,45 +81,53 @@ func (c *RerunWorkflowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		}
 	}
 
+	telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "filename", Value: filename})
+
 	client, err := GetGithubAppClientFromRequest(c.Config(), r)
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		err := telemetry.Error(ctx, span, err, "error getting github app client from request")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
 		return
 	}
 
 	latestWorkflowRun, err := commonutils.GetLatestWorkflowRun(client, owner, name, filename, branch)
-
 	if err != nil && errors.Is(err, commonutils.ErrNoWorkflowRuns) {
-		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, 400))
+		err = telemetry.Error(ctx, span, err, "no workflow runs found")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
 		return
 	} else if err != nil && errors.Is(err, commonutils.ErrWorkflowNotFound) {
-		w.WriteHeader(http.StatusNotFound)
-		c.WriteResult(w, r, filename)
+		err = telemetry.Error(ctx, span, err, "workflow not found")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusNotFound))
 		return
 	} else if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		err = telemetry.Error(ctx, span, err, "error getting latest workflow run")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
 		return
 	}
 
 	if latestWorkflowRun.GetStatus() == "in_progress" || latestWorkflowRun.GetStatus() == "queued" {
-		w.WriteHeader(409)
+		telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "existing-pending-workflow-status", Value: latestWorkflowRun.GetStatus()})
+		w.WriteHeader(http.StatusConflict)
 		c.WriteResult(w, r, latestWorkflowRun.GetHTMLURL())
 		return
 	}
 
 	_, err = client.Actions.RerunWorkflowByID(r.Context(), owner, name, latestWorkflowRun.GetID())
-
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		err = telemetry.Error(ctx, span, err, "error rerunning workflow")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
 		return
 	}
 
 	latestWorkflowRun, err = commonutils.GetLatestWorkflowRun(client, owner, name, filename, branch)
-
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		err = telemetry.Error(ctx, span, err, "error getting latest workflow run after rerunning workflow")
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
 		return
 	}
 
-	c.WriteResult(w, r, latestWorkflowRun.GetHTMLURL())
+	newWorkflowRunUrl := latestWorkflowRun.GetHTMLURL()
+	telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "new-workflow-run-url", Value: newWorkflowRunUrl})
+
+	c.WriteResult(w, r, newWorkflowRunUrl)
 }