Skip to content

Commit

Permalink
fix: improve stripe logging (#2219)
Browse files Browse the repository at this point in the history
  • Loading branch information
turip authored Feb 5, 2025
1 parent afc9bbf commit fbe8ccf
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/common/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func NewAppStripeService(logger *slog.Logger, db *entdb.Client, appsConfig confi
AppService: appService,
CustomerService: customerService,
SecretService: secretService,
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to create appstripe adapter: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions openmeter/app/stripe/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"log/slog"

"github.com/openmeterio/openmeter/openmeter/app"
appstripe "github.com/openmeterio/openmeter/openmeter/app/stripe"
Expand All @@ -24,6 +25,7 @@ type Config struct {
SecretService secret.Service
StripeClientFactory stripeclient.StripeClientFactory
StripeAppClientFactory stripeclient.StripeAppClientFactory
Logger *slog.Logger
}

func (c Config) Validate() error {
Expand All @@ -43,6 +45,10 @@ func (c Config) Validate() error {
return errors.New("secret service is required")
}

if c.Logger == nil {
return errors.New("logger is required")
}

return nil
}

Expand All @@ -66,6 +72,7 @@ func New(config Config) (appstripe.Adapter, error) {
// Create app stripe adapter
adapter := &adapter{
db: config.Client,
logger: config.Logger,
appService: config.AppService,
customerService: config.CustomerService,
secretService: config.SecretService,
Expand All @@ -81,6 +88,8 @@ var _ appstripe.Adapter = (*adapter)(nil)
type adapter struct {
db *entdb.Client

logger *slog.Logger

appService app.Service
customerService customer.Service
secretService secret.Service
Expand Down
3 changes: 2 additions & 1 deletion openmeter/app/stripe/adapter/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a adapter) UpsertStripeCustomerData(ctx context.Context, input appstripeen
}

// Get the stripe app client
stripeAppData, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID)
stripeAppData, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID, "upsertStripeCustomerData", "customer_id", input.CustomerID.ID, "stripe_customer_id", input.StripeCustomerID)
if err != nil {
return fmt.Errorf("failed to get stripe app client: %w", err)
}
Expand Down Expand Up @@ -232,6 +232,7 @@ func (a adapter) createStripeCustomer(ctx context.Context, input appstripeentity
AppID: input.AppID,
AppService: a.appService,
APIKey: apiKeySecret.Value,
Logger: a.logger.With("operation", "createStripeCustomer", "app_id", input.AppID.ID, "customer_id", input.CustomerID.ID),
})
if err != nil {
return appstripeentity.CreateStripeCustomerOutput{}, fmt.Errorf("failed to create stripe client: %w", err)
Expand Down
9 changes: 6 additions & 3 deletions openmeter/app/stripe/adapter/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (a adapter) UpdateAPIKey(ctx context.Context, input appstripeentity.UpdateA
AppID: input.AppID,
AppService: a.appService,
APIKey: input.APIKey,
Logger: a.logger.With("operation", "validateStripeAPIKey", "app_id", input.AppID.ID),
})
if err != nil {
return fmt.Errorf("failed to create stripe client: %w", err)
Expand Down Expand Up @@ -501,6 +502,7 @@ func (a adapter) CreateCheckoutSession(ctx context.Context, input appstripeentit
AppID: appID,
AppService: repo.appService,
APIKey: apiKeySecret.Value,
Logger: a.logger.With("operation", "createCheckoutSession", "app_id", appID.ID, "customer_id", customerID.ID),
})
if err != nil {
return appstripeentity.CreateCheckoutSessionOutput{}, fmt.Errorf("failed to create stripe client: %w", err)
Expand Down Expand Up @@ -568,7 +570,7 @@ func (a adapter) GetSupplierContact(ctx context.Context, input appstripeentity.G
}

// Get Stripe App client
_, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID)
_, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID, "getSupplierContact", "app_id", input.AppID.ID)
if err != nil {
return billing.SupplierContact{}, fmt.Errorf("failed to get stripe app client: %w", err)
}
Expand Down Expand Up @@ -612,7 +614,7 @@ func (a adapter) GetStripeInvoice(ctx context.Context, input appstripeentity.Get
}

