-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added prefix to look for containerid (#2341)
* feature: added prefix to look for containerid * fix:added seperate utils file and new test file and more tests * fix:test refactor --------- Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
- Loading branch information
Showing
4 changed files
with
192 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
detectors/node/opentelemetry-resource-detector-container/src/detectors/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export const CONTAINER_ID_LENGTH = 64; | ||
export const DEFAULT_CGROUP_V1_PATH = '/proc/self/cgroup'; | ||
export const DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo'; | ||
export const UTF8_UNICODE = 'utf8'; | ||
export const HOSTNAME = 'hostname'; | ||
export const MARKING_PREFIX = 'containers'; | ||
export const CRIO = 'crio-'; | ||
export const CRI_CONTAINERD = 'cri-containerd-'; | ||
export const DOCKER = 'docker-'; | ||
export const HEX_STRING_REGEX = /^[a-f0-9]+$/i; | ||
|
||
export function truncatePrefix(lastSection: string, prefix: string): string { | ||
return lastSection.substring(prefix.length); | ||
} | ||
|
||
export function extractContainerIdFromLine(line: string): string | undefined { | ||
if (!line) { | ||
return undefined; | ||
} | ||
const sections = line.split('/'); | ||
if (sections.length <= 1) { | ||
return undefined; | ||
} | ||
let lastSection = sections[sections.length - 1]; | ||
|
||
// Handle containerd v1.5.0+ format with systemd cgroup driver | ||
const colonIndex = lastSection.lastIndexOf(':'); | ||
if (colonIndex !== -1) { | ||
lastSection = lastSection.substring(colonIndex + 1); | ||
} | ||
|
||
// Truncate known prefixes from the last section | ||
if (lastSection.startsWith(CRIO)) { | ||
lastSection = truncatePrefix(lastSection, CRIO); | ||
} else if (lastSection.startsWith(DOCKER)) { | ||
lastSection = truncatePrefix(lastSection, DOCKER); | ||
} else if (lastSection.startsWith(CRI_CONTAINERD)) { | ||
lastSection = truncatePrefix(lastSection, CRI_CONTAINERD); | ||
} | ||
// Remove anything after the first period | ||
if (lastSection.includes('.')) { | ||
lastSection = lastSection.split('.')[0]; | ||
} | ||
// Check if the remaining string is a valid hex string | ||
if (HEX_STRING_REGEX.test(lastSection)) { | ||
return lastSection; | ||
} | ||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
detectors/node/opentelemetry-resource-detector-container/test/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { extractContainerIdFromLine } from '../src/detectors/utils'; | ||
|
||
describe(' extractContainerId from line tests', () => { | ||
it('should extract container ID from crio-prefixed line', () => { | ||
const line = | ||
'11:devices:/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod5c5979ec_6b2b_11e9_a923_42010a800002.slice/crio-1234567890abcdef.scope'; | ||
const expected = '1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
|
||
it('should extract container ID from docker-prefixed line', () => { | ||
const line = | ||
'11:devices:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
const expected = | ||
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
|
||
it('should extract container ID from cri-containerd-prefixed line', () => { | ||
const line = | ||
'11:devices:/kubepods/burstable/pod2c4b2241-5c01-11e9-8e4e-42010a800002/cri-containerd-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
const expected = | ||
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
|
||
it('should handle containerd v1.5.0+ format with systemd cgroup driver', () => { | ||
const line = | ||
'0::/system.slice/containerd.service/kubepods-burstable-pod2c4b2241-5c01-11e9-8e4e-42010a800002.slice:cri-containerd:1234567890abcdef'; | ||
const expected = '1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
|
||
it('should return undefined for invalid container ID', () => { | ||
const line = | ||
'11:devices:/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod5c5979ec_6b2b_11e9_a923_42010a800002.slice/invalid-id.scope'; | ||
assert.strictEqual(extractContainerIdFromLine(line), undefined); | ||
}); | ||
|
||
it('should return undefined for empty line', () => { | ||
const line = ''; | ||
assert.strictEqual(extractContainerIdFromLine(line), undefined); | ||
}); | ||
|
||
it('should return undefined for line without container ID', () => { | ||
const line = '11:devices:/'; | ||
assert.strictEqual(extractContainerIdFromLine(line), undefined); | ||
}); | ||
|
||
// Additional test cases | ||
it('should handle line with multiple colons', () => { | ||
const line = | ||
'0::/system.slice/containerd.service/kubepods-burstable-pod2c4b2241-5c01-11e9-8e4e-42010a800002.slice:cri-containerd-1234567890abcdef.extra'; | ||
const expected = '1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
|
||
it('should return containerid for valid hex string with any length', () => { | ||
const line = | ||
'11:devices:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcde'; | ||
assert.strictEqual( | ||
extractContainerIdFromLine(line), | ||
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcde' | ||
); | ||
}); | ||
|
||
it('should extract container ID with additional suffix', () => { | ||
const line = | ||
'11:devices:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef.suffix'; | ||
const expected = | ||
'1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; | ||
assert.strictEqual(extractContainerIdFromLine(line), expected); | ||
}); | ||
}); |