@opentelemetry/resource-detector-gcp 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"faas.js","sourceRoot":"","sources":["../../../src/detectors/faas.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH;;;GAGG;AAEH,yCAAyC;AAEzC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAC/C,MAAM,yBAAyB,GAAG,iBAAiB,CAAC;AACpD,MAAM,gBAAgB,GAAG,WAAW,CAAC;AACrC,MAAM,iBAAiB,GAAG,YAAY,CAAC;AACvC,MAAM,oBAAoB,GAAG,QAAQ,CAAC;AAE/B,KAAK,UAAU,UAAU;IAC9B,OAAO,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,KAAK,SAAS,CAAC;AACzD,CAAC;AAFD,gCAEC;AAEM,KAAK,UAAU,gBAAgB;IACpC,OAAO,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,KAAK,SAAS,CAAC;AAC9D,CAAC;AAFD,4CAEC;AAED;;;GAGG;AACI,KAAK,UAAU,QAAQ;IAC5B,OAAO,SAAS,CAAC,gBAAgB,CAAC,CAAC;AACrC,CAAC;AAFD,4BAEC;AAED;;;GAGG;AACI,KAAK,UAAU,WAAW;IAC/B,OAAO,SAAS,CAAC,iBAAiB,CAAC,CAAC;AACtC,CAAC;AAFD,kCAEC;AAED;;;;GAIG;AACI,KAAK,UAAU,YAAY;IAChC,mFAAmF;IACnF,2FAA2F;IAC3F,MAAM,EAAE,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAkB,gBAAgB,CAAC,CAAC;IACtE,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC;AACvB,CAAC;AALD,oCAKC;AAED;;;;GAIG;AACI,KAAK,UAAU,eAAe;IACnC,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAS,oBAAoB,CAAC,CAAC;IACrE,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AACnD,CAAC;AAHD,0CAGC;AAED,SAAS,SAAS,CAAC,GAAW;IAC5B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,GAAG,KAAK,SAAS,EAAE;QACrB,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,YAAY,CAAC,CAAC;KAC1D;IACD,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["/*\n * Copyright 2023 Google LLC\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Implementation in this file copied from\n * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/faas.go\n */\n\nimport * as metadata from 'gcp-metadata';\n\nconst ID_METADATA_ATTR = 'id';\nconst CLOUD_RUN_CONFIG_ENV = 'K_CONFIGURATION';\nconst CLOUD_FUNCTION_TARGET_ENV = 'FUNCTION_TARGET';\nconst FAAS_SERVICE_ENV = 'K_SERVICE';\nconst FAAS_REVISION_ENV = 'K_REVISION';\nconst REGION_METADATA_ATTR = 'region';\n\nexport async function onCloudRun(): Promise<boolean> {\n return process.env[CLOUD_RUN_CONFIG_ENV] !== undefined;\n}\n\nexport async function onCloudFunctions(): Promise<boolean> {\n return process.env[CLOUD_FUNCTION_TARGET_ENV] !== undefined;\n}\n\n/**\n * The name of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or {@link\n * onCloudFunctions()} is true before calling this, or it may throw exceptions.\n */\nexport async function faasName(): Promise<string> {\n return lookupEnv(FAAS_SERVICE_ENV);\n}\n\n/**\n * The version/revision of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or\n * {@link onCloudFunctions()} is true before calling this, or it may throw exceptions.\n */\nexport async function faasVersion(): Promise<string> {\n return lookupEnv(FAAS_REVISION_ENV);\n}\n\n/**\n * The ID for the running instance of a Cloud Run or Cloud Function. Check that {@link\n * onCloudRun()} or {@link onCloudFunctions()} is true before calling this, or it may throw\n * exceptions.\n */\nexport async function faasInstance(): Promise<string> {\n // May be a bignumber.js BigNumber which can just be converted with toString(). See\n // https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties\n const id = await metadata.instance<number | object>(ID_METADATA_ATTR);\n return id.toString();\n}\n\n/**\n * The cloud region where the running instance of a Cloud Run or Cloud Function is located.\n * Check that {@link onCloudRun()} or {@link onCloudFunctions()} is true before calling this,\n * or it may throw exceptions.\n */\nexport async function faasCloudRegion(): Promise<string> {\n const region = await metadata.instance<string>(REGION_METADATA_ATTR);\n return region.slice(region.lastIndexOf('/') + 1);\n}\n\nfunction lookupEnv(key: string): string {\n const val = process.env[key];\n if (val === undefined) {\n throw new Error(`Environment variable ${key} not found`);\n }\n return val;\n}\n"]}
@@ -0,0 +1,36 @@
1
+ export declare function onAppEngineStandard(): Promise<boolean>;
2
+ export declare function onAppEngine(): Promise<boolean>;
3
+ /**
4
+ * The service name of the app engine service. Check that {@link onAppEngine()} is true before
5
+ * calling this, or it may throw exceptions.
6
+ */
7
+ export declare function serviceName(): Promise<string>;
8
+ /**
9
+ * The service version of the app engine service. Check that {@link onAppEngine()} is true
10
+ * before calling this, or it may throw exceptions.
11
+ */
12
+ export declare function serviceVersion(): Promise<string>;
13
+ /**
14
+ * The service instance of the app engine service. Check that {@link onAppEngine()} is true
15
+ * before calling this, or it may throw exceptions.
16
+ */
17
+ export declare function serviceInstance(): Promise<string>;
18
+ /**
19
+ * The zone and region in which this program is running. Check that {@link onAppEngine()} is
20
+ * true before calling this, or it may throw exceptions.
21
+ */
22
+ export declare function flexAvailabilityZoneAndRegion(): Promise<{
23
+ zone: string;
24
+ region: string;
25
+ }>;
26
+ /**
27
+ * The zone the app engine service is running in. Check that {@link onAppEngineStandard()} is
28
+ * true before calling this, or it may throw exceptions.
29
+ */
30
+ export declare function standardAvailabilityZone(): Promise<string>;
31
+ /**
32
+ * The region the app engine service is running in. Check that {@link onAppEngineStandard()} is
33
+ * true before calling this, or it may throw exceptions.
34
+ */
35
+ export declare function standardCloudRegion(): Promise<string>;
36
+ //# sourceMappingURL=gae.d.ts.map
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ * Copyright The OpenTelemetry Authors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * https://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.standardCloudRegion = exports.standardAvailabilityZone = exports.flexAvailabilityZoneAndRegion = exports.serviceInstance = exports.serviceVersion = exports.serviceName = exports.onAppEngine = exports.onAppEngineStandard = void 0;
20
+ /**
21
+ * Implementation in this file copied from
22
+ * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/app_engine.go
23
+ */
24
+ const metadata = require("gcp-metadata");
25
+ const gce = require("./gce");
26
+ const faas = require("./faas");
27
+ const GAE_SERVICE_ENV = 'GAE_SERVICE';
28
+ const GAE_VERSION_ENV = 'GAE_VERSION';
29
+ const GAE_INSTANCE_ENV = 'GAE_INSTANCE';
30
+ const GAE_ENV = 'GAE_ENV';
31
+ const GAE_STANDARD = 'standard';
32
+ const ZONE_METADATA_ATTR = 'zone';
33
+ async function onAppEngineStandard() {
34
+ return process.env[GAE_ENV] === GAE_STANDARD;
35
+ }
36
+ exports.onAppEngineStandard = onAppEngineStandard;
37
+ async function onAppEngine() {
38
+ return process.env[GAE_SERVICE_ENV] !== undefined;
39
+ }
40
+ exports.onAppEngine = onAppEngine;
41
+ /**
42
+ * The service name of the app engine service. Check that {@link onAppEngine()} is true before
43
+ * calling this, or it may throw exceptions.
44
+ */
45
+ async function serviceName() {
46
+ return lookupEnv(GAE_SERVICE_ENV);
47
+ }
48
+ exports.serviceName = serviceName;
49
+ /**
50
+ * The service version of the app engine service. Check that {@link onAppEngine()} is true
51
+ * before calling this, or it may throw exceptions.
52
+ */
53
+ async function serviceVersion() {
54
+ return lookupEnv(GAE_VERSION_ENV);
55
+ }
56
+ exports.serviceVersion = serviceVersion;
57
+ /**
58
+ * The service instance of the app engine service. Check that {@link onAppEngine()} is true
59
+ * before calling this, or it may throw exceptions.
60
+ */
61
+ async function serviceInstance() {
62
+ return lookupEnv(GAE_INSTANCE_ENV);
63
+ }
64
+ exports.serviceInstance = serviceInstance;
65
+ /**
66
+ * The zone and region in which this program is running. Check that {@link onAppEngine()} is
67
+ * true before calling this, or it may throw exceptions.
68
+ */
69
+ async function flexAvailabilityZoneAndRegion() {
70
+ return await gce.availabilityZoneAndRegion();
71
+ }
72
+ exports.flexAvailabilityZoneAndRegion = flexAvailabilityZoneAndRegion;
73
+ /**
74
+ * The zone the app engine service is running in. Check that {@link onAppEngineStandard()} is
75
+ * true before calling this, or it may throw exceptions.
76
+ */
77
+ async function standardAvailabilityZone() {
78
+ const zone = await metadata.instance(ZONE_METADATA_ATTR);
79
+ // zone is of the form "projects/233510669999/zones/us15"
80
+ return zone.slice(zone.lastIndexOf('/') + 1);
81
+ }
82
+ exports.standardAvailabilityZone = standardAvailabilityZone;
83
+ /**
84
+ * The region the app engine service is running in. Check that {@link onAppEngineStandard()} is
85
+ * true before calling this, or it may throw exceptions.
86
+ */
87
+ async function standardCloudRegion() {
88
+ return await faas.faasCloudRegion();
89
+ }
90
+ exports.standardCloudRegion = standardCloudRegion;
91
+ function lookupEnv(key) {
92
+ const val = process.env[key];
93
+ if (val === undefined) {
94
+ throw new Error(`Environment variable ${key} not found`);
95
+ }
96
+ return val;
97
+ }
98
+ //# sourceMappingURL=gae.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gae.js","sourceRoot":"","sources":["../../../src/detectors/gae.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH;;;GAGG;AAEH,yCAAyC;AACzC,6BAA6B;AAC7B,+BAA+B;AAE/B,MAAM,eAAe,GAAG,aAAa,CAAC;AACtC,MAAM,eAAe,GAAG,aAAa,CAAC;AACtC,MAAM,gBAAgB,GAAG,cAAc,CAAC;AACxC,MAAM,OAAO,GAAG,SAAS,CAAC;AAC1B,MAAM,YAAY,GAAG,UAAU,CAAC;AAChC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAE3B,KAAK,UAAU,mBAAmB;IACvC,OAAO,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,YAAY,CAAC;AAC/C,CAAC;AAFD,kDAEC;AAEM,KAAK,UAAU,WAAW;IAC/B,OAAO,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,KAAK,SAAS,CAAC;AACpD,CAAC;AAFD,kCAEC;AAED;;;GAGG;AACI,KAAK,UAAU,WAAW;IAC/B,OAAO,SAAS,CAAC,eAAe,CAAC,CAAC;AACpC,CAAC;AAFD,kCAEC;AAED;;;GAGG;AACI,KAAK,UAAU,cAAc;IAClC,OAAO,SAAS,CAAC,eAAe,CAAC,CAAC;AACpC,CAAC;AAFD,wCAEC;AAED;;;GAGG;AACI,KAAK,UAAU,eAAe;IACnC,OAAO,SAAS,CAAC,gBAAgB,CAAC,CAAC;AACrC,CAAC;AAFD,0CAEC;AAED;;;GAGG;AACI,KAAK,UAAU,6BAA6B;IAIjD,OAAO,MAAM,GAAG,CAAC,yBAAyB,EAAE,CAAC;AAC/C,CAAC;AALD,sEAKC;AAED;;;GAGG;AACI,KAAK,UAAU,wBAAwB;IAC5C,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAS,kBAAkB,CAAC,CAAC;IACjE,yDAAyD;IACzD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAC/C,CAAC;AAJD,4DAIC;AAED;;;GAGG;AACI,KAAK,UAAU,mBAAmB;IACvC,OAAO,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;AACtC,CAAC;AAFD,kDAEC;AAED,SAAS,SAAS,CAAC,GAAW;IAC5B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,GAAG,KAAK,SAAS,EAAE;QACrB,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,YAAY,CAAC,CAAC;KAC1D;IACD,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["/*\n * Copyright 2023 Google LLC\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Implementation in this file copied from\n * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/app_engine.go\n */\n\nimport * as metadata from 'gcp-metadata';\nimport * as gce from './gce';\nimport * as faas from './faas';\n\nconst GAE_SERVICE_ENV = 'GAE_SERVICE';\nconst GAE_VERSION_ENV = 'GAE_VERSION';\nconst GAE_INSTANCE_ENV = 'GAE_INSTANCE';\nconst GAE_ENV = 'GAE_ENV';\nconst GAE_STANDARD = 'standard';\nconst ZONE_METADATA_ATTR = 'zone';\n\nexport async function onAppEngineStandard(): Promise<boolean> {\n return process.env[GAE_ENV] === GAE_STANDARD;\n}\n\nexport async function onAppEngine(): Promise<boolean> {\n return process.env[GAE_SERVICE_ENV] !== undefined;\n}\n\n/**\n * The service name of the app engine service. Check that {@link onAppEngine()} is true before\n * calling this, or it may throw exceptions.\n */\nexport async function serviceName(): Promise<string> {\n return lookupEnv(GAE_SERVICE_ENV);\n}\n\n/**\n * The service version of the app engine service. Check that {@link onAppEngine()} is true\n * before calling this, or it may throw exceptions.\n */\nexport async function serviceVersion(): Promise<string> {\n return lookupEnv(GAE_VERSION_ENV);\n}\n\n/**\n * The service instance of the app engine service. Check that {@link onAppEngine()} is true\n * before calling this, or it may throw exceptions.\n */\nexport async function serviceInstance(): Promise<string> {\n return lookupEnv(GAE_INSTANCE_ENV);\n}\n\n/**\n * The zone and region in which this program is running. Check that {@link onAppEngine()} is\n * true before calling this, or it may throw exceptions.\n */\nexport async function flexAvailabilityZoneAndRegion(): Promise<{\n zone: string;\n region: string;\n}> {\n return await gce.availabilityZoneAndRegion();\n}\n\n/**\n * The zone the app engine service is running in. Check that {@link onAppEngineStandard()} is\n * true before calling this, or it may throw exceptions.\n */\nexport async function standardAvailabilityZone(): Promise<string> {\n const zone = await metadata.instance<string>(ZONE_METADATA_ATTR);\n // zone is of the form \"projects/233510669999/zones/us15\"\n return zone.slice(zone.lastIndexOf('/') + 1);\n}\n\n/**\n * The region the app engine service is running in. Check that {@link onAppEngineStandard()} is\n * true before calling this, or it may throw exceptions.\n */\nexport async function standardCloudRegion(): Promise<string> {\n return await faas.faasCloudRegion();\n}\n\nfunction lookupEnv(key: string): string {\n const val = process.env[key];\n if (val === undefined) {\n throw new Error(`Environment variable ${key} not found`);\n }\n return val;\n}\n"]}
@@ -0,0 +1,25 @@
1
+ export declare function onGce(): Promise<boolean>;
2
+ /**
3
+ * The machine type of the instance on which this program is running. Check that {@link
4
+ * onGce()} is true before calling this, or it may throw exceptions.
5
+ */
6
+ export declare function hostType(): Promise<string>;
7
+ /**
8
+ * The instance ID of the instance on which this program is running. Check that {@link onGce()}
9
+ * is true before calling this, or it may throw exceptions.
10
+ */
11
+ export declare function hostId(): Promise<string>;
12
+ /**
13
+ * The instance ID of the instance on which this program is running. Check that {@link onGce()}
14
+ * is true before calling this, or it may throw exceptions.
15
+ */
16
+ export declare function hostName(): Promise<string>;
17
+ /**
18
+ * The zone and region in which this program is running. Check that {@link onGce()} is true
19
+ * before calling this, or it may throw exceptions.
20
+ */
21
+ export declare function availabilityZoneAndRegion(): Promise<{
22
+ zone: string;
23
+ region: string;
24
+ }>;
25
+ //# sourceMappingURL=gce.d.ts.map
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2022 Google LLC
4
+ * Copyright The OpenTelemetry Authors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * https://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.availabilityZoneAndRegion = exports.hostName = exports.hostId = exports.hostType = exports.onGce = void 0;
20
+ /**
21
+ * Implementation in this file copied from
22
+ * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gce.go
23
+ */
24
+ const api_1 = require("@opentelemetry/api");
25
+ const metadata = require("gcp-metadata");
26
+ const MACHINE_TYPE_METADATA_ATTR = 'machine-type';
27
+ const ID_METADATA_ATTR = 'id';
28
+ const HOST_NAME_METADATA_ATTR = 'name';
29
+ const ZONE_METADATA_ATTR = 'zone';
30
+ async function onGce() {
31
+ try {
32
+ await metadata.instance(MACHINE_TYPE_METADATA_ATTR);
33
+ return true;
34
+ }
35
+ catch (err) {
36
+ api_1.diag.debug('Could not fetch metadata attribute %s, assuming not on GCE. Error was %s', MACHINE_TYPE_METADATA_ATTR, err);
37
+ return false;
38
+ }
39
+ }
40
+ exports.onGce = onGce;
41
+ /**
42
+ * The machine type of the instance on which this program is running. Check that {@link
43
+ * onGce()} is true before calling this, or it may throw exceptions.
44
+ */
45
+ async function hostType() {
46
+ return metadata.instance(MACHINE_TYPE_METADATA_ATTR);
47
+ }
48
+ exports.hostType = hostType;
49
+ /**
50
+ * The instance ID of the instance on which this program is running. Check that {@link onGce()}
51
+ * is true before calling this, or it may throw exceptions.
52
+ */
53
+ async function hostId() {
54
+ // May be a bignumber.js BigNumber which can just be converted with toString(). See
55
+ // https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties
56
+ const id = await metadata.instance(ID_METADATA_ATTR);
57
+ return id.toString();
58
+ }
59
+ exports.hostId = hostId;
60
+ /**
61
+ * The instance ID of the instance on which this program is running. Check that {@link onGce()}
62
+ * is true before calling this, or it may throw exceptions.
63
+ */
64
+ async function hostName() {
65
+ return metadata.instance(HOST_NAME_METADATA_ATTR);
66
+ }
67
+ exports.hostName = hostName;
68
+ /**
69
+ * The zone and region in which this program is running. Check that {@link onGce()} is true
70
+ * before calling this, or it may throw exceptions.
71
+ */
72
+ async function availabilityZoneAndRegion() {
73
+ const fullZone = await metadata.instance(ZONE_METADATA_ATTR);
74
+ // Format described in
75
+ // https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata
76
+ const re = /projects\/\d+\/zones\/(?<zone>(?<region>\w+-\w+)-\w+)/;
77
+ const { zone, region } = fullZone.match(re)?.groups ?? {};
78
+ if (!zone || !region) {
79
+ throw new Error(`zone was not in the expected format: projects/PROJECT_NUM/zones/COUNTRY-REGION-ZONE. Got ${fullZone}`);
80
+ }
81
+ return { zone, region };
82
+ }
83
+ exports.availabilityZoneAndRegion = availabilityZoneAndRegion;
84
+ //# sourceMappingURL=gce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gce.js","sourceRoot":"","sources":["../../../src/detectors/gce.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH;;;GAGG;AAEH,4CAA0C;AAC1C,yCAAyC;AAEzC,MAAM,0BAA0B,GAAG,cAAc,CAAC;AAClD,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,uBAAuB,GAAG,MAAM,CAAC;AACvC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAE3B,KAAK,UAAU,KAAK;IACzB,IAAI;QACF,MAAM,QAAQ,CAAC,QAAQ,CAAS,0BAA0B,CAAC,CAAC;QAC5D,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,GAAG,EAAE;QACZ,UAAI,CAAC,KAAK,CACR,0EAA0E,EAC1E,0BAA0B,EAC1B,GAAG,CACJ,CAAC;QACF,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAZD,sBAYC;AAED;;;GAGG;AACI,KAAK,UAAU,QAAQ;IAC5B,OAAO,QAAQ,CAAC,QAAQ,CAAS,0BAA0B,CAAC,CAAC;AAC/D,CAAC;AAFD,4BAEC;AAED;;;GAGG;AACI,KAAK,UAAU,MAAM;IAC1B,mFAAmF;IACnF,2FAA2F;IAC3F,MAAM,EAAE,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAkB,gBAAgB,CAAC,CAAC;IACtE,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC;AACvB,CAAC;AALD,wBAKC;AAED;;;GAGG;AACI,KAAK,UAAU,QAAQ;IAC5B,OAAO,QAAQ,CAAC,QAAQ,CAAS,uBAAuB,CAAC,CAAC;AAC5D,CAAC;AAFD,4BAEC;AAED;;;GAGG;AACI,KAAK,UAAU,yBAAyB;IAI7C,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAS,kBAAkB,CAAC,CAAC;IAErE,sBAAsB;IACtB,8FAA8F;IAC9F,MAAM,EAAE,GAAG,uDAAuD,CAAC;IACnE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,EAAE,CAAC;IAC1D,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE;QACpB,MAAM,IAAI,KAAK,CACb,4FAA4F,QAAQ,EAAE,CACvG,CAAC;KACH;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAC1B,CAAC;AAjBD,8DAiBC","sourcesContent":["/*\n * Copyright 2022 Google LLC\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Implementation in this file copied from\n * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gce.go\n */\n\nimport { diag } from '@opentelemetry/api';\nimport * as metadata from 'gcp-metadata';\n\nconst MACHINE_TYPE_METADATA_ATTR = 'machine-type';\nconst ID_METADATA_ATTR = 'id';\nconst HOST_NAME_METADATA_ATTR = 'name';\nconst ZONE_METADATA_ATTR = 'zone';\n\nexport async function onGce(): Promise<boolean> {\n try {\n await metadata.instance<string>(MACHINE_TYPE_METADATA_ATTR);\n return true;\n } catch (err) {\n diag.debug(\n 'Could not fetch metadata attribute %s, assuming not on GCE. Error was %s',\n MACHINE_TYPE_METADATA_ATTR,\n err\n );\n return false;\n }\n}\n\n/**\n * The machine type of the instance on which this program is running. Check that {@link\n * onGce()} is true before calling this, or it may throw exceptions.\n */\nexport async function hostType(): Promise<string> {\n return metadata.instance<string>(MACHINE_TYPE_METADATA_ATTR);\n}\n\n/**\n * The instance ID of the instance on which this program is running. Check that {@link onGce()}\n * is true before calling this, or it may throw exceptions.\n */\nexport async function hostId(): Promise<string> {\n // May be a bignumber.js BigNumber which can just be converted with toString(). See\n // https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties\n const id = await metadata.instance<number | object>(ID_METADATA_ATTR);\n return id.toString();\n}\n\n/**\n * The instance ID of the instance on which this program is running. Check that {@link onGce()}\n * is true before calling this, or it may throw exceptions.\n */\nexport async function hostName(): Promise<string> {\n return metadata.instance<string>(HOST_NAME_METADATA_ATTR);\n}\n\n/**\n * The zone and region in which this program is running. Check that {@link onGce()} is true\n * before calling this, or it may throw exceptions.\n */\nexport async function availabilityZoneAndRegion(): Promise<{\n zone: string;\n region: string;\n}> {\n const fullZone = await metadata.instance<string>(ZONE_METADATA_ATTR);\n\n // Format described in\n // https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata\n const re = /projects\\/\\d+\\/zones\\/(?<zone>(?<region>\\w+-\\w+)-\\w+)/;\n const { zone, region } = fullZone.match(re)?.groups ?? {};\n if (!zone || !region) {\n throw new Error(\n `zone was not in the expected format: projects/PROJECT_NUM/zones/COUNTRY-REGION-ZONE. Got ${fullZone}`\n );\n }\n\n return { zone, region };\n}\n"]}
@@ -0,0 +1,20 @@
1
+ export declare function onGke(): Promise<boolean>;
2
+ /**
3
+ * The instance ID of the instance on which this program is running. Check that {@link onGke()}
4
+ * is true before calling this, or it may throw exceptions.
5
+ */
6
+ export declare function hostId(): Promise<string>;
7
+ /**
8
+ * The name of the GKE cluster in which this program is running. Check that {@link onGke()} is
9
+ * true before calling this, or it may throw exceptions.
10
+ */
11
+ export declare function clusterName(): Promise<string>;
12
+ /**
13
+ * The location of the cluster and whether the cluster is zonal or regional. Check that {@link
14
+ * onGke()} is true before calling this, or it may throw exceptions.
15
+ */
16
+ export declare function availabilityZoneOrRegion(): Promise<{
17
+ type: 'zone' | 'region';
18
+ value: string;
19
+ }>;
20
+ //# sourceMappingURL=gke.d.ts.map
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2022 Google LLC
4
+ * Copyright The OpenTelemetry Authors
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * https://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.availabilityZoneOrRegion = exports.clusterName = exports.hostId = exports.onGke = void 0;
20
+ /**
21
+ * Implementation in this file copied from
22
+ * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gke.go
23
+ */
24
+ const metadata = require("gcp-metadata");
25
+ const gce = require("./gce");
26
+ const KUBERNETES_SERVICE_HOST_ENV = 'KUBERNETES_SERVICE_HOST';
27
+ const CLUSTER_NAME_METADATA_ATTR = 'attributes/cluster-name';
28
+ const CLUSTER_LOCATION_METADATA_ATTR = 'attributes/cluster-location';
29
+ async function onGke() {
30
+ return process.env[KUBERNETES_SERVICE_HOST_ENV] !== undefined;
31
+ }
32
+ exports.onGke = onGke;
33
+ /**
34
+ * The instance ID of the instance on which this program is running. Check that {@link onGke()}
35
+ * is true before calling this, or it may throw exceptions.
36
+ */
37
+ async function hostId() {
38
+ return await gce.hostId();
39
+ }
40
+ exports.hostId = hostId;
41
+ /**
42
+ * The name of the GKE cluster in which this program is running. Check that {@link onGke()} is
43
+ * true before calling this, or it may throw exceptions.
44
+ */
45
+ async function clusterName() {
46
+ return metadata.instance(CLUSTER_NAME_METADATA_ATTR);
47
+ }
48
+ exports.clusterName = clusterName;
49
+ /**
50
+ * The location of the cluster and whether the cluster is zonal or regional. Check that {@link
51
+ * onGke()} is true before calling this, or it may throw exceptions.
52
+ */
53
+ async function availabilityZoneOrRegion() {
54
+ const clusterLocation = await metadata.instance(CLUSTER_LOCATION_METADATA_ATTR);
55
+ switch (countChar(clusterLocation, '-')) {
56
+ case 1:
57
+ return { type: 'region', value: clusterLocation };
58
+ case 2:
59
+ return { type: 'zone', value: clusterLocation };
60
+ default:
61
+ throw new Error(`unrecognized format for cluster location: ${clusterLocation}`);
62
+ }
63
+ }
64
+ exports.availabilityZoneOrRegion = availabilityZoneOrRegion;
65
+ function countChar(s, char) {
66
+ let count = 0;
67
+ for (let i = 0; i < s.length; i++) {
68
+ if (s[i] === char) {
69
+ count += 1;
70
+ }
71
+ }
72
+ return count;
73
+ }
74
+ //# sourceMappingURL=gke.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gke.js","sourceRoot":"","sources":["../../../src/detectors/gke.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH;;;GAGG;AAEH,yCAAyC;AACzC,6BAA6B;AAE7B,MAAM,2BAA2B,GAAG,yBAAyB,CAAC;AAC9D,MAAM,0BAA0B,GAAG,yBAAyB,CAAC;AAC7D,MAAM,8BAA8B,GAAG,6BAA6B,CAAC;AAE9D,KAAK,UAAU,KAAK;IACzB,OAAO,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,KAAK,SAAS,CAAC;AAChE,CAAC;AAFD,sBAEC;AAED;;;GAGG;AACI,KAAK,UAAU,MAAM;IAC1B,OAAO,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC;AAC5B,CAAC;AAFD,wBAEC;AAED;;;GAGG;AACI,KAAK,UAAU,WAAW;IAC/B,OAAO,QAAQ,CAAC,QAAQ,CAAS,0BAA0B,CAAC,CAAC;AAC/D,CAAC;AAFD,kCAEC;AAED;;;GAGG;AACI,KAAK,UAAU,wBAAwB;IAI5C,MAAM,eAAe,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAC7C,8BAA8B,CAC/B,CAAC;IACF,QAAQ,SAAS,CAAC,eAAe,EAAE,GAAG,CAAC,EAAE;QACvC,KAAK,CAAC;YACJ,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC;QACpD,KAAK,CAAC;YACJ,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC;QAClD;YACE,MAAM,IAAI,KAAK,CACb,6CAA6C,eAAe,EAAE,CAC/D,CAAC;KACL;AACH,CAAC;AAjBD,4DAiBC;AAED,SAAS,SAAS,CAAC,CAAS,EAAE,IAAY;IACxC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACjC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE;YACjB,KAAK,IAAI,CAAC,CAAC;SACZ;KACF;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/*\n * Copyright 2022 Google LLC\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Implementation in this file copied from\n * https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gke.go\n */\n\nimport * as metadata from 'gcp-metadata';\nimport * as gce from './gce';\n\nconst KUBERNETES_SERVICE_HOST_ENV = 'KUBERNETES_SERVICE_HOST';\nconst CLUSTER_NAME_METADATA_ATTR = 'attributes/cluster-name';\nconst CLUSTER_LOCATION_METADATA_ATTR = 'attributes/cluster-location';\n\nexport async function onGke(): Promise<boolean> {\n return process.env[KUBERNETES_SERVICE_HOST_ENV] !== undefined;\n}\n\n/**\n * The instance ID of the instance on which this program is running. Check that {@link onGke()}\n * is true before calling this, or it may throw exceptions.\n */\nexport async function hostId(): Promise<string> {\n return await gce.hostId();\n}\n\n/**\n * The name of the GKE cluster in which this program is running. Check that {@link onGke()} is\n * true before calling this, or it may throw exceptions.\n */\nexport async function clusterName(): Promise<string> {\n return metadata.instance<string>(CLUSTER_NAME_METADATA_ATTR);\n}\n\n/**\n * The location of the cluster and whether the cluster is zonal or regional. Check that {@link\n * onGke()} is true before calling this, or it may throw exceptions.\n */\nexport async function availabilityZoneOrRegion(): Promise<{\n type: 'zone' | 'region';\n value: string;\n}> {\n const clusterLocation = await metadata.instance<string>(\n CLUSTER_LOCATION_METADATA_ATTR\n );\n switch (countChar(clusterLocation, '-')) {\n case 1:\n return { type: 'region', value: clusterLocation };\n case 2:\n return { type: 'zone', value: clusterLocation };\n default:\n throw new Error(\n `unrecognized format for cluster location: ${clusterLocation}`\n );\n }\n}\n\nfunction countChar(s: string, char: string): number {\n let count = 0;\n for (let i = 0; i < s.length; i++) {\n if (s[i] === char) {\n count += 1;\n }\n }\n return count;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentelemetry/resource-detector-gcp",
3
- "version": "0.38.0",
3
+ "version": "0.40.0",
4
4
  "description": "OpenTelemetry SDK resource detector for GCP",
5
5
  "main": "build/src/index.js",
6
6
  "module": "build/esm/index.js",
@@ -46,15 +46,16 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@opentelemetry/api": "^1.0.0",
49
- "@opentelemetry/contrib-test-utils": "^0.50.0",
50
- "@opentelemetry/instrumentation-http": "^0.204.0",
49
+ "@opentelemetry/contrib-test-utils": "^0.51.0",
50
+ "@opentelemetry/instrumentation-http": "^0.205.0",
51
51
  "@opentelemetry/sdk-trace-base": "^2.0.0",
52
52
  "@types/mocha": "10.0.10",
53
53
  "@types/node": "18.18.14",
54
54
  "@types/semver": "7.5.8",
55
- "nock": "13.3.3",
55
+ "@types/sinon": "17.0.4",
56
+ "bignumber.js": "9.3.1",
56
57
  "nyc": "17.1.0",
57
- "rimraf": "5.0.10",
58
+ "sinon": "15.2.0",
58
59
  "typescript": "5.0.4"
59
60
  },
60
61
  "peerDependencies": {
@@ -68,5 +69,5 @@
68
69
  },
69
70
  "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/resource-detector-gcp#readme",
70
71
  "sideEffects": false,
71
- "gitHead": "0a45ac1b951d2acd2e40834e7ae012c04820faec"
72
+ "gitHead": "9d689e167e92b09fca91906e20790496a49af384"
72
73
  }