@gasket/plugin-elastic-apm 6.26.1 → 6.30.0
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.
- package/README.md +19 -0
- package/lib/index.d.ts +12 -1
- package/lib/index.js +29 -1
- package/lib/middleware.js +33 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -99,6 +99,25 @@ According to the [Elastic APM docs], the _Elastic APM agent for Node.js is a
|
|
|
99
99
|
singleton_. This means that you can require and configure singleton in various
|
|
100
100
|
hooks of your Gasket app, such as with the [init] or [middleware] lifecycles.
|
|
101
101
|
|
|
102
|
+
## Lifecycles
|
|
103
|
+
|
|
104
|
+
### apmTransaction
|
|
105
|
+
|
|
106
|
+
Enables customizing an APM transaction. Hooks receive the current APM [Transaction](https://www.elastic.co/guide/en/apm/agent/nodejs/current/transaction-api.html) and details about the request. Hooks may be asynchronous. The request details are as follows:
|
|
107
|
+
|
|
108
|
+
| Property | Description |
|
|
109
|
+
|----------|-------------|
|
|
110
|
+
| `req` | The HTTP request or framework-specific wrapper around it |
|
|
111
|
+
| `res` | The HTTP response or framework-specific wrapper around it |
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
// /lifecycles/apm-transaction.js
|
|
115
|
+
|
|
116
|
+
module.exports = (gasket, transaction, { req, res }) => {
|
|
117
|
+
transaction.setLabel('language', req.headers['accept-language']);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
102
121
|
## How it works
|
|
103
122
|
|
|
104
123
|
This plugin hooks the Gasket [preboot] lifecycle from [@gasket/plugin-start]
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
import type { AgentConfigOptions, Transaction } from 'elastic-apm-node';
|
|
2
3
|
|
|
3
4
|
declare module '@gasket/engine' {
|
|
4
5
|
export interface GasketConfig {
|
|
@@ -6,4 +7,14 @@ declare module '@gasket/engine' {
|
|
|
6
7
|
sensitiveCookies?: Array<string>
|
|
7
8
|
},
|
|
8
9
|
}
|
|
10
|
+
|
|
11
|
+
export interface HookExecTypes {
|
|
12
|
+
apmTransaction(
|
|
13
|
+
transaction: Transaction,
|
|
14
|
+
details: {
|
|
15
|
+
req: IncomingMessage,
|
|
16
|
+
res: ServerResponse
|
|
17
|
+
}
|
|
18
|
+
): MaybeAsync<void>
|
|
19
|
+
}
|
|
9
20
|
}
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { filterSensitiveCookies } = require('./cookies');
|
|
2
|
+
const middleware = require('./middleware');
|
|
2
3
|
const { dependencies } = require('../package.json');
|
|
3
4
|
|
|
4
5
|
const isDefined = o => typeof o !== 'undefined';
|
|
@@ -51,7 +52,9 @@ module.exports = {
|
|
|
51
52
|
},
|
|
52
53
|
preboot: {
|
|
53
54
|
handler: async (gasket) => {
|
|
54
|
-
const { config, logger } = gasket;
|
|
55
|
+
const { config, logger, command } = gasket;
|
|
56
|
+
|
|
57
|
+
if (command && command.id === 'local') return;
|
|
55
58
|
|
|
56
59
|
// prefer app-level dependency in case of duplicates
|
|
57
60
|
const apm = require(
|
|
@@ -80,6 +83,31 @@ module.exports = {
|
|
|
80
83
|
start: 'gasket start --require elastic-apm-node/start'
|
|
81
84
|
});
|
|
82
85
|
}
|
|
86
|
+
},
|
|
87
|
+
middleware,
|
|
88
|
+
metadata(gasket, meta) {
|
|
89
|
+
return {
|
|
90
|
+
...meta,
|
|
91
|
+
configurations: [{
|
|
92
|
+
name: 'elasticAPM',
|
|
93
|
+
link: 'README.md#configuration',
|
|
94
|
+
description: 'Configuration to provide additional setup helpers',
|
|
95
|
+
type: 'object'
|
|
96
|
+
}, {
|
|
97
|
+
name: 'elasticAPM.sensitiveCookies',
|
|
98
|
+
link: 'README.md#configuration',
|
|
99
|
+
description: 'List of sensitive cookies to filter',
|
|
100
|
+
type: 'string[]',
|
|
101
|
+
default: '[]'
|
|
102
|
+
}],
|
|
103
|
+
lifecycles: [{
|
|
104
|
+
name: 'apmTransaction',
|
|
105
|
+
method: 'exec',
|
|
106
|
+
description: 'Modify the APM transaction',
|
|
107
|
+
link: 'README.md#apmtransaction',
|
|
108
|
+
parent: 'middleware'
|
|
109
|
+
}]
|
|
110
|
+
};
|
|
83
111
|
}
|
|
84
112
|
}
|
|
85
113
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/* eslint-disable spaced-comment */
|
|
2
|
+
// @ts-check
|
|
3
|
+
/// <reference types="./index" />
|
|
4
|
+
/// <reference types="../../gasket-plugin-nextjs" />
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('@gasket/engine').Gasket} Gasket
|
|
8
|
+
* @typedef {import('http').IncomingMessage} Request
|
|
9
|
+
* @typedef {import('http').ServerResponse} Response
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { callbackify } = require('util');
|
|
13
|
+
const apm = require('elastic-apm-node');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Middleware for customizing transactions
|
|
17
|
+
*
|
|
18
|
+
* @param {Gasket} gasket The Gasket engine
|
|
19
|
+
* @param {Request} req The HTTP request being handled
|
|
20
|
+
* @param {Response} res The server response
|
|
21
|
+
*/
|
|
22
|
+
async function customizeTransaction(gasket, req, res) {
|
|
23
|
+
const transaction = apm.currentTransaction;
|
|
24
|
+
if (!transaction) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await gasket.exec('apmTransaction', transaction, { req, res });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = (gasket) => [
|
|
32
|
+
callbackify(async (req, res) => customizeTransaction(gasket, req, res))
|
|
33
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gasket/plugin-elastic-apm",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.30.0",
|
|
4
4
|
"description": "Adds Elastic APM instrumentation to your application",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"jest": {
|
|
69
69
|
"testEnvironment": "node"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "5261eb36af1fce802351cfaed11284ad48d244ee"
|
|
72
72
|
}
|