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
60 changes: 58 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.61.0",
"version": "5.62.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,6 +80,7 @@
"@athenna/config": "^5.6.0",
"@athenna/ioc": "^5.2.0",
"@athenna/logger": "^5.14.0",
"@athenna/otel": "file:../Otel",
"@athenna/test": "^5.6.0",
"@athenna/tsconfig": "^5.0.0",
"@athenna/view": "^5.4.0",
Expand Down
30 changes: 14 additions & 16 deletions src/handlers/FastifyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { Config } from '@athenna/config'
import { Is, Module } from '@athenna/common'
import { Request } from '#src/context/Request'
import { Response } from '#src/context/Response'
import type { Context as OtelContext } from '@opentelemetry/api'
import type { Context as OtelContext } from '@athenna/otel'
import type { RequestHandler } from '#src/types/contexts/Context'
import type { ErrorHandler } from '#src/types/contexts/ErrorContext'
import type { InterceptHandler, TerminateHandler } from '#src/types'
import { NotFoundException } from '#src/exceptions/NotFoundException'
import type { FastifyReply, FastifyRequest, RouteHandlerMethod } from 'fastify'

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

export class FastifyHandler {
Expand Down Expand Up @@ -132,22 +132,18 @@ export class FastifyHandler {
return req.otelContext as OtelContext
}

let otelContext = otelApi.context.active()
const bag = new Map<string | symbol, unknown>()

for (const binding of Config.get('http.otel.contextBindings', [])) {
const value = binding.resolve(ctx)
if (!otelModule) {
throw new Error('The package @athenna/otel is not installed')
}

if (Is.Undefined(value) && !binding.includeIfUndefined) {
continue
}
const otelContext = otelModule.Otel.createContext({
bindings: Config.get('http.otel.contextBindings', []),
resolveBinding: binding => binding.resolve(ctx)
})

bag.set(binding.key, value)
otelContext = otelContext.setValue(binding.key, value)
}
const bag = otelContext.getValue(otelCurrentContextBagKey)

req.data.otelCurrentContextBag = bag
otelContext = otelContext.setValue(otelCurrentContextBagKey as any, bag)
req.otelContext = otelContext

return otelContext as OtelContext
Expand All @@ -158,10 +154,12 @@ export class FastifyHandler {
ctx: any,
callback: () => any
) {
if (!this.isOtelContextEnabled() || !otelApi) {
if (!this.isOtelContextEnabled() || !otelModule) {
return callback()
}

return otelApi.context.with(this.getOrCreateOtelContext(req, ctx), callback)
return otelModule.Otel.withContext(callback, {
ctx: this.getOrCreateOtelContext(req, ctx)
})
}
}
Loading