Skip to content
Draft
Show file tree
Hide file tree
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
11 changes: 5 additions & 6 deletions billing/checkout/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import (
"github.com/raystack/frontier/billing/credit"

"github.com/google/uuid"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/raystack/frontier/billing/subscription"
"go.uber.org/zap"
frontierlogger "github.com/raystack/frontier/pkg/logger"

"github.com/raystack/frontier/billing/plan"
"github.com/raystack/frontier/billing/product"
Expand Down Expand Up @@ -183,12 +182,12 @@ func (s *Service) backgroundSync(ctx context.Context) {
defer record()
}

logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
customers, err := s.customerService.List(ctx, customer.Filter{
State: customer.ActiveState,
})
if err != nil {
logger.Error("checkout.backgroundSync", zap.Error(err))
logger.Error("checkout.backgroundSync", "error", err)
return
}

Expand All @@ -202,10 +201,10 @@ func (s *Service) backgroundSync(ctx context.Context) {
continue
}
if err := s.SyncWithProvider(ctx, customer.ID); err != nil {
logger.Error("checkout.SyncWithProvider", zap.Error(err), zap.String("customer_id", customer.ID))
logger.Error("checkout.SyncWithProvider", "error", err, "customer_id", customer.ID)
}
}
logger.Info("checkout.backgroundSync finished", zap.Duration("duration", time.Since(start)))
logger.Info("checkout.backgroundSync finished", "duration", time.Since(start))
}

func (s *Service) Create(ctx context.Context, ch Checkout) (Checkout, error) {
Expand Down
11 changes: 5 additions & 6 deletions billing/customer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
"github.com/raystack/frontier/billing"
"github.com/raystack/frontier/internal/metrics"

grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
frontierlogger "github.com/raystack/frontier/pkg/logger"
"golang.org/x/exp/slices"

"github.com/raystack/frontier/pkg/metadata"
Expand Down Expand Up @@ -390,12 +389,12 @@ func (s *Service) backgroundSync(ctx context.Context) {
record := metrics.BillingSyncLatency("customer")
defer record()
}
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
customers, err := s.List(ctx, Filter{
State: ActiveState,
})
if err != nil {
logger.Error("customer.backgroundSync", zap.Error(err))
logger.Error("customer.backgroundSync", "error", err)
return
}

Expand All @@ -409,11 +408,11 @@ func (s *Service) backgroundSync(ctx context.Context) {
continue
}
if err := s.SyncWithProvider(ctx, customer); err != nil {
logger.Error("customer.SyncWithProvider", zap.Error(err), zap.String("customer_id", customer.ID))
logger.Error("customer.SyncWithProvider", "error", err, "customer_id", customer.ID)
}
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
logger.Info("customer.backgroundSync finished", zap.Duration("duration", time.Since(start)))
logger.Info("customer.backgroundSync finished", "duration", time.Since(start))
}

// SyncWithProvider syncs the customer state with the billing provider
Expand Down
32 changes: 15 additions & 17 deletions billing/invoice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import (
"github.com/raystack/frontier/billing"
"github.com/raystack/frontier/internal/metrics"

"github.com/raystack/frontier/pkg/utils"
"github.com/raystack/salt/rql"
"go.uber.org/zap"

grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/raystack/frontier/billing/customer"
frontierlogger "github.com/raystack/frontier/pkg/logger"
"github.com/raystack/frontier/pkg/metadata"
"github.com/raystack/frontier/pkg/utils"
"github.com/raystack/salt/rql"
"github.com/stripe/stripe-go/v79/client"
)

Expand Down Expand Up @@ -112,7 +110,7 @@ func NewService(stripeClient *client.API, invoiceRepository Repository,
}

func (s *Service) Init(ctx context.Context) error {
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
if s.syncDelay != time.Duration(0) {
if s.syncJob != nil {
s.syncJob.Stop()
Expand Down Expand Up @@ -151,8 +149,8 @@ func (s *Service) Init(ctx context.Context) error {
s.creditOverdraftInvoiceCurrency = creditPrice.Currency
s.creditOverdraftUnitAmount = int64(float64(creditPrice.Amount) / float64(creditProduct.Config.CreditAmount))
logger.Info("credit overdraft product details",
zap.Int64("unit_amount", s.creditOverdraftUnitAmount),
zap.String("currency", s.creditOverdraftInvoiceCurrency))
"unit_amount", s.creditOverdraftUnitAmount,
"currency", s.creditOverdraftInvoiceCurrency)
}
return nil
}
Expand All @@ -170,12 +168,12 @@ func (s *Service) backgroundSync(ctx context.Context) {
record := metrics.BillingSyncLatency("invoice")
defer record()
}
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
customers, err := s.customerService.List(ctx, customer.Filter{
Online: utils.Bool(true),
})
if err != nil {
logger.Error("invoice.backgroundSync", zap.Error(err))
logger.Error("invoice.backgroundSync", "error", err)
return
}
for _, customr := range customers {
Expand All @@ -188,20 +186,20 @@ func (s *Service) backgroundSync(ctx context.Context) {
continue
}
if err := s.SyncWithProvider(ctx, customr); err != nil {
logger.Error("invoice.SyncWithProvider", zap.Error(err))
logger.Error("invoice.SyncWithProvider", "error", err)
}
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
}
if err := s.Reconcile(ctx); err != nil {
logger.Error("invoice.Reconcile", zap.Error(err))
logger.Error("invoice.Reconcile", "error", err)
}

if s.isCreditOverdraftDayOfInvoice() {
if err := s.GenerateForCredits(ctx); err != nil {
logger.Error("invoice.GenerateForCredits", zap.Error(err))
logger.Error("invoice.GenerateForCredits", "error", err)
}
}
logger.Info("invoice.backgroundSync finished", zap.Duration("duration", time.Since(start)))
logger.Info("invoice.backgroundSync finished", "duration", time.Since(start))
}

// TriggerCreditOverdraftInvoices is on-demand trigger for generating credit overdraft invoices
Expand Down Expand Up @@ -314,7 +312,7 @@ func (s *Service) List(ctx context.Context, filter Filter) ([]Invoice, error) {
// GetUpcoming returns the upcoming invoice for the customer based on the
// active subscription plan. If no upcoming invoice is found, it returns empty.
func (s *Service) GetUpcoming(ctx context.Context, customerID string) (Invoice, error) {
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
custmr, err := s.customerService.GetByID(ctx, customerID)
if err != nil {
return Invoice{}, fmt.Errorf("failed to find customer: %w", err)
Expand Down Expand Up @@ -431,7 +429,7 @@ func (s *Service) DeleteByCustomer(ctx context.Context, c customer.Customer) err
// reconcile the token balance once it's paid.
func (s *Service) GenerateForCredits(ctx context.Context) error {
var errs []error
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
if s.creditOverdraftUnitAmount == 0 || s.creditOverdraftInvoiceCurrency == "" {
// do not process if credit overdraft details not set
return nil
Expand All @@ -449,7 +447,7 @@ func (s *Service) GenerateForCredits(ctx context.Context) error {
defer func() {
unlockErr := lock.Unlock(ctx)
if unlockErr != nil {
logger.Error("failed to unlock", zap.Error(unlockErr), zap.String("key", GenerateForCreditLockKey))
logger.Error("failed to unlock", "error", unlockErr, "key", GenerateForCreditLockKey)
}
}()

Expand Down
11 changes: 5 additions & 6 deletions billing/subscription/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"github.com/raystack/frontier/billing/product"
"github.com/raystack/frontier/pkg/utils"

grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
frontierlogger "github.com/raystack/frontier/pkg/logger"

"github.com/raystack/frontier/billing/plan"

Expand Down Expand Up @@ -149,12 +148,12 @@ func (s *Service) backgroundSync(ctx context.Context) {
record := metrics.BillingSyncLatency("subscription")
defer record()
}
logger := grpczap.Extract(ctx)
logger := frontierlogger.FromContext(ctx)
customers, err := s.customerService.List(ctx, customer.Filter{
State: customer.ActiveState,
})
if err != nil {
logger.Error("subscription.backgroundSync", zap.Error(err))
logger.Error("subscription.backgroundSync", "error", err)
return
}

Expand All @@ -168,11 +167,11 @@ func (s *Service) backgroundSync(ctx context.Context) {
continue
}
if err := s.SyncWithProvider(ctx, customer); err != nil {
logger.Error("subscription.SyncWithProvider", zap.Error(err), zap.String("customer_id", customer.ID))
logger.Error("subscription.SyncWithProvider", "error", err, "customer_id", customer.ID)
}
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
logger.Info("subscription.backgroundSync finished", zap.Duration("duration", time.Since(start)))
logger.Info("subscription.backgroundSync finished", "duration", time.Since(start))
}

func (s *Service) TriggerSyncByProviderID(ctx context.Context, id string) error {
Expand Down
6 changes: 3 additions & 3 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"log/slog"

"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database"
Expand All @@ -15,10 +16,9 @@ import (
"github.com/raystack/frontier/internal/store/postgres"
"github.com/raystack/frontier/internal/store/postgres/migrations"
"github.com/raystack/frontier/pkg/db"
"github.com/raystack/salt/log"
)

func RunMigrations(logger log.Logger, config db.Config) error {
func RunMigrations(logger *slog.Logger, config db.Config) error {
m, err := getDatabaseMigrationInstance(config)
if err != nil {
return err
Expand All @@ -44,7 +44,7 @@ func RunMigrations(logger log.Logger, config db.Config) error {
return err
}

func RunRollback(logger log.Logger, config db.Config) error {
func RunRollback(logger *slog.Logger, config db.Config) error {
m, err := getDatabaseMigrationInstance(config)
if err != nil {
return err
Expand Down
18 changes: 9 additions & 9 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ import (

"github.com/raystack/frontier/core/event"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"

"github.com/raystack/frontier/billing/invoice"

"github.com/raystack/frontier/billing/usage"
Expand Down Expand Up @@ -72,6 +70,7 @@ import (
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/raystack/frontier/core/authenticate/token"

frontierlogger "github.com/raystack/frontier/pkg/logger"
"github.com/raystack/frontier/pkg/server"

"github.com/raystack/frontier/core/invitation"
Expand Down Expand Up @@ -107,14 +106,15 @@ import (
"github.com/raystack/frontier/internal/store/spicedb"
"github.com/raystack/frontier/pkg/db"

"log/slog"

"github.com/pkg/profile"
"github.com/raystack/salt/log"
)

var ruleCacheRefreshDelay = time.Minute * 2
var GetStripeClientFunc func(logger log.Logger, cfg *config.Frontier) *client.API
var GetStripeClientFunc func(logger *slog.Logger, cfg *config.Frontier) *client.API

func StartServer(logger *log.Zap, cfg *config.Frontier) error {
func StartServer(logger *slog.Logger, cfg *config.Frontier) error {
logger.Info("frontier starting", "version", config.Version)
if profiling := os.Getenv("FRONTIER_PROFILE"); profiling == "true" || profiling == "1" {
defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
Expand All @@ -123,7 +123,7 @@ func StartServer(logger *log.Zap, cfg *config.Frontier) error {
ctx, cancelFunc := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancelFunc()

ctx = ctxzap.ToContext(ctx, logger.GetInternalZapLogger().Desugar())
ctx = frontierlogger.ToContext(ctx, logger)

dbClient, err := setupDB(cfg.DB, logger)
if err != nil {
Expand Down Expand Up @@ -317,7 +317,7 @@ func StartServer(logger *log.Zap, cfg *config.Frontier) error {
}

func buildAPIDependencies(
logger log.Logger,
logger *slog.Logger,
cfg *config.Frontier,
dbc *db.Client,
sdb *spicedb.SpiceDB,
Expand Down Expand Up @@ -664,7 +664,7 @@ func (t *StripeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return resp, err
}

func getStripeClient(logger log.Logger, cfg *config.Frontier) *client.API {
func getStripeClient(logger *slog.Logger, cfg *config.Frontier) *client.API {
stripeLogLevel := stripe.LevelError
stripeBackends := &stripe.Backends{
API: stripe.GetBackendWithConfig(stripe.APIBackend, &stripe.BackendConfig{
Expand All @@ -691,7 +691,7 @@ func getStripeClient(logger log.Logger, cfg *config.Frontier) *client.API {
return stripeClient
}

func setupDB(cfg db.Config, logger log.Logger) (dbc *db.Client, err error) {
func setupDB(cfg db.Config, logger *slog.Logger) (dbc *db.Client, err error) {
// prefer use pgx instead of lib/pq for postgres to catch pg error
if cfg.Driver == "postgres" {
cfg.Driver = "pgx"
Expand Down
2 changes: 2 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"log/slog"
"os"

"github.com/pkg/profile"
Expand Down Expand Up @@ -82,6 +83,7 @@ func serverStartCommand() *cobra.Command {
panic(err)
}
logger := frontierlogger.InitLogger(appConfig.Log)
slog.SetDefault(logger)

if appConfig.App.Profiler {
// enable profilers
Expand Down
6 changes: 3 additions & 3 deletions core/authenticate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

"github.com/raystack/frontier/pkg/mailer"

"github.com/raystack/salt/log"
"log/slog"

"github.com/google/uuid"
"github.com/raystack/frontier/core/authenticate/strategy"
Expand Down Expand Up @@ -94,7 +94,7 @@ type UserPATService interface {
}

type Service struct {
log log.Logger
log *slog.Logger
cron *cron.Cron
flowRepo FlowRepository
userService UserService
Expand All @@ -108,7 +108,7 @@ type Service struct {
webAuth *webauthn.WebAuthn
}

func NewService(logger log.Logger, config Config, flowRepo FlowRepository,
func NewService(logger *slog.Logger, config Config, flowRepo FlowRepository,
mailDialer mailer.Dialer, tokenService TokenService, sessionService SessionService,
userService UserService, serviceUserService ServiceUserService, webAuthConfig *webauthn.WebAuthn,
userPATService UserPATService) *Service {
Expand Down
Loading
Loading