Skip to content

Commit 86814b8

Browse files
committed
fix common.getKeys logic
1 parent 9794ced commit 86814b8

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

lib/utilization/common.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ exports.getKeys = function getKeys(data, keys, allValuesMustBeValid = true) {
3838
const key = keys[i]
3939
if (!properties.hasOwn(data, key) || !data[key]) {
4040
logger.debug('Key %s missing from metadata', key)
41-
return null
41+
if (allValuesMustBeValid) {
42+
return null
43+
}
44+
continue
4245
}
46+
4347
let value = data[key]
4448
if (typeof value === 'number') {
4549
value = value.toString()
@@ -48,10 +52,11 @@ exports.getKeys = function getKeys(data, keys, allValuesMustBeValid = true) {
4852
if (!checkValueString(value)) {
4953
logger.debug('Invalid metadata value found: %s -> %s', key, value)
5054
if (allValuesMustBeValid) {
51-
// If any value is invalid, the whole thing must be trashed.
5255
return null
53-
} else continue
56+
}
57+
continue
5458
}
59+
5560
results[key] = value
5661
}
5762

lib/utilization/gcp-info.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,11 @@ function fetchGCPInfo(agent, callback) {
5050
data = null
5151
}
5252

53-
// Determine if we are in GCP Cloud Run by checking K_SERVICE.
54-
// If K_SERVICE is not set yet, fallback to checking for the presence of machineType and name.
55-
// If both are missing, assume Cloud Run (need only id and zone).
56-
console.debug('process.env.K_SERVICE: ', process.env.K_SERVICE)
57-
const needAllKeys = process.env.K_SERVICE
58-
? false
59-
: !!(data.machineType && data.name)
60-
console.debug('needAllKeys: ', needAllKeys)
61-
53+
// Most GCP services have id, zone, machineType, and name,
54+
// but GCP Cloud Run only has id and zone.
55+
// If K_SERVICE is set, we are in GCP Cloud Run.
56+
const needAllKeys = !process.env.K_SERVICE
6257
const results = common.getKeys(data, ['id', 'zone', 'machineType', 'name'], needAllKeys)
63-
console.debug('results: ', results)
6458
if (!results?.id || !results?.zone) {
6559
// id and zone are required
6660
logger.debug({ utilization: 'gcp' }, 'GCP metadata was invalid.')

0 commit comments

Comments
 (0)