newrelic 11.20.0 → 11.21.0

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/NEWS.md CHANGED
@@ -1,3 +1,22 @@
1
+ ### v11.21.0 (2024-06-25)
2
+
3
+ #### Features
4
+
5
+ * Added support for getting container ids from ECS metadata API ([#2292](https://github.com/newrelic/node-newrelic/pull/2292)) ([dbca830](https://github.com/newrelic/node-newrelic/commit/dbca830deb6c9420427b60df4875ba71939508c4))
6
+
7
+ #### Bug fixes
8
+
9
+ * Handled assigning headers in LambdaProxyWebRequest when there are no headers present ([#2293](https://github.com/newrelic/node-newrelic/pull/2293)) ([e4d22f3](https://github.com/newrelic/node-newrelic/commit/e4d22f38b70cee061b345d6dbc94a2783a164b76))
10
+
11
+ #### Documentation
12
+
13
+ * Updated compatibility report ([#2290](https://github.com/newrelic/node-newrelic/pull/2290)) ([b5fc893](https://github.com/newrelic/node-newrelic/commit/b5fc8932ed5a0372f8be3f0e53c6d9fa0cf12855))
14
+
15
+ #### Tests
16
+
17
+ * Fixed log error stack message truncating and failing equality test ([#2294](https://github.com/newrelic/node-newrelic/pull/2294)) ([8e06f0f](https://github.com/newrelic/node-newrelic/commit/8e06f0f77762922e7862446bdf85a32eef3f9096))
18
+ * Updated shimmer method in benchmark tests ([#2281](https://github.com/newrelic/node-newrelic/pull/2281)) ([1528d68](https://github.com/newrelic/node-newrelic/commit/1528d685c7fd8e9af23ea91bd66124b82dcdb523))
19
+
1
20
  ### v11.20.0 (2024-06-24)
2
21
 
3
22
  #### Features
@@ -80,7 +80,7 @@ function normalizeHeaders(event, lowerCaseKey = false) {
80
80
  const headers = event.multiValueHeaders ?? event.headers
81
81
 
82
82
  if (!headers) {
83
- return
83
+ return {}
84
84
  }
85
85
 
86
86
  return Object.fromEntries(
@@ -5,52 +5,152 @@
5
5
 
6
6
  'use strict'
7
7
 
8
- const logger = require('../logger').child({ component: 'docker-info' })
8
+ const fs = require('node:fs')
9
+ const http = require('node:http')
10
+ const log = require('../logger').child({ component: 'docker-info' })
9
11
  const common = require('./common')
10
12
  const NAMES = require('../metrics/names')
11
13
  const os = require('os')
12
14
  let vendorInfo = null
15
+
13
16
  const CGROUPS_V1_PATH = '/proc/self/cgroup'
14
17
  const CGROUPS_V2_PATH = '/proc/self/mountinfo'
18
+ const BOOT_ID_PROC_FILE = '/proc/sys/kernel/random/boot_id'
15
19
 
16
20
  module.exports.getVendorInfo = fetchDockerVendorInfo
17
21
  module.exports.clearVendorCache = function clearDockerVendorCache() {
18
22
  vendorInfo = null
19
23
  }
20
24
 
21
- module.exports.getBootId = function getBootId(agent, callback) {
25
+ module.exports.getBootId = function getBootId(agent, callback, logger = log) {
22
26
  if (!/linux/i.test(os.platform())) {
23
27
  logger.debug('Platform is not a flavor of linux, omitting boot info')
24
28
  return setImmediate(callback, null, null)
25
29
  }
26
30
 
27
- common.readProc('/proc/sys/kernel/random/boot_id', function readProcBootId(err, data) {
28
- if (!data) {
29
- bootIdError()
30
- return callback(null, null)
31
+ fs.access(BOOT_ID_PROC_FILE, fs.constants.F_OK, (err) => {
32
+ if (err == null) {
33
+ // The boot id proc file exists, so use it to get the container id.
34
+ return common.readProc(BOOT_ID_PROC_FILE, (_, data, cbAgent = agent) => {
35
+ readProcBootId({ data, agent: cbAgent, callback })
36
+ })
31
37
  }
32
38
 
33
- data = data.trim()
34
- const asciiData = Buffer.from(data, 'ascii').toString()
39
+ logger.debug('Container boot id is not available in cgroups info')
35
40
 
36
- if (data !== asciiData) {
37
- bootIdError()
41
+ if (hasAwsContainerApi() === false) {
42
+ // We don't seem to have a recognized location for getting the container
43
+ // identifier.
44
+ logger.debug('Container is not in a recognized ECS container, omitting boot info')
45
+ recordBootIdError(agent)
38
46
  return callback(null, null)
39
47
  }
40
48
 
41
- if (data.length !== 36) {
42
- bootIdError()
43
- if (data.length > 128) {
44
- data = data.substring(0, 128)
49
+ getEcsContainerId({ agent, callback, logger })
50
+ })
51
+ }
52
+
53
+ /**
54
+ * Queries the AWS ECS metadata API to get the boot id.
55
+ *
56
+ * @param {object} params Function parameters.
57
+ * @param {object} params.agent Newrelic agent instance.
58
+ * @param {Function} params.callback Typical error first callback. The second
59
+ * parameter is the boot id as a string.
60
+ * @param {object} [params.logger] Internal logger instance.
61
+ */
62
+ function getEcsContainerId({ agent, callback, logger }) {
63
+ const ecsApiUrl =
64
+ process.env.ECS_CONTAINER_METADATA_URI_V4 || process.env.ECS_CONTAINER_METADATA_URI
65
+ const req = http.request(ecsApiUrl, (res) => {
66
+ let body = Buffer.alloc(0)
67
+ res.on('data', (chunk) => {
68
+ body = Buffer.concat([body, chunk])
69
+ })
70
+ res.on('end', () => {
71
+ try {
72
+ const json = body.toString('utf8')
73
+ const data = JSON.parse(json)
74
+ if (data.DockerId == null) {
75
+ logger.debug('Failed to find DockerId in response, omitting boot info')
76
+ recordBootIdError(agent)
77
+ return callback(null, null)
78
+ }
79
+ callback(null, data.DockerId)
80
+ } catch (error) {
81
+ logger.debug('Failed to process ECS API response, omitting boot info: ' + error.message)
82
+ recordBootIdError(agent)
83
+ callback(null, null)
45
84
  }
46
- }
85
+ })
86
+ })
47
87
 
48
- return callback(null, data)
88
+ req.on('error', () => {
89
+ logger.debug('Failed to query ECS endpoint, omitting boot info')
90
+ recordBootIdError(agent)
91
+ callback(null, null)
49
92
  })
50
93
 
51
- function bootIdError() {
52
- agent.metrics.getOrCreateMetric(NAMES.UTILIZATION.BOOT_ID_ERROR).incrementCallCount()
94
+ req.end()
95
+ }
96
+
97
+ /**
98
+ * Inspects the running environment to determine if the AWS ECS metadata API
99
+ * is available.
100
+ *
101
+ * @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ec2-metadata.html
102
+ *
103
+ * @returns {boolean}
104
+ */
105
+ function hasAwsContainerApi() {
106
+ if (process.env.ECS_CONTAINER_METADATA_URI_V4 != null) {
107
+ return true
53
108
  }
109
+ return process.env.ECS_CONTAINER_METADATA_URI != null
110
+ }
111
+
112
+ /**
113
+ * Increments a supportability metric to indicate that there was an error
114
+ * while trying to read the boot id from the system.
115
+ *
116
+ * @param {object} agent Newrelic agent instance.
117
+ */
118
+ function recordBootIdError(agent) {
119
+ agent.metrics.getOrCreateMetric(NAMES.UTILIZATION.BOOT_ID_ERROR).incrementCallCount()
120
+ }
121
+
122
+ /**
123
+ * Utility function to parse a Docker boot id from a cgroup proc file.
124
+ *
125
+ * @param {Buffer} data The information from the proc file.
126
+ * @param {object} agent Newrelic agent instance.
127
+ * @param {Function} callback Typical error first callback. Second parameter
128
+ * is the boot id as a string.
129
+ *
130
+ * @returns {*}
131
+ */
132
+ function readProcBootId({ data, agent, callback }) {
133
+ if (!data) {
134
+ recordBootIdError(agent)
135
+ return callback(null, null)
136
+ }
137
+
138
+ data = data.trim()
139
+ const asciiData = Buffer.from(data, 'ascii').toString()
140
+
141
+ if (data !== asciiData) {
142
+ recordBootIdError(agent)
143
+ return callback(null, null)
144
+ }
145
+
146
+ if (data.length !== 36) {
147
+ recordBootIdError(agent)
148
+ if (data.length > 128) {
149
+ data = data.substring(0, 128)
150
+ }
151
+ }
152
+
153
+ return callback(null, data)
54
154
  }
55
155
 
56
156
  /**
@@ -58,8 +158,9 @@ module.exports.getBootId = function getBootId(agent, callback) {
58
158
  *
59
159
  * @param {object} agent NR instance
60
160
  * @param {Function} callback function to call when done
161
+ * @param {object} [logger] internal logger instance
61
162
  */
62
- function fetchDockerVendorInfo(agent, callback) {
163
+ function fetchDockerVendorInfo(agent, callback, logger = log) {
63
164
  if (!agent.config.utilization || !agent.config.utilization.detect_docker) {
64
165
  return callback(null, null)
65
166
  }
@@ -93,8 +194,9 @@ function fetchDockerVendorInfo(agent, callback) {
93
194
  *
94
195
  * @param {string} data file contents
95
196
  * @param {Function} callback function to call when done
197
+ * @param {object} [logger] internal logger instance
96
198
  */
97
- function parseCGroupsV2(data, callback) {
199
+ function parseCGroupsV2(data, callback, logger = log) {
98
200
  const containerLine = new RegExp('/docker/containers/([0-9a-f]{64})/')
99
201
  const line = containerLine.exec(data)
100
202
  if (line) {
@@ -110,8 +212,9 @@ function parseCGroupsV2(data, callback) {
110
212
  * e.g. - `4:cpu:/docker/f37a7e4d17017e7bf774656b19ca4360c6cdc4951c86700a464101d0d9ce97ee`
111
213
  *
112
214
  * @param {Function} callback function to call when done
215
+ * @param {object} [logger] internal logger instance
113
216
  */
114
- function findCGroupsV1(callback) {
217
+ function findCGroupsV1(callback, logger = log) {
115
218
  common.readProc(CGROUPS_V1_PATH, function getCGroup(err, data) {
116
219
  if (!data) {
117
220
  logger.debug(`${CGROUPS_V1_PATH} not found, exiting parsing containerId.`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "newrelic",
3
- "version": "11.20.0",
3
+ "version": "11.21.0",
4
4
  "author": "New Relic Node.js agent team <nodejs@newrelic.com>",
5
5
  "license": "Apache-2.0",
6
6
  "contributors": [