Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include request struct in logger context #472

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
24 changes: 1 addition & 23 deletions packages/restate-sdk/src/context_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import type {
} from "@restatedev/restate-sdk-core";
import { serde } from "@restatedev/restate-sdk-core";
import { RandImpl } from "./utils/rand.js";
import type { Headers } from "./endpoint/handlers/generic.js";
import type {
ReadableStreamDefaultReader,
WritableStreamDefaultWriter,
Expand All @@ -67,7 +66,6 @@ export type InternalCombineablePromise<T> = CombineablePromise<T> & {
};

export class ContextImpl implements ObjectContext, WorkflowContext {
private readonly invocationRequest: Request;
public readonly rand: Rand;

public readonly date: ContextDate = {
Expand All @@ -86,31 +84,11 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
readonly input: vm.WasmInput,
public readonly console: Console,
public readonly handlerKind: HandlerKind,
attemptHeaders: Headers,
extraArgs: unknown[],
private readonly invocationRequest: Request,
private readonly invocationEndPromise: CompletablePromise<void>,
private readonly inputReader: ReadableStreamDefaultReader<Uint8Array>,
private readonly outputWriter: WritableStreamDefaultWriter<Uint8Array>
) {
this.invocationRequest = {
id: input.invocation_id,
headers: input.headers.reduce((headers, { key, value }) => {
headers.set(key, value);
return headers;
}, new Map()),
attemptHeaders: Object.entries(attemptHeaders).reduce(
(headers, [key, value]) => {
if (value !== undefined) {
headers.set(key, value instanceof Array ? value[0] : value);
}
return headers;
},
new Map()
),
body: input.input,
extraArgs,
};

this.rand = new RandImpl(input.invocation_id, () => {
if (coreVm.is_inside_run()) {
throw new Error(
Expand Down
25 changes: 23 additions & 2 deletions packages/restate-sdk/src/endpoint/handlers/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { EndpointBuilder } from "../endpoint_builder.js";
import { type ReadableStream, TransformStream } from "node:stream/web";
import { OnceStream } from "../../utils/streams.js";
import { ContextImpl } from "../../context_impl.js";
import type { Request } from "../../context.js";
import * as vm from "./vm/sdk_shared_core_wasm_bindings.js";
import { CompletablePromise } from "../../utils/completable_promise.js";
import { HandlerKind } from "../../types/rpc.js";
Expand Down Expand Up @@ -252,12 +253,33 @@ export class GenericHandler implements RestateHandler {

// Get input
const input = coreVm.sys_input();

const invocationRequest: Request = {
id: input.invocation_id,
headers: input.headers.reduce((headers, { key, value }) => {
headers.set(key, value);
return headers;
}, new Map()),
attemptHeaders: Object.entries(headers).reduce(
(headers, [key, value]) => {
if (value !== undefined) {
headers.set(key, value instanceof Array ? value[0] : value);
}
return headers;
},
new Map()
),
body: input.input,
extraArgs,
};

// Prepare logger
const loggerContext = new LoggerContext(
input.invocation_id,
handler.component().name(),
handler.name(),
handler.kind() === HandlerKind.SERVICE ? undefined : input.key,
invocationRequest,
additionalContext
);
const ctxLogger = createLogger(
Expand Down Expand Up @@ -294,8 +316,7 @@ export class GenericHandler implements RestateHandler {
input,
ctxLogger,
handler.kind(),
headers,
extraArgs,
invocationRequest,
invocationEndPromise,
inputReader,
outputWriter
Expand Down
3 changes: 3 additions & 0 deletions packages/restate-sdk/src/logging/logger_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Request } from "../context.js";

/**
* Logger level.
*/
Expand Down Expand Up @@ -73,6 +75,7 @@ export class LoggerContext {
readonly serviceName: string,
readonly handlerName: string,
readonly key?: string,
readonly request?: Request,
readonly additionalContext?: { [name: string]: string }
) {
this.invocationTarget =
Expand Down
Loading