1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.detectServiceContext = exports.getDefaultResource = exports.getGlobalDescriptor = exports.getGKEDescriptor = exports.KUBERNETES_NAMESPACE_ID_PATH = exports.getGCEDescriptor = exports.getGAEDescriptor = exports.getCloudRunDescriptor = exports.getCloudFunctionDescriptor = void 0;
|
19 | const fs = require("fs");
|
20 | const gcpMetadata = require("gcp-metadata");
|
21 | const google_auth_library_1 = require("google-auth-library");
|
22 | const util_1 = require("util");
|
23 | const readFile = util_1.promisify(fs.readFile);
|
24 | function zoneFromQualifiedZone(qualified) {
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | return qualified.split('/').pop();
|
30 | }
|
31 | function regionFromQualifiedZone(qualified) {
|
32 |
|
33 |
|
34 | const zone = zoneFromQualifiedZone(qualified);
|
35 | const region = zone === undefined ? undefined : zone.slice(0, zone.lastIndexOf('-'));
|
36 | return region;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | async function getCloudFunctionDescriptor() {
|
44 |
|
45 | let region = undefined;
|
46 | if (!(process.env.GOOGLE_CLOUD_REGION || process.env.FUNCTION_REGION)) {
|
47 | const qualifiedZone = await gcpMetadata.instance('zone');
|
48 | region = regionFromQualifiedZone(qualifiedZone);
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 | return {
|
55 | type: 'cloud_function',
|
56 | labels: {
|
57 | function_name: process.env.K_SERVICE || process.env.FUNCTION_NAME,
|
58 | region: process.env.GOOGLE_CLOUD_REGION ||
|
59 | process.env.FUNCTION_REGION ||
|
60 | region,
|
61 | },
|
62 | };
|
63 | }
|
64 | exports.getCloudFunctionDescriptor = getCloudFunctionDescriptor;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | async function getCloudRunDescriptor() {
|
71 | const qualifiedZone = await gcpMetadata.instance('zone');
|
72 | const location = regionFromQualifiedZone(qualifiedZone);
|
73 | return {
|
74 | type: 'cloud_run_revision',
|
75 | labels: {
|
76 | location,
|
77 | service_name: process.env.K_SERVICE,
|
78 | revision_name: process.env.K_REVISION,
|
79 | configuration_name: process.env.K_CONFIGURATION,
|
80 | },
|
81 | };
|
82 | }
|
83 | exports.getCloudRunDescriptor = getCloudRunDescriptor;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | async function getGAEDescriptor() {
|
90 | const qualifiedZone = await gcpMetadata.instance('zone');
|
91 | const zone = zoneFromQualifiedZone(qualifiedZone);
|
92 | return {
|
93 | type: 'gae_app',
|
94 | labels: {
|
95 | module_id: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
|
96 | version_id: process.env.GAE_VERSION,
|
97 | zone,
|
98 | },
|
99 | };
|
100 | }
|
101 | exports.getGAEDescriptor = getGAEDescriptor;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | async function getGCEDescriptor() {
|
107 | const idResponse = await gcpMetadata.instance('id');
|
108 | const zoneResponse = await gcpMetadata.instance('zone');
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | const zone = zoneFromQualifiedZone(zoneResponse);
|
114 | return {
|
115 | type: 'gce_instance',
|
116 | labels: {
|
117 |
|
118 |
|
119 | instance_id: idResponse.toString(),
|
120 | zone,
|
121 | },
|
122 | };
|
123 | }
|
124 | exports.getGCEDescriptor = getGCEDescriptor;
|
125 | exports.KUBERNETES_NAMESPACE_ID_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/namespace';
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | async function getGKEDescriptor() {
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | const resp = await gcpMetadata.instance('attributes/cluster-name');
|
140 | const qualifiedZone = await gcpMetadata.instance('zone');
|
141 | const location = zoneFromQualifiedZone(qualifiedZone);
|
142 | let namespace = '';
|
143 | try {
|
144 | namespace = await readFile(exports.KUBERNETES_NAMESPACE_ID_PATH, 'utf8');
|
145 | }
|
146 | catch (err) {
|
147 |
|
148 | }
|
149 | return {
|
150 | type: 'k8s_container',
|
151 | labels: {
|
152 | location,
|
153 | cluster_name: resp,
|
154 | namespace_name: namespace,
|
155 | pod_name: process.env.HOSTNAME,
|
156 |
|
157 |
|
158 | container_name: process.env.CONTAINER_NAME,
|
159 | },
|
160 | };
|
161 | }
|
162 | exports.getGKEDescriptor = getGKEDescriptor;
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function getGlobalDescriptor() {
|
169 | return {
|
170 | type: 'global',
|
171 | };
|
172 | }
|
173 | exports.getGlobalDescriptor = getGlobalDescriptor;
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | async function getDefaultResource(auth) {
|
180 | const env = await auth.getEnv();
|
181 | switch (env) {
|
182 | case google_auth_library_1.GCPEnv.KUBERNETES_ENGINE:
|
183 | return getGKEDescriptor().catch(() => getGlobalDescriptor());
|
184 | case google_auth_library_1.GCPEnv.APP_ENGINE:
|
185 | return getGAEDescriptor().catch(() => getGlobalDescriptor());
|
186 | case google_auth_library_1.GCPEnv.CLOUD_FUNCTIONS:
|
187 | return getCloudFunctionDescriptor().catch(() => getGlobalDescriptor());
|
188 | case google_auth_library_1.GCPEnv.CLOUD_RUN:
|
189 | return getCloudRunDescriptor().catch(() => getGlobalDescriptor());
|
190 | case google_auth_library_1.GCPEnv.COMPUTE_ENGINE:
|
191 | return getGCEDescriptor().catch(() => getGlobalDescriptor());
|
192 | default:
|
193 | return getGlobalDescriptor();
|
194 | }
|
195 | }
|
196 | exports.getDefaultResource = getDefaultResource;
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | async function detectServiceContext(auth) {
|
206 | const env = await auth.getEnv();
|
207 | switch (env) {
|
208 | case google_auth_library_1.GCPEnv.APP_ENGINE:
|
209 | return {
|
210 | service: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
|
211 | version: process.env.GAE_VERSION || process.env.GAE_MODULE_VERSION,
|
212 | };
|
213 | case google_auth_library_1.GCPEnv.CLOUD_FUNCTIONS:
|
214 | return {
|
215 | service: process.env.FUNCTION_NAME,
|
216 | };
|
217 |
|
218 |
|
219 | case google_auth_library_1.GCPEnv.KUBERNETES_ENGINE:
|
220 | return {
|
221 | service: process.env.HOSTNAME,
|
222 | };
|
223 | case google_auth_library_1.GCPEnv.CLOUD_RUN:
|
224 | return {
|
225 | service: process.env.K_SERVICE,
|
226 | };
|
227 | case google_auth_library_1.GCPEnv.COMPUTE_ENGINE:
|
228 | return null;
|
229 | default:
|
230 | return null;
|
231 | }
|
232 | }
|
233 | exports.detectServiceContext = detectServiceContext;
|
234 |
|
\ | No newline at end of file |