Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-logger-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@restatedev/restate-sdk": patch
---

Add `ctx.console.child(...)` for immutable, replay-aware logger context enrichment.
1 change: 1 addition & 0 deletions packages/examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"greeter": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/greeter.ts",
"greeter_hooks": "RESTATE_LOGGING=ERROR tsx --tsconfig ./tsconfig.json ./src/greeter_with_hooks.ts",
"greeter_http1": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/greeter_http1.ts",
"logger_context": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/logger_context.ts",
"preview_serde": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/preview_serde.ts",
"zgreeter": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/zod_greeter.ts",
"workflow": "RESTATE_LOGGING=debug tsx --tsconfig ./tsconfig.json ./src/workflow.ts",
Expand Down
71 changes: 71 additions & 0 deletions packages/examples/node/src/logger_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { service, serve, type Context } from "@restatedev/restate-sdk";

interface CheckoutRequest {
orderId: string;
customerId: string;
paymentMethodId: string;
}

interface PaymentResult {
paymentId: string;
}

const checkout = service({
name: "checkout",
handlers: {
submit: async (ctx: Context, input: CheckoutRequest) => {
let contextLogger = ctx.console.child({
orderId: input.orderId,
customerId: input.customerId,
});

contextLogger.info("checkout started");

const payment = await ctx.run("charge payment", async () => {
return await chargePayment(input);
});

// This is rebuilt on replay from the journaled ctx.run result.
contextLogger = contextLogger.child({ paymentId: payment.paymentId });
contextLogger.info("payment charged");

const shipment = await ctx.run("create shipment", async () => {
return await createShipment(input.orderId);
});

contextLogger
.child({ shipmentId: shipment.shipmentId })
.info("shipment created");

return {
orderId: input.orderId,
paymentId: payment.paymentId,
shipmentId: shipment.shipmentId,
};
},
},
});

export type Checkout = typeof checkout;

/**
* Simulates charging a payment provider and returns a durable payment id.
*/
async function chargePayment(input: CheckoutRequest): Promise<PaymentResult> {
return {
paymentId: `payment-${input.paymentMethodId}`,
};
}

/**
* Simulates creating a shipment and returns a durable shipment id.
*/
async function createShipment(
orderId: string
): Promise<{ shipmentId: string }> {
return {
shipmentId: `shipment-${orderId}`,
};
}

serve({ services: [checkout] });
Loading
Loading