Skip to content

Commit

Permalink
chore: uses sinon snadbox and updated pid to be undefined instead of …
Browse files Browse the repository at this point in the history
…string
  • Loading branch information
mihirsoni committed Sep 20, 2020
1 parent 5fd27d4 commit 3f51629
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ResourceAttributes } from '../../../types';
class ProcessDetector implements Detector {
async detect(config: ResourceDetectionConfigWithLogger): Promise<Resource> {
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(' ') || '',
Expand All @@ -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'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
});
Expand All @@ -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(),
});
Expand Down

0 comments on commit 3f51629

Please sign in to comment.