@gasket/plugin-elastic-apm 6.28.0 → 6.34.2

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
@@ -99,6 +99,25 @@ According to the [Elastic APM docs], the _Elastic APM agent for Node.js is a
99
99
  singleton_. This means that you can require and configure singleton in various
100
100
  hooks of your Gasket app, such as with the [init] or [middleware] lifecycles.
101
101
 
102
+ ## Lifecycles
103
+
104
+ ### apmTransaction
105
+
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:
107
+
108
+ | Property | Description |
109
+ |----------|-------------|
110
+ | `req` | The HTTP request or framework-specific wrapper around it |
111
+ | `res` | The HTTP response or framework-specific wrapper around it |
112
+
113
+ ```javascript
114
+ // /lifecycles/apm-transaction.js
115
+
116
+ module.exports = (gasket, transaction, { req, res }) => {
117
+ transaction.setLabel('language', req.headers['accept-language']);
118
+ }
119
+ ```
120
+
102
121
  ## How it works
103
122
 
104
123
  This plugin hooks the Gasket [preboot] lifecycle from [@gasket/plugin-start]
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { AgentConfigOptions } from 'elastic-apm-node';
1
+ import type { IncomingMessage, ServerResponse } from 'http';
2
+ import type { AgentConfigOptions, Transaction } from 'elastic-apm-node';
2
3
 
3
4
  declare module '@gasket/engine' {
4
5
  export interface GasketConfig {
@@ -6,4 +7,14 @@ declare module '@gasket/engine' {
6
7
  sensitiveCookies?: Array<string>
7
8
  },
8
9
  }
10
+
11
+ export interface HookExecTypes {
12
+ apmTransaction(
13
+ transaction: Transaction,
14
+ details: {
15
+ req: IncomingMessage,
16
+ res: ServerResponse
17
+ }
18
+ ): MaybeAsync<void>
19
+ }
9
20
  }
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { filterSensitiveCookies } = require('./cookies');
2
+ const middleware = require('./middleware');
2
3
  const { dependencies } = require('../package.json');
3
4
 
4
5
  const isDefined = o => typeof o !== 'undefined';
@@ -83,6 +84,7 @@ module.exports = {
83
84
  });
84
85
  }
85
86
  },
87
+ middleware,
86
88
  metadata(gasket, meta) {
87
89
  return {
88
90
  ...meta,
@@ -97,6 +99,13 @@ module.exports = {
97
99
  description: 'List of sensitive cookies to filter',
98
100
  type: 'string[]',
99
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'
100
109
  }]
101
110
  };
102
111
  }
@@ -0,0 +1,33 @@
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');
13
+ const apm = require('elastic-apm-node');
14
+
15
+ /**
16
+ * Middleware for customizing transactions
17
+ *
18
+ * @param {Gasket} gasket The Gasket engine
19
+ * @param {Request} req The HTTP request being handled
20
+ * @param {Response} res The server response
21
+ */
22
+ async function customizeTransaction(gasket, req, res) {
23
+ const transaction = apm.currentTransaction;
24
+ if (!transaction) {
25
+ return;
26
+ }
27
+
28
+ await gasket.exec('apmTransaction', transaction, { req, res });
29
+ }
30
+
31
+ module.exports = (gasket) => [
32
+ callbackify(async (req, res) => customizeTransaction(gasket, req, res))
33
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "6.28.0",
3
+ "version": "6.34.2",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib",
6
6
  "types": "lib/index.d.ts",
@@ -43,10 +43,10 @@
43
43
  "elastic-apm-node": "^3.26.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@gasket/engine": "^6.26.1",
46
+ "@gasket/engine": "^6.34.2",
47
47
  "eslint": "^8.7.0",
48
48
  "eslint-config-godaddy": "^6.0.0",
49
- "eslint-plugin-jest": "^25.7.0",
49
+ "eslint-plugin-jest": "^27.0.0",
50
50
  "eslint-plugin-json": "^3.1.0",
51
51
  "eslint-plugin-mocha": "^10.0.3",
52
52
  "eslint-plugin-unicorn": "^40.1.0",
@@ -68,5 +68,5 @@
68
68
  "jest": {
69
69
  "testEnvironment": "node"
70
70
  },
71
- "gitHead": "b63c2abff1b9c8c2a47794f1439be00cc2243749"
71
+ "gitHead": "a92fc252c4bed8fe16980bdc9c5389ecf0a0f4d9"
72
72
  }