Skip to content

Commit 3cf0be6

Browse files
committed
docs: plugin
1 parent 9462df7 commit 3cf0be6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/server/src/appInsightsPlugin.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { FastifyInstance, FastifyPluginOptions } from 'fastify';
22
import fp from 'fastify-plugin';
33
import appInsights from 'applicationinsights';
44

5-
// based on https://github.com/microsoft/ApplicationInsights-node.js/issues/627#issuecomment-2194527018
65
declare module 'fastify' {
76
export interface FastifyRequest {
7+
// here we augment FastifyRequest interface as advised here: https://fastify.dev/docs/latest/Reference/Hooks/#using-hooks-to-inject-custom-properties
88
app: { start: number };
99
}
1010
}
1111

12+
// based on https://github.com/microsoft/ApplicationInsights-node.js/issues/627#issuecomment-2194527018
1213
export const appInsightsPlugin = fp(async (fastify: FastifyInstance, options: FastifyPluginOptions) => {
1314
if (!options.client) {
1415
console.log('App Insights not configured');
@@ -18,12 +19,15 @@ export const appInsightsPlugin = fp(async (fastify: FastifyInstance, options: Fa
1819
const client: appInsights.TelemetryClient = options.client;
1920
const urlsToIgnore = options.urlsToIgnore || [];
2021

21-
fastify.addHook('onRequest', async (request, _reply) => {
22+
fastify.addHook('onRequest', async function (this: FastifyInstance, request, _reply) {
23+
// const same = fastify === this;
24+
25+
// store the start time of the request
2226
const start = Date.now();
2327
request.app = { start };
2428
});
2529

26-
fastify.addHook('onResponse', async (request, reply) => {
30+
fastify.addHook('onResponse', async function (this: FastifyInstance, request, reply) {
2731
if (urlsToIgnore.includes(request.raw.url)) return;
2832

2933
const duration = Date.now() - request.app.start;

0 commit comments

Comments
 (0)