This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathcommon-utils.ts
99 lines (95 loc) · 3.3 KB
/
common-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 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 { Labels } from '@opencensus/core';
import * as resource from '@opencensus/resource-util';
import {
CLOUD_RESOURCE,
CONTAINER_RESOURCE,
HOST_RESOURCE,
K8S_RESOURCE,
} from '@opencensus/resource-util';
import { MonitoredResource } from './types';
const STACKDRIVER_PROJECT_ID_KEY = 'project_id';
const AWS_REGION_VALUE_PREFIX = 'aws:';
const K8S_CONTAINER = 'k8s_container';
const GCP_GCE_INSTANCE = 'gce_instance';
const AWS_EC2_INSTANCE = 'aws_ec2_instance';
/* Return a self-configured StackDriver monitored resource. */
export async function getDefaultResource(
projectId: string
): Promise<MonitoredResource> {
const labels: Labels = { project_id: projectId };
const autoDetectedResource = await resource.detectResource();
const [type, mappings] = getTypeAndMappings(autoDetectedResource.type);
Object.keys(mappings).forEach(key => {
if (autoDetectedResource.labels[mappings[key]]) {
if (
type === AWS_EC2_INSTANCE &&
mappings[key] === CLOUD_RESOURCE.REGION_KEY
) {
labels[key] = `${AWS_REGION_VALUE_PREFIX}${
autoDetectedResource.labels[mappings[key]]
}`;
} else {
labels[key] = autoDetectedResource.labels[mappings[key]];
}
}
});
if (Object.keys(labels).length !== Object.keys(mappings).length) {
return { type: 'global', labels: { project_id: projectId } };
}
return { type, labels };
}
function getTypeAndMappings(resourceType: string | null): [string, Labels] {
switch (resourceType) {
case resource.GCP_GCE_INSTANCE_TYPE:
// https://cloud.google.com/monitoring/api/resources#tag_gce_instance
return [
GCP_GCE_INSTANCE,
{
project_id: STACKDRIVER_PROJECT_ID_KEY,
instance_id: HOST_RESOURCE.ID_KEY,
zone: CLOUD_RESOURCE.ZONE_KEY,
},
];
case resource.K8S_CONTAINER_TYPE:
// https://cloud.google.com/monitoring/api/resources#tag_k8s_container
return [
K8S_CONTAINER,
{
project_id: STACKDRIVER_PROJECT_ID_KEY,
location: CLOUD_RESOURCE.ZONE_KEY,
cluster_name: K8S_RESOURCE.CLUSTER_NAME_KEY,
namespace_name: K8S_RESOURCE.NAMESPACE_NAME_KEY,
pod_name: K8S_RESOURCE.POD_NAME_KEY,
container_name: CONTAINER_RESOURCE.NAME_KEY,
},
];
case resource.AWS_EC2_INSTANCE_TYPE:
// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
return [
AWS_EC2_INSTANCE,
{
project_id: STACKDRIVER_PROJECT_ID_KEY,
instance_id: HOST_RESOURCE.ID_KEY,
region: CLOUD_RESOURCE.REGION_KEY,
aws_account: CLOUD_RESOURCE.ACCOUNT_ID_KEY,
},
];
default:
return ['global', {}];
}
}