@gasket/plugin-elastic-apm 6.47.0 → 7.0.0-7.0.1-canary.11.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/configure.js CHANGED
@@ -1,7 +1,3 @@
1
- /// <reference types="@gasket/plugin-command" />
2
-
3
- const isDefined = (o) => typeof o !== 'undefined';
4
-
5
1
  /**
6
2
  * Determines if the Elastic APM agent has sufficient config to be active
7
3
  * @param {import('./index').ExtendedAgentConfigOptions} config Apm agent config
@@ -27,23 +23,10 @@ const isActive = (config, env) => {
27
23
  return false;
28
24
  };
29
25
 
30
- /** @type {import('@gasket/engine').HookHandler<'configure'>} */
26
+ /** @type {import('@gasket/core').HookHandler<'configure'>} */
31
27
  module.exports = async function configure(gasket, config) {
32
- const { logger } = gasket;
33
28
  config.elasticAPM = config.elasticAPM || {};
34
29
 
35
- const { serverUrl, secretToken } = config.elasticAPM;
36
- if (isDefined(serverUrl)) {
37
- logger.notice(
38
- 'DEPRECATED config `elasticAPM.serverUrl`. Use env var: ELASTIC_APM_SERVER_URL'
39
- );
40
- }
41
- if (isDefined(secretToken)) {
42
- logger.notice(
43
- 'DEPRECATED config `elasticAPM.secretToken`. Use env var: ELASTIC_APM_SECRET_TOKEN'
44
- );
45
- }
46
-
47
30
  // eslint-disable-next-line no-process-env
48
31
  config.elasticAPM.active = isActive(config.elasticAPM, process.env);
49
32
 
package/lib/cookies.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Returns an array of cookie names which are considered sensitive because they
3
3
  * may contain session credential or PII
4
- * @param {import('@gasket/engine').GasketConfig} config the Gasket config
4
+ * @param {import('@gasket/core').GasketConfig} config the Gasket config
5
5
  * @returns {string[]} an array of cookie names
6
6
  */
7
7
  const sensitiveCookies = (config) => {
@@ -21,15 +21,16 @@ const sensitiveCookies = (config) => {
21
21
  */
22
22
  const filterSensitiveCookies = function (config) {
23
23
  return function (payload) {
24
+ const cookiesToRedact = sensitiveCookies(config);
25
+
24
26
  if (
25
27
  payload.context &&
26
- payload.context.request &&
27
- payload.context.request.headers &&
28
- payload.context.request.headers.cookie
28
+ payload.context.request &&
29
+ payload.context.request.headers &&
30
+ payload.context.request.headers.cookie
29
31
  ) {
30
32
  let cookie = payload.context.request.headers.cookie;
31
-
32
- sensitiveCookies(config).forEach((sc) => {
33
+ cookiesToRedact.forEach((sc) => {
33
34
  cookie = cookie.replace(
34
35
  new RegExp(sc + '=([^;]+)'),
35
36
  sc + '=[REDACTED]'
@@ -39,6 +40,17 @@ const filterSensitiveCookies = function (config) {
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
@@ -1,15 +1,15 @@
1
1
  import type { IncomingMessage, ServerResponse } from 'http';
2
- import type { Agent, AgentConfigOptions, Transaction, Payload } from 'elastic-apm-node';
3
- import type { MaybeAsync, GasketConfig } from '@gasket/engine';
2
+ import type { AgentConfigOptions, Transaction, Payload } from 'elastic-apm-node';
3
+ import type { MaybeAsync, GasketConfig } from '@gasket/core';
4
4
  import type { GasketData } from '@gasket/data';
5
5
 
6
6
  export function filterSensitiveCookies(config: GasketConfig): function(Payload): Payload;
7
7
 
8
- interface ExtendedAgentConfigOptions extends AgentConfigOptions {
8
+ export interface ExtendedAgentConfigOptions extends AgentConfigOptions {
9
9
  sensitiveCookies?: Array<string>;
10
10
  }
11
11
 
12
- declare module '@gasket/engine' {
12
+ declare module '@gasket/core' {
13
13
  export interface GasketConfig {
14
14
  elasticAPM?: ExtendedAgentConfigOptions;
15
15
  }
@@ -25,10 +25,17 @@ declare module '@gasket/engine' {
25
25
  req: IncomingMessage;
26
26
  res: ServerResponse & {
27
27
  locals?: {
28
- gasketData: GasketData;
28
+ gasketData: GasketData & {
29
+ locale?: string;
30
+ };
29
31
  };
30
32
  };
31
33
  }
32
34
  ): MaybeAsync<void>;
33
35
  }
34
36
  }
37
+
38
+ export default {
39
+ name: '@gasket/plugin-elastic-apm',
40
+ hooks: {}
41
+ };
package/lib/index.js CHANGED
@@ -1,12 +1,12 @@
1
- /// <reference types="@gasket/cli" />
1
+ /// <reference types="create-gasket-app" />
2
2
  /// <reference types="@gasket/plugin-metadata" />
3
3
 
4
4
  const middleware = require('./middleware');
5
5
  const preboot = require('./preboot');
6
6
  const configure = require('./configure');
7
- const { dependencies, name } = require('../package.json');
7
+ const { devDependencies, name } = require('../package.json');
8
8
 
9
- /** @type {import('@gasket/engine').Plugin} */
9
+ /** @type {import('@gasket/core').Plugin} */
10
10
  const plugin = {
11
11
  name,
12
12
  hooks: {
@@ -16,13 +16,17 @@ const plugin = {
16
16
  timing: {
17
17
  after: ['@gasket/plugin-start']
18
18
  },
19
- handler(gasket, { pkg }) {
19
+ handler(gasket, { pkg, files }) {
20
+ const generatorDir = `${__dirname}/../generator`;
21
+
20
22
  pkg.add('dependencies', {
21
- 'elastic-apm-node': dependencies['elastic-apm-node']
23
+ 'elastic-apm-node': devDependencies['elastic-apm-node']
22
24
  });
23
25
  pkg.add('scripts', {
24
- start: 'gasket start --require elastic-apm-node/start'
26
+ start: 'gasket start --require ./setup.js'
25
27
  });
28
+
29
+ files.add(`${generatorDir}/*`);
26
30
  }
27
31
  },
28
32
  middleware,
package/lib/middleware.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * Middleware for customizing transactions
5
- * @param {import('@gasket/engine').Gasket} gasket - The Gasket engine
5
+ * @param {import('@gasket/core').Gasket} gasket - The Gasket engine
6
6
  * @param {import('http').IncomingMessage} req - The HTTP request being handled
7
7
  * @param {import('http').ServerResponse} res - The server response
8
8
  */
@@ -23,9 +23,9 @@ async function customizeTransaction(gasket, req, res) {
23
23
 
24
24
  /**
25
25
  * Add middleware to gather config details
26
- * @type {import('@gasket/engine').HookHandler<'middleware'>}
26
+ * @type {import('@gasket/core').HookHandler<'middleware'>}
27
27
  */
28
- module.exports = (gasket) => {
28
+ module.exports = function middleware(gasket) {
29
29
  return (
30
30
  gasket.apm &&
31
31
  async function apmTransactionMiddleware(req, res, next) {
package/lib/preboot.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /// <reference types="@gasket/plugin-start" />
2
- /// <reference types="@gasket/plugin-log" />
2
+ /// <reference types="@gasket/plugin-logger" />
3
3
 
4
4
  const { filterSensitiveCookies } = require('./cookies');
5
5
 
6
- /** @type {import('@gasket/engine').HookHandler<'preboot'>} */
6
+ /** @type {import('@gasket/core').HookHandler<'preboot'>} */
7
7
  module.exports = async function preboot(gasket) {
8
8
  const { config, logger, command } = gasket;
9
9
 
@@ -18,11 +18,8 @@ module.exports = async function preboot(gasket) {
18
18
  }));
19
19
 
20
20
  if (!apm.isStarted()) {
21
- apm.start({
22
- ...config.elasticAPM
23
- });
24
- logger.notice(
25
- 'DEPRECATED started Elastic APM agent late. Use `--require elastic-apm-node/start`'
21
+ logger.warn(
22
+ 'Elastic APM agent is not started. Use `--require ./setup.js`'
26
23
  );
27
24
  }
28
25
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "6.47.0",
3
+ "version": "7.0.0-7.0.1-canary.11.0",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
- "main": "lib",
5
+ "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
8
8
  "lib"
@@ -41,11 +41,9 @@
41
41
  "url": "https://github.com/godaddy/gasket/issues"
42
42
  },
43
43
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
44
- "dependencies": {
45
- "elastic-apm-node": "^3.50.0"
46
- },
47
44
  "devDependencies": {
48
- "@gasket/engine": "^6.46.8",
45
+ "@gasket/core": "^7.0.0-7.0.1-canary.11.0",
46
+ "elastic-apm-node": "^4.4.1",
49
47
  "eslint": "^8.56.0",
50
48
  "eslint-config-godaddy": "^7.1.0",
51
49
  "eslint-plugin-jest": "^27.6.3",
@@ -72,5 +70,5 @@
72
70
  "jest": {
73
71
  "testEnvironment": "node"
74
72
  },
75
- "gitHead": "9feff1fa92be6b1b6ed2ab6d3028490165395c62"
73
+ "gitHead": "ee3641e5f9218418d54690c04456fb24b63dc339"
76
74
  }