-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/opentelemetry-resources/src/platform/node/detectors/ProcessDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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(' ') || '', | ||
}; | ||
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.PID] === '') { | ||
config.logger.debug( | ||
'ProcessDetector failed: unable to locate pid for currently running process' | ||
); | ||
return Resource.empty(); | ||
} else 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
export * from './EnvDetector'; | ||
export * from './ProcessDetector'; |
75 changes: 75 additions & 0 deletions
75
packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 { processDetector, Resource } from '../../src'; | ||
import { | ||
assertProcessResource, | ||
assertEmptyResource, | ||
} from '../util/resource-assertions'; | ||
import { NoopLogger } from '@opentelemetry/core'; | ||
|
||
describe('processDetector()', () => { | ||
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'], | ||
}); | ||
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 () => { | ||
Object.defineProperty(process, 'pid', { | ||
value: 1234, | ||
}); | ||
Object.defineProperty(process, 'title', { | ||
value: undefined, | ||
}); | ||
Object.defineProperty(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'], | ||
}); | ||
const resource: Resource = await processDetector.detect({ | ||
logger: new NoopLogger(), | ||
}); | ||
assertEmptyResource(resource); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters