Przeglądaj źródła

Add telemetry, make stripe key optional

Mauricio Araujo 2 lat temu
rodzic
commit
96475d33fa

+ 6 - 1
api/server/handlers/billing/create.go

@@ -10,6 +10,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/telemetry"
 )
 
 // CreateBillingHandler is a handler for creating payment methods
@@ -29,10 +30,14 @@ func NewCreateBillingHandler(
 }
 
 func (c *CreateBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
+	defer span.End()
+
+	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
 
 	clientSecret, err := c.Config().BillingManager.CreatePaymentMethod(proj)
 	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error creating payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating payment method: %w", err)))
 		return
 	}

+ 13 - 7
api/server/handlers/billing/customer.go

@@ -10,11 +10,12 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/telemetry"
 )
 
-// CreateBillingCustomerIfNotExistsHandler will create a new handler
+// CreateBillingCustomerHandler will create a new handler
 // for creating customers in the billing provider
-type CreateBillingCustomerIfNotExistsHandler struct {
+type CreateBillingCustomerHandler struct {
 	handlers.PorterHandlerReadWriter
 }
 
@@ -23,14 +24,17 @@ func NewCreateBillingCustomerIfNotExists(
 	config *config.Config,
 	decoderValidator shared.RequestDecoderValidator,
 	writer shared.ResultWriter,
-) *CreateBillingCustomerIfNotExistsHandler {
-	return &CreateBillingCustomerIfNotExistsHandler{
+) *CreateBillingCustomerHandler {
+	return &CreateBillingCustomerHandler{
 		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
 	}
 }
 
-func (c *CreateBillingCustomerIfNotExistsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
+	defer span.End()
+
+	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
 
 	request := &types.CreateBillingCustomerRequest{}
 	if ok := c.DecodeAndValidate(w, r, request); !ok {
@@ -45,6 +49,7 @@ func (c *CreateBillingCustomerIfNotExistsHandler) ServeHTTP(w http.ResponseWrite
 	// Create customer in Stripe
 	customerID, err := c.Config().BillingManager.CreateCustomer(request.UserEmail, proj)
 	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error creating billing customer")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
 		return
 	}
@@ -53,7 +58,8 @@ func (c *CreateBillingCustomerIfNotExistsHandler) ServeHTTP(w http.ResponseWrite
 	proj.BillingID = customerID
 	_, err = c.Repo().Project().UpdateProject(proj)
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error updating record: %w", err)))
+		err := telemetry.Error(ctx, span, err, "error updating project")
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error updating project: %w", err)))
 		return
 	}
 

+ 7 - 1
api/server/handlers/billing/delete.go

@@ -10,6 +10,7 @@ import (
 	"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"
 )
 
 // DeleteBillingHandler is a handler for deleting payment methods
@@ -28,14 +29,19 @@ func NewDeleteBillingHandler(
 }
 
 func (c *DeleteBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
+	defer span.End()
+
 	paymentMethodID, reqErr := requestutils.GetURLParamString(r, types.URLParamPaymentMethodID)
 	if reqErr != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error deleting payment method: %w", fmt.Errorf("invalid id parameter"))))
+		err := telemetry.Error(ctx, span, reqErr, "error deleting payment method")
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error deleting payment method: %w", err)))
 		return
 	}
 
 	err := c.Config().BillingManager.DeletePaymentMethod(paymentMethodID)
 	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error deleting payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error deleting payment method: %w", err)))
 		return
 	}

+ 6 - 1
api/server/handlers/billing/list.go

@@ -10,6 +10,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/telemetry"
 )
 
 // ListBillingHandler is a handler for listing payment methods
@@ -28,10 +29,14 @@ func NewListBillingHandler(
 }
 
 func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
+	defer span.End()
+
+	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
 
 	paymentMethods, err := c.Config().BillingManager.ListPaymentMethod(proj)
 	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error listing payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error listing payment method: %w", err)))
 		return
 	}

+ 1 - 1
api/server/shared/config/loader/loader.go

@@ -254,7 +254,7 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 	res.LaunchDarklyClient = launchDarklyClient
 
 	if sc.StripeSecretKey == "" {
-		return nil, fmt.Errorf("STRIPE_SECRET_KEY must be set")
+		res.Logger.Info().Msg("STRIPE_SECRET_KEY not set, all Stripe functionality will be disabled")
 	}
 
 	if sc.SlackClientID != "" && sc.SlackClientSecret != "" {

+ 3 - 3
internal/billing/billing.go

@@ -19,13 +19,13 @@ type BillingManager interface {
 
 	// CreateCustomer registers a project in the billing provider. This is currently a one-to-one
 	// mapping with projects and billing customers, because billing and usage are set per project.
-	CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error)
+	CreateCustomer(userEmail string, proj models.Project) (customerID string, err error)
 
 	// ListPaymentMethod will return all payment methods for the project
-	ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error)
+	ListPaymentMethod(proj models.Project) (paymentMethods []types.PaymentMethod, err error)
 
 	// CreatePaymentMethod will add a new payment method to the project in Stripe
-	CreatePaymentMethod(proj *models.Project) (clientSecret string, err error)
+	CreatePaymentMethod(proj models.Project) (clientSecret string, err error)
 
 	// DeletePaymentMethod will remove a payment method for the project in Stripe
 	DeletePaymentMethod(paymentMethodID string) (err error)

+ 3 - 3
internal/billing/stripe..go

@@ -18,7 +18,7 @@ type StripeBillingManager struct {
 }
 
 // CreateCustomer will create a customer in Stripe only if the project doesn't have a BillingID
-func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error) {
+func (s *StripeBillingManager) CreateCustomer(userEmail string, proj models.Project) (customerID string, err error) {
 	stripe.Key = s.StripeSecretKey
 
 	if proj.BillingID == "" {
@@ -42,7 +42,7 @@ func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Pro
 }
 
 // ListPaymentMethod will return all payment methods for the project
-func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
+func (s *StripeBillingManager) ListPaymentMethod(proj models.Project) (paymentMethods []types.PaymentMethod, err error) {
 	stripe.Key = s.StripeSecretKey
 
 	params := &stripe.PaymentMethodListParams{
@@ -59,7 +59,7 @@ func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentM
 }
 
 // CreatePaymentMethod will add a new payment method to the project in Stripe
-func (s *StripeBillingManager) CreatePaymentMethod(proj *models.Project) (clientSecret string, err error) {
+func (s *StripeBillingManager) CreatePaymentMethod(proj models.Project) (clientSecret string, err error) {
 	stripe.Key = s.StripeSecretKey
 
 	params := &stripe.SetupIntentParams{