-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add detector for container ID in cgroup v1 #515
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6bcd202
feat: copy docker detector over as is
rauno56 679c6a2
feat: make the detector sync
rauno56 7550b16
refactor: rename the detector file
rauno56 1a098cb
Change files
rauno56 8b76b68
fix: use the correct fs api
rauno56 06fa692
style: fix lint
rauno56 a328679
fix: remove reference to Detector
rauno56 b48fae0
fix: lint and build locally for once
rauno56 d5bbb47
fix: turn adding container ID on
rauno56 257ba70
test: test for correct format of the container ID
rauno56 13d6bbc
style: fix lint
rauno56 8ad8636
test: properly test for container id
rauno56 467e3ed
test: test most common formats of cgroup files for v1 detector
rauno56 b319b92
fix: use the implementation from java
rauno56 b75413c
test: add a case from ecs
rauno56 e0ed3c7
test: don't use assert.match since it's not suppored in 12.13
rauno56 542a195
test: check cgroup file in different GHA envs
rauno56 5f08bae
feat: stub fs for reliable tests
rauno56 2e15544
feat: only detect on linux
rauno56 979514c
fix: stub platform to "linux" for the cgroup detector test
rauno56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@splunk-otel-04620656-d9ff-4f33-b1c8-57d9680e41bb.json
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,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: add detector for container ID in cgroup v1", | ||
"packageName": "@splunk/otel", | ||
"email": "rauno56@gmail.com", | ||
"dependentChangeType": "patch" | ||
} |
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,135 @@ | ||
/* | ||
* Copyright Splunk Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
/* This is based on a detector from OTel https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-resources/src/detectors/ | ||
We're copying this code and changing the implementation to a synchronous one from async. This is required for our distribution to not incur ~1 second of overhead | ||
when setting up the tracing pipeline. This is a temporary solution until we can agree upon and implement a solution upstream. | ||
*/ | ||
|
||
/* | ||
* 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 { Resource, ResourceDetectionConfig } from '@opentelemetry/resources'; | ||
|
||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
|
||
import * as fs from 'fs'; | ||
import { platform } from 'os'; | ||
import { diag } from '@opentelemetry/api'; | ||
|
||
const isValidBase16String = (hexString: string) => { | ||
for (let ch = 0; ch < hexString.length; ch++) { | ||
const code = hexString.charCodeAt(ch); | ||
if ( | ||
(48 <= code && code <= 57) || | ||
(97 <= code && code <= 102) || | ||
(65 <= code && code <= 70) | ||
) { | ||
continue; | ||
} | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export class DockerCGroupV1Detector { | ||
public detect(_config?: ResourceDetectionConfig): Resource { | ||
if (platform() !== 'linux') { | ||
diag.debug('Docker CGROUP V1 Detector skipped: Not on linux'); | ||
return Resource.empty(); | ||
} | ||
try { | ||
const containerId = this._getContainerId(); | ||
return !containerId | ||
? Resource.empty() | ||
: new Resource({ | ||
[SemanticResourceAttributes.CONTAINER_ID]: containerId, | ||
}); | ||
} catch (e) { | ||
diag.info( | ||
'Docker CGROUP V1 Detector did not identify running inside a supported docker container, no docker attributes will be added to resource: ', | ||
e | ||
); | ||
return Resource.empty(); | ||
} | ||
} | ||
|
||
protected _getContainerId(): string | null { | ||
try { | ||
const rawData = fs.readFileSync('/proc/self/cgroup', 'utf8').trim(); | ||
return this._parseFile(rawData); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
const errorMessage = e.message; | ||
diag.info( | ||
'Docker CGROUP V1 Detector failed to read the Container ID: ', | ||
errorMessage | ||
); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/* | ||
This is very likely has false positives since it does not check for the ID length, | ||
but is very robust in usually finding the right thing, and if not, finding some | ||
identifier for differentiating between containers. | ||
It also matches Java: https://github.com/open-telemetry/opentelemetry-java/commit/2cb461d4aef16f1ac1c5e67edc2fb41f90ed96a3#diff-ad68bc34d4da31a50709591d4b7735f88c008be7ed1fc325c6367dd9df033452 | ||
*/ | ||
protected _parseFile(contents: string): string | null { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not important / nit, but |
||
if (typeof contents !== 'string') { | ||
return null; | ||
} | ||
for (const line of contents.split('\n')) { | ||
const lastSlashIdx = line.lastIndexOf('/'); | ||
if (lastSlashIdx < 0) { | ||
return null; | ||
} | ||
|
||
const lastSection = line.substring(lastSlashIdx + 1); | ||
let startIdx = lastSection.lastIndexOf('-'); | ||
let endIdx = lastSection.lastIndexOf('.'); | ||
|
||
startIdx = startIdx === -1 ? 0 : startIdx + 1; | ||
if (endIdx === -1) { | ||
endIdx = lastSection.length; | ||
} | ||
if (startIdx > endIdx) { | ||
return null; | ||
} | ||
|
||
const containerId = lastSection.substring(startIdx, endIdx); | ||
if (containerId && isValidBase16String(containerId)) { | ||
return containerId; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export const dockerCGroupV1Detector = new DockerCGroupV1Detector(); |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this mean logging this error on other platforms besides Linux?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, with the current implementation it would. Your suggestion?
How about we detect the platform and only call
_getContainerId()
if it's linux?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the whole detector could check for
process.platform === 'linux'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by "whole". Since it's currently the only platform-specific detector, added it into the detector itself. Once we have more, we could do it in
resource.ts
to optimize.