Giving every reporter what it needs
What you want to do
The logging service files lines by customer, and the shop needs the bank's receipt to refund an order. You want every line of the log to name the customer, and the receipt once the bank has given it, even when the journey stops right after.
The code
The shop is chapter 31's, without chapter 32's DebugReporter. The program gives each instance the customer's name with reportersGlobalMetadata, among the options of instance(...) beside the reporters of chapter 30. That makes it an entry of the journey's global metadata: data for its reporters, which no step reads:
const instance = shop.instance(order, {
reportersGlobalMetadata: { customer: order.customer },
});shop is made as chapter 24 makes it, and order is each of chapter 31's orders, Ann's and Pat's, which the loop hands over one at a time.
The charge asks for its step reporter, as the shipping does since chapter 9, and sets the receipt as an entry as soon as the bank gives it, before contributing it:
/**
* Charges an order's amount to its card through the payment client, and gives
* back its receipt, and the gift card's code when the order is a gift card.
* It gives every reporter the receipt as soon as the bank gives it.
*/
@step()
class Charge extends Step {
constructor(
@input("order", string) private readonly order: string,
@input("amount", integer) private readonly amount: number,
@input("card", string) private readonly card: string,
@input("giftCard", boolean) private readonly giftCard: boolean,
) {
super();
}
override async run(
@service("payments", PAYMENTS) payments: Payments,
@contributor() contributor: Contributor,
@stepReporter() reporter: StepReporter,
): Promise<Outcome> {
if (this.amount <= 0) return this.fail("nothing-to-charge");
const receipt = await payments.charge(this.order, this.card, this.amount);
if (receipt === undefined) return this.fail("card-expired");
await reporter.setGlobalMetadata("receipt", receipt);
contributor.contribute("receipt", receipt);
if (this.giftCard) contributor.contribute("code", `G-${this.order}`);
return this.succeed();
}
}The payment client it asks for is chapter 26's Payments, with chapter 7's token PAYMENTS. setGlobalMetadata returns a promise, like the step reporter's info, so the charge awaits it.
The log keeps each entry it receives in a Map, through receiveGlobalMetadata, a method a reporter may add beside report. A value comes as a PlainValue, from itinera/value: plain data, such as a string, a number or an object of them. The log starts each line with the customer's name and, once there is one, the receipt:
/**
* The shop's log: it writes a line for every event of a journey to the
* logging service, starting with the workflow's name, the journey's ID, the
* customer's name and, once the bank has given it, the receipt. A line the
* service refuses fails the report.
*/
class Log implements Reporter {
private readonly service = new LoggingService();
private readonly metadata = new Map<string, PlainValue>();
receiveGlobalMetadata(key: string, value: PlainValue): void {
this.metadata.set(key, value);
}
async report(event: Event): Promise<void> {
const said = detail(event);
const line = `log ${event.workflow} ${event.journeyId} ${this.whose()} #${String(event.sequence)} ${event.kind}`;
await this.service.write(said === undefined ? line : `${line} ${said}`);
}
/**
* The customer the journey is for and, once the bank has given it, the
* charge's receipt.
*/
private whose(): string {
const customer = this.text("customer") ?? "someone";
const receipt = this.text("receipt");
if (receipt === undefined) return `for ${customer}`;
return `for ${customer}, receipt ${receipt}`;
}
/** The entry of `key`, when it is text. */
private text(key: string): string | undefined {
const value = this.metadata.get(key);
return typeof value === "string" ? value : undefined;
}
}The logging service is chapter 31's. detail has one more case, which names the key of global_metadata_set:
case "global_metadata_set":
return event.key;The program runs Ann's and Pat's orders, and prints:
Ann's order:
log orders A-1 for Ann #1 journey_started order, customer, price, discount, parcel, card, giftCard
log orders A-1 for Ann #2 attempt_started lookup, attempt 1
log orders A-1 for Ann #3 service_provided lookup gets orderBook from ledger, a Ledger
the order book has no order A-1 yet
log orders A-1 for Ann #4 step_succeeded lookup
log orders A-1 for Ann #5 contribution_committed completed
log orders A-1 for Ann #6 hook_called alreadyCompleted: on step success of lookup
log orders A-1 for Ann #7 attempt_started price, attempt 1
log orders A-1 for Ann #8 step_succeeded price
log orders A-1 for Ann #9 contribution_committed amount
log orders A-1 for Ann #10 attempt_started charge, attempt 1
log orders A-1 for Ann #11 service_provided charge gets payments from bank, a Bank
the bank charges 30 for order A-1
log orders A-1 for Ann, receipt R-A-1 #12 global_metadata_set receipt
log orders A-1 for Ann, receipt R-A-1 #13 step_succeeded charge
log orders A-1 for Ann, receipt R-A-1 #14 contribution_committed receipt
log orders A-1 for Ann, receipt R-A-1 #15 hook_called audit: on step success of charge
log orders A-1 for Ann, receipt R-A-1 #16 contribution_committed audit
log orders A-1 for Ann, receipt R-A-1 #17 attempt_started ship, attempt 1
log orders A-1 for Ann, receipt R-A-1 #18 input_adapter_supplied ship gets weight from parcels
log orders A-1 for Ann, receipt R-A-1 #19 service_provided ship gets carrier from carriers, a MainCarrier
the main carrier books order A-1
log orders A-1 for Ann, receipt R-A-1 #20 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 for Ann, receipt R-A-1 #21 step_succeeded ship
log orders A-1 for Ann, receipt R-A-1 #22 contribution_committed tracking
log orders A-1 for Ann, receipt R-A-1 #23 optional_input_absent code
email to Ann: Order A-1 is on its way, tracking T-A-1.
log orders A-1 for Ann, receipt R-A-1 #24 hook_called notify: on workflow success
log orders A-1 for Ann, receipt R-A-1 #25 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
audit: charge took 30, receipt R-A-1
Pat's order:
log orders A-15 for Pat #1 journey_started order, customer, price, parcel, card, giftCard
log orders A-15 for Pat #2 attempt_started lookup, attempt 1
log orders A-15 for Pat #3 service_provided lookup gets orderBook from ledger, a Ledger
the order book has no order A-15 yet
log orders A-15 for Pat #4 step_succeeded lookup
log orders A-15 for Pat #5 contribution_committed completed
log orders A-15 for Pat #6 hook_called alreadyCompleted: on step success of lookup
log orders A-15 for Pat #7 attempt_started price, attempt 1
log orders A-15 for Pat #8 optional_input_absent discount
log orders A-15 for Pat #9 step_succeeded price
log orders A-15 for Pat #10 contribution_committed amount
log orders A-15 for Pat #11 attempt_started charge, attempt 1
log orders A-15 for Pat #12 service_provided charge gets payments from bank, a Bank
the bank charges 50 for order A-15
log orders A-15 for Pat, receipt R-A-15 #13 global_metadata_set receipt
the journey was aborted (reporter failed: the logging service is down)How it works
- Global metadata is for the reporters. Each entry is a key and a value, which every reporter of the journey receives. Steps and hooks never read it, so it changes no decision.
- The entries come before the first event. The journey ID comes first, under the key
journey_id, then the instance's entries, in the order they were given. So Pat's first line,journey_started, already names Pat. The keyjourney_idis the executor's own. - A reporter does what it wants with an entry. A reporter without
receiveGlobalMetadataaccepts every entry and does nothing with it, which is why chapter 11's desk is unchanged. The log keeps the entries, and reads them back from itsMap, checking withtypeofthat a value is a string before it writes it. - A step can set an entry while it runs. Every reporter receives it, then the journey emits
global_metadata_set, which names the key and who set it, but not the value, all before the promisesetGlobalMetadatareturns settles. The log wrote that line with the receipt in it, event 13, before it failed on the charge's success. - An entry stands whatever happens after. Pat's receipt never reached the data bag, since the journey was aborted first, but the log had it. So the shop finds Pat's payment,
R-A-15, in the log's last line, and can refund it.
Every line of the log now says whose order it is and, from the charge on, how it was paid. But the audit's line, which a hook writes, says neither which journey nor which attempt took the money.
Outcomes
We have shown how a journey's reporters receive data of their own, from the instance and from the steps, as global metadata.
| The usual way | With Itinera |
|---|---|
| A value every log line needs, such as a customer's name, is often passed down through every call, or kept in a global variable. | The instance gives it once to every reporter, before the first event. |
| A value a log line needs, found halfway through a request, is often lost when the request crashes right after. | A step sets it for every reporter as soon as it has it, and it stands whatever happens next. |