Skip to content

Commit

Permalink
feat: add telemetry to pr creation endpoint
Browse files Browse the repository at this point in the history
At the moment, this endpoint doesn't show any metrics in honeycomb, making it difficult to debug.

Refs POR-1756
  • Loading branch information
jose-fully-ported committed Sep 19, 2023
1 parent e34ed7d commit 0f54d0f
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions api/server/handlers/porter_app/create_secret_and_open_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/porter-dev/porter/internal/auth/token"
"github.com/porter-dev/porter/internal/integrations/ci/actions"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)

type OpenStackPRHandler struct {
Expand All @@ -34,6 +35,9 @@ func NewOpenStackPRHandler(
}

func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.NewSpan(r.Context(), "serve-open-stack-pr")
defer span.End()

user, _ := r.Context().Value(types.UserScope).(*models.User)
project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
Expand All @@ -45,11 +49,14 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

request := &types.CreateSecretAndOpenGHPRRequest{}
if ok := c.DecodeAndValidate(w, r, request); !ok {
err := telemetry.Error(ctx, span, nil, "error decoding request")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}

client, err := getGithubClient(c.Config(), request.GithubAppInstallationID)
if err != nil {
err := telemetry.Error(ctx, span, err, "error creating github client")
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
Expand All @@ -59,12 +66,16 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// generate porter jwt token
jwt, err := token.GetTokenForAPI(user.ID, project.ID)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error getting token for API: %w", err)))
err = fmt.Errorf("error getting token for API: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
encoded, err := jwt.EncodeToken(c.Config().TokenConf)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error encoding API token: %w", err)))
err = fmt.Errorf("error encoding API token: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

Expand All @@ -78,7 +89,9 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request.GithubRepoName,
)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error generating secret: %w", err)))
err = fmt.Errorf("error generating secret: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand Down Expand Up @@ -113,12 +126,16 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if unwrappedErr != nil {
if errors.Is(unwrappedErr, actions.ErrProtectedBranch) {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
return
} else if errors.Is(unwrappedErr, actions.ErrCreatePRForProtectedBranch) {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusPreconditionFailed))
return
}
} else {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error setting up application in the github "+
"repo: %w", err)))
err = fmt.Errorf("error setting up application in the github "+
"repo: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand All @@ -133,15 +150,19 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// update DB with the PR url
porterApp, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, appName)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("unable to get porter app db: %w", err)))
err = fmt.Errorf("unable to get porter app db: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

porterApp.PullRequestURL = pr.GetHTMLURL()

_, err = c.Repo().PorterApp().UpdatePorterApp(porterApp)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("unable to write pr url to porter app db: %w", err)))
err = fmt.Errorf("unable to write pr url to porter app db: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand Down

0 comments on commit 0f54d0f

Please sign in to comment.