Skip to content
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
3,149 changes: 2,969 additions & 180 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.62.0",
"version": "5.63.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -80,7 +80,7 @@
"@athenna/config": "^5.6.0",
"@athenna/ioc": "^5.2.0",
"@athenna/logger": "^5.14.0",
"@athenna/otel": "file:../Otel",
"@athenna/otel": "^5.13.0",
"@athenna/test": "^5.6.0",
"@athenna/tsconfig": "^5.0.0",
"@athenna/view": "^5.4.0",
Expand Down
10 changes: 8 additions & 2 deletions src/handlers/FastifyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { NotFoundException } from '#src/exceptions/NotFoundException'
import type { FastifyReply, FastifyRequest, RouteHandlerMethod } from 'fastify'

const otelModule = await Module.safeImport('@athenna/otel')
const otelCurrentContextBagKey = Symbol.for('athenna.otel.currentContextBag')

export class FastifyHandler {
/**
Expand Down Expand Up @@ -141,7 +140,7 @@ export class FastifyHandler {
resolveBinding: binding => binding.resolve(ctx)
})

const bag = otelContext.getValue(otelCurrentContextBagKey)
const bag = otelContext.getValue(this.getContextBagSymbol())

req.data.otelCurrentContextBag = bag
req.otelContext = otelContext
Expand All @@ -162,4 +161,11 @@ export class FastifyHandler {
ctx: this.getOrCreateOtelContext(req, ctx)
})
}

private static getContextBagSymbol() {
return (
otelModule?.Otel?.contextBagSymbol ||
Symbol.for('athenna.otel.currentContextBag')
)
}
}
28 changes: 0 additions & 28 deletions src/kernels/HttpKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,6 @@ export class HttpKernel {
await Server.plugin(staticPlugin, this.getConfig('http.static'))
}

/**
* Register the cls-rtracer plugin in the Http server.
*/
public async registerRTracer(): Promise<void> {
const rTracerPlugin = await Module.safeImport('cls-rtracer')

if (Config.is('http.rTracer.enabled', false)) {
debug(
'Not able to register rTracer plugin. Set the http.rTracer.enabled configuration as true.'
)

return
}

if (!rTracerPlugin) {
debug('Not able to register tracer plugin. Install cls-rtracer package.')

return
}

Server.middleware(async ctx => (ctx.data.traceId = rTracerPlugin.id()))

await Server.plugin(
rTracerPlugin.fastifyPlugin,
this.getConfig('http.rTracer')
)
}

/**
* Register the @athenna/vite plugin in the Http server.
*/
Expand Down
6 changes: 0 additions & 6 deletions tests/fixtures/config/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,5 @@ export default {
enabled: true,
root: Path.fixtures('config'),
prefix: '/static'
},
rTracer: {
echoHeader: false,
useHeader: false,
headerName: 'X-Request-Id',
useFastifyRequestId: false
}
}
35 changes: 0 additions & 35 deletions tests/unit/kernels/HttpKernelTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,6 @@ export default class HttpKernelTest {
assert.deepEqual(response.headers['cache-control'], 'public, max-age=0')
}

@Test()
public async shouldBeAbleToRegisterTheFastifyRTTracerPluginInTheHttpServer({ assert }: Context) {
const kernel = new HttpKernel()
await kernel.registerRTracer()
Server.get({ url: '/hello', handler: ctx => ctx.response.send({ traceId: ctx.data.traceId }) })

const response = await Server.request().get('hello')

assert.isDefined(response.json().traceId)
assert.isTrue(Server.fastify.hasPlugin('cls-rtracer'))
}

@Test()
public async shouldBeAbleToRegisterTheFastifyVitePluginInTheHttpServer({ assert }: Context) {
const kernel = new HttpKernel()
Expand Down Expand Up @@ -288,29 +276,6 @@ export default class HttpKernelTest {
assert.isFalse(Server.fastify.hasPlugin('@fastify/static'))
}

@Test()
public async shouldNotRegisterTheFastifyRTracerPluginIfThePackageIsNotInstalled({ assert }: Context) {
Mock.when(Module, 'safeImport').resolve(null)

const { HttpKernel } = await import(`../../../src/kernels/HttpKernel.js?v=${Math.random()}`)
const kernel = new HttpKernel()
await kernel.registerRTracer()

assert.isFalse(Server.fastify.hasPlugin('cls-rtracer'))
}

@Test()
@Cleanup(() => Config.set('http.rTracer.enabled', true))
public async shouldNotRegisterTheFastifyRTracerPluginIfTheConfigurationIsDisabled({ assert }: Context) {
Config.set('http.rTracer.enabled', false)

const { HttpKernel } = await import(`../../../src/kernels/HttpKernel.js?v=${Math.random()}`)
const kernel = new HttpKernel()
await kernel.registerRTracer()

assert.isFalse(Server.fastify.hasPlugin('cls-rtracer'))
}

@Test()
@Cleanup(() => Config.set('http.logger.enabled', true))
public async shouldNotRegisterTheLoggerTerminatorIfTheConfigIsDisabled({ assert }: Context) {
Expand Down
23 changes: 7 additions & 16 deletions tests/unit/server/ServerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { Config } from '@athenna/config'
import { OtelProvider } from '@athenna/otel'
import { Server, HttpServerProvider } from '#src'
import { context, createContextKey } from '@opentelemetry/api'
import { Test, AfterEach, BeforeEach, type Context, Cleanup } from '@athenna/test'
Expand All @@ -21,12 +22,14 @@ export default class ServerTest {
ioc.reconstruct()

context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable())
new OtelProvider().register()
new HttpServerProvider().register()
}

@AfterEach()
public async afterEach() {
context.disable()
await new OtelProvider().shutdown()
await new HttpServerProvider().shutdown()
}

Expand Down Expand Up @@ -280,17 +283,11 @@ export default class ServerTest {
Config.set('http.otel.contextBindings', [{ key: exampleIdKey, resolve: () => 'example-id-from-binding' }])

Server.terminate(() => {
terminateBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<
string | symbol,
unknown
>
terminateBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<string | symbol, unknown>
}).get({
url: '/test',
handler: async ctx => {
requestBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<
string | symbol,
unknown
>
requestBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<string | symbol, unknown>
requestBag.set(exampleIdKey, 'example-id-from-handler')

await ctx.response.send({
Expand Down Expand Up @@ -345,10 +342,7 @@ export default class ServerTest {
Config.set('http.otel.contextBindings', [{ key: 'exampleId', resolve: () => 'example-id-from-binding' }])

Server.setErrorHandler(async ctx => {
errorBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<
string | symbol,
unknown
>
errorBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<string | symbol, unknown>

await ctx.response.status(500).send({
exampleId: errorBag.get('exampleId')
Expand All @@ -358,10 +352,7 @@ export default class ServerTest {
Server.get({
url: '/boom',
handler: async () => {
requestBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<
string | symbol,
unknown
>
requestBag = context.active().getValue(otelCurrentContextBagKey as any) as Map<string | symbol, unknown>
requestBag.set('exampleId', 'example-id-from-handler')

throw new Error('boom')
Expand Down
Loading