@gasket/plugin-elastic-apm 6.45.0 → 6.46.1-7.0.0-cli.6.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 CHANGED
@@ -34,21 +34,36 @@ Add a `--require` flag to a `package.json` start script:
34
34
  "scripts": {
35
35
  "build": "gasket build",
36
36
  - "start": "gasket start",
37
- + "start": "gasket start --require elastic-apm-node/start",
37
+ + "start": "gasket start --require ./setup.js",
38
38
  "local": "gasket local"
39
39
  }
40
40
  ```
41
41
 
42
+ Add a `setup.js` script to the root of your app
43
+
44
+ ```
45
+ // setup.js
46
+ require('dotenv').config();
47
+
48
+ require('elastic-apm-node').start({
49
+ serviceName: 'my-service-name',
50
+ secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
51
+ serverUrl: process.env.ELASTIC_APM_SERVER_URL
52
+ // any additional configurations options
53
+ });
54
+ ```
55
+
42
56
  ## Configuration
43
57
 
44
58
  The [start recommendations] for the APM agent are to require it as early as
45
- possible in your app. For Gasket apps, using `--require elastic-apm-node/start`
59
+ possible in your app. For Gasket apps, using `--require ./setup.js`
46
60
  will accomplish this. To configure the APM agent, set the environment variables
47
61
  described in the [configuration options documentation].
48
62
 
49
63
  In particular, the APM server URL (`ELASTIC_APM_SERVER_URL`) and secret token
50
- (`ELASTIC_APM_SECRET_TOKEN`) are both required configuration. If either
51
- of these are not present, the APM agent will be disabled.
64
+ (`ELASTIC_APM_SECRET_TOKEN`) are both required configuration. If either of these
65
+ are not present, the APM agent will be disabled.
66
+
52
67
 
53
68
  ### Plugin Configurations
54
69
 
@@ -72,26 +87,29 @@ module.exports = {
72
87
  };
73
88
  ```
74
89
 
75
- ### Custom Start Configurations
90
+ #### Custom Filtering Sensitive Fields
76
91
 
77
- For scenarios where you need to configure the start options for the APM agent,
78
- you can do so in a custom setup script and require it instead.
79
-
80
- For example, add a `setup.js` script to the root of your app:
92
+ If your application’s users send session credentials or any other sensitive
93
+ information in their cookies, you may wish to filter them out before they are
94
+ stored in Elasticsearch. Specify a list of cookie names to redact in
95
+ `setup.js` using the [sanitizeFieldNames] configuration option:
81
96
 
82
97
  ```
83
98
  // setup.js
99
+ require('dotenv').config();
100
+
84
101
  require('elastic-apm-node').start({
85
- // any configuration options
86
- })
102
+ ...,
103
+ sanitizeFieldNames: ['foo', 'bar', '*token*']
104
+ });
87
105
  ```
88
106
 
89
- Then adjust your start script to require it instead:
107
+ The `sanitizeFieldNames` config option can be used for:
108
+ - request and response HTTP headers
109
+ - HTTP request cookies
110
+ - any form field captured during an `application/x-www-form-urlencoded` data request
90
111
 
91
- ```diff
92
- - "start": "gasket start --require elastic-apm-node/start",
93
- + "start": "gasket start --require ./setup.js",
94
- ```
112
+ To filter out other data, use the [APM Add Filter API].
95
113
 
96
114
  ### Custom Filters
97
115
 
@@ -103,7 +121,9 @@ hooks of your Gasket app, such as with the [init] or [middleware] lifecycles.
103
121
 
104
122
  ### apmTransaction
105
123
 
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:
124
+ Enables customizing an APM transaction. Hooks receive the current APM
125
+ [Transaction] and details about the request. Hooks may be asynchronous. The
126
+ request details are as follows:
107
127
 
108
128
  | Property | Description |
109
129
  |----------|-------------|
@@ -120,11 +140,11 @@ module.exports = (gasket, transaction, { req, res }) => {
120
140
 
121
141
  ## How it works
122
142
 
123
- This plugin hooks the Gasket [preboot] lifecycle from [@gasket/plugin-start]
124
- and will set up additional filtering, such as for sensitive cookies. If the
143
+ This plugin hooks the Gasket [preboot] lifecycle from [@gasket/plugin-start] and
144
+ will set up additional filtering, such as for sensitive cookies. If the
125
145
  `preboot` hook finds that the APM agent has not yet been started using the
126
- recommended `--require elastic-apm-node/start`, it will start it here.
127
- However, you risk not bootstrapping necessary modules with a late start.
146
+ recommended `--require elastic-apm-node/start`, it will start it here. However,
147
+ you risk not bootstrapping necessary modules with a late start.
128
148
 
129
149
  ## License
130
150
 
@@ -138,3 +158,6 @@ However, you risk not bootstrapping necessary modules with a late start.
138
158
  [configuration options documentation]:https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html
139
159
  [start recommendations]:https://www.elastic.co/guide/en/apm/agent/nodejs/master/agent-api.html#apm-start
140
160
  [Elastic APM docs]:https://www.elastic.co/guide/en/apm/agent/nodejs/master/agent-api.html
161
+ [sanitizeFieldNames]:https://www.elastic.co/guide/en/apm/agent/nodejs/4.x/configuration.html#sanitize-field-names
162
+ [APM Add Filter API]:https://www.elastic.co/guide/en/apm/agent/nodejs/4.x/agent-api.html#apm-add-filter
163
+ [Transaction]:(https://www.elastic.co/guide/en/apm/agent/nodejs/current/transaction-api.html)
package/lib/cookies.js CHANGED
@@ -24,6 +24,8 @@ const sensitiveCookies = (config) => {
24
24
  * @returns {object} a modified version of the incoming APM payload
25
25
  */
26
26
  const filterSensitiveCookies = (config) => (payload) => {
27
+ const cookiesToRedact = sensitiveCookies(config);
28
+
27
29
  if (
28
30
  payload.context &&
29
31
  payload.context.request &&
@@ -31,14 +33,24 @@ const filterSensitiveCookies = (config) => (payload) => {
31
33
  payload.context.request.headers.cookie
32
34
  ) {
33
35
  let cookie = payload.context.request.headers.cookie;
34
-
35
- sensitiveCookies(config).forEach((sc) => {
36
+ cookiesToRedact.forEach((sc) => {
36
37
  cookie = cookie.replace(new RegExp(sc + '=([^;]+)'), sc + '=[REDACTED]');
37
38
  });
38
39
 
39
40
  payload.context.request.headers.cookie = cookie;
40
41
  }
41
42
 
43
+ if (payload.context &&
44
+ payload.context.request &&
45
+ payload.context.request.cookies
46
+ ) {
47
+ cookiesToRedact.forEach((sc) => {
48
+ if (sc in payload.context.request.cookies) {
49
+ payload.context.request.cookies[sc] = '[REDACTED]';
50
+ }
51
+ });
52
+ }
53
+
42
54
  return payload;
43
55
  };
44
56
 
package/lib/index.d.ts CHANGED
@@ -7,7 +7,7 @@ declare module '@gasket/engine' {
7
7
  sensitiveCookies?: Array<string>
8
8
  },
9
9
  }
10
-
10
+
11
11
  export interface HookExecTypes {
12
12
  apmTransaction(
13
13
  transaction: Transaction,
package/lib/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  const { filterSensitiveCookies } = require('./cookies');
2
2
  const middleware = require('./middleware');
3
- const { dependencies } = require('../package.json');
4
-
5
- const isDefined = o => typeof o !== 'undefined';
3
+ const { devDependencies } = require('../package.json');
6
4
 
7
5
  /**
8
6
  * Determines if the Elastic APM agent has sufficient config to be active
@@ -11,18 +9,13 @@ const isDefined = o => typeof o !== 'undefined';
11
9
  * @returns {boolean} A combined config object
12
10
  */
13
11
  const isActive = (config, env) => {
14
- const { active, serverUrl, secretToken } = config;
12
+ const { active } = config;
15
13
 
16
14
  if (active || env.ELASTIC_APM_ACTIVE) {
17
15
  return true;
18
16
  }
19
17
 
20
- const combined = {
21
- serverUrl: serverUrl || env.ELASTIC_APM_SERVER_URL,
22
- secretToken: secretToken || env.ELASTIC_APM_SECRET_TOKEN
23
- };
24
-
25
- if (combined.serverUrl && combined.secretToken) {
18
+ if (env.ELASTIC_APM_SERVER_URL && env.ELASTIC_APM_SECRET_TOKEN) {
26
19
  return true;
27
20
  }
28
21
 
@@ -33,17 +26,8 @@ module.exports = {
33
26
  hooks: {
34
27
  configure: {
35
28
  handler: async (gasket, config) => {
36
- const { logger } = gasket;
37
29
  config.elasticAPM = config.elasticAPM || {};
38
30
 
39
- const { serverUrl, secretToken } = config.elasticAPM;
40
- if (isDefined(serverUrl)) {
41
- logger.notice('DEPRECATED config `elasticAPM.serverUrl`. Use env var: ELASTIC_APM_SERVER_URL');
42
- }
43
- if (isDefined(secretToken)) {
44
- logger.notice('DEPRECATED config `elasticAPM.secretToken`. Use env var: ELASTIC_APM_SECRET_TOKEN');
45
- }
46
-
47
31
  // eslint-disable-next-line no-process-env
48
32
  config.elasticAPM.active = isActive(config.elasticAPM, process.env);
49
33
 
@@ -57,15 +41,14 @@ module.exports = {
57
41
  if (command && command.id === 'local') return;
58
42
 
59
43
  // prefer app-level dependency in case of duplicates
60
- const apm = require(
61
- require.resolve('elastic-apm-node', { paths: [config.root, __dirname] })
62
- );
44
+ const apm = require(require.resolve('elastic-apm-node', {
45
+ paths: [config.root, __dirname]
46
+ }));
63
47
 
64
48
  if (!apm.isStarted()) {
65
- apm.start({
66
- ...config.elasticAPM
67
- });
68
- logger.notice('DEPRECATED started Elastic APM agent late. Use `--require elastic-apm-node/start`');
49
+ logger.warn(
50
+ 'Elastic APM agent is not started. Use `--require ./setup.js`'
51
+ );
69
52
  }
70
53
 
71
54
  apm.addFilter(filterSensitiveCookies(config));
@@ -75,38 +58,48 @@ module.exports = {
75
58
  timing: {
76
59
  after: ['@gasket/plugin-start']
77
60
  },
78
- handler(gasket, { pkg }) {
61
+ handler(gasket, { pkg, files }) {
62
+ const generatorDir = `${__dirname}/../generator`;
63
+
79
64
  pkg.add('dependencies', {
80
- 'elastic-apm-node': dependencies['elastic-apm-node']
65
+ 'elastic-apm-node': devDependencies['elastic-apm-node'],
66
+ 'dotenv': devDependencies.dotenv
81
67
  });
82
68
  pkg.add('scripts', {
83
- start: 'gasket start --require elastic-apm-node/start'
69
+ start: 'gasket start --require ./setup.js'
84
70
  });
71
+
72
+ files.add(`${generatorDir}/*`);
85
73
  }
86
74
  },
87
75
  middleware,
88
76
  metadata(gasket, meta) {
89
77
  return {
90
78
  ...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
- }]
79
+ configurations: [
80
+ {
81
+ name: 'elasticAPM',
82
+ link: 'README.md#configuration',
83
+ description: 'Configuration to provide additional setup helpers',
84
+ type: 'object'
85
+ },
86
+ {
87
+ name: 'elasticAPM.sensitiveCookies',
88
+ link: 'README.md#configuration',
89
+ description: 'List of sensitive cookies to filter',
90
+ type: 'string[]',
91
+ default: '[]'
92
+ }
93
+ ],
94
+ lifecycles: [
95
+ {
96
+ name: 'apmTransaction',
97
+ method: 'exec',
98
+ description: 'Modify the APM transaction',
99
+ link: 'README.md#apmtransaction',
100
+ parent: 'middleware'
101
+ }
102
+ ]
110
103
  };
111
104
  }
112
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "6.45.0",
3
+ "version": "6.46.1-7.0.0-cli.6.0",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib",
6
6
  "types": "lib/index.d.ts",
@@ -39,16 +39,14 @@
39
39
  "url": "https://github.com/godaddy/gasket/issues"
40
40
  },
41
41
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
42
- "dependencies": {
43
- "elastic-apm-node": "^3.50.0"
44
- },
45
42
  "devDependencies": {
46
- "@gasket/engine": "^6.45.0",
43
+ "@gasket/engine": "^6.46.1-7.0.0-cli.6.0",
44
+ "dot-env": "^0.0.1",
45
+ "elastic-apm-node": "^4.4.1",
47
46
  "eslint": "^8.56.0",
48
47
  "eslint-config-godaddy": "^7.0.2",
49
48
  "eslint-plugin-jest": "^27.6.3",
50
49
  "eslint-plugin-json": "^3.1.0",
51
- "eslint-plugin-mocha": "^10.2.0",
52
50
  "eslint-plugin-unicorn": "^44.0.0",
53
51
  "jest": "^29.7.0",
54
52
  "nyc": "^15.1.0"
@@ -68,5 +66,5 @@
68
66
  "jest": {
69
67
  "testEnvironment": "node"
70
68
  },
71
- "gitHead": "e567d11560a0d3721ffbd4649fa4ee8b6b739afb"
69
+ "gitHead": "0d3c61e5ba470fbca7ae5e331d15e41b57410fe0"
72
70
  }