// Get Stripe App client
_, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID)
_, stripeAppClient, err := a.getStripeAppClient(ctx, input.AppID, "getStripeInvoice", "app_id", input.AppID.ID, "stripe_invoice_id", input.StripeInvoiceID)
if err != nil {
return nil, fmt.Errorf("failed to get stripe app client: %w", err)
}
Expand Down Expand Up @@ -645,7 +647,7 @@ func (a adapter) GetMaskedSecretAPIKey(secretAPIKeyID secretentity.SecretID) (st
}

// getStripeAppClient returns a Stripe App Client based on App ID
func (a adapter) getStripeAppClient(ctx context.Context, appID app.AppID) (appstripeentity.AppData, stripeclient.StripeAppClient, error) {
func (a adapter) getStripeAppClient(ctx context.Context, appID app.AppID, logOperation string, logFields ...any) (appstripeentity.AppData, stripeclient.StripeAppClient, error) {
// Validate app id
if err := appID.Validate(); err != nil {
return appstripeentity.AppData{}, nil, fmt.Errorf("app id: %w", err)
Expand All @@ -670,6 +672,7 @@ func (a adapter) getStripeAppClient(ctx context.Context, appID app.AppID) (appst
AppID: appID,
AppService: a.appService,
APIKey: apiKeySecret.Value,
Logger: a.logger.With("operation", logOperation).With(logFields...),
})
if err != nil {
return stripeAppData, nil, fmt.Errorf("failed to create stripe client: %w", err)
Expand Down
7 changes: 6 additions & 1 deletion openmeter/app/stripe/client/appclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type StripeAppClientConfig struct {
AppService app.Service
AppID app.AppID
APIKey string
Logger *slog.Logger
}

func (c *StripeAppClientConfig) Validate() error {
Expand All @@ -92,6 +93,10 @@ func (c *StripeAppClientConfig) Validate() error {
return fmt.Errorf("api key is required")
}

if c.Logger == nil {
return fmt.Errorf("logger is required")
}

return nil
}

Expand All @@ -108,7 +113,7 @@ func NewStripeAppClient(config StripeAppClientConfig) (StripeAppClient, error) {

backend := stripe.GetBackendWithConfig(stripe.APIBackend, &stripe.BackendConfig{
LeveledLogger: leveledLogger{
logger: slog.Default(),
logger: config.Logger,
},
})
client := &client.API{}
Expand Down
7 changes: 6 additions & 1 deletion openmeter/app/stripe/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type StripeClientFactory = func(config StripeClientConfig) (StripeClient, error)
type StripeClientConfig struct {
Namespace string
APIKey string
Logger *slog.Logger
}

func (c *StripeClientConfig) Validate() error {
Expand All @@ -43,6 +44,10 @@ func (c *StripeClientConfig) Validate() error {
return fmt.Errorf("api key is required")
}

if c.Logger == nil {
return fmt.Errorf("logger is required")
}

return nil
}

Expand All @@ -58,7 +63,7 @@ func NewStripeClient(config StripeClientConfig) (StripeClient, error) {

backend := stripe.GetBackendWithConfig(stripe.APIBackend, &stripe.BackendConfig{
LeveledLogger: leveledLogger{
logger: slog.Default(),
logger: config.Logger,
},
})
client := &client.API{}
Expand Down
9 changes: 5 additions & 4 deletions openmeter/app/stripe/client/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ type leveledLogger struct {
}

func (l leveledLogger) Debugf(format string, args ...interface{}) {
l.logger.Debug(fmt.Sprintf(format, args...))
l.logger.Debug(fmt.Sprintf(format, args...), "source", "stripe-go")
}

func (l leveledLogger) Infof(format string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(format, args...))
l.logger.Info(fmt.Sprintf(format, args...), "source", "stripe-go")
}

func (l leveledLogger) Warnf(format string, args ...interface{}) {
l.logger.Warn(fmt.Sprintf(format, args...))
l.logger.Warn(fmt.Sprintf(format, args...), "source", "stripe-go")
}

func (l leveledLogger) Errorf(format string, args ...interface{}) {
l.logger.Error(fmt.Sprintf(format, args...))
// We don't want to pollute the logs with errors from the Stripe API as we are handling them in the application
l.logger.Warn(fmt.Sprintf(format, args...), "source", "stripe-go")
}
7 changes: 7 additions & 0 deletions openmeter/app/stripe/entity/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appstripeentityapp
import (
"errors"
"fmt"
"log/slog"

"github.com/openmeterio/openmeter/openmeter/app"
stripeapp "github.com/openmeterio/openmeter/openmeter/app/stripe"
Expand All @@ -16,6 +17,8 @@ type App struct {
app.AppBase
appstripeentity.AppData

Logger *slog.Logger `json:"-"`

AppService app.Service `json:"-"`
StripeAppClientFactory stripeclient.StripeAppClientFactory `json:"-"`
StripeAppService stripeapp.Service `json:"-"`
Expand Down Expand Up @@ -55,5 +58,9 @@ func (a App) Validate() error {
return errors.New("secret service is required")
}

if a.Logger == nil {
return errors.New("logger is required")
}

return nil
}
3 changes: 2 additions & 1 deletion openmeter/app/stripe/entity/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// getStripeClient gets the Stripe client for the app
func (a App) getStripeClient(ctx context.Context) (appstripeentity.AppData, stripeclient.StripeAppClient, error) {
func (a App) getStripeClient(ctx context.Context, logOperation string, logFields ...any) (appstripeentity.AppData, stripeclient.StripeAppClient, error) {
// Get Stripe App
stripeAppData, err := a.StripeAppService.GetStripeAppData(ctx, appstripeentity.GetStripeAppDataInput{
AppID: a.GetID(),
Expand All @@ -30,6 +30,7 @@ func (a App) getStripeClient(ctx context.Context) (appstripeentity.AppData, stri
AppID: a.GetID(),
AppService: a.AppService,
APIKey: apiKeySecret.Value,
Logger: a.Logger.With("operation", logOperation).With(logFields...),
})
if err != nil {
return appstripeentity.AppData{}, nil, fmt.Errorf("failed to create stripe client: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion openmeter/app/stripe/entity/app/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (a App) ValidateCustomerByID(ctx context.Context, customerID customer.Custo
}

// Stripe Client
stripeAppData, stripeClient, err := a.getStripeClient(ctx)
stripeAppData, stripeClient, err := a.getStripeClient(ctx, "validateCustomer", "customer_id", customerID.ID)
if err != nil {
return fmt.Errorf("failed to get stripe client: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions openmeter/app/stripe/entity/app/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (a App) UpsertInvoice(ctx context.Context, invoice billing.Invoice) (*billi
// DeleteInvoice deletes the invoice for the app
func (a App) DeleteInvoice(ctx context.Context, invoice billing.Invoice) error {
// Get the Stripe client
_, stripeClient, err := a.getStripeClient(ctx)
_, stripeClient, err := a.getStripeClient(ctx, "deleteInvoice", "invoice_id", invoice.ID, "stripe_invoice_id", invoice.ExternalIDs.GetInvoicingOrEmpty())
if err != nil {
return fmt.Errorf("failed to get stripe client: %w", err)
}
Expand All @@ -87,7 +87,7 @@ func (a App) DeleteInvoice(ctx context.Context, invoice billing.Invoice) error {
// FinalizeInvoice finalizes the invoice for the app
func (a App) FinalizeInvoice(ctx context.Context, invoice billing.Invoice) (*billing.FinalizeInvoiceResult, error) {
// Get the Stripe client
_, stripeClient, err := a.getStripeClient(ctx)
_, stripeClient, err := a.getStripeClient(ctx, "finalizeInvoice", "invoice_id", invoice.ID, "stripe_invoice_id", invoice.ExternalIDs.GetInvoicingOrEmpty())
if err != nil {
return nil, fmt.Errorf("failed to get stripe client: %w", err)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (a App) createInvoice(ctx context.Context, invoice billing.Invoice) (*billi
}

// Get the Stripe client
_, stripeClient, err := a.getStripeClient(ctx)
_, stripeClient, err := a.getStripeClient(ctx, "createInvoice", "customer_id", customerID.ID)
if err != nil {
return nil, fmt.Errorf("failed to get stripe client: %w", err)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (a App) updateInvoice(ctx context.Context, invoice billing.Invoice) (*billi
}

// Get the Stripe client
_, stripeClient, err := a.getStripeClient(ctx)
_, stripeClient, err := a.getStripeClient(ctx, "updateInvoice", "invoice_id", invoice.ID, "stripe_invoice_id", invoice.ExternalIDs.GetInvoicingOrEmpty())
if err != nil {
return nil, fmt.Errorf("failed to get stripe client: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions openmeter/app/stripe/service/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *Service) InstallAppWithAPIKey(ctx context.Context, input app.AppFactory
stripeClient, err := s.adapter.GetStripeClientFactory()(stripeclient.StripeClientConfig{
Namespace: input.Namespace,
APIKey: input.APIKey,
Logger: s.logger.With("operation", "installAppWithAPIKey", "namespace", input.Namespace, "app_name", input.Name),
})
if err != nil {
return nil, fmt.Errorf("failed to create stripe client: %w", err)
Expand Down Expand Up @@ -178,6 +179,7 @@ func (s *Service) UninstallApp(ctx context.Context, input app.UninstallAppInput)
AppID: input,
AppService: s.appService,
APIKey: apiKeySecret.Value,
Logger: s.logger.With("operation", "uninstalApp", "app_id", input.ID),
})
if err != nil {
return fmt.Errorf("failed to create stripe client")
Expand Down Expand Up @@ -216,6 +218,7 @@ func (s *Service) newApp(appBase app.AppBase, stripeApp appstripeentity.AppData)
StripeAppService: s,
SecretService: s.secretService,
StripeAppClientFactory: s.adapter.GetStripeAppClientFactory(),
Logger: s.logger,
}

if err := app.Validate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions openmeter/billing/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ type InvoiceExternalIDs struct {
Payment string `json:"payment,omitempty"`
}

func (i *InvoiceExternalIDs) GetInvoicingOrEmpty() string {
if i == nil {
return ""
}
return i.Invoicing
}

type InvoiceAvailableActions struct {
Advance *InvoiceAvailableActionDetails `json:"advance,omitempty"`
Approve *InvoiceAvailableActionDetails `json:"approve,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions test/app/stripe/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (s *StripeInvoiceTestSuite) SetupSuite() {
StripeAppClientFactory: func(config stripeclient.StripeAppClientConfig) (stripeclient.StripeAppClient, error) {
return stripeAppClient, nil
},
Logger: slog.Default(),
})
s.Require().NoError(err, "failed to create app stripe adapter")

Expand Down
1 change: 1 addition & 0 deletions test/app/stripe/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func NewTestEnv(t *testing.T, ctx context.Context) (TestEnv, error) {
StripeAppClientFactory: func(config stripeclient.StripeAppClientConfig) (stripeclient.StripeAppClient, error) {
return stripeAppClientMock, nil
},
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to create appstripe adapter: %w", err)
Expand Down
1 change: 1 addition & 0 deletions test/app/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func NewTestEnv(t *testing.T, ctx context.Context) (TestEnv, error) {
AppService: appService,
CustomerService: customerService,
SecretService: secretService,
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("failed to create appstripe adapter: %w", err)
Expand Down

0 comments on commit fbe8ccf

Please sign in to comment.