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-aws): add DetectorSync implementation for beanstalk detector #2330

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

import { diag } from '@opentelemetry/api';
import {
Detector,
DetectorSync,
IResource,
Resource,
ResourceAttributes,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import {
Expand Down Expand Up @@ -47,7 +49,7 @@ const DEFAULT_BEANSTALK_CONF_PATH =
const WIN_OS_BEANSTALK_CONF_PATH =
'C:\\Program Files\\Amazon\\XRay\\environment.conf';

export class AwsBeanstalkDetector implements Detector {
export class AwsBeanstalkDetector implements DetectorSync {
BEANSTALK_CONF_PATH: string;
private static readFileAsync = util.promisify(fs.readFile);
private static fileAccessAsync = util.promisify(fs.access);
Expand All @@ -60,7 +62,21 @@ export class AwsBeanstalkDetector implements Detector {
}
}

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

/**
* Attempts to obtain AWS Beanstalk configuration from the file
* system. If file is accesible and read succesfully it returns
* a promise containing a {@link ResourceAttributes}
* object with instance metadata. Returns a promise containing an
* empty {@link ResourceAttributes} if the file is not accesible or
* fails in the reading process.
*/
async _getAttributes(
_config?: ResourceDetectionConfig
): Promise<ResourceAttributes> {
try {
await AwsBeanstalkDetector.fileAccessAsync(
this.BEANSTALK_CONF_PATH,
Expand All @@ -73,17 +89,17 @@ export class AwsBeanstalkDetector implements Detector {
);
const parsedData = JSON.parse(rawData);

return new Resource({
return {
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
[SEMRESATTRS_SERVICE_NAME]: CLOUDPLATFORMVALUES_AWS_ELASTIC_BEANSTALK,
[SEMRESATTRS_SERVICE_NAMESPACE]: parsedData.environment_name,
[SEMRESATTRS_SERVICE_VERSION]: parsedData.version_label,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: parsedData.deployment_id,
});
};
} catch (e: any) {
diag.debug(`AwsBeanstalkDetector failed: ${e.message}`);
return Resource.empty();
return {};
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe('BeanstalkResourceDetector', () => {
.resolves(JSON.stringify(data));
sinon.stub(JSON, 'parse').returns(data);

const resource = await awsBeanstalkDetector.detect();
const resource = awsBeanstalkDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(fileStub);
sinon.assert.calledOnce(readStub);
Expand All @@ -74,7 +75,8 @@ describe('BeanstalkResourceDetector', () => {
.resolves(JSON.stringify(noisyData));
sinon.stub(JSON, 'parse').returns(noisyData);

const resource = await awsBeanstalkDetector.detect();
const resource = awsBeanstalkDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(fileStub);
sinon.assert.calledOnce(readStub);
Expand All @@ -95,7 +97,8 @@ describe('BeanstalkResourceDetector', () => {
.stub(AwsBeanstalkDetector, 'readFileAsync' as any)
.rejects(err);

const resource = await awsBeanstalkDetector.detect();
const resource = awsBeanstalkDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(fileStub);
sinon.assert.calledOnce(readStub);
Expand All @@ -111,7 +114,8 @@ describe('BeanstalkResourceDetector', () => {
.stub(AwsBeanstalkDetector, 'readFileAsync' as any)
.resolves(JSON.stringify(data));

const resource = await awsBeanstalkDetector.detect();
const resource = awsBeanstalkDetector.detect();
await resource.waitForAsyncAttributes?.();

sinon.assert.calledOnce(fileStub);
sinon.assert.notCalled(readStub);
Expand Down
2 changes: 1 addition & 1 deletion metapackages/auto-instrumentations-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function getDisabledInstrumentationsFromEnv() {
export function getResourceDetectorsFromEnv(): Array<Detector | DetectorSync> {
const resourceDetectors = new Map<
string,
Detector | DetectorSync | Detector[] | DetectorSync[]
Detector | DetectorSync | Detector[] | DetectorSync[] | Array<Detector | DetectorSync>
david-luna marked this conversation as resolved.
Show resolved Hide resolved
>([
[RESOURCE_DETECTOR_CONTAINER, containerDetector],
[RESOURCE_DETECTOR_ENVIRONMENT, envDetectorSync],
Expand Down
Loading