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

Commit 9414faf

Browse files
authored
Auto detect GCE, GKE and AWS EC2 resources (#312)
* add gcp resource * fix review comments
1 parent 77eada8 commit 9414faf

File tree

6 files changed

+651
-0
lines changed

6 files changed

+651
-0
lines changed

packages/opencensus-resource-util/package-lock.json

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-resource-util/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"types": "build/src/index.d.ts",
77
"repository": "census-instrumentation/opencensus-node",
88
"scripts": {
9+
"test": "nyc mocha build/test/**/*.js",
10+
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json",
911
"clean": "rimraf build/*",
1012
"check": "gts check",
1113
"compile": "tsc -p .",
@@ -44,6 +46,7 @@
4446
"gts": "^0.9.0",
4547
"mocha": "^5.1.1",
4648
"nyc": "^13.0.0",
49+
"nock": "^10.0.0",
4750
"rimraf": "^2.6.2",
4851
"typescript": "~3.2.0"
4952
},
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {CoreResource, Resource} from '@opencensus/core';
18+
import {getAwsEC2Resource, getComputerEngineResource, getKubernetesEngineResource, getResourceType, ResourceType} from './resource-utils';
19+
20+
/**
21+
* Returns a Resource. Detector sequentially runs resource detection from
22+
* environment variables, K8S, GCE and AWS.
23+
*/
24+
export async function detectResource(): Promise<Resource> {
25+
const resources = [CoreResource.createFromEnvironmentVariables()];
26+
27+
const resourceType = await getResourceType();
28+
if (resourceType === ResourceType.GCP_GKE_CONTAINER) {
29+
resources.push(await getKubernetesEngineResource());
30+
} else if (resourceType === ResourceType.GCP_GCE_INSTANCE) {
31+
resources.push(await getComputerEngineResource());
32+
}
33+
if (resourceType === ResourceType.AWS_EC2_INSTANCE) {
34+
resources.push(await getAwsEC2Resource());
35+
}
36+
37+
return CoreResource.mergeResources(resources);
38+
}

packages/opencensus-resource-util/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
export * from './resource-labels';
18+
export * from './detect-resource';

0 commit comments

Comments
 (0)