@gasket/plugin-elastic-apm 7.0.1-canary.11 → 7.0.1

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,38 +4,32 @@ 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
- Add a `--require` flag to a `package.json` start script:
25
+ Add `NODE_OPTIONS=--import=./setup.js` to the `package.json` start script:
32
26
 
33
27
  ```diff
34
28
  "scripts": {
35
- "build": "gasket build",
36
- - "start": "gasket start",
37
- + "start": "gasket start --require ./setup.js",
38
- "local": "gasket local"
29
+ "build": "next build",
30
+ - "start": "next start",
31
+ + "start": "NODE_OPTIONS=--import=./setup.js next start",
32
+ "local": "next dev"
39
33
  }
40
34
  ```
41
35
 
@@ -43,13 +37,16 @@ Add a `setup.js` script to the root of your app
43
37
 
44
38
  ```
45
39
  // setup.js
46
- require('dotenv').config();
40
+ import dotenv from 'dotenv/config';
41
+ import apm from 'elastic-apm-node';
47
42
 
48
- require('elastic-apm-node').start({
43
+ // Elastic APM setup
44
+ apm.start({
49
45
  serviceName: 'my-service-name',
46
+ captureHeaders: false,
50
47
  secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
51
48
  serverUrl: process.env.ELASTIC_APM_SERVER_URL
52
- // any additional configurations options
49
+ // additional configuration options
53
50
  });
54
51
  ```
55
52
 
@@ -68,7 +65,7 @@ are not present, the APM agent will be disabled.
68
65
  ### Plugin Configurations
69
66
 
70
67
  The Gasket plugin provides some additional setup helpers. These can be
71
- configured under `elasticAPM` in the `gasket.config.js`.
68
+ configured under `elasticAPM` in the `gasket.js`.
72
69
 
73
70
  - **`sensitiveCookies`** - (string[]) A list of sensitive cookies to filter
74
71
 
@@ -77,14 +74,14 @@ configured under `elasticAPM` in the `gasket.config.js`.
77
74
  If your application’s users send session credentials or any other sensitive
78
75
  information in their cookies, you may wish to filter them out before they are
79
76
  stored in Elasticsearch. Specify a list of cookie names to redact in
80
- `gasket.config.js`:
77
+ `gasket.js`:
81
78
 
82
79
  ```js
83
- module.exports = {
80
+ export default makeGasket({
84
81
  elasticAPM: {
85
82
  sensitiveCookies: ['my_jwt', 'userFullName']
86
83
  }
87
- };
84
+ });
88
85
  ```
89
86
 
90
87
  #### Custom Filtering Sensitive Fields
@@ -117,6 +114,35 @@ According to the [Elastic APM docs], the _Elastic APM agent for Node.js is a
117
114
  singleton_. This means that you can require and configure singleton in various
118
115
  hooks of your Gasket app, such as with the [init] or [middleware] lifecycles.
119
116
 
117
+ ## Actions
118
+
119
+ ### getApmTransaction
120
+
121
+ Use the `getApmTransaction` action to access and decorate the current APM
122
+ transaction. This action is available in any lifecycle hook or server-side code.
123
+
124
+ ```js
125
+ // example-plugin.js
126
+
127
+ export default {
128
+ name: 'example-plugin',
129
+ hooks: {
130
+ express(gasket, app) {
131
+ app.use(async (req, res, next) => {
132
+ const transaction = await gasket.actions.getApmTransaction(req);
133
+ const locale = await gasket.actions.getIntlLocale(req);
134
+ transaction.setLabel('locale', locale);
135
+ });
136
+ }
137
+ }
138
+ }
139
+ ```
140
+
141
+ In the above example, we are hooking the express lifecycle to add middleware
142
+ to decorate the transaction.
143
+ Calling `getApmTransaction` will also allow other plugins to decorate the
144
+ transaction by hooking the `apmTransaction` lifecycle discussed next.
145
+
120
146
  ## Lifecycles
121
147
 
122
148
  ### apmTransaction
@@ -131,20 +157,22 @@ request details are as follows:
131
157
  | `res` | The HTTP response or framework-specific wrapper around it |
132
158
 
133
159
  ```javascript
134
- // /lifecycles/apm-transaction.js
135
-
136
- module.exports = (gasket, transaction, { req, res }) => {
137
- transaction.setLabel('language', req.headers['accept-language']);
160
+ // example-plugin.js
161
+
162
+ export default {
163
+ name: 'example-plugin',
164
+ hooks: {
165
+ apmTransaction(gasket, transaction, { req, res }) => {
166
+ transaction.setLabel('language', req.headers['accept-language']);
167
+ }
168
+ }
138
169
  }
139
170
  ```
140
171
 
141
172
  ## How it works
142
173
 
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
145
- `preboot` hook finds that the APM agent has not yet been started using the
146
- recommended `--require elastic-apm-node/start`, it will start it here. However,
147
- you risk not bootstrapping necessary modules with a late start.
174
+ This plugin hooks the Gasket [configure] lifecycle to set additional filtering,
175
+ such as for sensitive cookies.
148
176
 
149
177
  ## License
150
178
 
package/lib/actions.js ADDED
@@ -0,0 +1,21 @@
1
+ /** @type {import('@gasket/core').ActionHandler<'getApmTransaction'>} */
2
+ async function getApmTransaction(gasket, req) {
3
+ const apm = require('elastic-apm-node');
4
+
5
+ if (!apm?.isStarted()) {
6
+ return;
7
+ }
8
+
9
+ const transaction = apm.currentTransaction;
10
+ if (!transaction) {
11
+ return;
12
+ }
13
+
14
+ await gasket.exec('apmTransaction', transaction, { req });
15
+
16
+ return transaction;
17
+ }
18
+
19
+ module.exports = {
20
+ getApmTransaction
21
+ };
package/lib/configure.js CHANGED
@@ -24,7 +24,7 @@ const isActive = (config, env) => {
24
24
  };
25
25
 
26
26
  /** @type {import('@gasket/core').HookHandler<'configure'>} */
