@gasket/plugin-elastic-apm 7.4.8 → 7.4.9

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.
Files changed (2) hide show
  1. package/EXAMPLES.md +133 -0
  2. package/package.json +7 -6
package/EXAMPLES.md ADDED
@@ -0,0 +1,133 @@
1
+ # Examples
2
+
3
+ This document provides working examples for all exported functions and lifecycle hooks from `@gasket/plugin-elastic-apm`.
4
+
5
+ ## Plugin Configuration
6
+
7
+ ### Basic Configuration
8
+
9
+ ```js
10
+ // gasket.js
11
+ import { makeGasket } from '@gasket/core';
12
+ import pluginElasticApm from '@gasket/plugin-elastic-apm';
13
+
14
+ export default makeGasket({
15
+ plugins: [
16
+ pluginElasticApm
17
+ ],
18
+ elasticAPM: {
19
+ sensitiveCookies: ['sessionId', 'authToken']
20
+ }
21
+ });
22
+ ```
23
+
24
+ ### Environment Variable Configuration
25
+
26
+ ```js
27
+ // setup.js
28
+ import 'dotenv/config';
29
+ import apm from 'elastic-apm-node';
30
+
31
+ apm.start({
32
+ serviceName: 'my-service',
33
+ secretToken: process.env.ELASTIC_APM_SECRET_TOKEN,
34
+ serverUrl: process.env.ELASTIC_APM_SERVER_URL
35
+ });
36
+ ```
37
+
38
+ ## Actions
39
+
40
+ ### getApmTransaction
41
+
42
+ Access and decorate the current APM transaction:
43
+
44
+ ```js
45
+ // In a middleware or lifecycle
46
+ export default {
47
+ name: 'my-plugin',
48
+ hooks: {
49
+ middleware(gasket, app) {
50
+ app.use(async (req, res, next) => {
51
+ const transaction = await gasket.actions.getApmTransaction(req);
52
+
53
+ if (transaction) {
54
+ transaction.setLabel('userId', req.user?.id);
55
+ transaction.setLabel('requestId', req.headers['x-request-id']);
56
+ }
57
+
58
+ next();
59
+ });
60
+ }
61
+ }
62
+ };
63
+ ```
64
+
65
+ ```js
66
+ // In Next.js API route
67
+ // pages/api/users.js
68
+ import gasket from '../../gasket.js';
69
+
70
+ export default async function handler(req, res) {
71
+ const transaction = await gasket.actions.getApmTransaction(req);
72
+
73
+ if (transaction) {
74
+ transaction.setLabel('endpoint', '/api/users');
75
+ }
76
+
77
+ // Your API logic here
78
+ res.json({ users: [] });
79
+ }
80
+ ```
81
+
82
+ ## Lifecycle Hooks
83
+
84
+ ### apmTransaction
85
+
86
+ Customize APM transaction with additional context:
87
+
88
+ ```js
89
+ // In a plugin
90
+ export default {
91
+ name: 'apm-customization-plugin',
92
+ hooks: {
93
+ apmTransaction(gasket, transaction, { req }) {
94
+ // Add custom labels
95
+ transaction.setLabel('userAgent', req.headers['user-agent']);
96
+ transaction.setLabel('locale', req.headers['accept-language']);
97
+
98
+ // Set custom name based on route
99
+ if (req.route?.path) {
100
+ transaction.name = `${req.method} ${req.route.path}`;
101
+ }
102
+
103
+ // Add user context
104
+ if (req.user) {
105
+ transaction.setUser({
106
+ id: req.user.id,
107
+ email: req.user.email
108
+ });
109
+ }
110
+ }
111
+ }
112
+ };
113
+ ```
114
+
115
+ ```js
116
+ // Conditional APM decoration
117
+ export default {
118
+ name: 'conditional-apm-plugin',
119
+ hooks: {
120
+ apmTransaction(gasket, transaction, { req }) {
121
+ // Only add expensive labels for non-health check requests
122
+ if (!req.path.startsWith('/health')) {
123
+ transaction.setLabel('expensive-operation', true);
124
+
125
+ // Add custom data based on request
126
+ if (req.path.startsWith('/api/')) {
127
+ transaction.setLabel('api-version', req.headers['api-version'] || 'v1');
128
+ }
129
+ }
130
+ }
131
+ }
132
+ };
133
+ ```
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@gasket/plugin-elastic-apm",
3
- "version": "7.4.8",
3
+ "version": "7.4.9",
4
4
  "description": "Adds Elastic APM instrumentation to your application",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
8
- "lib"
8
+ "lib",
9
+ "EXAMPLES.md"
9
10
  ],
10
11
  "repository": "godaddy/gasket.git",
11
12
  "publishConfig": {
@@ -22,16 +23,16 @@
22
23
  "bugs": "https://github.com/godaddy/gasket/issues",
23
24
  "homepage": "https://github.com/godaddy/gasket/tree/main/packages/gasket-plugin-elastic-apm",
24
25
  "dependencies": {
25
- "@gasket/request": "^7.5.3"
26
+ "@gasket/request": "^7.5.4"
26
27
  },
27
28
  "devDependencies": {
28
29
  "dotenv": "^16.4.7",
29
30
  "elastic-apm-node": "^4.11.0",
30
31
  "jest": "^29.7.0",
31
32
  "nyc": "^15.1.0",
32
- "@gasket/core": "^7.6.2",
33
- "@gasket/plugin-metadata": "^7.5.3",
34
- "create-gasket-app": "^7.4.12"
33
+ "@gasket/core": "^7.6.3",
34
+ "@gasket/plugin-metadata": "^7.5.4",
35
+ "create-gasket-app": "^7.4.13"
35
36
  },
36
37
  "jest": {
37
38
  "testEnvironment": "node"