Skip to content

Commit

Permalink
chore(checkout): add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMaidurov committed Feb 1, 2024
1 parent c86d4bb commit 45c959b
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 14 deletions.
61 changes: 58 additions & 3 deletions checkout/application/orderService.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/gob"
"errors"
"fmt"
"go.opencensus.io/trace"

"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/opencensus"
Expand Down Expand Up @@ -143,6 +144,9 @@ func (os *OrderService) Inject(
// SetSources sets sources for sessions carts items
// Deprecated: Sourcing moved to new module see sourcing module
func (os *OrderService) SetSources(ctx context.Context, session *web.Session) error {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/SetSources")
defer span.End()

if !os.deprecatedSourcingActive {
return nil
}
Expand All @@ -166,6 +170,9 @@ func (os *OrderService) SetSources(ctx context.Context, session *web.Session) er
// CurrentCartSaveInfos saves additional information on current cart
// Deprecated: method is not called within flamingo-commerce and method does not support multiple delivery addresses
func (os *OrderService) CurrentCartSaveInfos(ctx context.Context, session *web.Session, billingAddress *cart.Address, shippingAddress *cart.Address, purchaser *cart.Person, additionalData *cart.AdditionalData) error {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CurrentCartSaveInfos")
defer span.End()

os.logger.WithContext(ctx).Debug("CurrentCartSaveInfos call billingAddress:%v shippingAddress:%v payment:%v", billingAddress, shippingAddress)

if billingAddress == nil {
Expand Down Expand Up @@ -224,7 +231,10 @@ func (os *OrderService) CurrentCartSaveInfos(ctx context.Context, session *web.S
}

// GetPaymentGateway tries to get the supplied payment gateway by code from the registered payment gateways
func (os *OrderService) GetPaymentGateway(_ context.Context, paymentGatewayCode string) (interfaces.WebCartPaymentGateway, error) {
func (os *OrderService) GetPaymentGateway(ctx context.Context, paymentGatewayCode string) (interfaces.WebCartPaymentGateway, error) {
_, span := trace.StartSpan(ctx, "checkout/OrderService/GetPaymentGateway")
defer span.End()

gateway, ok := os.webCartPaymentGateways[paymentGatewayCode]
if !ok {
return nil, errors.New("Payment gateway " + paymentGatewayCode + " not found")
Expand All @@ -234,12 +244,18 @@ func (os *OrderService) GetPaymentGateway(_ context.Context, paymentGatewayCode
}

// GetAvailablePaymentGateways returns the list of registered WebCartPaymentGateway
func (os *OrderService) GetAvailablePaymentGateways(_ context.Context) map[string]interfaces.WebCartPaymentGateway {
func (os *OrderService) GetAvailablePaymentGateways(ctx context.Context) map[string]interfaces.WebCartPaymentGateway {
_, span := trace.StartSpan(ctx, "checkout/OrderService/GetAvailablePaymentGateways")
defer span.End()

return os.webCartPaymentGateways
}

// CurrentCartPlaceOrder places the current cart without additional payment processing
func (os *OrderService) CurrentCartPlaceOrder(ctx context.Context, session *web.Session, cartPayment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CurrentCartPlaceOrder")
defer span.End()

var info *PlaceOrderInfo
var err error
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
Expand All @@ -259,6 +275,9 @@ func (os *OrderService) CurrentCartPlaceOrder(ctx context.Context, session *web.
}

func (os *OrderService) placeOrder(ctx context.Context, session *web.Session, decoratedCart *decorator.DecoratedCart, payment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/placeOrder")
defer span.End()

validationResult := os.cartService.ValidateCart(ctx, session, decoratedCart)
if !validationResult.IsValid() {
// record cartValidationFailCount metric
Expand Down Expand Up @@ -288,16 +307,25 @@ func (os *OrderService) placeOrder(ctx context.Context, session *web.Session, de

// CancelOrder cancels an previously placed order and returns the restored cart with the order content
func (os *OrderService) CancelOrder(ctx context.Context, session *web.Session, order *PlaceOrderInfo) (*cart.Cart, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CancelOrder")
defer span.End()

return os.cartService.CancelOrder(ctx, session, order.PlacedOrders, order.Cart)
}

// CancelOrderWithoutRestore cancels an previously placed order
func (os *OrderService) CancelOrderWithoutRestore(ctx context.Context, session *web.Session, order *PlaceOrderInfo) error {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CancelOrderWithoutRestore")
defer span.End()

return os.cartService.CancelOrderWithoutRestore(ctx, session, order.PlacedOrders)
}

// CurrentCartPlaceOrderWithPaymentProcessing places the current cart which is fetched from the context
func (os *OrderService) CurrentCartPlaceOrderWithPaymentProcessing(ctx context.Context, session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CurrentCartPlaceOrderWithPaymentProcessing")
defer span.End()

var info *PlaceOrderInfo
var err error
// use a background context from here on to prevent the place order canceled by context cancel
Expand All @@ -322,6 +350,9 @@ func (os *OrderService) CurrentCartPlaceOrderWithPaymentProcessing(ctx context.C
// this function enables clients to pass a cart as is, without the usage of the cartReceiverService
func (os *OrderService) CartPlaceOrderWithPaymentProcessing(ctx context.Context, decoratedCart *decorator.DecoratedCart,
session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CartPlaceOrderWithPaymentProcessing")
defer span.End()

var info *PlaceOrderInfo
var err error
// use a background context from here on to prevent the place order canceled by context cancel
Expand All @@ -335,6 +366,9 @@ func (os *OrderService) CartPlaceOrderWithPaymentProcessing(ctx context.Context,
// CartPlaceOrder places the cart passed to the function
// this function enables clients to pass a cart as is, without the usage of the cartReceiverService
func (os *OrderService) CartPlaceOrder(ctx context.Context, decoratedCart *decorator.DecoratedCart, payment placeorder.Payment) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/CartPlaceOrder")
defer span.End()

var info *PlaceOrderInfo
var err error
web.RunWithDetachedContext(ctx, func(placeOrderContext context.Context) {
Expand All @@ -346,13 +380,19 @@ func (os *OrderService) CartPlaceOrder(ctx context.Context, decoratedCart *decor

// storeLastPlacedOrder stores the last placed order/cart in the session
func (os *OrderService) storeLastPlacedOrder(ctx context.Context, info *PlaceOrderInfo) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/storeLastPlacedOrder")
defer span.End()

session := web.SessionFromContext(ctx)

_ = session.Store(LastPlacedOrderSessionKey, info)
}

// LastPlacedOrder returns the last placed order/cart if available
func (os *OrderService) LastPlacedOrder(ctx context.Context) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/LastPlacedOrder")
defer span.End()

session := web.SessionFromContext(ctx)

lastPlacedOrder, found := session.Load(LastPlacedOrderSessionKey)
Expand All @@ -370,19 +410,28 @@ func (os *OrderService) LastPlacedOrder(ctx context.Context) (*PlaceOrderInfo, e

// HasLastPlacedOrder returns if a order has been previously placed
func (os *OrderService) HasLastPlacedOrder(ctx context.Context) bool {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/HasLastPlacedOrder")
defer span.End()

lastPlaced, err := os.LastPlacedOrder(ctx)

return lastPlaced != nil && err == nil
}

// ClearLastPlacedOrder clears the last placed cart, this can be useful if an cart / order is finished
func (os *OrderService) ClearLastPlacedOrder(ctx context.Context) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/ClearLastPlacedOrder")
defer span.End()

session := web.SessionFromContext(ctx)
session.Delete(LastPlacedOrderSessionKey)
}

// LastPlacedOrCurrentCart returns the decorated cart of the last placed order if there is one if not return the current cart
func (os *OrderService) LastPlacedOrCurrentCart(ctx context.Context) (*decorator.DecoratedCart, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/LastPlacedOrCurrentCart")
defer span.End()

lastPlacedOrder, err := os.LastPlacedOrder(ctx)
if err != nil {
os.logger.Warn("couldn't get last placed order:", err)
Expand Down Expand Up @@ -410,6 +459,9 @@ func (os *OrderService) LastPlacedOrCurrentCart(ctx context.Context) (*decorator
// is the same for the interface functions, therefore the common flow is placed in this private helper function
func (os *OrderService) placeOrderWithPaymentProcessing(ctx context.Context, decoratedCart *decorator.DecoratedCart,
session *web.Session) (*PlaceOrderInfo, error) {
ctx, span := trace.StartSpan(ctx, "checkout/OrderService/placeOrderWithPaymentProcessing")
defer span.End()

if !decoratedCart.Cart.IsPaymentSelected() {
// record noPaymentSelectionCount metric
stats.Record(ctx, noPaymentSelectionCount.M(1))
Expand Down Expand Up @@ -497,7 +549,10 @@ func (os *OrderService) placeOrderWithPaymentProcessing(ctx context.Context, dec
return placeOrderInfo, nil
}

func (os *OrderService) preparePlaceOrderInfo(_ context.Context, currentCart cart.Cart, placedOrderInfos placeorder.PlacedOrderInfos, cartPayment placeorder.Payment) *PlaceOrderInfo {
func (os *OrderService) preparePlaceOrderInfo(ctx context.Context, currentCart cart.Cart, placedOrderInfos placeorder.PlacedOrderInfos, cartPayment placeorder.Payment) *PlaceOrderInfo {
_, span := trace.StartSpan(ctx, "checkout/OrderService/preparePlaceOrderInfo")
defer span.End()

email := currentCart.GetContactMail()

placeOrderInfo := &PlaceOrderInfo{
Expand Down
25 changes: 20 additions & 5 deletions checkout/application/placeorder/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Coordinator) Inject(
// New acquires lock if possible and creates new process with first run call blocking
// returns error if already locked or error during run
func (c *Coordinator) New(ctx context.Context, cart cartDomain.Cart, returnURL *url.URL) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "placeorder/coordinator/New")
ctx, span := trace.StartSpan(ctx, "checkout/Coordinator/New")
defer span.End()

unlock, err := c.locker.TryLock(ctx, determineLockKeyForCart(cart), maxLockDuration)
Expand Down Expand Up @@ -163,6 +163,9 @@ func (c *Coordinator) New(ctx context.Context, cart cartDomain.Cart, returnURL *

// HasUnfinishedProcess checks for processes not in final state
func (c *Coordinator) HasUnfinishedProcess(ctx context.Context) (bool, error) {
_, span := trace.StartSpan(ctx, "checkout/Coordinator/HasUnfinishedProcess")
defer span.End()

last, err := c.LastProcess(ctx)
if err == ErrNoPlaceOrderProcess {
return false, nil
Expand All @@ -180,6 +183,9 @@ func (c *Coordinator) HasUnfinishedProcess(ctx context.Context) (bool, error) {
}

func (c *Coordinator) storeProcessContext(ctx context.Context, pctx process.Context) error {
_, span := trace.StartSpan(ctx, "checkout/Coordinator/storeProcessContext")
defer span.End()

session := web.SessionFromContext(ctx)
if session == nil {
return errors.New("session not available to check for last place order context")
Expand All @@ -189,6 +195,9 @@ func (c *Coordinator) storeProcessContext(ctx context.Context, pctx process.Cont
}

func (c *Coordinator) clearProcessContext(ctx context.Context) error {
_, span := trace.StartSpan(ctx, "checkout/Coordinator/clearProcessContext")
defer span.End()

session := web.SessionFromContext(ctx)
if session == nil {
return errors.New("session not available to check for last place order context")
Expand All @@ -199,6 +208,9 @@ func (c *Coordinator) clearProcessContext(ctx context.Context) error {

// LastProcess current place order process
func (c *Coordinator) LastProcess(ctx context.Context) (*process.Process, error) {
_, span := trace.StartSpan(ctx, "checkout/Coordinator/LastProcess")
defer span.End()

session := web.SessionFromContext(ctx)
if session == nil {
return nil, errors.New("session not available to check for last place order context")
Expand All @@ -219,7 +231,7 @@ func (c *Coordinator) LastProcess(ctx context.Context) (*process.Process, error)
// Cancel the process if it exists (blocking)
// be aware that all rollback callbacks are executed
func (c *Coordinator) Cancel(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "placeorder/coordinator/Cancel")
ctx, span := trace.StartSpan(ctx, "checkout/Coordinator/Cancel")
defer span.End()

var returnErr error
Expand Down Expand Up @@ -278,7 +290,7 @@ func (c *Coordinator) Cancel(ctx context.Context) error {

// ClearLastProcess removes last stored process
func (c *Coordinator) ClearLastProcess(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "placeorder/coordinator/Clear")
ctx, span := trace.StartSpan(ctx, "checkout/Coordinator/ClearLastProcess")
defer span.End()

var returnErr error
Expand All @@ -296,7 +308,7 @@ func (c *Coordinator) ClearLastProcess(ctx context.Context) error {
// Run returns immediately
func (c *Coordinator) Run(ctx context.Context) {
go func(ctx context.Context) {
ctx, span := trace.StartSpan(ctx, "placeorder/coordinator/Run")
ctx, span := trace.StartSpan(ctx, "checkout/Coordinator/Run")
defer span.End()

web.RunWithDetachedContext(ctx, func(ctx context.Context) {
Expand Down Expand Up @@ -360,7 +372,7 @@ func (c *Coordinator) proceedInStateMachineUntilNoStateChange(ctx context.Contex
// RunBlocking waits for the lock and starts the next processing
// RunBlocking waits until the process is finished and returns its result
func (c *Coordinator) RunBlocking(ctx context.Context) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "placeorder/coordinator/RunBlocking")
ctx, span := trace.StartSpan(ctx, "checkout/Coordinator/RunBlocking")
defer span.End()

var pctx *process.Context
Expand Down Expand Up @@ -432,6 +444,9 @@ func (c *Coordinator) RunBlocking(ctx context.Context) (*process.Context, error)
}

func (c *Coordinator) forceSessionUpdate(ctx context.Context) {
_, span := trace.StartSpan(ctx, "checkout/Coordinator/forceSessionUpdate")
defer span.End()

session := web.SessionFromContext(ctx)
_, err := c.sessionStore.Save(ctx, session)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions checkout/application/placeorder/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package placeorder

import (
"context"
"go.opencensus.io/trace"

"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"
)
Expand All @@ -22,12 +23,18 @@ func (h *Handler) Inject(

// StartPlaceOrder handles start place order command
func (h *Handler) StartPlaceOrder(ctx context.Context, command StartPlaceOrderCommand) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/StartPlaceOrder")
defer span.End()

_ = h.coordinator.ClearLastProcess(ctx)
return h.coordinator.New(ctx, command.Cart, command.ReturnURL)
}

// CurrentContext returns the last saved state
func (h *Handler) CurrentContext(ctx context.Context) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/CurrentContext")
defer span.End()

p, err := h.coordinator.LastProcess(ctx)
if err != nil {
return nil, err
Expand All @@ -39,11 +46,17 @@ func (h *Handler) CurrentContext(ctx context.Context) (*process.Context, error)

// ClearPlaceOrder clears the last placed order from the context store, only possible if order in final state
func (h *Handler) ClearPlaceOrder(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/ClearPlaceOrder")
defer span.End()

return h.coordinator.ClearLastProcess(ctx)
}

// RefreshPlaceOrder handles RefreshPlaceOrder command
func (h *Handler) RefreshPlaceOrder(ctx context.Context, _ RefreshPlaceOrderCommand) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/RefreshPlaceOrder")
defer span.End()

p, err := h.coordinator.LastProcess(ctx)
if err != nil {
return nil, err
Expand All @@ -58,15 +71,24 @@ func (h *Handler) RefreshPlaceOrder(ctx context.Context, _ RefreshPlaceOrderComm

// RefreshPlaceOrderBlocking handles RefreshPlaceOrder blocking
func (h *Handler) RefreshPlaceOrderBlocking(ctx context.Context, _ RefreshPlaceOrderCommand) (*process.Context, error) {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/RefreshPlaceOrderBlocking")
defer span.End()

return h.coordinator.RunBlocking(ctx)
}

// HasUnfinishedProcess checks for processes not in final state
func (h *Handler) HasUnfinishedProcess(ctx context.Context) (bool, error) {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/HasUnfinishedProcess")
defer span.End()

return h.coordinator.HasUnfinishedProcess(ctx)
}

// CancelPlaceOrder handles order cancellation, is blocking
func (h *Handler) CancelPlaceOrder(ctx context.Context, _ CancelPlaceOrderCommand) error {
ctx, span := trace.StartSpan(ctx, "checkout/Handler/CancelPlaceOrder")
defer span.End()

return h.coordinator.Cancel(ctx)
}
4 changes: 4 additions & 0 deletions checkout/application/placeorder/validate_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package placeorder
import (
"context"
"fmt"
"go.opencensus.io/trace"

"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"
"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/states"
Expand All @@ -21,6 +22,9 @@ const (

// PaymentValidator to decide over the next state
func PaymentValidator(ctx context.Context, p *process.Process, paymentService *application.PaymentService) process.RunResult {
ctx, span := trace.StartSpan(ctx, "checkout/PaymentValidator")
defer span.End()

cart := p.Context().Cart
gateway, err := paymentService.PaymentGatewayByCart(p.Context().Cart)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions checkout/domain/sourcingengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"context"
"fmt"
"go.opencensus.io/trace"
"math"

cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
Expand Down Expand Up @@ -122,6 +123,9 @@ func (s Sources) Reduce(reduceby Sources) Sources {
// SetSourcesForCartItems gets Sources and modifies the Cart Items
// todo move to application layer ?
func (se *SourcingEngine) SetSourcesForCartItems(ctx context.Context, session *web.Session, decoratedCart *decorator.DecoratedCart) error {
ctx, span := trace.StartSpan(ctx, "checkout/SourcingEngine/SetSourcesForCartItems")
defer span.End()

if se.SourcingService == nil {
return nil
}
Expand Down Expand Up @@ -156,6 +160,9 @@ func (se *SourcingEngine) SetSourcesForCartItems(ctx context.Context, session *w
}

func (se *SourcingEngine) sourceLocationForCartItem(ctx context.Context, session *web.Session, decoratedCart *decorator.DecoratedCart, deliveryCode string, item *decorator.DecoratedCartItem) (*Source, error) {
ctx, span := trace.StartSpan(ctx, "checkout/SourcingEngine/sourceLocationForCartItem")
defer span.End()

detailService, ok := se.SourcingService.(SourcingServiceDetail)
if ok {
sources, err := detailService.GetSourcesForItem(ctx, session, decoratedCart, deliveryCode, item)
Expand Down
Loading

0 comments on commit 45c959b

Please sign in to comment.