Skip to content

Commit

Permalink
fix: Fixed issue parsing docker container id
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Nov 7, 2024
1 parent 9e98b18 commit 2acc2b0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
24 changes: 21 additions & 3 deletions lib/utilization/docker-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,27 @@ function fetchDockerVendorInfo(agent, callback, logger = log) {
logger.debug(
`${CGROUPS_V2_PATH} not found, trying to parse container id from ${CGROUPS_V1_PATH}`
)
findCGroupsV1(callback)
findCGroupsV1(callback, logger)
return
}

parseCGroupsV2(data, callback)
parseCGroupsV2(
data,
(_, v2Data) => {
if (v2Data !== null) {
// We found a valid Docker identifier in the v2 file, so we are going
// to prioritize it.
return callback(null, v2Data)
}

// For some reason, we have a /proc/self/mountinfo but it does not have
// any Docker information in it (that we have detected). So we will
// fall back to trying the cgroups v1 file.
logger.debug(`Attempting to fall back to cgroups v1 parsing.`)
findCGroupsV1(callback, logger)
},
logger
)
})
}

Expand All @@ -136,6 +152,7 @@ function parseCGroupsV2(data, callback, logger = log) {
const containerLine = new RegExp('/docker/containers/([0-9a-f]{64})/')
const line = containerLine.exec(data)
if (line) {
logger.debug(`Found docker id from cgroups v2: ${line[1]}`)
callback(null, { id: line[1] })
} else {
logger.debug(`Found ${CGROUPS_V2_PATH} but failed to parse Docker container id.`)
Expand Down Expand Up @@ -169,7 +186,8 @@ function findCGroupsV1(callback, logger = log) {
})

if (id) {
vendorInfo = { id: id }
vendorInfo = { id }
logger.debug(`Found docker id from cgroups v1: ${id}`)
callback(null, vendorInfo)
} else {
logger.debug('No matching cpu group found.')
Expand Down
32 changes: 31 additions & 1 deletion test/unit/utilization/docker-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ test.beforeEach(async (ctx) => {
utilCommon.readProc = (path, cb) => {
cb(null, 'docker-1')
}
ctx.nr.utilCommon = utilCommon

const { getBootId } = require('../../../lib/utilization/docker-info')
const { getBootId, getVendorInfo } = require('../../../lib/utilization/docker-info')
ctx.nr.getBootId = getBootId
ctx.nr.getVendorInfo = getVendorInfo

ctx.nr.agent = helper.loadMockedAgent()
ctx.nr.agent.config.utilization = {
Expand Down Expand Up @@ -100,3 +102,31 @@ test('data on success', (t, end) => {
end()
}
})

test('falls back to v1 correctly', (t, end) => {
const { agent, logger, getVendorInfo, utilCommon } = t.nr
let invocation = 0

utilCommon.readProc = (path, callback) => {
if (invocation === 0) {
invocation += 1
return callback(null, 'invalid cgroups v2 file')
}
callback(null, '4:cpu:/docker/f37a7e4d17017e7bf774656b19ca4360c6cdc4951c86700a464101d0d9ce97ee')
}

getVendorInfo(agent, gotInfo, logger)

function gotInfo(error, info) {
assert.ifError(error)
assert.deepStrictEqual(info, {
id: 'f37a7e4d17017e7bf774656b19ca4360c6cdc4951c86700a464101d0d9ce97ee'
})
assert.deepStrictEqual(t.nr.logs, [
'Found /proc/self/mountinfo but failed to parse Docker container id.',
'Attempting to fall back to cgroups v1 parsing.',
'Found docker id from cgroups v1: f37a7e4d17017e7bf774656b19ca4360c6cdc4951c86700a464101d0d9ce97ee'
])
end()
}
})

0 comments on commit 2acc2b0

Please sign in to comment.