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

refactor: separate resource detector packages #1420

Merged

Conversation

markwolff
Copy link
Member

Which problem is this PR solving?

Short description of the changes

  • Since there are a lot of diffs, I am keeping these packages in core repo until published(?), then they can safely be moved to contrib.
  • Adds optional detectors[] config to detect-resources.ts.
    • node-sdk will fill these in with all of the "default" resource detectors.

/cc @mwear @dyladan

@@ -0,0 +1,63 @@
{
"name": "@opentelemetry/resource-detector-aws",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split them based on cloud vendor, so ec2, beanstalk, etc would all live here.

@markwolff markwolff marked this pull request as ready for review August 13, 2020 02:32
@markwolff markwolff marked this pull request as draft August 13, 2020 02:56
@codecov
Copy link

codecov bot commented Aug 13, 2020

Codecov Report

Merging #1420 into master will decrease coverage by 0.77%.
The diff coverage is 65.21%.

@@            Coverage Diff             @@
##           master    #1420      +/-   ##
==========================================
- Coverage   94.31%   93.53%   -0.78%     
==========================================
  Files         146      146              
  Lines        4396     4009     -387     
  Branches      893      774     -119     
==========================================
- Hits         4146     3750     -396     
- Misses        250      259       +9     
Impacted Files Coverage Δ
...resource-detector-gcp/src/detectors/GcpDetector.ts 95.23% <ø> (ø)
...ry-resources/src/platform/node/detect-resources.ts 20.83% <0.00%> (-75.47%) ⬇️
...sources/src/platform/node/detectors/EnvDetector.ts 86.66% <33.33%> (-8.89%) ⬇️
...ource-detector-aws/src/detectors/AwsEc2Detector.ts 97.87% <100.00%> (ø)
...metry-resource-detector-aws/src/detectors/index.ts 100.00% <100.00%> (ø)
...metry-resource-detector-gcp/src/detectors/index.ts 100.00% <100.00%> (ø)
packages/opentelemetry-sdk-node/src/sdk.ts 83.05% <100.00%> (+8.05%) ⬆️
packages/opentelemetry-plugin-http/src/http.ts
... and 4 more

@@ -128,4 +169,233 @@ describe('Node SDK', () => {
assert.ok(metrics.getMeterProvider() instanceof MeterProvider);
});
});

describe('detectResources', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the old detect-resources.test.ts tests required the actual detectors, so I moved these tests from @opentelemetry/resources tests to sdk.test.ts. This move affects codecov, so eventually new tests with test-stub detectors should be added to the resources package

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We should add a couple stub detectors like one that fails and one that succeeds.

const detectorName = DETECTORS[index].constructor
? DETECTORS[index].constructor.name
: 'Unknown detector';
const detectorName = resource.detectedBy;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified the Resource ctor to keep this functionality, but I don't think my change is ideal... /cc @mwear

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The detectors have access to the logger (via the config) and log info during a failure. You could consider moving the logging for successful detections into the detector as well.

Copy link
Member Author

@markwolff markwolff Aug 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mwear would that mean deleting this detectorName stuff altogether and letting the detectors handle the detector name stuff? i.e 81a07e2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I think you could go one step further, and have the Detector log the detected resources as they're found, rather than finding them all first and logging afterwards. Specifically, you could move this logging into the detector: https://github.com/open-telemetry/opentelemetry-js/pull/1420/files#diff-1328771aa7fff461aee162025e488e55R67-R79.

Do this only if you think it's an improvement though.

public async detectResources(config?: ResourceDetectionConfig) {
const internalConfig: ResourceDetectionConfig = {
...config,
detectors: [awsEc2Detector, gcpDetector, envDetector],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now provide detectors via sdk config

@markwolff markwolff marked this pull request as ready for review August 13, 2020 16:35
@dyladan
Copy link
Member

dyladan commented Aug 13, 2020

What do we think about keeping the env detector in the resources package? It is simple and has no external dependencies, and is required by the spec.

Copy link
Member

@mwear mwear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a couple of non-blocking questions / comments, but otherwise LGTM!

packages/opentelemetry-resource-detector-env/README.md Outdated Show resolved Hide resolved
const detectorName = DETECTORS[index].constructor
? DETECTORS[index].constructor.name
: 'Unknown detector';
const detectorName = resource.detectedBy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The detectors have access to the logger (via the config) and log info during a failure. You could consider moving the logging for successful detections into the detector as well.

packages/opentelemetry-resource-detector-aws/LICENSE Outdated Show resolved Hide resolved
[HOST_RESOURCE.TYPE]: instanceType,
},
);
config.logger.debug(`${this.constructor.name} found resource.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Success and failure could be logged by the caller, which would make it consistent across detectors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dyladan Moved in b295d8d but required to remove try/catch from aws detector. ptal

@@ -62,7 +63,10 @@ class GcpDetector implements Detector {
if (process.env.KUBERNETES_SERVICE_HOST)
this._addK8sLabels(labels, clusterName);

return new Resource(labels);
const resource = new Resource(labels);
config.logger.debug(`${this.constructor.name} found resource.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment wrt logging by the caller

@@ -61,7 +64,9 @@ class EnvDetector implements Detector {
const labels = this._parseResourceLabels(
process.env.OTEL_RESOURCE_LABELS
);
return new Resource(labels);
const resource = new Resource(labels);
config.logger.debug(`${this.constructor.name} found resource.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same :)

packages/opentelemetry-sdk-node/test/sdk.test.ts Outdated Show resolved Hide resolved
@@ -128,4 +169,233 @@ describe('Node SDK', () => {
assert.ok(metrics.getMeterProvider() instanceof MeterProvider);
});
});

describe('detectResources', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We should add a couple stub detectors like one that fails and one that succeeds.

@@ -44,10 +40,13 @@ export const detectResources = async (
);

const resources: Array<Resource> = await Promise.all(
DETECTORS.map(d => {
(internalConfig.detectors || []).map(async d => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick but this || could be ??

Copy link
Member

@vmarchaud vmarchaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very fond of this PR as AWS/GCP detectors are slow in my environment most time. 👍

@dyladan
Copy link
Member

dyladan commented Aug 18, 2020

Please resolve the conflicts and this can be merged


## Usage

> TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

@dyladan
Copy link
Member

dyladan commented Aug 18, 2020

@dyladan dyladan merged commit d68ff0f into open-telemetry:master Aug 19, 2020
@markwolff markwolff deleted the refactor/separate-resource-detectors branch August 19, 2020 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants