apitally 0.24.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/dist/adonisjs/configure.cjs +1 -1
  2. package/dist/adonisjs/index.cjs +4 -1
  3. package/dist/adonisjs/index.cjs.map +1 -1
  4. package/dist/adonisjs/index.d.cts +2 -0
  5. package/dist/adonisjs/index.d.ts +2 -0
  6. package/dist/adonisjs/index.js +2 -0
  7. package/dist/adonisjs/index.js.map +1 -1
  8. package/dist/adonisjs/middleware.cjs +9 -5
  9. package/dist/adonisjs/middleware.cjs.map +1 -1
  10. package/dist/adonisjs/middleware.d.cts +2 -0
  11. package/dist/adonisjs/middleware.d.ts +2 -0
  12. package/dist/adonisjs/middleware.js +6 -2
  13. package/dist/adonisjs/middleware.js.map +1 -1
  14. package/dist/adonisjs/provider.cjs +3 -3
  15. package/dist/adonisjs/provider.d.cts +2 -0
  16. package/dist/adonisjs/provider.d.ts +2 -0
  17. package/dist/adonisjs/stubs/config/apitally.stub +1 -0
  18. package/dist/common/client.cjs +13 -9
  19. package/dist/common/client.cjs.map +1 -1
  20. package/dist/common/client.d.cts +3 -0
  21. package/dist/common/client.d.ts +3 -0
  22. package/dist/common/client.js +4 -0
  23. package/dist/common/client.js.map +1 -1
  24. package/dist/common/consumerRegistry.d.cts +2 -0
  25. package/dist/common/consumerRegistry.d.ts +2 -0
  26. package/dist/common/requestCounter.d.cts +2 -0
  27. package/dist/common/requestCounter.d.ts +2 -0
  28. package/dist/common/requestLogger.cjs +14 -5
  29. package/dist/common/requestLogger.cjs.map +1 -1
  30. package/dist/common/requestLogger.d.cts +6 -1
  31. package/dist/common/requestLogger.d.ts +6 -1
  32. package/dist/common/requestLogger.js +11 -2
  33. package/dist/common/requestLogger.js.map +1 -1
  34. package/dist/common/serverErrorCounter.cjs +1 -1
  35. package/dist/common/serverErrorCounter.d.cts +2 -0
  36. package/dist/common/serverErrorCounter.d.ts +2 -0
  37. package/dist/common/spanCollector.cjs +217 -0
  38. package/dist/common/spanCollector.cjs.map +1 -0
  39. package/dist/common/spanCollector.d.cts +50 -0
  40. package/dist/common/spanCollector.d.ts +50 -0
  41. package/dist/common/spanCollector.js +194 -0
  42. package/dist/common/spanCollector.js.map +1 -0
  43. package/dist/common/types.d.cts +2 -0
  44. package/dist/common/types.d.ts +2 -0
  45. package/dist/common/validationErrorCounter.d.cts +2 -0
  46. package/dist/common/validationErrorCounter.d.ts +2 -0
  47. package/dist/elysia/index.cjs +4 -1
  48. package/dist/elysia/index.cjs.map +1 -1
  49. package/dist/elysia/index.d.cts +2 -0
  50. package/dist/elysia/index.d.ts +2 -0
  51. package/dist/elysia/index.js +2 -0
  52. package/dist/elysia/index.js.map +1 -1
  53. package/dist/elysia/plugin.cjs +16 -8
  54. package/dist/elysia/plugin.cjs.map +1 -1
  55. package/dist/elysia/plugin.d.cts +4 -0
  56. package/dist/elysia/plugin.d.ts +4 -0
  57. package/dist/elysia/plugin.js +9 -1
  58. package/dist/elysia/plugin.js.map +1 -1
  59. package/dist/elysia/utils.cjs +1 -1
  60. package/dist/elysia/utils.d.cts +2 -0
  61. package/dist/elysia/utils.d.ts +2 -0
  62. package/dist/express/index.cjs +4 -1
  63. package/dist/express/index.cjs.map +1 -1
  64. package/dist/express/index.d.cts +2 -0
  65. package/dist/express/index.d.ts +2 -0
  66. package/dist/express/index.js +2 -0
  67. package/dist/express/index.js.map +1 -1
  68. package/dist/express/middleware.cjs +92 -84
  69. package/dist/express/middleware.cjs.map +1 -1
  70. package/dist/express/middleware.d.cts +2 -0
  71. package/dist/express/middleware.d.ts +2 -0
  72. package/dist/express/middleware.js +85 -77
  73. package/dist/express/middleware.js.map +1 -1
  74. package/dist/fastify/index.cjs +4 -1
  75. package/dist/fastify/index.cjs.map +1 -1
  76. package/dist/fastify/index.d.cts +2 -0
  77. package/dist/fastify/index.d.ts +2 -0
  78. package/dist/fastify/index.js +2 -0
  79. package/dist/fastify/index.js.map +1 -1
  80. package/dist/fastify/plugin.cjs +20 -11
  81. package/dist/fastify/plugin.cjs.map +1 -1
  82. package/dist/fastify/plugin.d.cts +4 -0
  83. package/dist/fastify/plugin.d.ts +4 -0
  84. package/dist/fastify/plugin.js +14 -5
  85. package/dist/fastify/plugin.js.map +1 -1
  86. package/dist/h3/index.cjs +4 -1
  87. package/dist/h3/index.cjs.map +1 -1
  88. package/dist/h3/index.d.cts +2 -0
  89. package/dist/h3/index.d.ts +2 -0
  90. package/dist/h3/index.js +2 -0
  91. package/dist/h3/index.js.map +1 -1
  92. package/dist/h3/plugin.cjs +20 -12
  93. package/dist/h3/plugin.cjs.map +1 -1
  94. package/dist/h3/plugin.d.cts +4 -0
  95. package/dist/h3/plugin.d.ts +4 -0
  96. package/dist/h3/plugin.js +13 -5
  97. package/dist/h3/plugin.js.map +1 -1
  98. package/dist/h3/utils.cjs +1 -1
  99. package/dist/hapi/index.cjs +4 -1
  100. package/dist/hapi/index.cjs.map +1 -1
  101. package/dist/hapi/index.d.cts +2 -0
  102. package/dist/hapi/index.d.ts +2 -0
  103. package/dist/hapi/index.js +2 -0
  104. package/dist/hapi/index.js.map +1 -1
  105. package/dist/hapi/plugin.cjs +15 -8
  106. package/dist/hapi/plugin.cjs.map +1 -1
  107. package/dist/hapi/plugin.d.cts +4 -0
  108. package/dist/hapi/plugin.d.ts +4 -0
  109. package/dist/hapi/plugin.js +10 -3
  110. package/dist/hapi/plugin.js.map +1 -1
  111. package/dist/hapi/utils.cjs +1 -1
  112. package/dist/hapi/utils.d.cts +2 -0
  113. package/dist/hapi/utils.d.ts +2 -0
  114. package/dist/hono/index.cjs +4 -1
  115. package/dist/hono/index.cjs.map +1 -1
  116. package/dist/hono/index.d.cts +2 -0
  117. package/dist/hono/index.d.ts +2 -0
  118. package/dist/hono/index.js +2 -0
  119. package/dist/hono/index.js.map +1 -1
  120. package/dist/hono/middleware.cjs +13 -9
  121. package/dist/hono/middleware.cjs.map +1 -1
  122. package/dist/hono/middleware.d.cts +2 -0
  123. package/dist/hono/middleware.d.ts +2 -0
  124. package/dist/hono/middleware.js +6 -2
  125. package/dist/hono/middleware.js.map +1 -1
  126. package/dist/hono/utils.cjs +1 -1
  127. package/dist/hono/utils.d.cts +2 -0
  128. package/dist/hono/utils.d.ts +2 -0
  129. package/dist/koa/index.cjs +4 -1
  130. package/dist/koa/index.cjs.map +1 -1
  131. package/dist/koa/index.d.cts +2 -0
  132. package/dist/koa/index.d.ts +2 -0
  133. package/dist/koa/index.js +2 -0
  134. package/dist/koa/index.js.map +1 -1
  135. package/dist/koa/middleware.cjs +13 -9
  136. package/dist/koa/middleware.cjs.map +1 -1
  137. package/dist/koa/middleware.d.cts +2 -0
  138. package/dist/koa/middleware.d.ts +2 -0
  139. package/dist/koa/middleware.js +8 -4
  140. package/dist/koa/middleware.js.map +1 -1
  141. package/dist/loggers/console.d.cts +4 -0
  142. package/dist/loggers/console.d.ts +4 -0
  143. package/dist/loggers/hapi.d.cts +4 -0
  144. package/dist/loggers/hapi.d.ts +4 -0
  145. package/dist/loggers/index.cjs +5 -5
  146. package/dist/loggers/index.d.cts +4 -0
  147. package/dist/loggers/index.d.ts +4 -0
  148. package/dist/loggers/nestjs.cjs +1 -1
  149. package/dist/loggers/nestjs.d.cts +4 -0
  150. package/dist/loggers/nestjs.d.ts +4 -0
  151. package/dist/loggers/pino.cjs +1 -1
  152. package/dist/loggers/pino.d.cts +4 -0
  153. package/dist/loggers/pino.d.ts +4 -0
  154. package/dist/loggers/winston.cjs +1 -1
  155. package/dist/loggers/winston.d.cts +4 -0
  156. package/dist/loggers/winston.d.ts +4 -0
  157. package/dist/nestjs/index.cjs +3 -0
  158. package/dist/nestjs/index.cjs.map +1 -1
  159. package/dist/nestjs/index.d.cts +2 -0
  160. package/dist/nestjs/index.d.ts +2 -0
  161. package/dist/nestjs/index.js +2 -0
  162. package/dist/nestjs/index.js.map +1 -1
  163. package/package.json +6 -2
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/koa/middleware.ts"],"sourcesContent":["import Koa from \"koa\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport { ApitallyClient } from \"../common/client.js\";\nimport { consumerFromStringOrObject } from \"../common/consumerRegistry.js\";\nimport { getPackageVersion } from \"../common/packageVersions.js\";\nimport type { LogRecord } from \"../common/requestLogger.js\";\nimport { convertBody, convertHeaders } from \"../common/requestLogger.js\";\nimport {\n ApitallyConfig,\n ApitallyConsumer,\n PathInfo,\n StartupData,\n} from \"../common/types.js\";\nimport { patchConsole, patchWinston } from \"../loggers/index.js\";\n\nexport function useApitally(app: Koa, config: ApitallyConfig) {\n const client = new ApitallyClient(config);\n const middleware = getMiddleware(client);\n app.use(middleware);\n\n const setStartupData = (attempt: number = 1) => {\n const appInfo = getAppInfo(app, config.appVersion);\n if (appInfo.paths.length > 0 || attempt >= 10) {\n client.setStartupData(appInfo);\n client.startSync();\n } else {\n setTimeout(() => setStartupData(attempt + 1), 500);\n }\n };\n setTimeout(() => setStartupData(), 500);\n}\n\nfunction getMiddleware(client: ApitallyClient) {\n const logsContext = new AsyncLocalStorage<LogRecord[]>();\n\n if (client.requestLogger.config.captureLogs) {\n patchConsole(logsContext);\n patchWinston(logsContext);\n }\n\n return async (ctx: Koa.Context, next: Koa.Next) => {\n if (!client.isEnabled() || ctx.request.method.toUpperCase() === \"OPTIONS\") {\n await next();\n return;\n }\n\n await logsContext.run([], async () => {\n let path: string | undefined;\n let statusCode: number | undefined;\n let serverError: Error | undefined;\n const startTime = performance.now();\n try {\n await next();\n } catch (error: any) {\n path = getPath(ctx);\n statusCode = error.statusCode || error.status || 500;\n if (path && statusCode === 500 && error instanceof Error) {\n serverError = error;\n client.serverErrorCounter.addServerError({\n consumer: getConsumer(ctx)?.identifier,\n method: ctx.request.method,\n path,\n type: error.name,\n msg: error.message,\n traceback: error.stack || \"\",\n });\n }\n throw error;\n } finally {\n const responseTime = performance.now() - startTime;\n const consumer = getConsumer(ctx);\n client.consumerRegistry.addOrUpdateConsumer(consumer);\n if (!path) {\n path = getPath(ctx);\n }\n if (path) {\n try {\n client.requestCounter.addRequest({\n consumer: consumer?.identifier,\n method: ctx.request.method,\n path,\n statusCode: statusCode || ctx.response.status,\n responseTime,\n requestSize: ctx.request.length,\n responseSize: ctx.response.length,\n });\n } catch (error) {\n client.logger.error(\n \"Error while logging request in Apitally middleware.\",\n { context: ctx, error },\n );\n }\n }\n if (client.requestLogger.enabled) {\n const logs = logsContext.getStore();\n client.requestLogger.logRequest(\n {\n timestamp: Date.now() / 1000,\n method: ctx.request.method,\n path,\n url: ctx.request.href,\n headers: convertHeaders(ctx.request.headers),\n size: ctx.request.length,\n consumer: consumer?.identifier,\n body: convertBody(\n ctx.request.body,\n ctx.request.get(\"content-type\"),\n ),\n },\n {\n statusCode: statusCode || ctx.response.status,\n responseTime: responseTime / 1000,\n headers: convertHeaders(ctx.response.headers),\n size: ctx.response.length,\n body: convertBody(\n ctx.response.body,\n ctx.response.get(\"content-type\"),\n ),\n },\n serverError,\n logs,\n );\n }\n }\n });\n };\n}\n\nfunction getPath(ctx: Koa.Context) {\n return ctx._matchedRoute || ctx.routePath; // _matchedRoute is set by koa-router, routePath is set by koa-route\n}\n\nexport function setConsumer(\n ctx: Koa.Context,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n ctx.state.apitallyConsumer = consumer || undefined;\n}\n\nfunction getConsumer(ctx: Koa.Context) {\n if (ctx.state.apitallyConsumer) {\n return consumerFromStringOrObject(ctx.state.apitallyConsumer);\n } else if (ctx.state.consumerIdentifier) {\n // For backwards compatibility\n process.emitWarning(\n \"The consumerIdentifier property on the ctx.state object is deprecated. Use apitallyConsumer instead.\",\n \"DeprecationWarning\",\n );\n return consumerFromStringOrObject(ctx.state.consumerIdentifier);\n }\n return null;\n}\n\nfunction getAppInfo(app: Koa, appVersion?: string): StartupData {\n const versions: Array<[string, string]> = [\n [\"nodejs\", process.version.replace(/^v/, \"\")],\n ];\n const koaVersion = getPackageVersion(\"koa\");\n const apitallyVersion = getPackageVersion(\"../..\");\n if (koaVersion) {\n versions.push([\"koa\", koaVersion]);\n }\n if (apitallyVersion) {\n versions.push([\"apitally\", apitallyVersion]);\n }\n if (appVersion) {\n versions.push([\"app\", appVersion]);\n }\n return {\n paths: listEndpoints(app),\n versions: Object.fromEntries(versions),\n client: \"js:koa\",\n };\n}\n\nfunction isKoaRouterMiddleware(middleware: any) {\n return (\n typeof middleware === \"function\" &&\n middleware.router &&\n Array.isArray(middleware.router.stack)\n );\n}\n\nfunction listEndpoints(app: Koa) {\n const endpoints: Array<PathInfo> = [];\n app.middleware.forEach((middleware: any) => {\n if (isKoaRouterMiddleware(middleware)) {\n middleware.router.stack.forEach((layer: any) => {\n if (layer.methods && layer.methods.length > 0) {\n layer.methods.forEach((method: string) => {\n if (![\"HEAD\", \"OPTIONS\"].includes(method.toUpperCase())) {\n endpoints.push({\n method: method.toUpperCase(),\n path: layer.path,\n });\n }\n });\n }\n });\n }\n });\n return endpoints;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AACA;;;;;;AAAA,8BAAkC;AAElC,oBAA+B;AAC/B,8BAA2C;AAC3C,6BAAkC;AAElC,2BAA4C;AAO5C,qBAA2C;AAEpC,SAASA,YAAYC,KAAUC,QAAsB;AAC1D,QAAMC,SAAS,IAAIC,6BAAeF,MAAAA;AAClC,QAAMG,aAAaC,cAAcH,MAAAA;AACjCF,MAAIM,IAAIF,UAAAA;AAER,QAAMG,iBAAiB,wBAACC,UAAkB,MAAC;AACzC,UAAMC,UAAUC,WAAWV,KAAKC,OAAOU,UAAU;AACjD,QAAIF,QAAQG,MAAMC,SAAS,KAAKL,WAAW,IAAI;AAC7CN,aAAOK,eAAeE,OAAAA;AACtBP,aAAOY,UAAS;IAClB,OAAO;AACLC,iBAAW,MAAMR,eAAeC,UAAU,CAAA,GAAI,GAAA;IAChD;EACF,GARuB;AASvBO,aAAW,MAAMR,eAAAA,GAAkB,GAAA;AACrC;AAfgBR;AAiBhB,SAASM,cAAcH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iddleware","use","setStartupData","attempt","appInfo","getAppInfo","appVersion","paths","length","startSync","setTimeout","logsContext","AsyncLocalStorage","requestLogger","captureLogs","patchConsole","patchWinston","ctx","next","isEnabled","request","method","toUpperCase","run","path","statusCode","serverError","startTime","performance","now","error","getPath","status","Error","serverErrorCounter","addServerError","consumer","getConsumer","identifier","type","name","msg","message","traceback","stack","responseTime","consumerRegistry","addOrUpdateConsumer","requestCounter","addRequest","response","requestSize","responseSize","logger","context","enabled","logs","getStore","logRequest","timestamp","Date","url","href","headers","convertHeaders","size","body","convertBody","get","_matchedRoute","routePath","setConsumer","state","apitallyConsumer","undefined","consumerFromStringOrObject","consumerIdentifier","process","emitWarning","versions","version","replace","koaVersion","getPackageVersion","apitallyVersion","push","listEndpoints","Object","fromEntries","isKoaRouterMiddleware","router","Array","isArray","endpoints","forEach","layer","methods","includes"]}
1
+ {"version":3,"sources":["../../src/koa/middleware.ts"],"sourcesContent":["import Koa from \"koa\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport { ApitallyClient } from \"../common/client.js\";\nimport { consumerFromStringOrObject } from \"../common/consumerRegistry.js\";\nimport { getPackageVersion } from \"../common/packageVersions.js\";\nimport type { LogRecord } from \"../common/requestLogger.js\";\nimport { convertBody, convertHeaders } from \"../common/requestLogger.js\";\nimport {\n ApitallyConfig,\n ApitallyConsumer,\n PathInfo,\n StartupData,\n} from \"../common/types.js\";\nimport { patchConsole, patchWinston } from \"../loggers/index.js\";\n\nexport function useApitally(app: Koa, config: ApitallyConfig) {\n const client = new ApitallyClient(config);\n const middleware = getMiddleware(client);\n app.use(middleware);\n\n const setStartupData = (attempt: number = 1) => {\n const appInfo = getAppInfo(app, config.appVersion);\n if (appInfo.paths.length > 0 || attempt >= 10) {\n client.setStartupData(appInfo);\n client.startSync();\n } else {\n setTimeout(() => setStartupData(attempt + 1), 500);\n }\n };\n setTimeout(() => setStartupData(), 500);\n}\n\nfunction getMiddleware(client: ApitallyClient) {\n const logsContext = new AsyncLocalStorage<LogRecord[]>();\n\n if (client.requestLogger.config.captureLogs) {\n patchConsole(logsContext);\n patchWinston(logsContext);\n }\n\n return async (ctx: Koa.Context, next: Koa.Next) => {\n if (!client.isEnabled() || ctx.request.method.toUpperCase() === \"OPTIONS\") {\n await next();\n return;\n }\n\n await logsContext.run([], async () => {\n let path: string | undefined;\n let statusCode: number | undefined;\n let serverError: Error | undefined;\n const startTime = performance.now();\n const spanHandle = client.spanCollector.startSpan();\n try {\n await spanHandle.runInContext(next);\n } catch (error: any) {\n path = getPath(ctx);\n statusCode = error.statusCode || error.status || 500;\n if (path && statusCode === 500 && error instanceof Error) {\n serverError = error;\n client.serverErrorCounter.addServerError({\n consumer: getConsumer(ctx)?.identifier,\n method: ctx.request.method,\n path,\n type: error.name,\n msg: error.message,\n traceback: error.stack || \"\",\n });\n }\n throw error;\n } finally {\n const responseTime = performance.now() - startTime;\n\n if (!path) {\n path = getPath(ctx);\n }\n\n spanHandle.setName(`${ctx.request.method} ${path}`);\n const spans = spanHandle.end();\n const traceId = spanHandle.traceId;\n\n const consumer = getConsumer(ctx);\n client.consumerRegistry.addOrUpdateConsumer(consumer);\n\n if (path) {\n try {\n client.requestCounter.addRequest({\n consumer: consumer?.identifier,\n method: ctx.request.method,\n path,\n statusCode: statusCode || ctx.response.status,\n responseTime,\n requestSize: ctx.request.length,\n responseSize: ctx.response.length,\n });\n } catch (error) {\n client.logger.error(\n \"Error while logging request in Apitally middleware.\",\n { context: ctx, error },\n );\n }\n }\n\n if (client.requestLogger.enabled) {\n const logs = logsContext.getStore();\n client.requestLogger.logRequest(\n {\n timestamp: Date.now() / 1000,\n method: ctx.request.method,\n path,\n url: ctx.request.href,\n headers: convertHeaders(ctx.request.headers),\n size: ctx.request.length,\n consumer: consumer?.identifier,\n body: convertBody(\n ctx.request.body,\n ctx.request.get(\"content-type\"),\n ),\n },\n {\n statusCode: statusCode || ctx.response.status,\n responseTime: responseTime / 1000,\n headers: convertHeaders(ctx.response.headers),\n size: ctx.response.length,\n body: convertBody(\n ctx.response.body,\n ctx.response.get(\"content-type\"),\n ),\n },\n serverError,\n logs,\n spans,\n traceId,\n );\n }\n }\n });\n };\n}\n\nfunction getPath(ctx: Koa.Context) {\n return ctx._matchedRoute || ctx.routePath; // _matchedRoute is set by koa-router, routePath is set by koa-route\n}\n\nexport function setConsumer(\n ctx: Koa.Context,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n ctx.state.apitallyConsumer = consumer || undefined;\n}\n\nfunction getConsumer(ctx: Koa.Context) {\n if (ctx.state.apitallyConsumer) {\n return consumerFromStringOrObject(ctx.state.apitallyConsumer);\n } else if (ctx.state.consumerIdentifier) {\n // For backwards compatibility\n process.emitWarning(\n \"The consumerIdentifier property on the ctx.state object is deprecated. Use apitallyConsumer instead.\",\n \"DeprecationWarning\",\n );\n return consumerFromStringOrObject(ctx.state.consumerIdentifier);\n }\n return null;\n}\n\nfunction getAppInfo(app: Koa, appVersion?: string): StartupData {\n const versions: Array<[string, string]> = [\n [\"nodejs\", process.version.replace(/^v/, \"\")],\n ];\n const koaVersion = getPackageVersion(\"koa\");\n const apitallyVersion = getPackageVersion(\"../..\");\n if (koaVersion) {\n versions.push([\"koa\", koaVersion]);\n }\n if (apitallyVersion) {\n versions.push([\"apitally\", apitallyVersion]);\n }\n if (appVersion) {\n versions.push([\"app\", appVersion]);\n }\n return {\n paths: listEndpoints(app),\n versions: Object.fromEntries(versions),\n client: \"js:koa\",\n };\n}\n\nfunction isKoaRouterMiddleware(middleware: any) {\n return (\n typeof middleware === \"function\" &&\n middleware.router &&\n Array.isArray(middleware.router.stack)\n );\n}\n\nfunction listEndpoints(app: Koa) {\n const endpoints: Array<PathInfo> = [];\n app.middleware.forEach((middleware: any) => {\n if (isKoaRouterMiddleware(middleware)) {\n middleware.router.stack.forEach((layer: any) => {\n if (layer.methods && layer.methods.length > 0) {\n layer.methods.forEach((method: string) => {\n if (![\"HEAD\", \"OPTIONS\"].includes(method.toUpperCase())) {\n endpoints.push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M,KAAK;MAAC;MAAYD;KAAgB;EAC7C;AACA,MAAItF,YAAY;AACdiF,aAASM,KAAK;MAAC;MAAOvF;KAAW;EACnC;AACA,SAAO;IACLC,OAAOuF,cAAcnG,GAAAA;IACrB4F,UAAUQ,OAAOC,YAAYT,QAAAA;IAC7B1F,QAAQ;EACV;AACF;AApBSQ;AAsBT,SAAS4F,sBAAsBlG,YAAe;AAC5C,SACE,OAAOA,eAAe,cACtBA,WAAWmG,UACXC,MAAMC,QAAQrG,WAAWmG,OAAOlD,KAAK;AAEzC;AANSiD;AAQT,SAASH,cAAcnG,KAAQ;AAC7B,QAAM0G,YAA6B,CAAA;AACnC1G,MAAII,WAAWuG,QAAQ,CAACvG,eAAAA;AACtB,QAAIkG,sBAAsBlG,UAAAA,GAAa;AACrCA,iBAAWmG,OAAOlD,MAAMsD,QAAQ,CAACC,UAAAA;AAC/B,YAAIA,MAAMC,WAAWD,MAAMC,QAAQhG,SAAS,GAAG;AAC7C+F,gBAAMC,QAAQF,QAAQ,CAACjF,WAAAA;AACrB,gBAAI,CAAC;cAAC;cAAQ;cAAWoF,SAASpF,OAAOC,YAAW,CAAA,GAAK;AACvD+E,wBAAUR,KAAK;gBACbxE,QAAQA,OAAOC,YAAW;gBAC1BE,MAAM+E,MAAM/E;cACd,CAAA;YACF;UACF,CAAA;QACF;MACF,CAAA;IACF;EACF,CAAA;AACA,SAAO6E;AACT;AAnBSP;","names":["useApitally","app","config","client","ApitallyClient","middleware","getMiddleware","use","setStartupData","attempt","appInfo","getAppInfo","appVersion","paths","length","startSync","setTimeout","logsContext","AsyncLocalStorage","requestLogger","captureLogs","patchConsole","patchWinston","ctx","next","isEnabled","request","method","toUpperCase","run","path","statusCode","serverError","startTime","performance","now","spanHandle","spanCollector","startSpan","runInContext","error","getPath","status","Error","serverErrorCounter","addServerError","consumer","getConsumer","identifier","type","name","msg","message","traceback","stack","responseTime","setName","spans","end","traceId","consumerRegistry","addOrUpdateConsumer","requestCounter","addRequest","response","requestSize","responseSize","logger","context","enabled","logs","getStore","logRequest","timestamp","Date","url","href","headers","convertHeaders","size","body","convertBody","get","_matchedRoute","routePath","setConsumer","state","apitallyConsumer","undefined","consumerFromStringOrObject","consumerIdentifier","process","emitWarning","versions","version","replace","koaVersion","getPackageVersion","apitallyVersion","push","listEndpoints","Object","fromEntries","isKoaRouterMiddleware","router","Array","isArray","endpoints","forEach","layer","methods","includes"]}
@@ -1,5 +1,7 @@
1
+ import '../common/spanCollector.cjs';
1
2
  import { ApitallyConsumer, ApitallyConfig } from '../common/types.cjs';
2
3
  import Koa from 'koa';
4
+ import '@opentelemetry/sdk-trace-base';
3
5
  import '../common/logging.cjs';
4
6
  import 'winston';
5
7
  import '../common/requestLogger.cjs';
@@ -1,5 +1,7 @@
1
+ import '../common/spanCollector.js';
1
2
  import { ApitallyConsumer, ApitallyConfig } from '../common/types.js';
2
3
  import Koa from 'koa';
4
+ import '@opentelemetry/sdk-trace-base';
3
5
  import '../common/logging.js';
4
6
  import 'winston';
5
7
  import '../common/requestLogger.js';
@@ -39,8 +39,9 @@ function getMiddleware(client) {
39
39
  let statusCode;
40
40
  let serverError;
41
41
  const startTime = performance.now();
42
+ const spanHandle = client.spanCollector.startSpan();
42
43
  try {
43
- await next();
44
+ await spanHandle.runInContext(next);
44
45
  } catch (error) {
45
46
  path = getPath(ctx);
46
47
  statusCode = error.statusCode || error.status || 500;
@@ -58,11 +59,14 @@ function getMiddleware(client) {
58
59
  throw error;
59
60
  } finally {
60
61
  const responseTime = performance.now() - startTime;
61
- const consumer = getConsumer(ctx);
62
- client.consumerRegistry.addOrUpdateConsumer(consumer);
63
62
  if (!path) {
64
63
  path = getPath(ctx);
65
64
  }
65
+ spanHandle.setName(`${ctx.request.method} ${path}`);
66
+ const spans = spanHandle.end();
67
+ const traceId = spanHandle.traceId;
68
+ const consumer = getConsumer(ctx);
69
+ client.consumerRegistry.addOrUpdateConsumer(consumer);
66
70
  if (path) {
67
71
  try {
68
72
  client.requestCounter.addRequest({
@@ -98,7 +102,7 @@ function getMiddleware(client) {
98
102
  headers: convertHeaders(ctx.response.headers),
99
103
  size: ctx.response.length,
100
104
  body: convertBody(ctx.response.body, ctx.response.get("content-type"))
101
- }, serverError, logs);
105
+ }, serverError, logs, spans, traceId);
102
106
  }
103
107
  }
104
108
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/koa/middleware.ts"],"sourcesContent":["import Koa from \"koa\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport { ApitallyClient } from \"../common/client.js\";\nimport { consumerFromStringOrObject } from \"../common/consumerRegistry.js\";\nimport { getPackageVersion } from \"../common/packageVersions.js\";\nimport type { LogRecord } from \"../common/requestLogger.js\";\nimport { convertBody, convertHeaders } from \"../common/requestLogger.js\";\nimport {\n ApitallyConfig,\n ApitallyConsumer,\n PathInfo,\n StartupData,\n} from \"../common/types.js\";\nimport { patchConsole, patchWinston } from \"../loggers/index.js\";\n\nexport function useApitally(app: Koa, config: ApitallyConfig) {\n const client = new ApitallyClient(config);\n const middleware = getMiddleware(client);\n app.use(middleware);\n\n const setStartupData = (attempt: number = 1) => {\n const appInfo = getAppInfo(app, config.appVersion);\n if (appInfo.paths.length > 0 || attempt >= 10) {\n client.setStartupData(appInfo);\n client.startSync();\n } else {\n setTimeout(() => setStartupData(attempt + 1), 500);\n }\n };\n setTimeout(() => setStartupData(), 500);\n}\n\nfunction getMiddleware(client: ApitallyClient) {\n const logsContext = new AsyncLocalStorage<LogRecord[]>();\n\n if (client.requestLogger.config.captureLogs) {\n patchConsole(logsContext);\n patchWinston(logsContext);\n }\n\n return async (ctx: Koa.Context, next: Koa.Next) => {\n if (!client.isEnabled() || ctx.request.method.toUpperCase() === \"OPTIONS\") {\n await next();\n return;\n }\n\n await logsContext.run([], async () => {\n let path: string | undefined;\n let statusCode: number | undefined;\n let serverError: Error | undefined;\n const startTime = performance.now();\n try {\n await next();\n } catch (error: any) {\n path = getPath(ctx);\n statusCode = error.statusCode || error.status || 500;\n if (path && statusCode === 500 && error instanceof Error) {\n serverError = error;\n client.serverErrorCounter.addServerError({\n consumer: getConsumer(ctx)?.identifier,\n method: ctx.request.method,\n path,\n type: error.name,\n msg: error.message,\n traceback: error.stack || \"\",\n });\n }\n throw error;\n } finally {\n const responseTime = performance.now() - startTime;\n const consumer = getConsumer(ctx);\n client.consumerRegistry.addOrUpdateConsumer(consumer);\n if (!path) {\n path = getPath(ctx);\n }\n if (path) {\n try {\n client.requestCounter.addRequest({\n consumer: consumer?.identifier,\n method: ctx.request.method,\n path,\n statusCode: statusCode || ctx.response.status,\n responseTime,\n requestSize: ctx.request.length,\n responseSize: ctx.response.length,\n });\n } catch (error) {\n client.logger.error(\n \"Error while logging request in Apitally middleware.\",\n { context: ctx, error },\n );\n }\n }\n if (client.requestLogger.enabled) {\n const logs = logsContext.getStore();\n client.requestLogger.logRequest(\n {\n timestamp: Date.now() / 1000,\n method: ctx.request.method,\n path,\n url: ctx.request.href,\n headers: convertHeaders(ctx.request.headers),\n size: ctx.request.length,\n consumer: consumer?.identifier,\n body: convertBody(\n ctx.request.body,\n ctx.request.get(\"content-type\"),\n ),\n },\n {\n statusCode: statusCode || ctx.response.status,\n responseTime: responseTime / 1000,\n headers: convertHeaders(ctx.response.headers),\n size: ctx.response.length,\n body: convertBody(\n ctx.response.body,\n ctx.response.get(\"content-type\"),\n ),\n },\n serverError,\n logs,\n );\n }\n }\n });\n };\n}\n\nfunction getPath(ctx: Koa.Context) {\n return ctx._matchedRoute || ctx.routePath; // _matchedRoute is set by koa-router, routePath is set by koa-route\n}\n\nexport function setConsumer(\n ctx: Koa.Context,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n ctx.state.apitallyConsumer = consumer || undefined;\n}\n\nfunction getConsumer(ctx: Koa.Context) {\n if (ctx.state.apitallyConsumer) {\n return consumerFromStringOrObject(ctx.state.apitallyConsumer);\n } else if (ctx.state.consumerIdentifier) {\n // For backwards compatibility\n process.emitWarning(\n \"The consumerIdentifier property on the ctx.state object is deprecated. Use apitallyConsumer instead.\",\n \"DeprecationWarning\",\n );\n return consumerFromStringOrObject(ctx.state.consumerIdentifier);\n }\n return null;\n}\n\nfunction getAppInfo(app: Koa, appVersion?: string): StartupData {\n const versions: Array<[string, string]> = [\n [\"nodejs\", process.version.replace(/^v/, \"\")],\n ];\n const koaVersion = getPackageVersion(\"koa\");\n const apitallyVersion = getPackageVersion(\"../..\");\n if (koaVersion) {\n versions.push([\"koa\", koaVersion]);\n }\n if (apitallyVersion) {\n versions.push([\"apitally\", apitallyVersion]);\n }\n if (appVersion) {\n versions.push([\"app\", appVersion]);\n }\n return {\n paths: listEndpoints(app),\n versions: Object.fromEntries(versions),\n client: \"js:koa\",\n };\n}\n\nfunction isKoaRouterMiddleware(middleware: any) {\n return (\n typeof middleware === \"function\" &&\n middleware.router &&\n Array.isArray(middleware.router.stack)\n );\n}\n\nfunction listEndpoints(app: Koa) {\n const endpoints: Array<PathInfo> = [];\n app.middleware.forEach((middleware: any) => {\n if (isKoaRouterMiddleware(middleware)) {\n middleware.router.stack.forEach((layer: any) => {\n if (layer.methods && layer.methods.length > 0) {\n layer.methods.forEach((method: string) => {\n if (![\"HEAD\", \"OPTIONS\"].includes(method.toUpperCase())) {\n endpoints.push({\n method: method.toUpperCase(),\n path: layer.path,\n });\n }\n });\n }\n });\n }\n });\n return endpoints;\n}\n"],"mappings":";;AACA,SAASA,yBAAyB;AAElC,SAASC,sBAAsB;AAC/B,SAASC,kCAAkC;AAC3C,SAASC,yBAAyB;AAElC,SAASC,aAAaC,sBAAsB;AAO5C,SAASC,cAAcC,oBAAoB;AAEpC,SAASC,YAAYC,KAAUC,QAAsB;AAC1D,QAAMC,SAAS,IAAIV,eAAeS,MAAAA;AAClC,QAAME,aAAaC,cAAcF,MAAAA;AACjCF,MAAIK,IAAIF,UAAAA;AAER,QAAMG,iBAAiB,wBAACC,UAAkB,MAAC;AACzC,UAAMC,UAAUC,WAAWT,KAAKC,OAAOS,UAAU;AACjD,QAAIF,Q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;cAAWwE,SAASxE,OAAOC,YAAW,CAAA,GAAK;AACvDmE,wBAAUR,KAAK;gBACb5D,QAAQA,OAAOC,YAAW;gBAC1BE,MAAMmE,MAAMnE;cACd,CAAA;YACF;UACF,CAAA;QACF;MACF,CAAA;IACF;EACF,CAAA;AACA,SAAOiE;AACT;AAnBSP;","names":["AsyncLocalStorage","ApitallyClient","consumerFromStringOrObject","getPackageVersion","convertBody","convertHeaders","patchConsole","patchWinston","useApitally","app","config","client","middleware","getMiddleware","use","setStartupData","attempt","appInfo","getAppInfo","appVersion","paths","length","startSync","setTimeout","logsContext","requestLogger","captureLogs","ctx","next","isEnabled","request","method","toUpperCase","run","path","statusCode","serverError","startTime","performance","now","error","getPath","status","Error","serverErrorCounter","addServerError","consumer","getConsumer","identifier","type","name","msg","message","traceback","stack","responseTime","consumerRegistry","addOrUpdateConsumer","requestCounter","addRequest","response","requestSize","responseSize","logger","context","enabled","logs","getStore","logRequest","timestamp","Date","url","href","headers","size","body","get","_matchedRoute","routePath","setConsumer","state","apitallyConsumer","undefined","consumerIdentifier","process","emitWarning","versions","version","replace","koaVersion","apitallyVersion","push","listEndpoints","Object","fromEntries","isKoaRouterMiddleware","router","Array","isArray","endpoints","forEach","layer","methods","includes"]}
1
+ {"version":3,"sources":["../../src/koa/middleware.ts"],"sourcesContent":["import Koa from \"koa\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport { ApitallyClient } from \"../common/client.js\";\nimport { consumerFromStringOrObject } from \"../common/consumerRegistry.js\";\nimport { getPackageVersion } from \"../common/packageVersions.js\";\nimport type { LogRecord } from \"../common/requestLogger.js\";\nimport { convertBody, convertHeaders } from \"../common/requestLogger.js\";\nimport {\n ApitallyConfig,\n ApitallyConsumer,\n PathInfo,\n StartupData,\n} from \"../common/types.js\";\nimport { patchConsole, patchWinston } from \"../loggers/index.js\";\n\nexport function useApitally(app: Koa, config: ApitallyConfig) {\n const client = new ApitallyClient(config);\n const middleware = getMiddleware(client);\n app.use(middleware);\n\n const setStartupData = (attempt: number = 1) => {\n const appInfo = getAppInfo(app, config.appVersion);\n if (appInfo.paths.length > 0 || attempt >= 10) {\n client.setStartupData(appInfo);\n client.startSync();\n } else {\n setTimeout(() => setStartupData(attempt + 1), 500);\n }\n };\n setTimeout(() => setStartupData(), 500);\n}\n\nfunction getMiddleware(client: ApitallyClient) {\n const logsContext = new AsyncLocalStorage<LogRecord[]>();\n\n if (client.requestLogger.config.captureLogs) {\n patchConsole(logsContext);\n patchWinston(logsContext);\n }\n\n return async (ctx: Koa.Context, next: Koa.Next) => {\n if (!client.isEnabled() || ctx.request.method.toUpperCase() === \"OPTIONS\") {\n await next();\n return;\n }\n\n await logsContext.run([], async () => {\n let path: string | undefined;\n let statusCode: number | undefined;\n let serverError: Error | undefined;\n const startTime = performance.now();\n const spanHandle = client.spanCollector.startSpan();\n try {\n await spanHandle.runInContext(next);\n } catch (error: any) {\n path = getPath(ctx);\n statusCode = error.statusCode || error.status || 500;\n if (path && statusCode === 500 && error instanceof Error) {\n serverError = error;\n client.serverErrorCounter.addServerError({\n consumer: getConsumer(ctx)?.identifier,\n method: ctx.request.method,\n path,\n type: error.name,\n msg: error.message,\n traceback: error.stack || \"\",\n });\n }\n throw error;\n } finally {\n const responseTime = performance.now() - startTime;\n\n if (!path) {\n path = getPath(ctx);\n }\n\n spanHandle.setName(`${ctx.request.method} ${path}`);\n const spans = spanHandle.end();\n const traceId = spanHandle.traceId;\n\n const consumer = getConsumer(ctx);\n client.consumerRegistry.addOrUpdateConsumer(consumer);\n\n if (path) {\n try {\n client.requestCounter.addRequest({\n consumer: consumer?.identifier,\n method: ctx.request.method,\n path,\n statusCode: statusCode || ctx.response.status,\n responseTime,\n requestSize: ctx.request.length,\n responseSize: ctx.response.length,\n });\n } catch (error) {\n client.logger.error(\n \"Error while logging request in Apitally middleware.\",\n { context: ctx, error },\n );\n }\n }\n\n if (client.requestLogger.enabled) {\n const logs = logsContext.getStore();\n client.requestLogger.logRequest(\n {\n timestamp: Date.now() / 1000,\n method: ctx.request.method,\n path,\n url: ctx.request.href,\n headers: convertHeaders(ctx.request.headers),\n size: ctx.request.length,\n consumer: consumer?.identifier,\n body: convertBody(\n ctx.request.body,\n ctx.request.get(\"content-type\"),\n ),\n },\n {\n statusCode: statusCode || ctx.response.status,\n responseTime: responseTime / 1000,\n headers: convertHeaders(ctx.response.headers),\n size: ctx.response.length,\n body: convertBody(\n ctx.response.body,\n ctx.response.get(\"content-type\"),\n ),\n },\n serverError,\n logs,\n spans,\n traceId,\n );\n }\n }\n });\n };\n}\n\nfunction getPath(ctx: Koa.Context) {\n return ctx._matchedRoute || ctx.routePath; // _matchedRoute is set by koa-router, routePath is set by koa-route\n}\n\nexport function setConsumer(\n ctx: Koa.Context,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n ctx.state.apitallyConsumer = consumer || undefined;\n}\n\nfunction getConsumer(ctx: Koa.Context) {\n if (ctx.state.apitallyConsumer) {\n return consumerFromStringOrObject(ctx.state.apitallyConsumer);\n } else if (ctx.state.consumerIdentifier) {\n // For backwards compatibility\n process.emitWarning(\n \"The consumerIdentifier property on the ctx.state object is deprecated. Use apitallyConsumer instead.\",\n \"DeprecationWarning\",\n );\n return consumerFromStringOrObject(ctx.state.consumerIdentifier);\n }\n return null;\n}\n\nfunction getAppInfo(app: Koa, appVersion?: string): StartupData {\n const versions: Array<[string, string]> = [\n [\"nodejs\", process.version.replace(/^v/, \"\")],\n ];\n const koaVersion = getPackageVersion(\"koa\");\n const apitallyVersion = getPackageVersion(\"../..\");\n if (koaVersion) {\n versions.push([\"koa\", koaVersion]);\n }\n if (apitallyVersion) {\n versions.push([\"apitally\", apitallyVersion]);\n }\n if (appVersion) {\n versions.push([\"app\", appVersion]);\n }\n return {\n paths: listEndpoints(app),\n versions: Object.fromEntries(versions),\n client: \"js:koa\",\n };\n}\n\nfunction isKoaRouterMiddleware(middleware: any) {\n return (\n typeof middleware === \"function\" &&\n middleware.router &&\n Array.isArray(middleware.router.stack)\n );\n}\n\nfunction listEndpoints(app: Koa) {\n const endpoints: Array<PathInfo> = [];\n app.middleware.forEach((middleware: any) => {\n if (isKoaRouterMiddleware(middleware)) {\n middleware.router.stack.forEach((layer: any) => {\n if (layer.methods && layer.methods.length > 0) {\n layer.methods.forEach((method: string) => {\n if (![\"HEAD\", \"OPTIONS\"].includes(method.toUpperCase())) {\n endpoints.push({\n method: method.toUpperCase(),\n path: layer.path,\n });\n }\n });\n }\n });\n }\n });\n return endpoints;\n}\n"],"mappings":";;AACA,SAASA,yBAAyB;AAElC,SAASC,sBAAsB;AAC/B,SAASC,kCAAkC;AAC3C,SAASC,yBAAyB;AAElC,SAASC,aAAaC,sBAAsB;AAO5C,SAASC,cAAcC,oBAAoB;AAEpC,SAASC,YAAYC,KAAUC,QAAsB;AAC1D,QAAMC,SAAS,IAAIV,eAAeS,MAAAA;AAClC,QAAME,aAAaC,cAAcF,MAAAA;AACjCF,MAAIK,IAAIF,UAAAA;AAER,QAAMG,iBAAiB,wBAACC,UAAkB,MAAC;AACzC,UAAMC,UAAUC,WAAWT,KAAKC,OAAOS,UAAU;AACjD,QAAIF,QAAQG,MAAMC,SAAS,KAAKL,WAAW,IAAI;AAC7CL,aAAOI,eAAeE,OAAAA;AACtBN,aAAOW,UAAS;IAClB,OAAO;AACLC,iBAAW,MAAMR,eAAeC,UAAU,CAAA,GAAI,GAAA;IAChD;EACF,GARuB;AASvBO,aAAW,MAAMR,eAAAA,GAAkB,GAAA;AACrC;AAfgBP;AAiBhB,SAASK,cAAcF,QAAsB;AAC3C,QAAMa,cAAc,IAAIxB,kBAAAA;AAExB,MAAIW,OAAOc,cAAcf,OAAOgB,aAAa;AAC3CpB,iBAAakB,WAAAA;AACbjB,iBAAaiB,WAAAA;EACf;AAEA,SAAO,OAAOG,KAAkBC,SAAAA;AAC9B,QAAI,CAACjB,OAAOkB,UAAS,KAAMF,IAAIG,QAAQC,OAAOC,YAAW,MAAO,WAAW;AACzE,YAAMJ,KAAAA;AACN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U,YAAmB;AAC/C,QAAM2E,WAAoC;IACxC;MAAC;MAAUF,QAAQG,QAAQC,QAAQ,MAAM,EAAA;;;AAE3C,QAAMC,aAAa9F,kBAAkB,KAAA;AACrC,QAAM+F,kBAAkB/F,kBAAkB,OAAA;AAC1C,MAAI8F,YAAY;AACdH,aAASK,KAAK;MAAC;MAAOF;KAAW;EACnC;AACA,MAAIC,iBAAiB;AACnBJ,aAASK,KAAK;MAAC;MAAYD;KAAgB;EAC7C;AACA,MAAI/E,YAAY;AACd2E,aAASK,KAAK;MAAC;MAAOhF;KAAW;EACnC;AACA,SAAO;IACLC,OAAOgF,cAAc3F,GAAAA;IACrBqF,UAAUO,OAAOC,YAAYR,QAAAA;IAC7BnF,QAAQ;EACV;AACF;AApBSO;AAsBT,SAASqF,sBAAsB3F,YAAe;AAC5C,SACE,OAAOA,eAAe,cACtBA,WAAW4F,UACXC,MAAMC,QAAQ9F,WAAW4F,OAAO9C,KAAK;AAEzC;AANS6C;AAQT,SAASH,cAAc3F,KAAQ;AAC7B,QAAMkG,YAA6B,CAAA;AACnClG,MAAIG,WAAWgG,QAAQ,CAAChG,eAAAA;AACtB,QAAI2F,sBAAsB3F,UAAAA,GAAa;AACrCA,iBAAW4F,OAAO9C,MAAMkD,QAAQ,CAACC,UAAAA;AAC/B,YAAIA,MAAMC,WAAWD,MAAMC,QAAQzF,SAAS,GAAG;AAC7CwF,gBAAMC,QAAQF,QAAQ,CAAC7E,WAAAA;AACrB,gBAAI,CAAC;cAAC;cAAQ;cAAWgF,SAAShF,OAAOC,YAAW,CAAA,GAAK;AACvD2E,wBAAUR,KAAK;gBACbpE,QAAQA,OAAOC,YAAW;gBAC1BE,MAAM2E,MAAM3E;cACd,CAAA;YACF;UACF,CAAA;QACF;MACF,CAAA;IACF;EACF,CAAA;AACA,SAAOyE;AACT;AAnBSP;","names":["AsyncLocalStorage","ApitallyClient","consumerFromStringOrObject","getPackageVersion","convertBody","convertHeaders","patchConsole","patchWinston","useApitally","app","config","client","middleware","getMiddleware","use","setStartupData","attempt","appInfo","getAppInfo","appVersion","paths","length","startSync","setTimeout","logsContext","requestLogger","captureLogs","ctx","next","isEnabled","request","method","toUpperCase","run","path","statusCode","serverError","startTime","performance","now","spanHandle","spanCollector","startSpan","runInContext","error","getPath","status","Error","serverErrorCounter","addServerError","consumer","getConsumer","identifier","type","name","msg","message","traceback","stack","responseTime","setName","spans","end","traceId","consumerRegistry","addOrUpdateConsumer","requestCounter","addRequest","response","requestSize","responseSize","logger","context","enabled","logs","getStore","logRequest","timestamp","Date","url","href","headers","size","body","get","_matchedRoute","routePath","setConsumer","state","apitallyConsumer","undefined","consumerIdentifier","process","emitWarning","versions","version","replace","koaVersion","apitallyVersion","push","listEndpoints","Object","fromEntries","isKoaRouterMiddleware","router","Array","isArray","endpoints","forEach","layer","methods","includes"]}
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.cjs';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.cjs';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.cjs';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.cjs';
6
10
 
7
11
  declare function patchConsole(logsContext: AsyncLocalStorage<LogRecord[]>): void;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.js';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.js';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.js';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.js';
6
10
 
7
11
  declare function patchConsole(logsContext: AsyncLocalStorage<LogRecord[]>): void;
@@ -3,6 +3,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  import { LogRecord } from '../common/requestLogger.cjs';
4
4
  import 'node:buffer';
5
5
  import 'node:http';
6
+ import '../common/spanCollector.cjs';
7
+ import '@opentelemetry/sdk-trace-base';
8
+ import '../common/logging.cjs';
9
+ import 'winston';
6
10
  import '../common/tempGzipFile.cjs';
7
11
 
8
12
  declare function handleHapiRequestEvent(event: RequestEvent, logsContext: AsyncLocalStorage<LogRecord[]>): void;
@@ -3,6 +3,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  import { LogRecord } from '../common/requestLogger.js';
4
4
  import 'node:buffer';
5
5
  import 'node:http';
6
+ import '../common/spanCollector.js';
7
+ import '@opentelemetry/sdk-trace-base';
8
+ import '../common/logging.js';
9
+ import 'winston';
6
10
  import '../common/tempGzipFile.js';
7
11
 
8
12
  declare function handleHapiRequestEvent(event: RequestEvent, logsContext: AsyncLocalStorage<LogRecord[]>): void;
@@ -25,11 +25,11 @@ __export(loggers_exports, {
25
25
  patchWinston: () => import_winston.patchWinston
26
26
  });
27
27
  module.exports = __toCommonJS(loggers_exports);
28
- var import_console = require("./console.js");
29
- var import_hapi = require("./hapi.js");
30
- var import_nestjs = require("./nestjs.js");
31
- var import_pino = require("./pino.js");
32
- var import_winston = require("./winston.js");
28
+ var import_console = require('./console.cjs');
29
+ var import_hapi = require('./hapi.cjs');
30
+ var import_nestjs = require('./nestjs.cjs');
31
+ var import_pino = require('./pino.cjs');
32
+ var import_winston = require('./winston.cjs');
33
33
  // Annotate the CommonJS export names for ESM import in node:
34
34
  0 && (module.exports = {
35
35
  handleHapiRequestEvent,
@@ -7,5 +7,9 @@ import 'node:async_hooks';
7
7
  import '../common/requestLogger.cjs';
8
8
  import 'node:buffer';
9
9
  import 'node:http';
10
+ import '../common/spanCollector.cjs';
11
+ import '@opentelemetry/sdk-trace-base';
12
+ import '../common/logging.cjs';
13
+ import 'winston';
10
14
  import '../common/tempGzipFile.cjs';
11
15
  import '@hapi/hapi';
@@ -7,5 +7,9 @@ import 'node:async_hooks';
7
7
  import '../common/requestLogger.js';
8
8
  import 'node:buffer';
9
9
  import 'node:http';
10
+ import '../common/spanCollector.js';
11
+ import '@opentelemetry/sdk-trace-base';
12
+ import '../common/logging.js';
13
+ import 'winston';
10
14
  import '../common/tempGzipFile.js';
11
15
  import '@hapi/hapi';
@@ -32,7 +32,7 @@ __export(nestjs_exports, {
32
32
  patchNestLogger: () => patchNestLogger
33
33
  });
34
34
  module.exports = __toCommonJS(nestjs_exports);
35
- var import_utils = require("./utils.js");
35
+ var import_utils = require('./utils.cjs');
36
36
  const MAX_BUFFER_SIZE = 1e3;
37
37
  let isPatched = false;
38
38
  let globalLogsContext;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.cjs';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.cjs';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.cjs';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.cjs';
6
10
 
7
11
  declare function patchNestLogger(logsContext: AsyncLocalStorage<LogRecord[]>): Promise<void>;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.js';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.js';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.js';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.js';
6
10
 
7
11
  declare function patchNestLogger(logsContext: AsyncLocalStorage<LogRecord[]>): Promise<void>;
@@ -32,7 +32,7 @@ __export(pino_exports, {
32
32
  patchPinoLogger: () => patchPinoLogger
33
33
  });
34
34
  module.exports = __toCommonJS(pino_exports);
35
- var import_utils = require("./utils.js");
35
+ var import_utils = require('./utils.cjs');
36
36
  var _a;
37
37
  const MAX_BUFFER_SIZE = 1e3;
38
38
  const originalStreamSym = /* @__PURE__ */ Symbol.for("apitally.originalStream");
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.cjs';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.cjs';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.cjs';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.cjs';
6
10
 
7
11
  declare function patchPinoLogger(logger: any, logsContext: AsyncLocalStorage<LogRecord[]>): Promise<boolean>;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.js';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.js';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.js';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.js';
6
10
 
7
11
  declare function patchPinoLogger(logger: any, logsContext: AsyncLocalStorage<LogRecord[]>): Promise<boolean>;
@@ -32,7 +32,7 @@ __export(winston_exports, {
32
32
  patchWinston: () => patchWinston
33
33
  });
34
34
  module.exports = __toCommonJS(winston_exports);
35
- var import_utils = require("./utils.js");
35
+ var import_utils = require('./utils.cjs');
36
36
  const MAX_BUFFER_SIZE = 1e3;
37
37
  let isPatched = false;
38
38
  let globalLogsContext;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.cjs';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.cjs';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.cjs';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.cjs';
6
10
 
7
11
  declare function patchWinston(logsContext: AsyncLocalStorage<LogRecord[]>): Promise<void>;
@@ -2,6 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { LogRecord } from '../common/requestLogger.js';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
+ import '../common/spanCollector.js';
6
+ import '@opentelemetry/sdk-trace-base';
7
+ import '../common/logging.js';
8
+ import 'winston';
5
9
  import '../common/tempGzipFile.js';
6
10
 
7
11
  declare function patchWinston(logsContext: AsyncLocalStorage<LogRecord[]>): Promise<void>;
@@ -29,12 +29,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
29
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
30
  var nestjs_exports = {};
31
31
  __export(nestjs_exports, {
32
+ ApitallySpanProcessor: () => import_spanCollector.ApitallySpanProcessor,
32
33
  setConsumer: () => setConsumer,
33
34
  useApitally: () => useApitally
34
35
  });
35
36
  module.exports = __toCommonJS(nestjs_exports);
36
37
  var import_common = require("@nestjs/common");
37
38
  var import_rxjs = require("rxjs");
39
+ var import_spanCollector = require('../common/spanCollector.cjs');
38
40
  var _a;
39
41
  function _ts_decorate(decorators, target, key, desc) {
40
42
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -99,6 +101,7 @@ ApitallyInterceptor = _ts_decorate([
99
101
  ], ApitallyInterceptor);
100
102
  // Annotate the CommonJS export names for ESM import in node:
101
103
  0 && (module.exports = {
104
+ ApitallySpanProcessor,
102
105
  setConsumer,
103
106
  useApitally
104
107
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import {\n CallHandler,\n ExecutionContext,\n INestApplication,\n Injectable,\n NestInterceptor,\n} from \"@nestjs/common\";\nimport { catchError, throwError } from \"rxjs\";\n\nimport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\nexport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\n\nlet setConsumerFn:\n | ((\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n ) => void)\n | null = null;\n\nexport async function useApitally(\n app: INestApplication,\n config: ApitallyConfig,\n) {\n const httpAdapter = app.getHttpAdapter();\n const instance = httpAdapter.getInstance();\n const platform = httpAdapter.constructor.name\n .toLowerCase()\n .includes(\"fastify\")\n ? \"fastify\"\n : \"express\";\n\n if (platform === \"express\") {\n const { useApitally, setConsumer } = await import(\"../express/index.js\");\n setConsumerFn = setConsumer;\n useApitally(instance, config);\n } else if (platform === \"fastify\") {\n const { apitallyPlugin, setConsumer } = await import(\"../fastify/index.js\");\n setConsumerFn = setConsumer;\n await instance.register(apitallyPlugin, config);\n }\n\n app.useGlobalInterceptors(new ApitallyInterceptor(platform));\n}\n\nexport function setConsumer(\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n if (setConsumerFn) {\n setConsumerFn(request, consumer);\n }\n}\n\n@Injectable()\nclass ApitallyInterceptor implements NestInterceptor {\n constructor(private readonly platform: \"express\" | \"fastify\") {}\n\n intercept(context: ExecutionContext, next: CallHandler) {\n return next.handle().pipe(\n catchError((exception) => {\n if (context.getType() === \"http\") {\n const ctx = context.switchToHttp();\n const res = ctx.getResponse();\n\n if (this.platform === \"express\" && res.locals) {\n res.locals.serverError = exception;\n } else if (\n this.platform === \"fastify\" &&\n (!exception.statusCode || exception.statusCode === 500)\n ) {\n res.serverError = exception;\n }\n }\n return throwError(() => exception);\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oBAMO;AACP,kBAAuC;;;;;;;;;;;;;AAKvC,IAAIA,gBAKO;AAEX,eAAsBC,YACpBC,KACAC,QAAsB;AAEtB,QAAMC,cAAcF,IAAIG,eAAc;AACtC,QAAMC,WAAWF,YAAYG,YAAW;AACxC,QAAMC,WAAWJ,YAAY,YAAYK,KACtCC,YAAW,EACXC,SAAS,SAAA,IACR,YACA;AAEJ,MAAIH,aAAa,WAAW;AAC1B,UAAM,EAAEP,aAAAA,cAAaW,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AAClDZ,oBAAgBY;AAChBX,IAAAA,aAAYK,UAAUH,MAAAA;EACxB,WAAWK,aAAa,WAAW;AACjC,UAAM,EAAEK,gBAAgBD,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AACrDZ,oBAAgBY;AAChB,UAAMN,SAASQ,SAASD,gBAAgBV,MAAAA;EAC1C;AAEAD,MAAIa,sBAAsB,IAAIC,oBAAoBR,QAAAA,CAAAA;AACpD;AAvBsBP;AAyBf,SAASW,YACdK,SACAC,UAAsD;AAEtD,MAAIlB,eAAe;AACjBA,kBAAciB,SAASC,QAAAA;EACzB;AACF;AAPgBN;AAShB,IACMI,uBADN,WACMA;;EACJ,YAA6BR,UAAiC;SAAjCA,WAAAA;EAAkC;EAE/DW,UAAUC,SAA2BC,MAAmB;AACtD,WAAOA,KAAKC,OAAM,EAAGC,SACnBC,wBAAW,CAACC,cAAAA;AACV,UAAIL,QAAQM,QAAO,MAAO,QAAQ;AAChC,cAAMC,MAAMP,QAAQQ,aAAY;AAChC,cAAMC,MAAMF,IAAIG,YAAW;AAE3B,YAAI,KAAKtB,aAAa,aAAaqB,IAAIE,QAAQ;AAC7CF,cAAIE,OAAOC,cAAcP;QAC3B,WACE,KAAKjB,aAAa,cACjB,CAACiB,UAAUQ,cAAcR,UAAUQ,eAAe,MACnD;AACAJ,cAAIG,cAAcP;QACpB;MACF;AACA,iBAAOS,wBAAW,MAAMT,SAAAA;IAC1B,CAAA,CAAA;EAEJ;AACF,GAvBMT,mCADN;;;;;;;;","names":["setConsumerFn","useApitally","app","config","httpAdapter","getHttpAdapter","instance","getInstance","platform","name","toLowerCase","includes","setConsumer","apitallyPlugin","register","useGlobalInterceptors","ApitallyInterceptor","request","consumer","intercept","context","next","handle","pipe","catchError","exception","getType","ctx","switchToHttp","res","getResponse","locals","serverError","statusCode","throwError"]}
1
+ {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import {\n CallHandler,\n ExecutionContext,\n INestApplication,\n Injectable,\n NestInterceptor,\n} from \"@nestjs/common\";\nimport { catchError, throwError } from \"rxjs\";\n\nimport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\nexport { ApitallySpanProcessor } from \"../common/spanCollector.js\";\nexport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\n\nlet setConsumerFn:\n | ((\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n ) => void)\n | null = null;\n\nexport async function useApitally(\n app: INestApplication,\n config: ApitallyConfig,\n) {\n const httpAdapter = app.getHttpAdapter();\n const instance = httpAdapter.getInstance();\n const platform = httpAdapter.constructor.name\n .toLowerCase()\n .includes(\"fastify\")\n ? \"fastify\"\n : \"express\";\n\n if (platform === \"express\") {\n const { useApitally, setConsumer } = await import(\"../express/index.js\");\n setConsumerFn = setConsumer;\n useApitally(instance, config);\n } else if (platform === \"fastify\") {\n const { apitallyPlugin, setConsumer } = await import(\"../fastify/index.js\");\n setConsumerFn = setConsumer;\n await instance.register(apitallyPlugin, config);\n }\n\n app.useGlobalInterceptors(new ApitallyInterceptor(platform));\n}\n\nexport function setConsumer(\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n if (setConsumerFn) {\n setConsumerFn(request, consumer);\n }\n}\n\n@Injectable()\nclass ApitallyInterceptor implements NestInterceptor {\n constructor(private readonly platform: \"express\" | \"fastify\") {}\n\n intercept(context: ExecutionContext, next: CallHandler) {\n return next.handle().pipe(\n catchError((exception) => {\n if (context.getType() === \"http\") {\n const ctx = context.switchToHttp();\n const res = ctx.getResponse();\n\n if (this.platform === \"express\" && res.locals) {\n res.locals.serverError = exception;\n } else if (\n this.platform === \"fastify\" &&\n (!exception.statusCode || exception.statusCode === 500)\n ) {\n res.serverError = exception;\n }\n }\n return throwError(() => exception);\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oBAMO;AACP,kBAAuC;AAGvC,2BAAsC;;;;;;;;;;;;;AAGtC,IAAIA,gBAKO;AAEX,eAAsBC,YACpBC,KACAC,QAAsB;AAEtB,QAAMC,cAAcF,IAAIG,eAAc;AACtC,QAAMC,WAAWF,YAAYG,YAAW;AACxC,QAAMC,WAAWJ,YAAY,YAAYK,KACtCC,YAAW,EACXC,SAAS,SAAA,IACR,YACA;AAEJ,MAAIH,aAAa,WAAW;AAC1B,UAAM,EAAEP,aAAAA,cAAaW,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AAClDZ,oBAAgBY;AAChBX,IAAAA,aAAYK,UAAUH,MAAAA;EACxB,WAAWK,aAAa,WAAW;AACjC,UAAM,EAAEK,gBAAgBD,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AACrDZ,oBAAgBY;AAChB,UAAMN,SAASQ,SAASD,gBAAgBV,MAAAA;EAC1C;AAEAD,MAAIa,sBAAsB,IAAIC,oBAAoBR,QAAAA,CAAAA;AACpD;AAvBsBP;AAyBf,SAASW,YACdK,SACAC,UAAsD;AAEtD,MAAIlB,eAAe;AACjBA,kBAAciB,SAASC,QAAAA;EACzB;AACF;AAPgBN;AAShB,IACMI,uBADN,WACMA;;EACJ,YAA6BR,UAAiC;SAAjCA,WAAAA;EAAkC;EAE/DW,UAAUC,SAA2BC,MAAmB;AACtD,WAAOA,KAAKC,OAAM,EAAGC,SACnBC,wBAAW,CAACC,cAAAA;AACV,UAAIL,QAAQM,QAAO,MAAO,QAAQ;AAChC,cAAMC,MAAMP,QAAQQ,aAAY;AAChC,cAAMC,MAAMF,IAAIG,YAAW;AAE3B,YAAI,KAAKtB,aAAa,aAAaqB,IAAIE,QAAQ;AAC7CF,cAAIE,OAAOC,cAAcP;QAC3B,WACE,KAAKjB,aAAa,cACjB,CAACiB,UAAUQ,cAAcR,UAAUQ,eAAe,MACnD;AACAJ,cAAIG,cAAcP;QACpB;MACF;AACA,iBAAOS,wBAAW,MAAMT,SAAAA;IAC1B,CAAA,CAAA;EAEJ;AACF,GAvBMT,mCADN;;;;;;;;","names":["setConsumerFn","useApitally","app","config","httpAdapter","getHttpAdapter","instance","getInstance","platform","name","toLowerCase","includes","setConsumer","apitallyPlugin","register","useGlobalInterceptors","ApitallyInterceptor","request","consumer","intercept","context","next","handle","pipe","catchError","exception","getType","ctx","switchToHttp","res","getResponse","locals","serverError","statusCode","throwError"]}
@@ -1,11 +1,13 @@
1
1
  import { INestApplication } from '@nestjs/common';
2
2
  import { ApitallyConfig, ApitallyConsumer } from '../common/types.cjs';
3
+ export { ApitallySpanProcessor } from '../common/spanCollector.cjs';
3
4
  import '../common/logging.cjs';
4
5
  import 'winston';
5
6
  import '../common/requestLogger.cjs';
6
7
  import 'node:buffer';
7
8
  import 'node:http';
8
9
  import '../common/tempGzipFile.cjs';
10
+ import '@opentelemetry/sdk-trace-base';
9
11
 
10
12
  declare function useApitally(app: INestApplication, config: ApitallyConfig): Promise<void>;
11
13
  declare function setConsumer(request: any, consumer: ApitallyConsumer | string | null | undefined): void;
@@ -1,11 +1,13 @@
1
1
  import { INestApplication } from '@nestjs/common';
2
2
  import { ApitallyConfig, ApitallyConsumer } from '../common/types.js';
3
+ export { ApitallySpanProcessor } from '../common/spanCollector.js';
3
4
  import '../common/logging.js';
4
5
  import 'winston';
5
6
  import '../common/requestLogger.js';
6
7
  import 'node:buffer';
7
8
  import 'node:http';
8
9
  import '../common/tempGzipFile.js';
10
+ import '@opentelemetry/sdk-trace-base';
9
11
 
10
12
  declare function useApitally(app: INestApplication, config: ApitallyConfig): Promise<void>;
11
13
  declare function setConsumer(request: any, consumer: ApitallyConsumer | string | null | undefined): void;
@@ -14,6 +14,7 @@ function _ts_metadata(k, v) {
14
14
  __name(_ts_metadata, "_ts_metadata");
15
15
  import { Injectable } from "@nestjs/common";
16
16
  import { catchError, throwError } from "rxjs";
17
+ import { ApitallySpanProcessor } from "../common/spanCollector.js";
17
18
  let setConsumerFn = null;
18
19
  async function useApitally(app, config) {
19
20
  const httpAdapter = app.getHttpAdapter();
@@ -65,6 +66,7 @@ ApitallyInterceptor = _ts_decorate([
65
66
  ])
66
67
  ], ApitallyInterceptor);
67
68
  export {
69
+ ApitallySpanProcessor,
68
70
  setConsumer,
69
71
  useApitally
70
72
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import {\n CallHandler,\n ExecutionContext,\n INestApplication,\n Injectable,\n NestInterceptor,\n} from \"@nestjs/common\";\nimport { catchError, throwError } from \"rxjs\";\n\nimport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\nexport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\n\nlet setConsumerFn:\n | ((\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n ) => void)\n | null = null;\n\nexport async function useApitally(\n app: INestApplication,\n config: ApitallyConfig,\n) {\n const httpAdapter = app.getHttpAdapter();\n const instance = httpAdapter.getInstance();\n const platform = httpAdapter.constructor.name\n .toLowerCase()\n .includes(\"fastify\")\n ? \"fastify\"\n : \"express\";\n\n if (platform === \"express\") {\n const { useApitally, setConsumer } = await import(\"../express/index.js\");\n setConsumerFn = setConsumer;\n useApitally(instance, config);\n } else if (platform === \"fastify\") {\n const { apitallyPlugin, setConsumer } = await import(\"../fastify/index.js\");\n setConsumerFn = setConsumer;\n await instance.register(apitallyPlugin, config);\n }\n\n app.useGlobalInterceptors(new ApitallyInterceptor(platform));\n}\n\nexport function setConsumer(\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n if (setConsumerFn) {\n setConsumerFn(request, consumer);\n }\n}\n\n@Injectable()\nclass ApitallyInterceptor implements NestInterceptor {\n constructor(private readonly platform: \"express\" | \"fastify\") {}\n\n intercept(context: ExecutionContext, next: CallHandler) {\n return next.handle().pipe(\n catchError((exception) => {\n if (context.getType() === \"http\") {\n const ctx = context.switchToHttp();\n const res = ctx.getResponse();\n\n if (this.platform === \"express\" && res.locals) {\n res.locals.serverError = exception;\n } else if (\n this.platform === \"fastify\" &&\n (!exception.statusCode || exception.statusCode === 500)\n ) {\n res.serverError = exception;\n }\n }\n return throwError(() => exception);\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAIEA,kBAEK;AACP,SAASC,YAAYC,kBAAkB;AAKvC,IAAIC,gBAKO;AAEX,eA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umerFn","useApitally","app","config","httpAdapter","getHttpAdapter","instance","getInstance","platform","name","toLowerCase","includes","setConsumer","apitallyPlugin","register","useGlobalInterceptors","ApitallyInterceptor","request","consumer","intercept","context","next","handle","pipe","exception","getType","ctx","switchToHttp","res","getResponse","locals","serverError","statusCode"]}
1
+ {"version":3,"sources":["../../src/nestjs/index.ts"],"sourcesContent":["import {\n CallHandler,\n ExecutionContext,\n INestApplication,\n Injectable,\n NestInterceptor,\n} from \"@nestjs/common\";\nimport { catchError, throwError } from \"rxjs\";\n\nimport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\nexport { ApitallySpanProcessor } from \"../common/spanCollector.js\";\nexport type { ApitallyConfig, ApitallyConsumer } from \"../common/types.js\";\n\nlet setConsumerFn:\n | ((\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n ) => void)\n | null = null;\n\nexport async function useApitally(\n app: INestApplication,\n config: ApitallyConfig,\n) {\n const httpAdapter = app.getHttpAdapter();\n const instance = httpAdapter.getInstance();\n const platform = httpAdapter.constructor.name\n .toLowerCase()\n .includes(\"fastify\")\n ? \"fastify\"\n : \"express\";\n\n if (platform === \"express\") {\n const { useApitally, setConsumer } = await import(\"../express/index.js\");\n setConsumerFn = setConsumer;\n useApitally(instance, config);\n } else if (platform === \"fastify\") {\n const { apitallyPlugin, setConsumer } = await import(\"../fastify/index.js\");\n setConsumerFn = setConsumer;\n await instance.register(apitallyPlugin, config);\n }\n\n app.useGlobalInterceptors(new ApitallyInterceptor(platform));\n}\n\nexport function setConsumer(\n request: any,\n consumer: ApitallyConsumer | string | null | undefined,\n) {\n if (setConsumerFn) {\n setConsumerFn(request, consumer);\n }\n}\n\n@Injectable()\nclass ApitallyInterceptor implements NestInterceptor {\n constructor(private readonly platform: \"express\" | \"fastify\") {}\n\n intercept(context: ExecutionContext, next: CallHandler) {\n return next.handle().pipe(\n catchError((exception) => {\n if (context.getType() === \"http\") {\n const ctx = context.switchToHttp();\n const res = ctx.getResponse();\n\n if (this.platform === \"express\" && res.locals) {\n res.locals.serverError = exception;\n } else if (\n this.platform === \"fastify\" &&\n (!exception.statusCode || exception.statusCode === 500)\n ) {\n res.serverError = exception;\n }\n }\n return throwError(() => exception);\n }),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAIEA,kBAEK;AACP,SAASC,YAAYC,kBAAkB;AAGvC,SAASC,6BAA6B;AAGtC,IAAIC,gBAKO;AAEX,eAAsBC,YACpBC,KACAC,QAAsB;AAEtB,QAAMC,cAAcF,IAAIG,eAAc;AACtC,QAAMC,WAAWF,YAAYG,YAAW;AACxC,QAAMC,WAAWJ,YAAY,YAAYK,KACtCC,YAAW,EACXC,SAAS,SAAA,IACR,YACA;AAEJ,MAAIH,aAAa,WAAW;AAC1B,UAAM,EAAEP,aAAAA,cAAaW,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AAClDZ,oBAAgBY;AAChBX,IAAAA,aAAYK,UAAUH,MAAAA;EACxB,WAAWK,aAAa,WAAW;AACjC,UAAM,EAAEK,gBAAgBD,aAAAA,aAAW,IAAK,MAAM,OAAO,qBAAA;AACrDZ,oBAAgBY;AAChB,UAAMN,SAASQ,SAASD,gBAAgBV,MAAAA;EAC1C;AAEAD,MAAIa,sBAAsB,IAAIC,oBAAoBR,QAAAA,CAAAA;AACpD;AAvBsBP;AAyBf,SAASW,YACdK,SACAC,UAAsD;AAEtD,MAAIlB,eAAe;AACjBA,kBAAciB,SAASC,QAAAA;EACzB;AACF;AAPgBN;AAShB,IACMI,uBADN,WACMA;;EACJ,YAA6BR,UAAiC;SAAjCA,WAAAA;EAAkC;EAE/DW,UAAUC,SAA2BC,MAAmB;AACtD,WAAOA,KAAKC,OAAM,EAAGC,KACnB1B,WAAW,CAAC2B,cAAAA;AACV,UAAIJ,QAAQK,QAAO,MAAO,QAAQ;AAChC,cAAMC,MAAMN,QAAQO,aAAY;AAChC,cAAMC,MAAMF,IAAIG,YAAW;AAE3B,YAAI,KAAKrB,aAAa,aAAaoB,IAAIE,QAAQ;AAC7CF,cAAIE,OAAOC,cAAcP;QAC3B,WACE,KAAKhB,aAAa,cACjB,CAACgB,UAAUQ,cAAcR,UAAUQ,eAAe,MACnD;AACAJ,cAAIG,cAAcP;QACpB;MACF;AACA,aAAO1B,WAAW,MAAM0B,SAAAA;IAC1B,CAAA,CAAA;EAEJ;AACF,GAvBMR,mCADN;;;;;;;;","names":["Injectable","catchError","throwError","ApitallySpanProcessor","setConsumerFn","useApitally","app","config","httpAdapter","getHttpAdapter","instance","getInstance","platform","name","toLowerCase","includes","setConsumer","apitallyPlugin","register","useGlobalInterceptors","ApitallyInterceptor","request","consumer","intercept","context","next","handle","pipe","exception","getType","ctx","switchToHttp","res","getResponse","locals","serverError","statusCode"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apitally",
3
- "version": "0.24.0",
3
+ "version": "0.25.1",
4
4
  "description": "Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.",
5
5
  "author": "Apitally <hello@apitally.io>",
6
6
  "license": "MIT",
@@ -203,6 +203,9 @@
203
203
  "printWidth": 80
204
204
  },
205
205
  "dependencies": {
206
+ "@opentelemetry/api": "^1.9.0",
207
+ "@opentelemetry/context-async-hooks": "^2.0.0",
208
+ "@opentelemetry/sdk-trace-base": "^2.0.0",
206
209
  "async-lock": "^1",
207
210
  "fetch-retry": "^6",
208
211
  "winston": "^3"
@@ -300,13 +303,14 @@
300
303
  "class-validator": "^0.14.0",
301
304
  "copyfiles": "^2.4.1",
302
305
  "elysia": "^1.3.7",
306
+ "esbuild-fix-imports-plugin": "^1.0.23",
303
307
  "eslint": "^9.0.0",
304
308
  "express": "^5.1.0",
305
309
  "express-validator": "^7.2.1",
306
310
  "fastify": "^5.0.0",
307
311
  "fastify-plugin": "^5.0.1",
308
312
  "globals": "^17.0.0",
309
- "h3": "2.0.1-rc.6",
313
+ "h3": "2.0.1-rc.8",
310
314
  "hono": "^4.6.5",
311
315
  "koa": "^3.0.0",
312
316
  "koa-bodyparser": "^4.4.1",