generateTraceId function in Motia codebase.

Ramu NarasingaRamu Narasinga
1 min read

In this article, we review generateTraceId function in Motia codebase. We will look at:

  1. generateTraceId fn definition.

  2. Usage in Motia codebase.

Press enter or click to view image in full size

generateTraceId fn definition

In motia/packages/core/generate-trace-id.ts, you will find the following code:

export const generateTraceId = () => {
  const datePart = String(Date.now()).slice(6)
  const randomPart = generateCode(5)
  return `${randomPart}-${datePart}`
}

There’s datePart and randomPart. You will find the generateCode defined in the same file:

export const generateCode = (length = 6) => {
  const chars = 'ABCDEFGHJKMNPQRSTUVWXYZ123456789'
  return Array.from({ length }, () => chars[Math.floor(Math.random() * chars.length)]).join('')
}

TraceID is a combination of random part and date part.

Usage in Motia codebase

You will find 4 references in the Motia codebase (There could be more).

Press enter or click to view image in full size

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Want to learn from open-source? Solve challenges inspired by open-source projects.

References:

  1. https://github.com/MotiaDev/motia/blob/main/packages/core/src/generate-trace-id.ts

  2. https://github.com/MotiaDev/motia/blob/main/packages/core/src/cron-handler.ts#L3

  3. https://github.com/MotiaDev/motia/blob/main/packages/core/src/server.ts#L12

0
Subscribe to my newsletter

Read articles from Ramu Narasinga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ramu Narasinga
Ramu Narasinga

I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.