@gasket/plugin-elastic-apm 7.0.0-next.9 → 7.0.1-canary.10

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.
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Determines if the Elastic APM agent has sufficient config to be active
3
+ * @param {import('./index').ExtendedAgentConfigOptions} config Apm agent config
4
+ * @param {NodeJS.ProcessEnv} env Environment variables
5
+ * @returns {boolean} A combined config object
6
+ */
7
+ const isActive = (config, env) => {
8
+ const { active, serverUrl, secretToken } = config;
9
+
10
+ if (active || env.ELASTIC_APM_ACTIVE) {
11
+ return true;
12
+ }
13
+
14
+ const combined = {
15
+ serverUrl: serverUrl || env.ELASTIC_APM_SERVER_URL,
16
+ secretToken: secretToken || env.ELASTIC_APM_SECRET_TOKEN
17
+ };
18
+
19
+ if (combined.serverUrl && combined.secretToken) {
20
+ return true;
21
+ }
22
+
23
+ return false;
24
+ };
25
+
26
+ /** @type {import('@gasket/core').HookHandler<'configure'>} */
27
+ module.exports = async function configure(gasket, config) {
28
+ config.elasticAPM = config.elasticAPM || {};
29
+
30
+ // eslint-disable-next-line no-process-env
31
+ config.elasticAPM.active = isActive(config.elasticAPM, process.env);
32
+
33
+ return { ...config };
34
+ };
package/lib/cookies.js CHANGED
@@ -1,8 +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
- *
5
- * @param {*} config the Gasket config object
4
+ * @param {import('@gasket/core').GasketConfig} config the Gasket config
6
5
  * @returns {string[]} an array of cookie names
7
6
  */
8
7
  const sensitiveCookies = (config) => {
@@ -18,40 +17,42 @@ const sensitiveCookies = (config) => {
18
17
 
19
18
  /**
20
19
  * Redacts the contents of user-specified sensitive cookies
21
- *
22
- * @param {object} config The Gasket config
23
- * @param {object} payload The APM payload
24
- * @returns {object} a modified version of the incoming APM payload
20
+ * @type {import('./index').filterSensitiveCookies}
25
21
  */
26
- const filterSensitiveCookies = (config) => (payload) => {
27
- const cookiesToRedact = sensitiveCookies(config);
22
+ const filterSensitiveCookies = function (config) {
23
+ return function (payload) {
24
+ const cookiesToRedact = sensitiveCookies(config);
28
25
 
29
- if (
30
- payload.context &&
26
+ if (
27
+ payload.context &&
31
28
  payload.context.request &&
32
29
  payload.context.request.headers &&
33
30
  payload.context.request.headers.cookie
34
- ) {
35
- let cookie = payload.context.request.headers.cookie;
36
- cookiesToRedact.forEach((sc) => {
37
- cookie = cookie.replace(new RegExp(sc + '=([^;]+)'), sc + '=[REDACTED]');
38
- });
31
+ ) {
32
+ let cookie = payload.context.request.headers.cookie;
33
+ cookiesToRedact.forEach((sc) => {
34
+ cookie = cookie.replace(
35
+ new RegExp(sc + '=([^;]+)'),
36
+ sc + '=[REDACTED]'
37
+ );
38
+ });
39
39
 
40
- payload.context.request.headers.cookie = cookie;
41
- }
40
+ payload.context.request.headers.cookie = cookie;
41
+ }
42
42
 
43
- if (payload.context &&
43
+ if (payload.context &&
44
44
  payload.context.request &&
45
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
- }
46
+ ) {
47
+ cookiesToRedact.forEach((sc) => {
48
+ if (sc in payload.context.request.cookies) {
49
+ payload.context.request.cookies[sc] = '[REDACTED]';
50
+ }
51
+ });
52
+ }
53
53
 
54
- return payload;
54
+ return payload;
55
+ };
55
56
  };
56
57
 
57
58
  module.exports = {
package/lib/index.d.ts CHANGED
@@ -1,12 +1,17 @@
1
1
  import type { IncomingMessage, ServerResponse } from 'http';
2
- import type { AgentConfigOptions, Transaction } from 'elastic-apm-node';
3
- import type { MaybeAsync } from '@gasket/engine';
2
+ import type { AgentConfigOptions, Transaction, Payload } from 'elastic-apm-node';
3
+ import type { MaybeAsync, GasketConfig } from '@gasket/core';
4
+ import type { GasketData } from '@gasket/data';
4
5
 
5
- declare module '@gasket/engine' {
6
+ export function filterSensitiveCookies(config: GasketConfig): function(Payload): Payload;
7
+
8
+ export interface ExtendedAgentConfigOptions extends AgentConfigOptions {
9
+ sensitiveCookies?: Array<string>;
10
+ }
11
+
12
+ declare module '@gasket/core' {
6
13
  export interface GasketConfig {
7
- elasticAPM?: AgentConfigOptions & {
8
- sensitiveCookies?: Array<string>
9
- },
14
+ elasticAPM?: ExtendedAgentConfigOptions;
10
15
  }
11
16
 
12
17
  export interface Gasket {
@@ -17,9 +22,20 @@ declare module '@gasket/engine' {
17
22
  apmTransaction(
18
23
  transaction: Transaction,
19
24
  details: {
20
- req: IncomingMessage,
21
- res: ServerResponse
25
+ req: IncomingMessage;
26
+ res: ServerResponse & {
27
+ locals?: {
28
+ gasketData: GasketData & {
29
+ locale?: string;
30
+ };
31
+ };
32
+ };
22
33
  }
23
- ): MaybeAsync<void>
34
+ ): MaybeAsync<void>;
24
35
  }
25
36
  }
37
+
38
+ export default {
39
+ name: '@gasket/plugin-elastic-apm',
40
+ hooks: {}
41
+ };
package/lib/index.js CHANGED
@@ -1,59 +1,17 @@
1
- const { filterSensitiveCookies } = require('./cookies');
2
- const middleware = require('./middleware');
3
- const { devDependencies } = require('../package.json');
4
-
5
- /**
6
- * Determines if the Elastic APM agent has sufficient config to be active
7
- * @param {object} config gasket config
8
- * @param {object<string,any>} env environment variables
9
- * @returns {boolean} A combined config object
10
- */
11
- const isActive = (config, env) => {
12
- const { active } = config;
13
-
14
- if (active || env.ELASTIC_APM_ACTIVE) {
15
- return true;
16
- }
17
-
18
- if (env.ELASTIC_APM_SERVER_URL && env.ELASTIC_APM_SECRET_TOKEN) {
19
- return true;
20
- }
1
+ /// <reference types="create-gasket-app" />
2
+ /// <reference types="@gasket/plugin-metadata" />
21
3
 
22
- return false;
23
- };
4
+ const middleware = require('./middleware');
5
+ const preboot = require('./preboot');
6
+ const configure = require('./configure');
7
+ const { devDependencies, name } = require('../package.json');
24
8
 
25
- module.exports = {
9
+ /** @type {import('@gasket/core').Plugin} */
10
+ const plugin = {
11
+ name,
26
12
  hooks: {
27
- configure: {
28
- handler: (gasket, config) => {
29
- config.elasticAPM = config.elasticAPM || {};
30
-
31
- // eslint-disable-next-line no-process-env
32
- config.elasticAPM.active = isActive(config.elasticAPM, process.env);
33
-
34
- return { ...config };
35
- }
36
- },
37
- preboot: {
38
- handler: async (gasket) => {
39
- const { config, logger, command } = gasket;
40
-
41
- if (command && command.id === 'local') return;
42
-
43
- // prefer app-level dependency in case of duplicates
44
- const apm = require(require.resolve('elastic-apm-node', {
45
- paths: [config.root, __dirname]
46
- }));
47
-
48
- if (!apm.isStarted()) {
49
- logger.warn(
50
- 'Elastic APM agent is not started. Use `--require ./setup.js`'
51
- );
52
- }
53
-
54
- apm.addFilter(filterSensitiveCookies(config));
55
- }
56
- },
13
+ configure,
14
+ preboot,
57
15
  create: {
58
16
  timing: {
59
17
  after: ['@gasket/plugin-start']
@@ -62,8 +20,7 @@ module.exports = {
62
20
  const generatorDir = `${__dirname}/../generator`;
63
21
 
64
22
  pkg.add('dependencies', {
65
- 'elastic-apm-node': devDependencies['elastic-apm-node'],
66
- 'dotenv': devDependencies.dotenv
23
+ 'elastic-apm-node': devDependencies['elastic-apm-node']
67
24
  });
68
25
  pkg.add('scripts', {
69
26
  start: 'gasket start --require ./setup.js'
@@ -104,3 +61,5 @@ module.exports = {
104
61
  }
105
62
  }
106
63
  };
64
+
65
+ module.exports = plugin;
package/lib/middleware.js CHANGED
@@ -1,22 +1,10 @@
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');
1
+ /// <reference types="@gasket/plugin-express" />
13
2
 
14
3
  /**
15
4
  * Middleware for customizing transactions
16
- *
17
- * @param {Gasket} gasket The Gasket engine
18
- * @param {Request} req The HTTP request being handled
19
- * @param {Response} res The server response
5
+ * @param {import('@gasket/core').Gasket} gasket - The Gasket engine
6
+ * @param {import('http').IncomingMessage} req - The HTTP request being handled
7
+ * @param {import('http').ServerResponse} res - The server response
20
8
  */
21
9
  async function customizeTransaction(gasket, req, res) {
22
10
  const apm = gasket.apm;
@@ -33,9 +21,20 @@ async function customizeTransaction(gasket, req, res) {
33
21
  await gasket.exec('apmTransaction', transaction, { req, res });
34
22
  }
35
23
 
36
- module.exports = (gasket) => {
24
+ /**
25
+ * Add middleware to gather config details
26
+ * @type {import('@gasket/core').HookHandler<'middleware'>}
27
+ */
28
+ module.exports = function middleware(gasket) {
37
29
  return (
38
- gasket.apm
39
- && [callbackify(async (req, res) => customizeTransaction(gasket, req, res))]
30
+ gasket.apm &&
31
+ async function apmTransactionMiddleware(req, res, next) {
32
+ try {
33
+ customizeTransaction(gasket, req, res);
34
+ } catch (error) {
35
+ return next(error);
36
+ }
37
+ next();
38
+ }
40
39
  );
41
40
  };
package/lib/preboot.js ADDED
@@ -0,0 +1,29 @@
1
+ /// <reference types="@gasket/plugin-start" />
2
+ /// <reference types="@gasket/plugin-logger" />
3
+
4
+ const { filterSensitiveCookies } = require('./cookies');
5
+
6
+ /** @type {import('@gasket/core').HookHandler<'preboot'>} */
7
+ module.exports = async function preboot(gasket) {
8
+ const { config, logger, command } = gasket;
9
+
10
+ if (command && command.id === 'local') return;
11
+
12
+ /**
13
+ * @type {import('elastic-apm-node')}
14
+ * Note: Prefer app-level dependency in case of duplicates
15
+ */
16
+ const apm = require(require.resolve('elastic-apm-node', {
17
+ paths: [config.root, __dirname]
18
+ }));
19
+
20
+ if (!apm.isStarted()) {
21
+ logger.warn(
22
+ 'Elastic APM agent is not started. Use `--require ./setup.js`'
23
+ );
24
+ }
25
+
26
+ apm.addFilter(filterSensitiveCookies(config));
27
+
28
+ gasket.apm = apm;
29
+ };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "7.0.0-next.9",
3
+ "version": "7.0.1-canary.10",
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"
@@ -14,8 +14,10 @@
14
14
  "test:runner": "jest test/*.test.js",
15
15
  "test:watch": "npm run test:runner -- --watch",
16
16
  "test:coverage": "npm run test:runner -- --coverage --coverageReporters text",
17
- "posttest": "npm run lint",
18
- "report": "npm run test:runner -- --coverage --coverageReporters lcov"
17
+ "posttest": "npm run lint && npm run typecheck",
18
+ "report": "npm run test:runner -- --coverage --coverageReporters lcov",
19
+ "typecheck": "tsc",
20
+ "typecheck:watch": "tsc --watch"
19
21
  },
20
22
  "repository": {
21
23
  "type": "git",
@@ -40,24 +42,26 @@
40
42
  },
41
43
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
42
44
  "devDependencies": {
43
- "@gasket/engine": "^7.0.0-next.9",
44
- "dot-env": "^0.0.1",
45
+ "@gasket/core": "^7.0.1-canary.10",
45
46
  "elastic-apm-node": "^4.4.1",
46
47
  "eslint": "^8.56.0",
47
- "eslint-config-godaddy": "^7.0.2",
48
+ "eslint-config-godaddy": "^7.1.0",
48
49
  "eslint-plugin-jest": "^27.6.3",
49
50
  "eslint-plugin-json": "^3.1.0",
50
51
  "eslint-plugin-unicorn": "^44.0.0",
51
52
  "jest": "^29.7.0",
52
- "nyc": "^15.1.0"
53
+ "nyc": "^15.1.0",
54
+ "typescript": "^5.4.5"
53
55
  },
54
56
  "eslintConfig": {
55
57
  "extends": [
56
58
  "godaddy",
57
- "plugin:jest/recommended"
59
+ "plugin:jest/recommended",
60
+ "plugin:jsdoc/recommended-typescript-flavor"
58
61
  ],
59
62
  "plugins": [
60
- "unicorn"
63
+ "unicorn",
64
+ "jsdoc"
61
65
  ],
62
66
  "rules": {
63
67
  "unicorn/filename-case": "error"
@@ -66,5 +70,5 @@
66
70
  "jest": {
67
71
  "testEnvironment": "node"
68
72
  },
69
- "gitHead": "d993b3d02586581fee56576d860eb2a8393e601c"
73
+ "gitHead": "ade41f0398f6adedb73d47a4a2dc9bff30e12437"
70
74
  }