File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed
detectors/node/opentelemetry-resource-detector-container Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -57,9 +57,29 @@ export class ContainerDetector implements Detector {
5757 this . UTF8_UNICODE
5858 ) ;
5959 const splitData = rawData . trim ( ) . split ( '\n' ) ;
60- for ( const str of splitData ) {
61- if ( str . length >= this . CONTAINER_ID_LENGTH ) {
62- return str . substring ( str . length - this . CONTAINER_ID_LENGTH ) ;
60+ for ( const line of splitData ) {
61+ const lastSlashIdx = line . lastIndexOf ( '/' ) ;
62+ if ( lastSlashIdx === - 1 ) {
63+ continue ;
64+ }
65+ const lastSection = line . substring ( lastSlashIdx + 1 ) ;
66+ const colonIdx = lastSection . lastIndexOf ( ':' ) ;
67+ if ( colonIdx !== - 1 ) {
68+ // since containerd v1.5.0+, containerId is divided by the last colon when the cgroupDriver is systemd:
69+ // https://github.com/containerd/containerd/blob/release/1.5/pkg/cri/server/helpers_linux.go#L64
70+ return lastSection . substring ( colonIdx + 1 ) ;
71+ } else {
72+ let startIdx = lastSection . lastIndexOf ( '-' ) ;
73+ let endIdx = lastSection . lastIndexOf ( '.' ) ;
74+
75+ startIdx = startIdx === - 1 ? 0 : startIdx + 1 ;
76+ if ( endIdx === - 1 ) {
77+ endIdx = lastSection . length ;
78+ }
79+ if ( startIdx > endIdx ) {
80+ continue ;
81+ }
82+ return lastSection . substring ( startIdx , endIdx ) ;
6383 }
6484 }
6585 return undefined ;
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ import { ContainerDetector } from '../src';
2828describe ( 'ContainerDetector' , ( ) => {
2929 let readStub ;
3030 const correctCgroupV1Data =
31- 'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm' ;
31+ '12:pids:/kubepods.slice/ bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm' ;
3232 const correctCgroupV2Data = `tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
3333 fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
3434 sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse` ;
You can’t perform that action at this time.
0 commit comments