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

Commit a157106

Browse files
authored
Refactor resource util to match specs. (#485)
* Refactor resource util to match specs. * add check for AWS_EC2_INSTANCE type
1 parent cd046a0 commit a157106

File tree

7 files changed

+186
-117
lines changed

7 files changed

+186
-117
lines changed

packages/opencensus-exporter-stackdriver/src/common-utils.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
import {Labels} from '@opencensus/core';
1818
import * as resource from '@opencensus/resource-util';
19+
import {CLOUD_RESOURCE, CONTAINER_RESOURCE, HOST_RESOURCE, K8S_RESOURCE} from '@opencensus/resource-util';
1920
import {MonitoredResource} from './types';
2021

2122
const STACKDRIVER_PROJECT_ID_KEY = 'project_id';
2223
const 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. */
2529
export 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:

packages/opencensus-exporter-stackdriver/test/test-stackdriver-monitoring-utils.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ describe('Stackdriver Stats Exporter Utils', () => {
466466

467467
it('should return a k8s MonitoredResource', async () => {
468468
process.env.OC_RESOURCE_TYPE = 'k8s.io/container';
469-
process.env.OC_RESOURCE_LABELS = 'k8s.io/pod/name=pod-xyz-123,' +
470-
'k8s.io/container/name=c1,k8s.io/namespace/name=default,' +
471-
'cloud.google.com/gce/zone=zone1';
469+
process.env.OC_RESOURCE_LABELS = 'k8s.pod.name=pod-xyz-123,' +
470+
'container.name=c1,k8s.namespace.name=default,' +
471+
'cloud.zone=zone1';
472472
CoreResource.setup();
473473
const monitoredResource = await getDefaultResource('my-project-id');
474474
const {type, labels} = monitoredResource;
@@ -486,8 +486,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
486486

487487
it('should return a gce MonitoredResource', async () => {
488488
process.env.OC_RESOURCE_TYPE = 'cloud.google.com/gce/instance';
489-
process.env.OC_RESOURCE_LABELS = 'cloud.google.com/gce/instance_id=id1,' +
490-
'cloud.google.com/gce/zone=zone1';
489+
process.env.OC_RESOURCE_LABELS = 'host.id=id1,cloud.zone=zone1';
491490
CoreResource.setup();
492491
const monitoredResource = await getDefaultResource('my-project-id');
493492
const {type, labels} = monitoredResource;
@@ -503,8 +502,8 @@ describe('Stackdriver Stats Exporter Utils', () => {
503502

504503
it('should return a aws MonitoredResource', async () => {
505504
process.env.OC_RESOURCE_TYPE = 'aws.com/ec2/instance';
506-
process.env.OC_RESOURCE_LABELS = 'aws.com/ec2/account_id=id1,' +
507-
'aws.com/ec2/instance_id=instance1,aws.com/ec2/region=region1';
505+
process.env.OC_RESOURCE_LABELS = 'cloud.account.id=id1,' +
506+
'host.id=instance1,cloud.region=region1';
508507
CoreResource.setup();
509508
const monitoredResource = await getDefaultResource('my-project-id');
510509
const {type, labels} = monitoredResource;
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
/** Constants for Cloud Resource environment. */
18+
export const CLOUD_RESOURCE = {
19+
/** The type of the Resource. */
20+
TYPE: 'cloud',
21+
22+
/**
23+
* Key for the name of the cloud provider. Example values are aws, azure,
24+
* gcp.
25+
*/
26+
PROVIDER_KEY: 'cloud.provider',
27+
28+
/** The value of the provider when running in AWS. */
29+
PROVIDER_AWS: 'aws',
30+
31+
/** The value of the provider when running in AZURE. */
32+
PROVIDER_AZURE: 'azure',
33+
34+
/** The value of the provider when running in GCP. */
35+
PROVIDER_GCP: 'gcp',
36+
37+
/** Key for the region in which entities are running. */
38+
REGION_KEY: 'cloud.region',
39+
40+
/** Key for the cloud account id used to identify different entities. */
41+
ACCOUNT_ID_KEY: 'cloud.account.id',
42+
43+
/** Key for the zone in which entities are running. */
44+
ZONE_KEY: 'cloud.zone'
45+
};
46+
47+
/** Constants for K8S container Resource. */
48+
export const CONTAINER_RESOURCE = {
49+
/** Kubernetes resources key that represents a type of the resource. */
50+
TYPE: 'container',
51+
52+
/** Key for the container name. */
53+
NAME_KEY: 'container.name',
54+
55+
/** Key for the container image name. */
56+
IMAGE_NAME_KEY: 'container.image.name',
57+
58+
/** Key for the container image tag. */
59+
IMAGE_TAG_KEY: 'container.image.tag'
60+
};
61+
62+
/**
63+
* Constants for Host Resource. A host is defined as a general computing
64+
* instance.
65+
*/
66+
export const HOST_RESOURCE = {
67+
/** The type of the Resource. */
68+
TYPE: 'host',
69+
70+
/**
71+
* Key for the hostname of the host.
72+
* It contains what the `hostname` command returns on the host machine.
73+
*/
74+
HOSTNAME_KEY: 'host.hostname',
75+
76+
/**
77+
* Key for the name of the host.
78+
* It may contain what `hostname` returns on Unix systems, the fully
79+
* qualified, or a name specified by the user.
80+
*/
81+
NAME_KEY: 'host.name',
82+
83+
/** Key for the unique host id (instance id in Cloud). */
84+
ID_KEY: 'host.id',
85+
86+
/** Key for the type of the host (machine type). */
87+
TYPE_KEY: 'host.type'
88+
};
89+
90+
/** Constants for Kubernetes deployment service Resource. */
91+
export const K8S_RESOURCE = {
92+
/** The type of the Resource. */
93+
TYPE: 'k8s',
94+
95+
/** Key for the name of the cluster. */
96+
CLUSTER_NAME_KEY: 'k8s.cluster.name',
97+
98+
/** Key for the name of the namespace. */
99+
NAMESPACE_NAME_KEY: 'k8s.namespace.name',
100+
101+
/** Key for the name of the pod. */
102+
POD_NAME_KEY: 'k8s.pod.name'
103+
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
*/
1616

1717
export * from './resource-labels';
18+
export * from './constants';
1819
export * from './detect-resource';

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,8 @@
1919
/** AWS key that represents a type of the resource. */
2020
export const AWS_EC2_INSTANCE_TYPE = 'aws.com/ec2/instance';
2121

22-
/** AWS key that represents a region for the VM. */
23-
export const AWS_REGION_KEY = 'aws.com/ec2/region';
24-
25-
/** AWS key that represents the AWS account number for the VM. */
26-
export const AWS_ACCOUNT_KEY = 'aws.com/ec2/account_id';
27-
28-
/** AWS key that represents the VM instance identifier assigned by AWS. */
29-
export const AWS_INSTANCE_ID_KEY = 'aws.com/ec2/instance_id';
30-
3122
/** GCP GCE key that represents a type of the resource. */
3223
export const GCP_GCE_INSTANCE_TYPE = 'cloud.google.com/gce/instance';
3324

34-
/** GCP GCE key that represents the GCP account number for the instance. */
35-
export const GCP_ACCOUNT_ID_KEY = 'cloud.google.com/gce/project_id';
36-
37-
/**
38-
* GCP GCE key that represents the numeric VM instance identifier assigned by
39-
* GCE.
40-
*/
41-
export const GCP_INSTANCE_ID_KEY = 'cloud.google.com/gce/instance_id';
42-
43-
/** GCP GCE key that represents the GCE zone in which the VM is running. */
44-
export const GCP_ZONE_KEY = 'cloud.google.com/gce/zone';
45-
4625
/** Kubernetes resources key that represents a type of the resource. */
4726
export const K8S_CONTAINER_TYPE = 'k8s.io/container';
48-
49-
/**
50-
* Kubernetes resources key that represents the name for the cluster the
51-
* container is running in.
52-
*/
53-
export const K8S_CLUSTER_NAME_KEY = 'k8s.io/cluster/name';
54-
55-
/** Kubernetes resources key that represents the name of the container. */
56-
export const K8S_CONTAINER_NAME_KEY = 'k8s.io/container/name';
57-
58-
/**
59-
* Kubernetes resources key that represents the identifier for the GCE
60-
* instance the container is running in.
61-
*/
62-
export const K8S_NAMESPACE_NAME_KEY = 'k8s.io/namespace/name';
63-
64-
/**
65-
* Kubernetes resources key that represents the identifier for the pod the
66-
* container is running in.
67-
*/
68-
export const K8S_POD_NAME_KEY = 'k8s.io/pod/name';

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {Labels, Resource} from '@opencensus/core';
2323
import * as gcpMetadata from 'gcp-metadata';
2424
import * as http from 'http';
2525
import * as os from 'os';
26+
import {CLOUD_RESOURCE, CONTAINER_RESOURCE, HOST_RESOURCE, K8S_RESOURCE} from './constants';
2627
import * as constants from './resource-labels';
2728

2829
export const AWS_INSTANCE_IDENTITY_DOCUMENT_URI =
@@ -70,11 +71,10 @@ async function isRunningOnComputeEngine() {
7071
async function isRunningOnAwsEc2() {
7172
try {
7273
const awsIdentityDocument: Labels = await awsMetadataAccessor();
73-
awsResourceLabels[constants.AWS_ACCOUNT_KEY] =
74+
awsResourceLabels[CLOUD_RESOURCE.ACCOUNT_ID_KEY] =
7475
awsIdentityDocument.accountId;
75-
awsResourceLabels[constants.AWS_REGION_KEY] = awsIdentityDocument.region;
76-
awsResourceLabels[constants.AWS_INSTANCE_ID_KEY] =
77-
awsIdentityDocument.instanceId;
76+
awsResourceLabels[CLOUD_RESOURCE.REGION_KEY] = awsIdentityDocument.region;
77+
awsResourceLabels[HOST_RESOURCE.ID_KEY] = awsIdentityDocument.instanceId;
7878
return true;
7979
} catch {
8080
return false;
@@ -86,9 +86,9 @@ export async function getComputerEngineResource(): Promise<Resource> {
8686
if (Object.keys(gceResourceLabels).length === 0) {
8787
const [projectId, instanceId, zoneId] =
8888
await Promise.all([getProjectId(), getInstanceId(), getZone()]);
89-
gceResourceLabels[constants.GCP_ACCOUNT_ID_KEY] = projectId;
90-
gceResourceLabels[constants.GCP_INSTANCE_ID_KEY] = instanceId;
91-
gceResourceLabels[constants.GCP_ZONE_KEY] = zoneId;
89+
gceResourceLabels[CLOUD_RESOURCE.ACCOUNT_ID_KEY] = projectId;
90+
gceResourceLabels[HOST_RESOURCE.ID_KEY] = instanceId;
91+
gceResourceLabels[CLOUD_RESOURCE.ZONE_KEY] = zoneId;
9292
}
9393
return {type: constants.GCP_GCE_INSTANCE_TYPE, labels: gceResourceLabels};
9494
}
@@ -98,13 +98,13 @@ export async function getKubernetesEngineResource(): Promise<Resource> {
9898
if (Object.keys(gkeResourceLabels).length === 0) {
9999
const [projectId, zoneId, clusterName, hostname] = await Promise.all(
100100
[getProjectId(), getZone(), getClusterName(), getHostname()]);
101-
gkeResourceLabels[constants.GCP_ACCOUNT_ID_KEY] = projectId;
102-
gkeResourceLabels[constants.GCP_ZONE_KEY] = zoneId;
103-
gkeResourceLabels[constants.K8S_CLUSTER_NAME_KEY] = clusterName;
104-
gkeResourceLabels[constants.K8S_NAMESPACE_NAME_KEY] =
101+
gkeResourceLabels[CLOUD_RESOURCE.ACCOUNT_ID_KEY] = projectId;
102+
gkeResourceLabels[CLOUD_RESOURCE.ZONE_KEY] = zoneId;
103+
gkeResourceLabels[K8S_RESOURCE.CLUSTER_NAME_KEY] = clusterName;
104+
gkeResourceLabels[K8S_RESOURCE.NAMESPACE_NAME_KEY] =
105105
process.env.NAMESPACE || '';
106-
gkeResourceLabels[constants.K8S_POD_NAME_KEY] = hostname;
107-
gkeResourceLabels[constants.K8S_CONTAINER_NAME_KEY] =
106+
gkeResourceLabels[K8S_RESOURCE.POD_NAME_KEY] = hostname;
107+
gkeResourceLabels[CONTAINER_RESOURCE.NAME_KEY] =
108108
process.env.CONTAINER_NAME || '';
109109
}
110110
return {type: constants.K8S_CONTAINER_TYPE, labels: gkeResourceLabels};

0 commit comments

Comments
 (0)