27
- module.exports = async function configure(gasket, config) {
27
+ module.exports = function configure(gasket, config) {
28
28
  config.elasticAPM = config.elasticAPM || {};
29
29
 
30
30
  // eslint-disable-next-line no-process-env
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
@@ -1,7 +1,7 @@
1
- import type { IncomingMessage, ServerResponse } from 'http';
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';
1
+ import type { IncomingMessage } from 'http';
2
+ import type { AgentConfigOptions, Transaction, Payload, Agent } from 'elastic-apm-node';
3
+ import type { MaybeAsync, GasketConfig, Plugin } from '@gasket/core';
4
+ import type { Request } from 'express';
5
5
 
6
6
  export function filterSensitiveCookies(config: GasketConfig): function(Payload): Payload;
7
7
 
@@ -18,24 +18,25 @@ declare module '@gasket/core' {
18
18
  apm?: Agent;
19
19
  }
20
20
 
21
+ export interface GasketActions {
22
+ async getApmTransaction(
23
+ req: IncomingMessage | Request
24
+ ): Promise<Transaction | void>
25
+ }
26
+
21
27
  export interface HookExecTypes {
22
28
  apmTransaction(
23
29
  transaction: Transaction,
24
- details: {
25
- req: IncomingMessage;
26
- res: ServerResponse & {
27
- locals?: {
28
- gasketData: GasketData & {
29
- locale?: string;
30
- };
31
- };
32
- };
30
+ context: {
31
+ req: IncomingMessage | Request;
33
32
  }
34
33
  ): MaybeAsync<void>;
35
34
  }
36
35
  }
37
36
 
38
- export default {
37
+ const plugin: Plugin = {
39
38
  name: '@gasket/plugin-elastic-apm',
40
39
  hooks: {}
41
40
  };
41
+
42
+ export = plugin;
package/lib/index.js CHANGED
@@ -1,38 +1,34 @@
1
1
  /// <reference types="create-gasket-app" />
2
2
  /// <reference types="@gasket/plugin-metadata" />
3
3
 
4
- const middleware = require('./middleware');
5
- const preboot = require('./preboot');
4
+ const actions = require('./actions');
5
+ const create = require('./create');
6
6
  const configure = require('./configure');
7
- const { devDependencies, name } = require('../package.json');
7
+ const {
8
+ name,
9
+ version,
10
+ description
11
+ } = require('../package.json');
8
12
 
9
13
  /** @type {import('@gasket/core').Plugin} */
10
14
  const plugin = {
11
15
  name,
16
+ version,
17
+ description,
18
+ actions,
12
19
  hooks: {
13
20
  configure,
14
- preboot,
15
- create: {
16
- timing: {
17
- after: ['@gasket/plugin-start']
18
- },
19
- handler(gasket, { pkg, files }) {
20
- const generatorDir = `${__dirname}/../generator`;
21
-
22
- pkg.add('dependencies', {
23
- 'elastic-apm-node': devDependencies['elastic-apm-node']
24
- });
25
- pkg.add('scripts', {
26
- start: 'gasket start --require ./setup.js'
27
- });
28
-
29
- files.add(`${generatorDir}/*`);
30
- }
31
- },
32
- middleware,
21
+ create,
33
22
  metadata(gasket, meta) {
34
23
  return {
35
24
  ...meta,
25
+ actions: [
26
+ {
27
+ name: 'getApmTransaction',
28
+ description: 'Get the APM transaction data',
29
+ link: 'README.md#getApmTransaction'
30
+ }
31
+ ],
36
32
  configurations: [
37
33
  {
38
34
  name: 'elasticAPM',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "7.0.1-canary.11",
3
+ "version": "7.0.1",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -10,12 +10,10 @@
10
10
  "scripts": {
11
11
  "lint": "eslint .",
12
12
  "lint:fix": "npm run lint -- --fix",
13
- "test": "npm run test:runner",
14
- "test:runner": "jest test/*.test.js",
15
- "test:watch": "npm run test:runner -- --watch",
16
- "test:coverage": "npm run test:runner -- --coverage --coverageReporters text",
13
+ "test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
14
+ "test:watch": "npm run test -- --watch",
15
+ "test:coverage": "npm run test -- --coverage",
17
16
  "posttest": "npm run lint && npm run typecheck",
18
- "report": "npm run test:runner -- --coverage --coverageReporters lcov",
19
17
  "typecheck": "tsc",
20
18
  "typecheck:watch": "tsc --watch"
21
19
  },
@@ -33,22 +31,20 @@
33
31
  "plugin"
34
32
  ],
35
33
  "author": "GoDaddy Operating Company, LLC",
36
- "maintainers": [
37
- "Keith Bartholomew <kbartholomew@godaddy.com>"
38
- ],
39
34
  "license": "MIT",
40
35
  "bugs": {
41
36
  "url": "https://github.com/godaddy/gasket/issues"
42
37
  },
43
38
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
44
39
  "devDependencies": {
45
- "@gasket/core": "^7.0.1-canary.11",
40
+ "@gasket/core": "^7.0.1",
41
+ "dotenv": "^16.4.5",
46
42
  "elastic-apm-node": "^4.4.1",
47
43
  "eslint": "^8.56.0",
48
- "eslint-config-godaddy": "^7.1.0",
49
- "eslint-plugin-jest": "^27.6.3",
44
+ "eslint-config-godaddy": "^7.1.1",
45
+ "eslint-plugin-jest": "^28.6.0",
50
46
  "eslint-plugin-json": "^3.1.0",
51
- "eslint-plugin-unicorn": "^44.0.0",
47
+ "eslint-plugin-unicorn": "^55.0.0",
52
48
  "jest": "^29.7.0",
53
49
  "nyc": "^15.1.0",
54
50
  "typescript": "^5.4.5"
@@ -70,5 +66,5 @@
70
66
  "jest": {
71
67
  "testEnvironment": "node"
72
68
  },
73
- "gitHead": "110ae20e5ebd4a3609eb781fc63827bf586483b8"
69
+ "gitHead": "0426b7f57bc2618606955912c0f3cd55174f5367"
74
70
  }
package/lib/middleware.js DELETED
@@ -1,40 +0,0 @@
1
- /// <reference types="@gasket/plugin-express" />
2
-
3
- /**
4
- * Middleware for customizing transactions
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
8
- */
9
- async function customizeTransaction(gasket, req, res) {
10
- const apm = gasket.apm;
11
-
12
- if (!apm?.isStarted()) {
13
- return;
14
- }
15
-
16
- const transaction = apm.currentTransaction;
17
- if (!transaction) {
18
- return;
19
- }
20
-
21
- await gasket.exec('apmTransaction', transaction, { req, res });
22
- }
23
-
24
- /**
25
- * Add middleware to gather config details
26
- * @type {import('@gasket/core').HookHandler<'middleware'>}
27
- */
28
- module.exports = function middleware(gasket) {
29
- return (
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
- }
39
- );
40
- };
package/lib/preboot.js DELETED
@@ -1,29 +0,0 @@
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
- };