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

Commit

Permalink
Auto detect GCE, GKE and AWS EC2 resources (#312)
Browse files Browse the repository at this point in the history
* add gcp resource

* fix review comments
  • Loading branch information
mayurkale22 authored Jan 30, 2019
1 parent 77eada8 commit 9414faf
Show file tree
Hide file tree
Showing 6 changed files with 651 additions and 0 deletions.
99 changes: 99 additions & 0 deletions packages/opencensus-resource-util/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/opencensus-resource-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"types": "build/src/index.d.ts",
"repository": "census-instrumentation/opencensus-node",
"scripts": {
"test": "nyc mocha build/test/**/*.js",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json",
"clean": "rimraf build/*",
"check": "gts check",
"compile": "tsc -p .",
Expand Down Expand Up @@ -44,6 +46,7 @@
"gts": "^0.9.0",
"mocha": "^5.1.1",
"nyc": "^13.0.0",
"nock": "^10.0.0",
"rimraf": "^2.6.2",
"typescript": "~3.2.0"
},
Expand Down
38 changes: 38 additions & 0 deletions packages/opencensus-resource-util/src/detect-resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2019, OpenCensus 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
*
* http://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 {CoreResource, Resource} from '@opencensus/core';
import {getAwsEC2Resource, getComputerEngineResource, getKubernetesEngineResource, getResourceType, ResourceType} from './resource-utils';

/**
* Returns a Resource. Detector sequentially runs resource detection from
* environment variables, K8S, GCE and AWS.
*/
export async function detectResource(): Promise<Resource> {
const resources = [CoreResource.createFromEnvironmentVariables()];

const resourceType = await getResourceType();
if (resourceType === ResourceType.GCP_GKE_CONTAINER) {
resources.push(await getKubernetesEngineResource());
} else if (resourceType === ResourceType.GCP_GCE_INSTANCE) {
resources.push(await getComputerEngineResource());
}
if (resourceType === ResourceType.AWS_EC2_INSTANCE) {
resources.push(await getAwsEC2Resource());
}

return CoreResource.mergeResources(resources);
}
3 changes: 3 additions & 0 deletions packages/opencensus-resource-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './resource-labels';
export * from './detect-resource';
Loading

0 comments on commit 9414faf

Please sign in to comment.