UNPKG

@lokalise/fastify-extras

Version:

Opinionated set of fastify plugins, commonly used in Lokalise

105 lines 4.29 kB
import fp from 'fastify-plugin'; import { FifoMap } from 'toad-cache'; export class NewRelicTransactionManager { newrelic; isEnabled; transactionMap; constructor(isNewRelicEnabled, newrelic) { this.isEnabled = isNewRelicEnabled; this.newrelic = newrelic; this.transactionMap = new FifoMap(2000); } static async create(isNewRelicEnabled) { let newrelic = undefined; if (isNewRelicEnabled) { // newrelic import is returning a wrapper, property `default` is the actual newrelic object // @ts-ignore newrelic = (await import('newrelic')).default; } return new NewRelicTransactionManager(isNewRelicEnabled, newrelic); } static createDisabled() { return new NewRelicTransactionManager(false, undefined); } addCustomAttribute(attrName, attrValue) { if (!this.isEnabled) return; // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.addCustomAttribute(attrName, attrValue); } addCustomAttributes(_uniqueTransactionKey, atts) { if (!this.isEnabled) return; // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.addCustomAttributes(atts); } setUserID(userId) { if (!this.isEnabled) return; // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.setUserID(userId); } setControllerName(name, action) { if (!this.isEnabled) return; // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.setControllerName(name, action); } /** * @param transactionName - used for grouping similar transactions together * @param uniqueTransactionKey - used for identifying specific ongoing transaction. Must be reasonably unique to reduce possibility of collisions */ start(transactionName, uniqueTransactionKey) { if (!this.isEnabled) return; // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.startBackgroundTransaction(transactionName, () => { // biome-ignore lint/style/noNonNullAssertion: It should be defined this.transactionMap.set(uniqueTransactionKey, this.newrelic.getTransaction()); }); } /** * @param transactionName - used for grouping similar transactions together * @param uniqueTransactionKey - used for identifying specific ongoing transaction. Must be reasonably unique to reduce possibility of collisions * * @param transactionGroup - group is used for grouping related transactions with different names */ startWithGroup(transactionName, uniqueTransactionKey, transactionGroup) { if (!this.isEnabled) { return; } // biome-ignore lint/style/noNonNullAssertion: It should be defined this.newrelic.startBackgroundTransaction(transactionName, transactionGroup, () => { // biome-ignore lint/style/noNonNullAssertion: It should be defined this.transactionMap.set(uniqueTransactionKey, this.newrelic.getTransaction()); }); } stop(uniqueTransactionKey) { if (!this.isEnabled) return; const transaction = this.transactionMap.get(uniqueTransactionKey) ?? null; if (!transaction) return; transaction.end(); this.transactionMap.delete(uniqueTransactionKey); } } async function plugin(fastify, opts) { const manager = await NewRelicTransactionManager.create(opts.isEnabled); fastify.decorate('newrelicTransactionManager', manager); if (opts.isEnabled) { fastify.addHook('onClose', async () => { return new Promise((resolve, reject) => { manager.newrelic?.shutdown((error) => { if (error) return reject(error); resolve(); }); }); }); } } export const newrelicTransactionManagerPlugin = fp(plugin, { fastify: '5.x', name: 'newrelic-transaction-manager-plugin', }); //# sourceMappingURL=newrelicTransactionManagerPlugin.js.map