@gasket/plugin-elastic-apm 7.0.0-next.44 → 7.0.0-next.45

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/lib/actions.js ADDED
@@ -0,0 +1,21 @@
1
+ /** @type {import('@gasket/core').HookHandler<'actions'>} */
2
+ module.exports = function actions(gasket) {
3
+ return {
4
+ async getApmTransaction(req) {
5
+ const apm = require('elastic-apm-node');
6
+
7
+ if (!apm?.isStarted()) {
8
+ return;
9
+ }
10
+
11
+ const transaction = apm.currentTransaction;
12
+ if (!transaction) {
13
+ return;
14
+ }
15
+
16
+ await gasket.exec('apmTransaction', transaction, { req });
17
+
18
+ return transaction;
19
+ }
20
+ };
21
+ };
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
@@ -2,6 +2,8 @@ import type { IncomingMessage, ServerResponse } from 'http';
2
2
  import type { AgentConfigOptions, Transaction, Payload } from 'elastic-apm-node';
3
3
  import type { MaybeAsync, GasketConfig } from '@gasket/core';
4
4
  import type { GasketData } from '@gasket/data';
5
+ import type { GasketRequest } from '@gasket/core';
6
+ import type { Request } from 'express';
5
7
 
6
8
  export function filterSensitiveCookies(config: GasketConfig): function(Payload): Payload;
7
9
 
@@ -18,18 +20,17 @@ declare module '@gasket/core' {
18
20
  apm?: Agent;
19
21
  }
20
22
 
23
+ export interface GasketActions {
24
+ async getApmTransaction(
25
+ req: IncomingMessage | Request
26
+ ): Promise<Transaction | void>
27
+ }
28
+
21
29
  export interface HookExecTypes {
22
30
  apmTransaction(
23
31
  transaction: Transaction,
24
- details: {
25
- req: IncomingMessage;
26
- res: ServerResponse & {
27
- locals?: {
28
- gasketData: GasketData & {
29
- locale?: string;
30
- };
31
- };
32
- };
32
+ context: {
33
+ req: IncomingMessage | Request;
33
34
  }
34
35
  ): MaybeAsync<void>;
35
36
  }
package/lib/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  /// <reference types="create-gasket-app" />
2
2
  /// <reference types="@gasket/plugin-metadata" />
3
3
 
4
+ const actions = require('./actions');
5
+ const create = require('./create');
4
6
  const middleware = require('./middleware');
5
- const preboot = require('./preboot');
6
7
  const configure = require('./configure');
7
8
  const {
8
9
  name,
9
10
  version,
10
- description,
11
- devDependencies
11
+ description
12
12
  } = require('../package.json');
13
13
 
14
14
  /** @type {import('@gasket/core').Plugin} */
@@ -17,22 +17,9 @@ const plugin = {
17
17
  version,
18
18
  description,
19
19
  hooks: {
20
+ actions,
20
21
  configure,
21
- // @ts-expect-error - TODO: will be cleaned up in the apm tune up ticket
22
- // https://godaddy-corp.atlassian.net/browse/PFX-628
23
- preboot,
24
- create(gasket, { pkg, files }) {
25
- const generatorDir = `${__dirname}/../generator`;
26
-
27
- pkg.add('dependencies', {
28
- 'elastic-apm-node': devDependencies['elastic-apm-node']
29
- });
30
- pkg.add('scripts', {
31
- start: 'gasket start --require ./setup.js'
32
- });
33
-
34
- files.add(`${generatorDir}/*`);
35
- },
22
+ create,
36
23
  middleware,
37
24
  metadata(gasket, meta) {
38
25
  return {
package/lib/middleware.js CHANGED
@@ -4,11 +4,9 @@
4
4
  * Middleware for customizing transactions
5
5
  * @param {import('@gasket/core').Gasket} gasket - The Gasket engine
6
6
  * @param {import('http').IncomingMessage} req - The HTTP request being handled
7
- * @param {import('http').ServerResponse} res - The server response
8
7
  */
9
- async function customizeTransaction(gasket, req, res) {
10
- const apm = gasket.apm;
11
-
8
+ async function customizeTransaction(gasket, req) {
9
+ const apm = require('elastic-apm-node');
12
10
  if (!apm?.isStarted()) {
13
11
  return;
14
12
  }
@@ -18,7 +16,7 @@ async function customizeTransaction(gasket, req, res) {
18
16
  return;
19
17
  }
20
18
 
21
- await gasket.exec('apmTransaction', transaction, { req, res });
19
+ await gasket.exec('apmTransaction', transaction, { req });
22
20
  }
23
21
 
24
22
  /**
@@ -26,11 +24,14 @@ async function customizeTransaction(gasket, req, res) {
26
24
  * @type {import('@gasket/core').HookHandler<'middleware'>}
27
25
  */
28
26
  module.exports = function middleware(gasket) {
27
+ const apm = require('elastic-apm-node');
28
+ // eslint-disable-next-line no-undefined
29
+ const isStarted = apm.isStarted() || undefined;
29
30
  return (
30
- gasket.apm &&
31
+ isStarted &&
31
32
  async function apmTransactionMiddleware(req, res, next) {
32
33
  try {
33
- customizeTransaction(gasket, req, res);
34
+ customizeTransaction(gasket, req);
34
35
  } catch (error) {
35
36
  return next(error);
36
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "7.0.0-next.44",
3
+ "version": "7.0.0-next.45",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
44
44
  "devDependencies": {
45
- "@gasket/core": "^7.0.0-next.44",
45
+ "@gasket/core": "^7.0.0-next.45",
46
+ "dotenv": "^16.4.5",
46
47
  "elastic-apm-node": "^4.4.1",
47
48
  "eslint": "^8.56.0",
48
49
  "eslint-config-godaddy": "^7.1.0",
@@ -70,5 +71,5 @@
70
71
  "jest": {
71
72
  "testEnvironment": "node"
72
73
  },
73
- "gitHead": "3a1d51a5e98ce4bad06faa2fceae90c580c8a0a9"
74
+ "gitHead": "cca9f460bdf2859d000443b25a76927251560f48"
74
75
  }
package/lib/preboot.js DELETED
@@ -1,30 +0,0 @@
1
- /// <reference types="@gasket/plugin-logger" />
2
-
3
- const { filterSensitiveCookies } = require('./cookies');
4
-
5
- // @ts-expect-error - TODO: will be cleaned up in the apm tune up ticket
6
- // https://godaddy-corp.atlassian.net/browse/PFX-628
7
- /** @type {import('@gasket/core').HookHandler<'preboot'>} */
8
- module.exports = async function preboot(gasket) {
9
- const { config, logger, command } = gasket;
10
-
11
- if (command && command.id === 'local') return;
12
-
13
- /**
14
- * @type {import('elastic-apm-node')}
15
- * Note: Prefer app-level dependency in case of duplicates
16
- */
17
- const apm = require(require.resolve('elastic-apm-node', {
18
- paths: [config.root, __dirname]
19
- }));
20
-
21
- if (!apm.isStarted()) {
22
- logger.warn(
23
- 'Elastic APM agent is not started. Use `--require ./setup.js`'
24
- );
25
- }
26
-
27
- apm.addFilter(filterSensitiveCookies(config));
28
-
29
- gasket.apm = apm;
30
- };