diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts index ba8ee52fd3..90a1851490 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts @@ -29,7 +29,7 @@ import { ResourceAttributes } from '../../../types'; class ProcessDetector implements Detector { async detect(config: ResourceDetectionConfigWithLogger): Promise { const processResource: ResourceAttributes = { - [PROCESS_RESOURCE.PID]: process.pid || '', + [PROCESS_RESOURCE.PID]: process.pid, [PROCESS_RESOURCE.NAME]: process.title || '', [PROCESS_RESOURCE.COMMAND]: process.argv[1] || '', [PROCESS_RESOURCE.COMMAND_LINE]: process.argv.join(' ') || '', @@ -47,7 +47,7 @@ class ProcessDetector implements Detector { processResource: ResourceAttributes, config: ResourceDetectionConfigWithLogger ) { - if (processResource[PROCESS_RESOURCE.PID] === '') { + if (processResource[PROCESS_RESOURCE.PID] === undefined) { config.logger.debug( 'ProcessDetector failed: unable to locate pid for currently running process' ); diff --git a/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts b/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts index ab235bba3f..96a80ce43b 100644 --- a/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +import * as sinon from 'sinon'; import { processDetector, Resource } from '../../src'; import { assertProcessResource, @@ -22,16 +22,23 @@ import { import { NoopLogger } from '@opentelemetry/core'; describe('processDetector()', () => { + let sandbox: sinon.SinonSandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + }); + + afterEach(() => { + sandbox.restore(); + }); + it('should return resource information from process', async () => { - Object.defineProperty(process, 'pid', { - value: 1234, - }); - Object.defineProperty(process, 'title', { - value: 'otProcess', - }); - Object.defineProperty(process, 'argv', { - value: ['/tmp/node', '/home/ot/test.js', 'arg1', 'arg2'], - }); + sandbox.stub(process, 'pid').value(1234); + sandbox.stub(process, 'title').value('otProcess'); + sandbox + .stub(process, 'argv') + .value(['/tmp/node', '/home/ot/test.js', 'arg1', 'arg2']); + const resource: Resource = await processDetector.detect({ logger: new NoopLogger(), }); @@ -43,30 +50,21 @@ describe('processDetector()', () => { }); }); it('should return empty resources if title, command and commondLine is missing', async () => { - Object.defineProperty(process, 'pid', { - value: 1234, - }); - Object.defineProperty(process, 'title', { - value: undefined, - }); - Object.defineProperty(process, 'argv', { - value: [], - }); + sandbox.stub(process, 'pid').value(1234); + sandbox.stub(process, 'title').value(undefined); + sandbox.stub(process, 'argv').value([]); const resource: Resource = await processDetector.detect({ logger: new NoopLogger(), }); assertEmptyResource(resource); }); it('should return empty resources if pid is missing', async () => { - Object.defineProperty(process, 'pid', { - value: undefined, - }); - Object.defineProperty(process, 'title', { - value: 'otProcess', - }); - Object.defineProperty(process, 'argv', { - value: ['/tmp/node', '/home/ot/test.js', 'arg1', 'arg2'], - }); + sandbox.stub(process, 'pid').value(undefined); + sandbox.stub(process, 'title').value('otProcess'); + sandbox + .stub(process, 'argv') + .value(['/tmp/node', '/home/ot/test.js', 'arg1', 'arg2']); + const resource: Resource = await processDetector.detect({ logger: new NoopLogger(), });