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: process resource detector #1531

Merged
merged 7 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/opentelemetry-resources/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,24 @@ export const SERVICE_RESOURCE = {
/** The version string of the service API or implementation. */
VERSION: 'service.version',
};

/** Attributes describing a Process. */
export const PROCESS_RESOURCE = {
/** A command which launced this proces. */
COMMAND: 'process.command',

/** The full command with arguments as string. */
COMMAND_LINE: 'process.command_line',

/** A name given to currently running porcess defaults to executable (process.title) . */
NAME: 'process.executable.name',

/** An owner of currently running process. */
OWNER: 'process.owner',

/** The full path to the process executable. */
PATH: 'process.executable.path',

/** Process identifier of currently running process. */
PID: 'process.id',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
Detector,
Resource,
PROCESS_RESOURCE,
ResourceDetectionConfigWithLogger,
} from '../../../';
import { ResourceAttributes } from '../../../types';

/**
* ProcessDetector will be used to detect the resoureces related current process running
* and being instumented from the NodeJS Process module.
*/
class ProcessDetector implements Detector {
async detect(config: ResourceDetectionConfigWithLogger): Promise<Resource> {
const processResource: ResourceAttributes = {
[PROCESS_RESOURCE.PID]: process.pid,
[PROCESS_RESOURCE.NAME]: process.title || '',
[PROCESS_RESOURCE.COMMAND]: process.argv[1] || '',
[PROCESS_RESOURCE.COMMAND_LINE]: process.argv.join(' ') || '',
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
dyladan marked this conversation as resolved.
Show resolved Hide resolved
};
return this._getResourceAttributes(processResource, config);
}
/**
* Validates process resource attribute map from process varaibls
*
* @param processResource The unsantized resource attributes from process as key/value pairs.
* @param config: Config
* @returns The sanitized resource attributes.
*/
private _getResourceAttributes(
processResource: ResourceAttributes,
config: ResourceDetectionConfigWithLogger
) {
if (
processResource[PROCESS_RESOURCE.NAME] === '' ||
processResource[PROCESS_RESOURCE.PATH] === '' ||
processResource[PROCESS_RESOURCE.COMMAND] === '' ||
processResource[PROCESS_RESOURCE.COMMAND_LINE] === ''
) {
config.logger.debug(
'ProcessDetector failed: Unable to find required process resources. '
);
return Resource.empty();
} else {
return new Resource({
...processResource,
});
}
}
}

export const processDetector = new ProcessDetector();
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './EnvDetector';
export * from './ProcessDetector';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* 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,
assertEmptyResource,
} from '../util/resource-assertions';
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 () => {
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(),
});
assertProcessResource(resource, {
pid: 1234,
name: 'otProcess',
command: '/home/ot/test.js',
commandLine: '/tmp/node /home/ot/test.js arg1 arg2',
});
});
it('should return empty resources if title, command and commondLine is missing', async () => {
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);
});
});
40 changes: 40 additions & 0 deletions packages/opentelemetry-resources/test/util/resource-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
K8S_RESOURCE,
TELEMETRY_SDK_RESOURCE,
SERVICE_RESOURCE,
PROCESS_RESOURCE,
} from '../../src/constants';

/**
Expand Down Expand Up @@ -260,6 +261,45 @@ export const assertServiceResource = (
);
};

/**
* Test utility method to validate a process resources
*
* @param resource the Resource to validate
* @param validations validations for the resource attributes
*/
export const assertProcessResource = (
resource: Resource,
validations: {
pid?: number;
name?: string;
command?: string;
commandLine?: string;
}
) => {
assert.strictEqual(
resource.attributes[PROCESS_RESOURCE.PID],
validations.pid
);
if (validations.name) {
assert.strictEqual(
resource.attributes[PROCESS_RESOURCE.NAME],
validations.name
);
}
if (validations.command) {
assert.strictEqual(
resource.attributes[PROCESS_RESOURCE.COMMAND],
validations.command
);
}
if (validations.commandLine) {
assert.strictEqual(
resource.attributes[PROCESS_RESOURCE.COMMAND_LINE],
validations.commandLine
);
}
};

/**
* Test utility method to validate an empty resource
*
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
Resource,
ResourceDetectionConfig,
envDetector,
processDetector,
} from '@opentelemetry/resources';
import { BatchSpanProcessor, SpanProcessor } from '@opentelemetry/tracing';
import { NodeSDKConfiguration } from './types';
Expand Down Expand Up @@ -131,7 +132,7 @@ export class NodeSDK {
/** Detect resource attributes */
public async detectResources(config?: ResourceDetectionConfig) {
const internalConfig: ResourceDetectionConfig = {
detectors: [awsEc2Detector, gcpDetector, envDetector],
detectors: [awsEc2Detector, gcpDetector, envDetector, processDetector],
...config,
};

Expand Down