@gasket/plugin-elastic-apm 7.0.0-next.42 → 7.0.0-next.45

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 CHANGED
@@ -4,28 +4,22 @@ Adds Elastic APM instrumentation to your application
4
4
 
5
5
  ## Installation
6
6
 
7
- #### New apps
8
-
9
7
  ```
10
- gasket create <app-name> --plugins @gasket/plugin-elastic-apm
8
+ npm i @gasket/plugin-elastic-apm
11
9
  ```
12
10
 
13
- #### Existing apps
11
+ Update your `gasket` file plugin configuration:
14
12
 
15
- ```
16
- npm install @gasket/plugin-elastic-apm elastic-apm-node
17
- ```
13
+ ```diff
14
+ // gasket.js
18
15
 
19
- Modify `plugins` section of your `gasket.config.js`:
16
+ + import pluginElasticApm from '@gasket/plugin-elastic-apm';
20
17
 
21
- ```diff
22
- module.exports = {
23
- plugins: {
24
- add: [
25
- + '@gasket/plugin-elastic-apm'
26
- ]
27
- }
28
- }
18
+ export default makeGasket({
19
+ plugins: [
20
+ + pluginElasticApm
21
+ ]
22
+ });
29
23
  ```
30
24
 
31
25
  Add a `--require` flag to a `package.json` start script:
@@ -77,14 +71,14 @@ configured under `elasticAPM` in the `gasket.config.js`.
77
71
  If your application’s users send session credentials or any other sensitive
78
72
  information in their cookies, you may wish to filter them out before they are
79
73
  stored in Elasticsearch. Specify a list of cookie names to redact in
80
- `gasket.config.js`:
74
+ `gasket.js`:
81
75
 
82
76
  ```js
83
- module.exports = {
77
+ export default makeGasket({
84
78
  elasticAPM: {
85
79
  sensitiveCookies: ['my_jwt', 'userFullName']
86
80
  }
87
- };
81
+ });
88
82
  ```
89
83
 
90
84
  #### Custom Filtering Sensitive Fields
@@ -131,10 +125,15 @@ request details are as follows:
131
125
  | `res` | The HTTP response or framework-specific wrapper around it |
132
126
 
133
127
  ```javascript
