Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POR-1756: add telemetry to pr creation endpoint #3598

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 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,9 +35,12 @@ func NewOpenStackPRHandler(
}

func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, _ := r.Context().Value(types.UserScope).(*models.User)
project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
ctx, span := telemetry.NewSpan(r.Context(), "serve-open-stack-pr")
defer span.End()

user, _ := ctx.Value(types.UserScope).(*models.User)
project, _ := ctx.Value(types.ProjectScope).(*models.Project)
cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
if reqErr != nil {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(reqErr, http.StatusBadRequest))
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))
jose-fully-ported marked this conversation as resolved.
Show resolved Hide resolved
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))
Comment on lines +92 to +94

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here, also prefer passing all error messages to client and obfuscating if necessary on client-side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

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