Skip to content
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(detector-container)!: change implementation to DetectorSync interface #2334

Merged
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 { 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 @@

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 {};

Check warning on line 69 in detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts

View check run for this annotation

Codecov / codecov/patch

detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts#L69

Added line #L69 was not covered by tests
}
}

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.