Skip to main content

Tracing Requests

Understand how to trace requests in your REST API application of Athenna.

Introduction

Briefly, tracing represents a single user's journey through an entire app stack. By tracing through a stack, developers can identify what happened with determined request and also identify bottlenecks and focus on improving performance.

Basic usage

Athenna uses the cls-rtracer plugin inside HttpKernel. All the configurations that cls-rtracer supports can be set inside

Path.config('http.ts')

./src/config/http.ts

file in the rTracer object:

Path.config('http.ts')
export default {
rTracer: {
enabled: true,
echoHeader: false,
useHeader: false,
headerName: 'X-Request-Id',
useFastifyRequestId: false
}
}

When trace is set to true, the cls-rtracer plugin will be enabled and a UUID will be generated for each request. This UUID will be available in request.id property of your routes:

Path.routes('http.ts')
import { Log } from '@athenna/logger'
import { Route } from '@athenna/http'

Route.get('/', ({ request }) => {
Log.info(request.id) // 123e4567-e89b-12d3-a456-426614174000

return 'Hello World!'
})
tip

Athenna request logger enabled by http.logger configuration will automatically log the request ID for you depending on the formatter your are using.

Disabling tracer

The HttpKernel class will automatically disable the plugin registration if the package does not exist, so to disable tracer in Athenna you need to remove the cls-rtracer package from your application:

npm remove cls-rtracer

You can also disable by setting http.rTracer.enabled to false:

Path.config('http.ts')
export default {
rTracer: {
enabled: false
}
}

Or by setting the trace option as false when booting the server in Ignite.httpServer() method:

Path.bin('main.ts')
import { Ignite } from '@athenna/core'

const ignite = await new Ignite().load(import.meta.url)

await ignite.httpServer({
trace: false 👈
})