From c28627d16f2ee02a6a0a477bcfcae0e6b5909c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Thu, 19 Oct 2023 10:12:49 +0200 Subject: [PATCH] chore(logging): purchases logging (#567) --- codex/contracts/market.nim | 2 +- codex/purchasing/states/cancelled.nim | 7 +++++++ codex/purchasing/states/error.nim | 8 ++++++++ codex/purchasing/states/finished.nim | 5 +++++ codex/purchasing/states/started.nim | 5 +++++ codex/purchasing/states/submitted.nim | 6 ++++++ codex/sales/states/cancelled.nim | 2 +- codex/sales/states/errored.nim | 2 +- codex/sales/states/failed.nim | 2 +- codex/sales/states/filled.nim | 2 +- codex/sales/states/filling.nim | 2 +- codex/sales/states/finished.nim | 2 +- codex/sales/states/ignored.nim | 2 +- codex/sales/states/initialproving.nim | 2 +- codex/sales/states/payout.nim | 2 +- codex/sales/states/preparing.nim | 2 +- codex/sales/states/proving.nim | 2 +- codex/sales/states/unknown.nim | 2 +- 18 files changed, 44 insertions(+), 13 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index c69d5e2d..c8ed9ffb 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -12,7 +12,7 @@ import ./marketplace export market logScope: - topics = "marketplace onchain market" + topics = "marketplace onchain market" type OnChainMarket* = ref object of Market diff --git a/codex/purchasing/states/cancelled.nim b/codex/purchasing/states/cancelled.nim index cb0be05d..aedca207 100644 --- a/codex/purchasing/states/cancelled.nim +++ b/codex/purchasing/states/cancelled.nim @@ -1,10 +1,14 @@ import pkg/metrics +import pkg/chronicles import ../statemachine import ./errorhandling import ./error declareCounter(codexPurchasesCancelled, "codex purchases cancelled") +logScope: + topics = "marketplace purchases cancelled" + type PurchaseCancelled* = ref object of ErrorHandlingState method `$`*(state: PurchaseCancelled): string = @@ -13,6 +17,9 @@ method `$`*(state: PurchaseCancelled): string = method run*(state: PurchaseCancelled, machine: Machine): Future[?State] {.async.} = codexPurchasesCancelled.inc() let purchase = Purchase(machine) + + warn "Request cancelled, withdrawing remaining funds", requestId = $purchase.requestId await purchase.market.withdrawFunds(purchase.requestId) + let error = newException(Timeout, "Purchase cancelled due to timeout") return some State(PurchaseErrored(error: error)) diff --git a/codex/purchasing/states/error.nim b/codex/purchasing/states/error.nim index c1fbbc6a..f8e92760 100644 --- a/codex/purchasing/states/error.nim +++ b/codex/purchasing/states/error.nim @@ -1,8 +1,13 @@ import pkg/metrics +import pkg/chronicles import ../statemachine +import ../../utils/exceptions declareCounter(codexPurchasesError, "codex purchases error") +logScope: + topics = "marketplace purchases errored" + type PurchaseErrored* = ref object of PurchaseState error*: ref CatchableError @@ -12,4 +17,7 @@ method `$`*(state: PurchaseErrored): string = method run*(state: PurchaseErrored, machine: Machine): Future[?State] {.async.} = codexPurchasesError.inc() let purchase = Purchase(machine) + + error "Purchasing error", error=state.error.msgDetail, requestId = purchase.requestId + purchase.future.fail(state.error) diff --git a/codex/purchasing/states/finished.nim b/codex/purchasing/states/finished.nim index 6e28bec2..75590d29 100644 --- a/codex/purchasing/states/finished.nim +++ b/codex/purchasing/states/finished.nim @@ -1,8 +1,12 @@ import pkg/metrics +import pkg/chronicles import ../statemachine declareCounter(codexPurchasesFinished, "codex purchases finished") +logScope: + topics = "marketplace purchases finished" + type PurchaseFinished* = ref object of PurchaseState method `$`*(state: PurchaseFinished): string = @@ -11,4 +15,5 @@ method `$`*(state: PurchaseFinished): string = method run*(state: PurchaseFinished, machine: Machine): Future[?State] {.async.} = codexPurchasesFinished.inc() let purchase = Purchase(machine) + info "Purchase finished", requestId = purchase.requestId purchase.future.complete() diff --git a/codex/purchasing/states/started.nim b/codex/purchasing/states/started.nim index 26cc871d..313c949f 100644 --- a/codex/purchasing/states/started.nim +++ b/codex/purchasing/states/started.nim @@ -1,4 +1,5 @@ import pkg/metrics +import pkg/chronicles import ../statemachine import ./errorhandling import ./finished @@ -6,6 +7,9 @@ import ./failed declareCounter(codexPurchasesStarted, "codex purchases started") +logScope: + topics = "marketplace purchases started" + type PurchaseStarted* = ref object of ErrorHandlingState method `$`*(state: PurchaseStarted): string = @@ -17,6 +21,7 @@ method run*(state: PurchaseStarted, machine: Machine): Future[?State] {.async.} let clock = purchase.clock let market = purchase.market + info "All required slots filled, purchase started", requestId = purchase.requestId let failed = newFuture[void]() proc callback(_: RequestId) = diff --git a/codex/purchasing/states/submitted.nim b/codex/purchasing/states/submitted.nim index 4505ae91..53c36864 100644 --- a/codex/purchasing/states/submitted.nim +++ b/codex/purchasing/states/submitted.nim @@ -1,9 +1,13 @@ import pkg/metrics +import pkg/chronicles import ../statemachine import ./errorhandling import ./started import ./cancelled +logScope: + topics = "marketplace purchases submitted" + declareCounter(codexPurchasesSubmitted, "codex purchases submitted") type PurchaseSubmitted* = ref object of ErrorHandlingState @@ -18,6 +22,8 @@ method run*(state: PurchaseSubmitted, machine: Machine): Future[?State] {.async. let market = purchase.market let clock = purchase.clock + info "Request submitted, waiting for slots to be filled", requestId = purchase.requestId + proc wait {.async.} = let done = newFuture[void]() proc callback(_: RequestId) = diff --git a/codex/sales/states/cancelled.nim b/codex/sales/states/cancelled.nim index f611d5e2..0e7e5259 100644 --- a/codex/sales/states/cancelled.nim +++ b/codex/sales/states/cancelled.nim @@ -4,7 +4,7 @@ import ./errorhandling import ./errored logScope: - topics = "marketplace sales cancelled" + topics = "marketplace sales cancelled" type SaleCancelled* = ref object of ErrorHandlingState diff --git a/codex/sales/states/errored.nim b/codex/sales/states/errored.nim index 4533583e..a6d5bfb4 100644 --- a/codex/sales/states/errored.nim +++ b/codex/sales/states/errored.nim @@ -7,7 +7,7 @@ import ../salesagent import ../../utils/exceptions logScope: - topics = "marketplace sales errored" + topics = "marketplace sales errored" type SaleErrored* = ref object of SaleState error*: ref CatchableError diff --git a/codex/sales/states/failed.nim b/codex/sales/states/failed.nim index b6ab5f60..aced9c56 100644 --- a/codex/sales/states/failed.nim +++ b/codex/sales/states/failed.nim @@ -4,7 +4,7 @@ import ./errorhandling import ./errored logScope: - topics = "marketplace sales failed" + topics = "marketplace sales failed" type SaleFailed* = ref object of ErrorHandlingState diff --git a/codex/sales/states/filled.nim b/codex/sales/states/filled.nim index 2ba14672..5139a8c3 100644 --- a/codex/sales/states/filled.nim +++ b/codex/sales/states/filled.nim @@ -11,7 +11,7 @@ import ./proving import ./provingsimulated logScope: - topics = "marketplace sales filled" + topics = "marketplace sales filled" type SaleFilled* = ref object of ErrorHandlingState diff --git a/codex/sales/states/filling.nim b/codex/sales/states/filling.nim index 045b482d..5cf6a06d 100644 --- a/codex/sales/states/filling.nim +++ b/codex/sales/states/filling.nim @@ -8,7 +8,7 @@ import ./cancelled import ./failed logScope: - topics = "marketplace sales filling" + topics = "marketplace sales filling" type SaleFilling* = ref object of ErrorHandlingState diff --git a/codex/sales/states/finished.nim b/codex/sales/states/finished.nim index 1a8c5151..5b1c6003 100644 --- a/codex/sales/states/finished.nim +++ b/codex/sales/states/finished.nim @@ -7,7 +7,7 @@ import ./cancelled import ./failed logScope: - topics = "marketplace sales finished" + topics = "marketplace sales finished" type SaleFinished* = ref object of ErrorHandlingState diff --git a/codex/sales/states/ignored.nim b/codex/sales/states/ignored.nim index 5955937e..fa0641cc 100644 --- a/codex/sales/states/ignored.nim +++ b/codex/sales/states/ignored.nim @@ -5,7 +5,7 @@ import ../salesagent import ./errorhandling logScope: - topics = "marketplace sales ignored" + topics = "marketplace sales ignored" type SaleIgnored* = ref object of ErrorHandlingState diff --git a/codex/sales/states/initialproving.nim b/codex/sales/states/initialproving.nim index 7dff1dc1..7f5da6b6 100644 --- a/codex/sales/states/initialproving.nim +++ b/codex/sales/states/initialproving.nim @@ -7,7 +7,7 @@ import ./cancelled import ./failed logScope: - topics = "marketplace sales initial-proving" + topics = "marketplace sales initial-proving" type SaleInitialProving* = ref object of ErrorHandlingState diff --git a/codex/sales/states/payout.nim b/codex/sales/states/payout.nim index 8d1a53b9..68cdfbc9 100644 --- a/codex/sales/states/payout.nim +++ b/codex/sales/states/payout.nim @@ -8,7 +8,7 @@ import ./failed import ./finished logScope: - topics = "marketplace sales payout" + topics = "marketplace sales payout" type SalePayout* = ref object of ErrorHandlingState diff --git a/codex/sales/states/preparing.nim b/codex/sales/states/preparing.nim index d6a2270e..bb0fe743 100644 --- a/codex/sales/states/preparing.nim +++ b/codex/sales/states/preparing.nim @@ -16,7 +16,7 @@ type SalePreparing* = ref object of ErrorHandlingState logScope: - topics = "marketplace sales preparing" + topics = "marketplace sales preparing" method `$`*(state: SalePreparing): string = "SalePreparing" diff --git a/codex/sales/states/proving.nim b/codex/sales/states/proving.nim index 3d73ad06..5586c9ca 100644 --- a/codex/sales/states/proving.nim +++ b/codex/sales/states/proving.nim @@ -11,7 +11,7 @@ import ./errored import ./payout logScope: - topics = "marketplace sales proving" + topics = "marketplace sales proving" type SlotNotFilledError* = object of CatchableError diff --git a/codex/sales/states/unknown.nim b/codex/sales/states/unknown.nim index 601be1f3..ed0a96b3 100644 --- a/codex/sales/states/unknown.nim +++ b/codex/sales/states/unknown.nim @@ -9,7 +9,7 @@ import ./cancelled import ./payout logScope: - topics = "marketplace sales unknown" + topics = "marketplace sales unknown" type SaleUnknown* = ref object of SaleState