134
- // /lifecycles/apm-transaction.js
135
-
136
- module.exports = (gasket, transaction, { req, res }) => {
137
- transaction.setLabel('language', req.headers['accept-language']);
128
+ // example-plugin.js
129
+
130
+ export default {
131
+ name: 'example-plugin',
132
+ hooks: {
133
+ apmTransaction(gasket, transaction, { req, res }) => {
134
+ transaction.setLabel('language', req.headers['accept-language']);
135
+ }
136
+ }
138
137
  }
139
138
  ```
140
139
 
package/lib/actions.js ADDED
@@ -0,0 +1,21 @@
1
+ /** @type {import('@gasket/core').HookHandler<'actions'>} */
2
+ module.exports = function actions(gasket) {
3
+ return {
4
+ async getApmTransaction(req) {
5
+ const apm = require('elastic-apm-node');
6
+
7
+ if (!apm?.isStarted()) {
8
+ return;
9
+ }
10
+
11
+ const transaction = apm.currentTransaction;
12
+ if (!transaction) {
13
+ return;
14
+ }
15
+
16
+ await gasket.exec('apmTransaction', transaction, { req });
17
+
18
+ return transaction;
19
+ }
20
+ };
21
+ };
package/lib/create.js ADDED
@@ -0,0 +1,24 @@
1
+ const { name, version, devDependencies } = require('../package.json');
2
+
3
+ /** @type {import('@gasket/core').HookHandler<'create'>} */
4
+ module.exports = function create(gasket, { pkg, files, gasketConfig }) {
5
+ const generatorDir = `${__dirname}/../generator`;
6
+
7
+ gasketConfig.addPlugin('pluginElasticApm', name);
8
+
9
+ pkg.add('dependencies', {
10
+ [name]: `^${version}`,
11
+ 'dotenv': devDependencies.dotenv,
12
+ 'elastic-apm-node': devDependencies['elastic-apm-node']
13
+ });
14
+
15
+ pkg.extend((current) => {
16
+ return {
17
+ scripts: {
18
+ start: `NODE_OPTIONS=--import=./setup.js ${current.scripts.start}`
19
+ }
20
+ };
21
+ });
22
+
23
+ files.add(`${generatorDir}/*`);
24
+ };
package/lib/index.d.ts CHANGED
@@ -2,6 +2,8 @@ import type { IncomingMessage, ServerResponse } from 'http';
2
2
  import type { AgentConfigOptions, Transaction, Payload } from 'elastic-apm-node';
3
3
  import type { MaybeAsync, GasketConfig } from '@gasket/core';
4
4
  import type { GasketData } from '@gasket/data';
5
+ import type { GasketRequest } from '@gasket/core';
6
+ import type { Request } from 'express';
5
7
 
6
8
  export function filterSensitiveCookies(config: GasketConfig): function(Payload): Payload;
7
9
 
@@ -18,18 +20,17 @@ declare module '@gasket/core' {
18
20
  apm?: Agent;
19
21
  }
20
22
 
23
+ export interface GasketActions {
24
+ async getApmTransaction(
25
+ req: IncomingMessage | Request
26
+ ): Promise<Transaction | void>
27
+ }
28
+
21
29
  export interface HookExecTypes {
22
30
  apmTransaction(
23
31
  transaction: Transaction,
24
- details: {
25
- req: IncomingMessage;
26
- res: ServerResponse & {
27
- locals?: {
28
- gasketData: GasketData & {
29
- locale?: string;
30
- };
31
- };
32
- };
32
+ context: {
33
+ req: IncomingMessage | Request;
33
34
  }
34
35
  ): MaybeAsync<void>;
35
36
  }
package/lib/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  /// <reference types="create-gasket-app" />
2
2
  /// <reference types="@gasket/plugin-metadata" />
3
3
 
4
+ const actions = require('./actions');
5
+ const create = require('./create');
4
6
  const middleware = require('./middleware');
5
- const preboot = require('./preboot');
6
7
  const configure = require('./configure');
7
8
  const {
8
9
  name,
9
10
  version,
10
- description,
11
- devDependencies
11
+ description
12
12
  } = require('../package.json');
13
13
 
14
14
  /** @type {import('@gasket/core').Plugin} */
@@ -17,22 +17,9 @@ const plugin = {
17
17
  version,
18
18
  description,
19
19
  hooks: {
20
+ actions,
20
21
  configure,
21
- // @ts-expect-error - TODO: will be cleaned up in the apm tune up ticket
22
- // https://godaddy-corp.atlassian.net/browse/PFX-628
23
- preboot,
24
- create(gasket, { pkg, files }) {
25
- const generatorDir = `${__dirname}/../generator`;
26
-
27
- pkg.add('dependencies', {
28
- 'elastic-apm-node': devDependencies['elastic-apm-node']
29
- });
30
- pkg.add('scripts', {
31
- start: 'gasket start --require ./setup.js'
32
- });
33
-
34
- files.add(`${generatorDir}/*`);
35
- },
22
+ create,
36
23
  middleware,
37
24
  metadata(gasket, meta) {
38
25
  return {
package/lib/middleware.js CHANGED
@@ -4,11 +4,9 @@
4
4
  * Middleware for customizing transactions
5
5
  * @param {import('@gasket/core').Gasket} gasket - The Gasket engine
6
6
  * @param {import('http').IncomingMessage} req - The HTTP request being handled
7
- * @param {import('http').ServerResponse} res - The server response
8
7
  */
9
- async function customizeTransaction(gasket, req, res) {
10
- const apm = gasket.apm;
11
-
8
+ async function customizeTransaction(gasket, req) {
9
+ const apm = require('elastic-apm-node');
12
10
  if (!apm?.isStarted()) {
13
11
  return;
14
12
  }
@@ -18,7 +16,7 @@ async function customizeTransaction(gasket, req, res) {
18
16
  return;
19
17
  }
20
18
 
21
- await gasket.exec('apmTransaction', transaction, { req, res });
19
+ await gasket.exec('apmTransaction', transaction, { req });
22
20
  }
23
21
 
24
22
  /**
@@ -26,11 +24,14 @@ async function customizeTransaction(gasket, req, res) {
26
24
  * @type {import('@gasket/core').HookHandler<'middleware'>}
27
25
  */
28
26
  module.exports = function middleware(gasket) {
27
+ const apm = require('elastic-apm-node');
28
+ // eslint-disable-next-line no-undefined
29
+ const isStarted = apm.isStarted() || undefined;
29
30
  return (
30
- gasket.apm &&
31
+ isStarted &&
31
32
  async function apmTransactionMiddleware(req, res, next) {
32
33
  try {
33
- customizeTransaction(gasket, req, res);
34
+ customizeTransaction(gasket, req);
34
35
  } catch (error) {
35
36
  return next(error);
36
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "7.0.0-next.42",
3
+ "version": "7.0.0-next.45",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
44
44
  "devDependencies": {
45
- "@gasket/core": "^7.0.0-next.42",
45
+ "@gasket/core": "^7.0.0-next.45",
46
+ "dotenv": "^16.4.5",
46
47
  "elastic-apm-node": "^4.4.1",
47
48
  "eslint": "^8.56.0",
48
49
  "eslint-config-godaddy": "^7.1.0",
@@ -70,5 +71,5 @@
70
71
  "jest": {
71
72
  "testEnvironment": "node"
72
73
  },
73
- "gitHead": "d96c86a52f608ea702f9b91a5a72f3b19389877c"
74
+ "gitHead": "cca9f460bdf2859d000443b25a76927251560f48"
74
75
  }
package/lib/preboot.js DELETED
@@ -1,30 +0,0 @@
1
- /// <reference types="@gasket/plugin-logger" />
2
-
3
- const { filterSensitiveCookies } = require('./cookies');
4
-
5
- // @ts-expect-error - TODO: will be cleaned up in the apm tune up ticket
6
- // https://godaddy-corp.atlassian.net/browse/PFX-628
7
- /** @type {import('@gasket/core').HookHandler<'preboot'>} */
8
- module.exports = async function preboot(gasket) {
9
- const { config, logger, command } = gasket;
10
-
11
- if (command && command.id === 'local') return;
12
-
13
- /**
14
- * @type {import('elastic-apm-node')}
15
- * Note: Prefer app-level dependency in case of duplicates
16
- */
17
- const apm = require(require.resolve('elastic-apm-node', {
18
- paths: [config.root, __dirname]
19
- }));
20
-
21
- if (!apm.isStarted()) {
22
- logger.warn(
23
- 'Elastic APM agent is not started. Use `--require ./setup.js`'
24
- );
25
- }
26
-
27
- apm.addFilter(filterSensitiveCookies(config));
28
-
29
- gasket.apm = apm;
30
- };