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 {
57
57
this . UTF8_UNICODE
58
58
) ;
59
59
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 ) ;
63
83
}
64
84
}
65
85
return undefined ;
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ import { ContainerDetector } from '../src';
28
28
describe ( 'ContainerDetector' , ( ) => {
29
29
let readStub ;
30
30
const correctCgroupV1Data =
31
- 'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm' ;
31
+ '12:pids:/kubepods.slice/ bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm' ;
32
32
const correctCgroupV2Data = `tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
33
33
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
34
34
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse` ;
You can’t perform that action at this time.
0 commit comments