Skip to content

Commit

Permalink
feat(detector-container)!: change implementation to DetectorSync inte…
Browse files Browse the repository at this point in the history
…rface (#2334)
  • Loading branch information
david-luna authored Aug 7, 2024
1 parent 5952127 commit 18a5731
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/resources": "^1.0.0",
"@opentelemetry/resources": "^1.10.0",
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-container#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/
import {
Detector,
DetectorSync,
IResource,
Resource,
ResourceAttributes,
ResourceDetectionConfig,
} from '@opentelemetry/resources';

Expand All @@ -26,7 +28,7 @@ import * as util from 'util';
import { diag } from '@opentelemetry/api';
import { extractContainerIdFromLine } from './utils';

export class ContainerDetector implements Detector {
export class ContainerDetector implements DetectorSync {
readonly CONTAINER_ID_LENGTH = 64;
readonly DEFAULT_CGROUP_V1_PATH = '/proc/self/cgroup';
readonly DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo';
Expand All @@ -40,20 +42,31 @@ export class ContainerDetector implements Detector {

private static readFileAsync = util.promisify(fs.readFile);

async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
detect(_config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
}

/**
* Attempts to obtain the container ID from the file system. If the
* file read is successful it returns a promise containing a {@link ResourceAttributes}
* object with the container ID. Returns a promise containing an
* empty {@link ResourceAttributes} if the paths do not exist or fail
* to read.
*/
async _getAttributes(): Promise<ResourceAttributes> {
try {
const containerId = await this._getContainerId();
return !containerId
? Resource.empty()
: new Resource({
? {}
: {
[SEMRESATTRS_CONTAINER_ID]: containerId,
});
};
} catch (e) {
diag.info(
'Container Detector did not identify running inside a supported container, no container attributes will be added to resource: ',
e
);
return Resource.empty();
return {};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as sinon from 'sinon';
import * as assert from 'assert';
import { Resource } from '@opentelemetry/resources';

import { containerDetector } from '../src';
import {
assertContainerResource,
Expand Down Expand Up @@ -46,7 +46,8 @@ describe('ContainerDetector', () => {
.stub(ContainerDetector, 'readFileAsync' as any)
.resolves(undefined);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();

assert.deepStrictEqual(resource.attributes, {});
assert.ok(resource);
Expand All @@ -57,7 +58,8 @@ describe('ContainerDetector', () => {
.stub(ContainerDetector, 'readFileAsync' as any)
.resolves(correctCgroupV1Data);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(readStub);

Expand All @@ -73,7 +75,8 @@ describe('ContainerDetector', () => {
readStub.onFirstCall().resolves('');
readStub.onSecondCall().resolves(correctCgroupV2Data);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();
sinon.assert.calledTwice(readStub);

assert.ok(resource);
Expand All @@ -88,7 +91,8 @@ describe('ContainerDetector', () => {
readStub.onFirstCall().resolves('');
readStub.onSecondCall().resolves(wrongCgroupV2Data);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();
sinon.assert.calledTwice(readStub);

assert.ok(resource);
Expand All @@ -109,7 +113,8 @@ describe('ContainerDetector', () => {
.stub(ContainerDetector, 'readFileAsync' as any)
.resolves('');

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();
assert.deepStrictEqual(resource.attributes, {});

sinon.assert.calledTwice(readStub);
Expand All @@ -125,7 +130,8 @@ describe('ContainerDetector', () => {
.stub(ContainerDetector, 'readFileAsync' as any)
.rejects(errorMsg.fileNotFoundError);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(readStub);
assertEmptyResource(resource);
Expand All @@ -142,7 +148,8 @@ describe('ContainerDetector', () => {
.stub(ContainerDetector, 'readFileAsync' as any)
.rejects(errorMsg.fileNotFoundError);

const resource: Resource = await containerDetector.detect();
const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();
sinon.assert.calledOnce(readStub);
assertEmptyResource(resource);
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18a5731

Please sign in to comment.