1616
1717import { Labels } from '@opencensus/core' ;
1818import * as resource from '@opencensus/resource-util' ;
19+ import { CLOUD_RESOURCE , CONTAINER_RESOURCE , HOST_RESOURCE , K8S_RESOURCE } from '@opencensus/resource-util' ;
1920import { MonitoredResource } from './types' ;
2021
2122const STACKDRIVER_PROJECT_ID_KEY = 'project_id' ;
2223const AWS_REGION_VALUE_PREFIX = 'aws:' ;
24+ const K8S_CONTAINER = 'k8s_container' ;
25+ const GCP_GCE_INSTANCE = 'gce_instance' ;
26+ const AWS_EC2_INSTANCE = 'aws_ec2_instance' ;
2327
2428/* Return a self-configured StackDriver monitored resource. */
2529export async function getDefaultResource ( projectId : string ) :
@@ -29,7 +33,8 @@ export async function getDefaultResource(projectId: string):
2933 const [ type , mappings ] = getTypeAndMappings ( autoDetectedResource . type ) ;
3034 Object . keys ( mappings ) . forEach ( ( key ) => {
3135 if ( autoDetectedResource . labels [ mappings [ key ] ] ) {
32- if ( mappings [ key ] === resource . AWS_REGION_KEY ) {
36+ if ( type === AWS_EC2_INSTANCE &&
37+ mappings [ key ] === CLOUD_RESOURCE . REGION_KEY ) {
3338 labels [ key ] = `${ AWS_REGION_VALUE_PREFIX } ${
3439 autoDetectedResource . labels [ mappings [ key ] ] } `;
3540 } else {
@@ -45,32 +50,32 @@ function getTypeAndMappings(resourceType: string|null): [string, Labels] {
4550 case resource . GCP_GCE_INSTANCE_TYPE :
4651 // https://cloud.google.com/monitoring/api/resources#tag_gce_instance
4752 return [
48- 'gce_instance' , {
53+ GCP_GCE_INSTANCE , {
4954 'project_id' : STACKDRIVER_PROJECT_ID_KEY ,
50- 'instance_id' : resource . GCP_INSTANCE_ID_KEY ,
51- 'zone' : resource . GCP_ZONE_KEY
55+ 'instance_id' : HOST_RESOURCE . ID_KEY ,
56+ 'zone' : CLOUD_RESOURCE . ZONE_KEY
5257 }
5358 ] ;
5459 case resource . K8S_CONTAINER_TYPE :
5560 // https://cloud.google.com/monitoring/api/resources#tag_k8s_container
5661 return [
57- 'k8s_container' , {
62+ K8S_CONTAINER , {
5863 'project_id' : STACKDRIVER_PROJECT_ID_KEY ,
59- 'location' : resource . GCP_ZONE_KEY ,
60- 'cluster_name' : resource . K8S_CLUSTER_NAME_KEY ,
61- 'namespace_name' : resource . K8S_NAMESPACE_NAME_KEY ,
62- 'pod_name' : resource . K8S_POD_NAME_KEY ,
63- 'container_name' : resource . K8S_CONTAINER_NAME_KEY
64+ 'location' : CLOUD_RESOURCE . ZONE_KEY ,
65+ 'cluster_name' : K8S_RESOURCE . CLUSTER_NAME_KEY ,
66+ 'namespace_name' : K8S_RESOURCE . NAMESPACE_NAME_KEY ,
67+ 'pod_name' : K8S_RESOURCE . POD_NAME_KEY ,
68+ 'container_name' : CONTAINER_RESOURCE . NAME_KEY
6469 }
6570 ] ;
6671 case resource . AWS_EC2_INSTANCE_TYPE :
6772 // https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
6873 return [
69- 'aws_ec2_instance' , {
74+ AWS_EC2_INSTANCE , {
7075 'project_id' : STACKDRIVER_PROJECT_ID_KEY ,
71- 'instance_id' : resource . AWS_INSTANCE_ID_KEY ,
72- 'region' : resource . AWS_REGION_KEY ,
73- 'aws_account' : resource . AWS_ACCOUNT_KEY
76+ 'instance_id' : HOST_RESOURCE . ID_KEY ,
77+ 'region' : CLOUD_RESOURCE . REGION_KEY ,
78+ 'aws_account' : CLOUD_RESOURCE . ACCOUNT_ID_KEY
7479 }
7580 ] ;
7681 default :
0 commit comments