Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Auto detect GCE, GKE and AWS EC2 resources#312

Merged
mayurkale22 merged 2 commits intocensus-instrumentation:masterfrom
mayurkale22:gke_gcp_aws_resources
Jan 30, 2019
Merged

Auto detect GCE, GKE and AWS EC2 resources#312
mayurkale22 merged 2 commits intocensus-instrumentation:masterfrom
mayurkale22:gke_gcp_aws_resources

Conversation

@mayurkale22
Copy link
Copy Markdown
Member

This PR is to automatically detect monitored resources based the environment that the application is running in, according to OpenCensus MonitoredResource specs. This util should be independent of specific exporters, since it's supposed to be shared between the Stackdriver Stats exporter and Stackdriver Trace exporter.

I will send another PR to integrate this detected resources (detectResource) to the Stackdriver exporter.

Fixes #38.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Jan 28, 2019

Codecov Report

Merging #312 into master will increase coverage by 0.11%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #312      +/-   ##
=========================================
+ Coverage   94.79%   94.9%   +0.11%     
=========================================
  Files         114     118       +4     
  Lines        7721    8030     +309     
  Branches      703     717      +14     
=========================================
+ Hits         7319    7621     +302     
- Misses        402     409       +7
Impacted Files Coverage Δ
src/detect-resource.ts 100% <0%> (ø)
src/resource-labels.ts 100% <0%> (ø)
test/test-detect-resource.ts 100% <0%> (ø)
src/resource-utils.ts 92.85% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 77eada8...7406d7b. Read the comment docs.

Copy link
Copy Markdown
Contributor

@draffensperger draffensperger left a comment

Choose a reason for hiding this comment

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

LGTM, though left a number of nit picky comments.

export async function detectResource(): Promise<Resource> {
const resources: Resource[] = [];
const resourceFromEnv = CoreResource.createFromEnvironmentVariables();
resources.push(resourceFromEnv);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you just do const resources = [CoreResource.createFromEnvironmentVariables()]?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

sure.

} else if (resourceType === ResourceType.GCP_GCE_INSTANCE) {
resources.push(await getComputerEngineResource());
}
if (resourceType === ResourceType.AWS_EC2_INSTANCE) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For consistency, should this have an else before the if similar to the resourceType === ResourceType.GCP_GCE_INSTANCE above?

}
if (resourceType === ResourceType.AWS_EC2_INSTANCE) {
resources.push(await getAwsEC2Resource());
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What would you think about adding an exhuaustiveness check here? See https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html

Would it make sense for this to be a switch?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Initial if...else block is to pick GKE or GCE, since one instance cannot be both a GCE instance and a GKE instance


/** Determine the compute environment in which the code is running. */
export async function getResourceType(): Promise<ResourceType> {
if (!resourceType) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional nit: what would you think about making this first line to be if (resourceType) return resourceType;, which would allow if statements below to be less nested.

} catch {
/* ignore */
}
return '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What would you think about moving this return '' up into the catch block? Then you could potentially remove the /* ignore */ comment since the error handling just causes an empty string to return.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good call. I was thinking about it, but missed somehow. done.

} catch {
/* ignore */
}
return '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar comment here on moving the return '' up into the catch

* See the License for the specific language governing permissions and
* limitations under the License.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional (may just be my opinion): I tend to like to have index.ts just export symbols and then have more clearly named files that do the heavy lifting. E.g. calling this one detect-resource.ts or similar and same for its test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make Sense, Done.

Copy link
Copy Markdown
Contributor

@songy23 songy23 left a comment

Choose a reason for hiding this comment

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

LGTM

@mayurkale22 mayurkale22 merged commit 9414faf into census-instrumentation:master Jan 30, 2019
@mayurkale22 mayurkale22 deleted the gke_gcp_aws_resources branch January 30, 2019 01:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants