Seeing what happened
What you want to do
When an order ends the way it did, you want to know who decided what, and when: which step reported which outcome, what the executor decided because of it, and in what order. And you want every journey of every workflow to tell you that the same way, so that one log reads them all, without a println in any step.
The code
Everything that happens in a journey is an event, and the executor reports every one. A reporter receives them, one at a time, and decides what to do with each. The shop's log is a reporter: a class implementing the Reporter interface, from org.itineradev.itinera.report, whose report method writes a line for each event. The line gives the event's number and its kind, and, for the kinds the log says more about, the step the event is about and what it names.
/** The shop's log: it writes a line for every event of a journey. */
class Log : Reporter {
override fun report(event: Event) {
when (val detail = detail(event)) {
null -> println("log #${event.sequence} ${event.kind}")
else -> println("log #${event.sequence} ${event.kind} $detail")
}
}
}
/**
* What the log says after an event's kind, for the kinds it says more about: the step the event is
* about, and what it names.
*/
fun detail(event: Event): String? =
when (event) {
is JourneyStarted -> event.initialKeys.joinToString(", ")
is AttemptStarted -> "${event.step}"
is ServiceProvided ->
"${event.step} gets ${event.key} from ${event.provider}, a ${event.implementation}"
is StepSucceeded -> "${event.step}"
is StepFailed -> "${event.step}: ${event.reason.code}"
is ContributionCommitted -> event.key
is StepGivenUp -> "${event.step}"
is JourneyFailed -> "${event.step}"
else -> null
}
The workflow lists the log in its @Workflow annotation, with reporters = [Log::class]. Its providers no longer print, since the log now says so, and the carrier's is now called mainCarrier, with key = "carrier":
/** The shop's workflow: it charges, then ships, and every journey is logged. */
@Workflow(name = "orders", reporters = [Log::class])
@WithStep(Charge::class, name = "charge")
@WithStep(Ship::class, name = "ship")
class Orders : ItineraWorkflow {
/** The bank, as the payment client of the journey. */
@Provider(key = "payments") fun bank(): Provided<Payments> = provide(Bank())
/** The main carrier, as the carrier of the journey. */
@Provider(key = "carrier")
fun mainCarrier(): Provided<Carrier> = provide(MainCarrier(maxWeight = 20))
}
Everything else is chapter 7's shop, which runs Ann's and Gil's orders as chapter 6 shows. The faked bank and carrier still print what they do. It prints:
Ann's order:
log #1 journey_started order, price, weight
log #2 attempt_started charge
log #3 service_provided charge gets payments from bank, a ch08_seeing_what_happened.Bank
the bank charges 40 for order A-1
log #4 step_succeeded charge
log #5 contribution_committed receipt
log #6 attempt_started ship
log #7 service_provided ship gets carrier from mainCarrier, a ch08_seeing_what_happened.MainCarrier
the carrier books order A-1
the parcel leaves with receipt R-A-1
log #8 step_succeeded ship
log #9 contribution_committed tracking
log #10 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
Gil's order:
log #1 journey_started order, price, weight
log #2 attempt_started charge
log #3 service_provided charge gets payments from bank, a ch08_seeing_what_happened.Bank
the bank charges 45 for order A-7
log #4 step_succeeded charge
log #5 contribution_committed receipt
log #6 attempt_started ship
log #7 service_provided ship gets carrier from mainCarrier, a ch08_seeing_what_happened.MainCarrier
the carrier books order A-7
log #8 step_failed ship: too-heavy
log #9 step_given_up ship
log #10 journey_failed ship
the journey failed, with receipt R-A-7 and tracking noneHow it works
- Every event says who decided what, and when. An event records one thing that happened, and who it came from. Some are facts: the journey started, an attempt of a step started, a service was provided, a contribution was committed, and the step's own outcome, which the step decided and returned, and which the executor records, as
step_failedwithtoo-heavyfor Gil's shipping. Others are the executor's decisions about that outcome:step_given_upsays it won't run the shipping again, andjourney_failedthat the journey failed because of it. Each event's number says when, in the journey's order, and its timestamp when on the clock. - The executor records which provider served which step. When a step asks for a service, the executor calls its provider, lends the service, and records
service_provided, naming the step, the key, the provider, by its method's name, and the implementation it made:ship gets carrier from mainCarrier, a ch08_seeing_what_happened.MainCarrier. It is the instance's class's name, with its package, for people to read rather than for code to compare. This fact says which tool a step got, never the service itself: like your data, a service never goes into an event. - Every journey reports the same stream. The events come in a fixed order and shape, whoever wrote the steps: a step's outcome, then what the executor decided about it. So one reporter reads every journey of every workflow the same way. Every decision is reported in the journey's events, naming who made it.
- Every event carries the same fields. In Kotlin, an event is an
Event, fromorg.itineradev.itinera.event, and every event haskind, anEventKindsuch asEventKind.STEP_FAILED, which prints asstep_failed;sequence, which numbers the journey's events from 1; atimestamp; and thejourneyIdand theworkflowit belongs to. The log leaves out the timestamp and the journey ID, which change on every run. - What else an event says depends on its kind.
Eventis a sealed interface with a data class for each kind, such asStepFailedandServiceProvided, each adding what its kind carries.detailmatches the event withwhen, and each branchis StepFailed ->lets the compiler treat the event as that class, so inside it the event has areason, whosecodethe log writes. Itselsegivesnullfor every kind the log says nothing more about. - The executor's events name your data, and never carry its value.
journey_startedlistsorder,priceandweight, but not Ann's price, andcontribution_committednamesreceipt, not the receipt. A step's reason is carried, since the step wrote it, astoo-heavyis for Gil's shipping. The executor's events only say what happened to your data. - A reporter decides where events go. Its
reportreceives each event and returns nothing. The workflow lists the classes of the reporters every journey gets, and each journey gets its own, made with its constructor, which takes nothing. A reporter only ever sees the events of its own journey. - Events are delivered as they happen. Each event reaches the log at once, before the journey goes on, so the log's lines and the bank's interleave in the order things happened.
But one line in Ann's output still comes from a println in a step: "the parcel leaves with receipt R-A-1". Only the shipping knows the parcel left, and it says so outside the log, unnumbered and tied to no journey.
Outcomes
We have shown how a reporter sees who decided what in a journey, and when, as events.
| The usual way | With Itinera |
|---|---|
| When an order fails, code usually keeps no record of whether a function's own error, a caller's retry or a fallback decided it, unless someone thought to log it. | Every event says whether a step reported it or the executor decided it, in order, the same way for every workflow. |
| A log is often written after the fact, or only where code remembers to, so its lines rarely match the order things happened in. | Each event reaches the reporter as it happens, numbered in the journey's order. |
| Logs often hold the data they talk about, such as amounts and card numbers, because that is what was at hand. | The executor's events name the data the journey started with or a step gave back, and never carry its value. |