Skip to content

Actionhero tracing with Async Local Storage #2384

Answered by evantahler
IgorHulyaschy asked this question in Q&A
Discussion options

You must be logged in to vote

Would it work to store the local storage instance on the instance of the action's data object?

import { Initializer, ActionProcessor, action } from "actionhero";
import { AsyncLocalStorage } from "node:async_hooks";

const asyncLocalStorage = new AsyncLocalStorage();
let idSeq = 0;

function logWithId(msg: string) {
  const id = asyncLocalStorage.getStore();
  console.log(`${id !== undefined ? id : "-"}:`, msg);
}

const asyncHookActionMiddleware: action.ActionMiddleware = {
  name: "authenticated-action",
  global: true,
  preProcessor: async (data: ActionProcessor<any>) => {
    asyncLocalStorage.run(idSeq++, () => {
      const id = asyncLocalStorage.getStore();
      logWithId("start");

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@IgorHulyaschy
Comment options

Answer selected by evantahler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants