@lokalise/fastify-extras 27.0.0 → 27.0.2
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.
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { TransactionObservabilityManager } from '@lokalise/node-core';
|
|
2
2
|
import type { FastifyPluginCallback } from 'fastify';
|
|
3
|
+
import type { getTransaction as GetTransaction, shutdown as Shutdown, startBackgroundTransaction as startBackgroundTransactionType } from 'newrelic';
|
|
4
|
+
interface Newrelic {
|
|
5
|
+
startBackgroundTransaction: typeof startBackgroundTransactionType;
|
|
6
|
+
shutdown: typeof Shutdown;
|
|
7
|
+
getTransaction: typeof GetTransaction;
|
|
8
|
+
addCustomAttribute(key: string, value: string | number | boolean): void;
|
|
9
|
+
addCustomAttributes(atts: {
|
|
10
|
+
[key: string]: string | number | boolean;
|
|
11
|
+
}): void;
|
|
12
|
+
}
|
|
3
13
|
declare module 'fastify' {
|
|
4
14
|
interface FastifyInstance {
|
|
5
15
|
newrelicTransactionManager: NewRelicTransactionManager;
|
|
@@ -9,9 +19,11 @@ export interface NewRelicTransactionManagerOptions {
|
|
|
9
19
|
isEnabled: boolean;
|
|
10
20
|
}
|
|
11
21
|
export declare class NewRelicTransactionManager implements TransactionObservabilityManager {
|
|
22
|
+
readonly newrelic?: Newrelic;
|
|
12
23
|
private readonly isEnabled;
|
|
13
24
|
private readonly transactionMap;
|
|
14
|
-
constructor(
|
|
25
|
+
private constructor();
|
|
26
|
+
static create(isNewRelicEnabled: boolean): Promise<NewRelicTransactionManager>;
|
|
15
27
|
addCustomAttribute(attrName: string, attrValue: string | number | boolean): void;
|
|
16
28
|
addCustomAttributes(_uniqueTransactionKey: string, atts: {
|
|
17
29
|
[p: string]: string | number | boolean;
|
|
@@ -30,3 +42,4 @@ export declare class NewRelicTransactionManager implements TransactionObservabil
|
|
|
30
42
|
stop(uniqueTransactionKey: string): void;
|
|
31
43
|
}
|
|
32
44
|
export declare const newrelicTransactionManagerPlugin: FastifyPluginCallback<NewRelicTransactionManagerOptions>;
|
|
45
|
+
export {};
|
|
@@ -1,28 +1,33 @@
|
|
|
1
1
|
import fp from 'fastify-plugin';
|
|
2
2
|
import { FifoMap } from 'toad-cache';
|
|
3
|
-
let newrelic;
|
|
4
3
|
export class NewRelicTransactionManager {
|
|
4
|
+
newrelic;
|
|
5
5
|
isEnabled;
|
|
6
6
|
transactionMap;
|
|
7
|
-
constructor(isNewRelicEnabled) {
|
|
8
|
-
if (isNewRelicEnabled) {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
10
|
-
newrelic = require('newrelic');
|
|
11
|
-
}
|
|
7
|
+
constructor(isNewRelicEnabled, newrelic) {
|
|
12
8
|
this.isEnabled = isNewRelicEnabled;
|
|
9
|
+
this.newrelic = newrelic;
|
|
13
10
|
this.transactionMap = new FifoMap(2000);
|
|
14
11
|
}
|
|
12
|
+
static async create(isNewRelicEnabled) {
|
|
13
|
+
let newrelic = undefined;
|
|
14
|
+
if (isNewRelicEnabled) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
newrelic = await import('newrelic');
|
|
17
|
+
}
|
|
18
|
+
return new NewRelicTransactionManager(isNewRelicEnabled, newrelic);
|
|
19
|
+
}
|
|
15
20
|
addCustomAttribute(attrName, attrValue) {
|
|
16
21
|
if (!this.isEnabled) {
|
|
17
22
|
return;
|
|
18
23
|
}
|
|
19
|
-
newrelic
|
|
24
|
+
this.newrelic?.addCustomAttribute(attrName, attrValue);
|
|
20
25
|
}
|
|
21
26
|
addCustomAttributes(_uniqueTransactionKey, atts) {
|
|
22
27
|
if (!this.isEnabled) {
|
|
23
28
|
return;
|
|
24
29
|
}
|
|
25
|
-
newrelic
|
|
30
|
+
this.newrelic?.addCustomAttributes(atts);
|
|
26
31
|
}
|
|
27
32
|
/**
|
|
28
33
|
* @param transactionName - used for grouping similar transactions together
|
|
@@ -32,8 +37,9 @@ export class NewRelicTransactionManager {
|
|
|
32
37
|
if (!this.isEnabled) {
|
|
33
38
|
return;
|
|
34
39
|
}
|
|
35
|
-
newrelic
|
|
36
|
-
this
|
|
40
|
+
this.newrelic?.startBackgroundTransaction(transactionName, () => {
|
|
41
|
+
// biome-ignore lint/style/noNonNullAssertion: At this point it should be defined
|
|
42
|
+
this.transactionMap.set(uniqueTransactionKey, this.newrelic.getTransaction());
|
|
37
43
|
});
|
|
38
44
|
}
|
|
39
45
|
/**
|
|
@@ -45,8 +51,9 @@ export class NewRelicTransactionManager {
|
|
|
45
51
|
if (!this.isEnabled) {
|
|
46
52
|
return;
|
|
47
53
|
}
|
|
48
|
-
newrelic
|
|
49
|
-
this
|
|
54
|
+
this.newrelic?.startBackgroundTransaction(transactionName, transactionGroup, () => {
|
|
55
|
+
// biome-ignore lint/style/noNonNullAssertion: At this point it should be defined
|
|
56
|
+
this.transactionMap.set(uniqueTransactionKey, this.newrelic.getTransaction());
|
|
50
57
|
});
|
|
51
58
|
}
|
|
52
59
|
stop(uniqueTransactionKey) {
|
|
@@ -61,13 +68,13 @@ export class NewRelicTransactionManager {
|
|
|
61
68
|
this.transactionMap.delete(uniqueTransactionKey);
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
|
-
function plugin(fastify, opts
|
|
65
|
-
const manager =
|
|
71
|
+
async function plugin(fastify, opts) {
|
|
72
|
+
const manager = await NewRelicTransactionManager.create(opts.isEnabled);
|
|
66
73
|
fastify.decorate('newrelicTransactionManager', manager);
|
|
67
74
|
if (opts.isEnabled) {
|
|
68
75
|
fastify.addHook('onClose', async () => {
|
|
69
76
|
return new Promise((resolve, reject) => {
|
|
70
|
-
newrelic
|
|
77
|
+
manager.newrelic?.shutdown((error) => {
|
|
71
78
|
if (error) {
|
|
72
79
|
return reject(error);
|
|
73
80
|
}
|
|
@@ -76,7 +83,6 @@ function plugin(fastify, opts, done) {
|
|
|
76
83
|
});
|
|
77
84
|
});
|
|
78
85
|
}
|
|
79
|
-
done();
|
|
80
86
|
}
|
|
81
87
|
export const newrelicTransactionManagerPlugin = fp(plugin, {
|
|
82
88
|
fastify: '5.x',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"newrelicTransactionManagerPlugin.js","sourceRoot":"","sources":["../../lib/plugins/newrelicTransactionManagerPlugin.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAO/B,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"newrelicTransactionManagerPlugin.js","sourceRoot":"","sources":["../../lib/plugins/newrelicTransactionManagerPlugin.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAO/B,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAoBpC,MAAM,OAAO,0BAA0B;IACrB,QAAQ,CAAW;IAElB,SAAS,CAAS;IAClB,cAAc,CAA4B;IAE3D,YAAoB,iBAA0B,EAAE,QAAmB;QACjE,IAAI,CAAC,SAAS,GAAG,iBAAiB,CAAA;QAClC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,cAAc,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACzC,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,iBAA0B;QACnD,IAAI,QAAQ,GAAyB,SAAS,CAAA;QAC9C,IAAI,iBAAiB,EAAE,CAAC;YACtB,aAAa;YACb,QAAQ,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAA;QACrC,CAAC;QAED,OAAO,IAAI,0BAA0B,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAA;IACpE,CAAC;IAEM,kBAAkB,CAAC,QAAgB,EAAE,SAAoC;QAC9E,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAM;QACR,CAAC;QAED,IAAI,CAAC,QAAQ,EAAE,kBAAkB,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;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