@opentelemetry/resource-detector-gcp 0.39.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.
- package/build/esm/detectors/GcpDetector.d.ts +6 -21
- package/build/esm/detectors/GcpDetector.js +150 -99
- package/build/esm/detectors/GcpDetector.js.map +1 -1
- package/build/esm/detectors/faas.d.ts +25 -0
- package/build/esm/detectors/faas.js +75 -0
- package/build/esm/detectors/faas.js.map +1 -0
- package/build/esm/detectors/gae.d.ts +36 -0
- package/build/esm/detectors/gae.js +87 -0
- package/build/esm/detectors/gae.js.map +1 -0
- package/build/esm/detectors/gce.d.ts +25 -0
- package/build/esm/detectors/gce.js +76 -0
- package/build/esm/detectors/gce.js.map +1 -0
- package/build/esm/detectors/gke.d.ts +20 -0
- package/build/esm/detectors/gke.js +67 -0
- package/build/esm/detectors/gke.js.map +1 -0
- package/build/src/detectors/GcpDetector.d.ts +6 -21
- package/build/src/detectors/GcpDetector.js +151 -99
- package/build/src/detectors/GcpDetector.js.map +1 -1
- package/build/src/detectors/faas.d.ts +25 -0
- package/build/src/detectors/faas.js +84 -0
- package/build/src/detectors/faas.js.map +1 -0
- package/build/src/detectors/gae.d.ts +36 -0
- package/build/src/detectors/gae.js +98 -0
- package/build/src/detectors/gae.js.map +1 -0
- package/build/src/detectors/gce.d.ts +25 -0
- package/build/src/detectors/gce.js +84 -0
- package/build/src/detectors/gce.js.map +1 -0
- package/build/src/detectors/gke.d.ts +20 -0
- package/build/src/detectors/gke.js +74 -0
- package/build/src/detectors/gke.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Google LLC
|
|
3
|
+
* Copyright The OpenTelemetry Authors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Implementation in this file copied from
|
|
19
|
+
* https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gce.go
|
|
20
|
+
*/
|
|
21
|
+
import { diag } from '@opentelemetry/api';
|
|
22
|
+
import * as metadata from 'gcp-metadata';
|
|
23
|
+
const MACHINE_TYPE_METADATA_ATTR = 'machine-type';
|
|
24
|
+
const ID_METADATA_ATTR = 'id';
|
|
25
|
+
const HOST_NAME_METADATA_ATTR = 'name';
|
|
26
|
+
const ZONE_METADATA_ATTR = 'zone';
|
|
27
|
+
export async function onGce() {
|
|
28
|
+
try {
|
|
29
|
+
await metadata.instance(MACHINE_TYPE_METADATA_ATTR);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
diag.debug('Could not fetch metadata attribute %s, assuming not on GCE. Error was %s', MACHINE_TYPE_METADATA_ATTR, err);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The machine type of the instance on which this program is running. Check that {@link
|
|
39
|
+
* onGce()} is true before calling this, or it may throw exceptions.
|
|
40
|
+
*/
|
|
41
|
+
export async function hostType() {
|
|
42
|
+
return metadata.instance(MACHINE_TYPE_METADATA_ATTR);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The instance ID of the instance on which this program is running. Check that {@link onGce()}
|
|
46
|
+
* is true before calling this, or it may throw exceptions.
|
|
47
|
+
*/
|
|
48
|
+
export async function hostId() {
|
|
49
|
+
// May be a bignumber.js BigNumber which can just be converted with toString(). See
|
|
50
|
+
// https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties
|
|
51
|
+
const id = await metadata.instance(ID_METADATA_ATTR);
|
|
52
|
+
return id.toString();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The instance ID of the instance on which this program is running. Check that {@link onGce()}
|
|
56
|
+
* is true before calling this, or it may throw exceptions.
|
|
57
|
+
*/
|
|
58
|
+
export async function hostName() {
|
|
59
|
+
return metadata.instance(HOST_NAME_METADATA_ATTR);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The zone and region in which this program is running. Check that {@link onGce()} is true
|
|
63
|
+
* before calling this, or it may throw exceptions.
|
|
64
|
+
*/
|
|
65
|
+
export async function availabilityZoneAndRegion() {
|
|
66
|
+
const fullZone = await metadata.instance(ZONE_METADATA_ATTR);
|
|
67
|
+
// Format described in
|
|
68
|
+
// https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata
|
|
69
|
+
const re = /projects\/\d+\/zones\/(?<zone>(?<region>\w+-\w+)-\w+)/;
|
|
70
|
+
const { zone, region } = fullZone.match(re)?.groups ?? {};
|
|
71
|
+
if (!zone || !region) {
|
|
72
|
+
throw new Error(`zone was not in the expected format: projects/PROJECT_NUM/zones/COUNTRY-REGION-ZONE. Got ${fullZone}`);
|
|
73
|
+
}
|
|
74
|
+
return { zone, region };
|
|
75
|
+
}
|
|
76
|
+
//# 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,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AAEzC,MAAM,0BAA0B,GAAG,cAAc,CAAC;AACl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AK,CACb,4FAA4F,QAAQ,EAAE,CACvG,CAAC;KACH;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAC1B,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/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,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 Google LLC
|
|
3
|
+
* Copyright The OpenTelemetry Authors
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Implementation in this file copied from
|
|
19
|
+
* https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/gke.go
|
|
20
|
+
*/
|
|
21
|
+
import * as metadata from 'gcp-metadata';
|
|
22
|
+
import * as gce from './gce';
|
|
23
|
+
const KUBERNETES_SERVICE_HOST_ENV = 'KUBERNETES_SERVICE_HOST';
|
|
24
|
+
const CLUSTER_NAME_METADATA_ATTR = 'attributes/cluster-name';
|
|
25
|
+
const CLUSTER_LOCATION_METADATA_ATTR = 'attributes/cluster-location';
|
|
26
|
+
export async function onGke() {
|
|
27
|
+
return process.env[KUBERNETES_SERVICE_HOST_ENV] !== undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The instance ID of the instance on which this program is running. Check that {@link onGke()}
|
|
31
|
+
* is true before calling this, or it may throw exceptions.
|
|
32
|
+
*/
|
|
33
|
+
export async function hostId() {
|
|
34
|
+
return await gce.hostId();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The name of the GKE cluster in which this program is running. Check that {@link onGke()} is
|
|
38
|
+
* true before calling this, or it may throw exceptions.
|
|
39
|
+
*/
|
|
40
|
+
export async function clusterName() {
|
|
41
|
+
return metadata.instance(CLUSTER_NAME_METADATA_ATTR);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The location of the cluster and whether the cluster is zonal or regional. Check that {@link
|
|
45
|
+
* onGke()} is true before calling this, or it may throw exceptions.
|
|
46
|
+
*/
|
|
47
|
+
export async function availabilityZoneOrRegion() {
|
|
48
|
+
const clusterLocation = await metadata.instance(CLUSTER_LOCATION_METADATA_ATTR);
|
|
49
|
+
switch (countChar(clusterLocation, '-')) {
|
|
50
|
+
case 1:
|
|
51
|
+
return { type: 'region', value: clusterLocation };
|
|
52
|
+
case 2:
|
|
53
|
+
return { type: 'zone', value: clusterLocation };
|
|
54
|
+
default:
|
|
55
|
+
throw new Error(`unrecognized format for cluster location: ${clusterLocation}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function countChar(s, char) {
|
|
59
|
+
let count = 0;
|
|
60
|
+
for (let i = 0; i < s.length; i++) {
|
|
61
|
+
if (s[i] === char) {
|
|
62
|
+
count += 1;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return count;
|
|
66
|
+
}
|
|
67
|
+
//# 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,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAE7B,MAAM,2BAA2B,GAAG,yBAAyB,CAAC;AAC9D,MAAM,0BAA0B,GAAG,yBAAyB,CAAC;AAC7D,MAAM,8BAA8B,GAAG,6BAA6B,CAAC;AAErE,MAAM,CAAC,KAAK,UAAU,KAAK;IACzB,OAAO,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,KAAK,SAAS,CAAC;AAChE,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM;IAC1B,OAAO,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC;AAC5B,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW;IAC/B,OAAO,QAAQ,CAAC,QAAQ,CAAS,0BAA0B,CAAC,CAAC;AAC/D,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,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;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"]}
|
|
@@ -1,26 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DetectedResource, ResourceDetector } from '@opentelemetry/resources';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* the instance. Returns an empty Resource if detection fails.
|
|
3
|
+
* Google Cloud resource detector which populates attributes based on the environment this
|
|
4
|
+
* process is running in. If not on GCP, returns an empty resource.
|
|
6
5
|
*/
|
|
7
|
-
declare class GcpDetector implements ResourceDetector {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* Asynchronously gather GCP cloud metadata.
|
|
11
|
-
*/
|
|
12
|
-
private _getAttributes;
|
|
13
|
-
/** Gets project id from GCP project metadata. */
|
|
14
|
-
private _getProjectId;
|
|
15
|
-
/** Gets instance id from GCP instance metadata. */
|
|
16
|
-
private _getInstanceId;
|
|
17
|
-
/** Gets zone from GCP instance metadata. */
|
|
18
|
-
private _getZone;
|
|
19
|
-
/** Gets cluster name from GCP instance metadata. */
|
|
20
|
-
private _getClusterName;
|
|
21
|
-
/** Gets hostname from GCP instance metadata. */
|
|
22
|
-
private _getHostname;
|
|
6
|
+
export declare class GcpDetector implements ResourceDetector {
|
|
7
|
+
private _asyncAttributes;
|
|
8
|
+
detect(): DetectedResource;
|
|
23
9
|
}
|
|
24
10
|
export declare const gcpDetector: GcpDetector;
|
|
25
|
-
export {};
|
|
26
11
|
//# sourceMappingURL=GcpDetector.d.ts.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*
|
|
3
|
+
* Copyright 2022 Google LLC
|
|
3
4
|
* Copyright The OpenTelemetry Authors
|
|
4
5
|
*
|
|
5
6
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -15,115 +16,166 @@
|
|
|
15
16
|
* limitations under the License.
|
|
16
17
|
*/
|
|
17
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.gcpDetector = void 0;
|
|
19
|
-
const gcpMetadata = require("gcp-metadata");
|
|
19
|
+
exports.gcpDetector = exports.GcpDetector = void 0;
|
|
20
20
|
const api_1 = require("@opentelemetry/api");
|
|
21
21
|
const core_1 = require("@opentelemetry/core");
|
|
22
22
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
24
|
+
const metadata = require("gcp-metadata");
|
|
25
|
+
const faas = require("./faas");
|
|
26
|
+
const gae = require("./gae");
|
|
27
|
+
const gce = require("./gce");
|
|
28
|
+
const gke = require("./gke");
|
|
29
|
+
const ATTRIBUTE_NAMES = [
|
|
30
|
+
semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM,
|
|
31
|
+
semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
|
|
32
|
+
semantic_conventions_1.SEMRESATTRS_CLOUD_REGION,
|
|
33
|
+
semantic_conventions_1.SEMRESATTRS_K8S_CLUSTER_NAME,
|
|
34
|
+
semantic_conventions_1.SEMRESATTRS_HOST_TYPE,
|
|
35
|
+
semantic_conventions_1.SEMRESATTRS_HOST_ID,
|
|
36
|
+
semantic_conventions_1.SEMRESATTRS_HOST_NAME,
|
|
37
|
+
semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER,
|
|
38
|
+
semantic_conventions_1.SEMRESATTRS_CLOUD_ACCOUNT_ID,
|
|
39
|
+
semantic_conventions_1.SEMRESATTRS_FAAS_NAME,
|
|
40
|
+
semantic_conventions_1.SEMRESATTRS_FAAS_VERSION,
|
|
41
|
+
semantic_conventions_1.SEMRESATTRS_FAAS_INSTANCE,
|
|
42
|
+
];
|
|
43
|
+
async function detect() {
|
|
44
|
+
if (!(await metadata.isAvailable())) {
|
|
45
|
+
return (0, resources_1.emptyResource)();
|
|
46
|
+
}
|
|
47
|
+
// Note the order of these if checks is significant with more specific resources coming
|
|
48
|
+
// first. E.g. Cloud Functions gen2 are executed in Cloud Run so it must be checked first.
|
|
49
|
+
if (await gke.onGke()) {
|
|
50
|
+
return await gkeResource();
|
|
32
51
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*/
|
|
36
|
-
_getAttributes() {
|
|
37
|
-
const isAvail = gcpMetadata.isAvailable();
|
|
38
|
-
const attributes = {
|
|
39
|
-
[semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER]: (async () => {
|
|
40
|
-
return (await isAvail) ? semantic_conventions_1.CLOUDPROVIDERVALUES_GCP : undefined;
|
|
41
|
-
})(),
|
|
42
|
-
[semantic_conventions_1.SEMRESATTRS_CLOUD_ACCOUNT_ID]: this._getProjectId(isAvail),
|
|
43
|
-
[semantic_conventions_1.SEMRESATTRS_HOST_ID]: this._getInstanceId(isAvail),
|
|
44
|
-
[semantic_conventions_1.SEMRESATTRS_HOST_NAME]: this._getHostname(isAvail),
|
|
45
|
-
[semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: this._getZone(isAvail),
|
|
46
|
-
};
|
|
47
|
-
// Add resource attributes for K8s.
|
|
48
|
-
if (process.env.KUBERNETES_SERVICE_HOST) {
|
|
49
|
-
attributes[semantic_conventions_1.SEMRESATTRS_K8S_CLUSTER_NAME] = this._getClusterName(isAvail);
|
|
50
|
-
attributes[semantic_conventions_1.SEMRESATTRS_K8S_NAMESPACE_NAME] = (async () => {
|
|
51
|
-
return (await isAvail) ? process.env.NAMESPACE : undefined;
|
|
52
|
-
})();
|
|
53
|
-
attributes[semantic_conventions_1.SEMRESATTRS_K8S_POD_NAME] = (async () => {
|
|
54
|
-
return (await isAvail) ? process.env.HOSTNAME : undefined;
|
|
55
|
-
})();
|
|
56
|
-
attributes[semantic_conventions_1.SEMRESATTRS_CONTAINER_NAME] = (async () => {
|
|
57
|
-
return (await isAvail) ? process.env.CONTAINER_NAME : undefined;
|
|
58
|
-
})();
|
|
59
|
-
}
|
|
60
|
-
return attributes;
|
|
52
|
+
else if (await faas.onCloudFunctions()) {
|
|
53
|
+
return await cloudFunctionsResource();
|
|
61
54
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (!(await isAvail)) {
|
|
65
|
-
return undefined;
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
return await gcpMetadata.project('project-id');
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
return '';
|
|
72
|
-
}
|
|
55
|
+
else if (await faas.onCloudRun()) {
|
|
56
|
+
return await cloudRunResource();
|
|
73
57
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
58
|
+
else if (await gae.onAppEngine()) {
|
|
59
|
+
return await gaeResource();
|
|
60
|
+
}
|
|
61
|
+
else if (await gce.onGce()) {
|
|
62
|
+
return await gceResource();
|
|
63
|
+
}
|
|
64
|
+
return (0, resources_1.emptyResource)();
|
|
65
|
+
}
|
|
66
|
+
async function gkeResource() {
|
|
67
|
+
const [zoneOrRegion, k8sClusterName, hostId] = await Promise.all([
|
|
68
|
+
gke.availabilityZoneOrRegion(),
|
|
69
|
+
gke.clusterName(),
|
|
70
|
+
gke.hostId(),
|
|
71
|
+
]);
|
|
72
|
+
return await makeResource({
|
|
73
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM]: semantic_conventions_1.CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,
|
|
74
|
+
[zoneOrRegion.type === 'zone'
|
|
75
|
+
? semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE
|
|
76
|
+
: semantic_conventions_1.SEMRESATTRS_CLOUD_REGION]: zoneOrRegion.value,
|
|
77
|
+
[semantic_conventions_1.SEMRESATTRS_K8S_CLUSTER_NAME]: k8sClusterName,
|
|
78
|
+
[semantic_conventions_1.SEMRESATTRS_HOST_ID]: hostId,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async function cloudRunResource() {
|
|
82
|
+
const [faasName, faasVersion, faasInstance, faasCloudRegion] = await Promise.all([
|
|
83
|
+
faas.faasName(),
|
|
84
|
+
faas.faasVersion(),
|
|
85
|
+
faas.faasInstance(),
|
|
86
|
+
faas.faasCloudRegion(),
|
|
87
|
+
]);
|
|
88
|
+
return await makeResource({
|
|
89
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM]: semantic_conventions_1.CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,
|
|
90
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_NAME]: faasName,
|
|
91
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_VERSION]: faasVersion,
|
|
92
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
|
|
93
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async function cloudFunctionsResource() {
|
|
97
|
+
const [faasName, faasVersion, faasInstance, faasCloudRegion] = await Promise.all([
|
|
98
|
+
faas.faasName(),
|
|
99
|
+
faas.faasVersion(),
|
|
100
|
+
faas.faasInstance(),
|
|
101
|
+
faas.faasCloudRegion(),
|
|
102
|
+
]);
|
|
103
|
+
return await makeResource({
|
|
104
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM]: semantic_conventions_1.CLOUDPLATFORMVALUES_GCP_CLOUD_FUNCTIONS,
|
|
105
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_NAME]: faasName,
|
|
106
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_VERSION]: faasVersion,
|
|
107
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
|
|
108
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async function gaeResource() {
|
|
112
|
+
let zone, region;
|
|
113
|
+
if (await gae.onAppEngineStandard()) {
|
|
114
|
+
[zone, region] = await Promise.all([
|
|
115
|
+
gae.standardAvailabilityZone(),
|
|
116
|
+
gae.standardCloudRegion(),
|
|
117
|
+
]);
|
|
86
118
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (!(await isAvail)) {
|
|
90
|
-
return undefined;
|
|
91
|
-
}
|
|
92
|
-
try {
|
|
93
|
-
const zoneId = await gcpMetadata.instance('zone');
|
|
94
|
-
if (zoneId) {
|
|
95
|
-
return zoneId.split('/').pop();
|
|
96
|
-
}
|
|
97
|
-
return '';
|
|
98
|
-
}
|
|
99
|
-
catch {
|
|
100
|
-
return '';
|
|
101
|
-
}
|
|
119
|
+
else {
|
|
120
|
+
({ zone, region } = await gce.availabilityZoneAndRegion());
|
|
102
121
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
122
|
+
const [faasName, faasVersion, faasInstance] = await Promise.all([
|
|
123
|
+
gae.serviceName(),
|
|
124
|
+
gae.serviceVersion(),
|
|
125
|
+
gae.serviceInstance(),
|
|
126
|
+
]);
|
|
127
|
+
return await makeResource({
|
|
128
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM]: semantic_conventions_1.CLOUDPLATFORMVALUES_GCP_APP_ENGINE,
|
|
129
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_NAME]: faasName,
|
|
130
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_VERSION]: faasVersion,
|
|
131
|
+
[semantic_conventions_1.SEMRESATTRS_FAAS_INSTANCE]: faasInstance,
|
|
132
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zone,
|
|
133
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_REGION]: region,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
async function gceResource() {
|
|
137
|
+
const [zoneAndRegion, hostType, hostId, hostName] = await Promise.all([
|
|
138
|
+
gce.availabilityZoneAndRegion(),
|
|
139
|
+
gce.hostType(),
|
|
140
|
+
gce.hostId(),
|
|
141
|
+
gce.hostName(),
|
|
142
|
+
]);
|
|
143
|
+
return await makeResource({
|
|
144
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PLATFORM]: semantic_conventions_1.CLOUDPLATFORMVALUES_GCP_COMPUTE_ENGINE,
|
|
145
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zoneAndRegion.zone,
|
|
146
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_REGION]: zoneAndRegion.region,
|
|
147
|
+
[semantic_conventions_1.SEMRESATTRS_HOST_TYPE]: hostType,
|
|
148
|
+
[semantic_conventions_1.SEMRESATTRS_HOST_ID]: hostId,
|
|
149
|
+
[semantic_conventions_1.SEMRESATTRS_HOST_NAME]: hostName,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
async function makeResource(attrs) {
|
|
153
|
+
const project = await metadata.project('project-id');
|
|
154
|
+
return (0, resources_1.resourceFromAttributes)({
|
|
155
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER]: semantic_conventions_1.CLOUDPROVIDERVALUES_GCP,
|
|
156
|
+
[semantic_conventions_1.SEMRESATTRS_CLOUD_ACCOUNT_ID]: project,
|
|
157
|
+
...attrs,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Google Cloud resource detector which populates attributes based on the environment this
|
|
162
|
+
* process is running in. If not on GCP, returns an empty resource.
|
|
163
|
+
*/
|
|
164
|
+
class GcpDetector {
|
|
165
|
+
async _asyncAttributes() {
|
|
166
|
+
const resource = await api_1.context.with((0, core_1.suppressTracing)(api_1.context.active()), detect);
|
|
167
|
+
return resource.attributes;
|
|
114
168
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return '';
|
|
125
|
-
}
|
|
169
|
+
detect() {
|
|
170
|
+
const asyncAttributes = this._asyncAttributes();
|
|
171
|
+
const attributes = {};
|
|
172
|
+
ATTRIBUTE_NAMES.forEach(name => {
|
|
173
|
+
// Each resource attribute is determined asynchronously in _gatherData().
|
|
174
|
+
attributes[name] = asyncAttributes.then(data => data[name]);
|
|
175
|
+
});
|
|
176
|
+
return { attributes };
|
|
126
177
|
}
|
|
127
178
|
}
|
|
179
|
+
exports.GcpDetector = GcpDetector;
|
|
128
180
|
exports.gcpDetector = new GcpDetector();
|
|
129
181
|
//# sourceMappingURL=GcpDetector.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GcpDetector.js","sourceRoot":"","sources":["../../../src/detectors/GcpDetector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAA4C;AAC5C,4CAA6C;AAC7C,8CAAsD;AAOtD,8EAW6C;AAE7C;;;;GAIG;AACH,MAAM,WAAW;IACf,MAAM,CAAC,OAAiC;QACtC,MAAM,UAAU,GAAG,aAAO,CAAC,IAAI,CAAC,IAAA,sBAAe,EAAC,aAAO,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,CACtE,IAAI,CAAC,cAAc,EAAE,CACtB,CAAC;QACF,OAAO,EAAE,UAAU,EAAE,CAAC;IACxB,CAAC;IAED;;OAEG;IACK,cAAc;QACpB,MAAM,OAAO,GAAG,WAAW,CAAC,WAAW,EAAE,CAAC;QAE1C,MAAM,UAAU,GAA+B;YAC7C,CAAC,iDAA0B,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE;gBACxC,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,8CAAuB,CAAC,CAAC,CAAC,SAAS,CAAC;YAC/D,CAAC,CAAC,EAAE;YACJ,CAAC,mDAA4B,CAAC,EAAE,IAAI,CAAC,aAA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C,UAAU,CAAC,CAAC;SAC/C;QAAC,MAAM;YACN,OAAO,EAAE,CAAC;SACX;IACH,CAAC;CACF;AAEY,QAAA,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC","sourcesContent":["/*\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\nimport * as gcpMetadata from 'gcp-metadata';\nimport { context } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n ResourceDetectionConfig,\n ResourceDetector,\n DetectedResource,\n DetectedResourceAttributes,\n} from '@opentelemetry/resources';\nimport {\n CLOUDPROVIDERVALUES_GCP,\n SEMRESATTRS_CLOUD_ACCOUNT_ID,\n SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,\n SEMRESATTRS_CLOUD_PROVIDER,\n SEMRESATTRS_CONTAINER_NAME,\n SEMRESATTRS_HOST_ID,\n SEMRESATTRS_HOST_NAME,\n SEMRESATTRS_K8S_CLUSTER_NAME,\n SEMRESATTRS_K8S_NAMESPACE_NAME,\n SEMRESATTRS_K8S_POD_NAME,\n} from '@opentelemetry/semantic-conventions';\n\n/**\n * The GcpDetector can be used to detect if a process is running in the Google\n * Cloud Platform and return a {@link Resource} populated with metadata about\n * the instance. Returns an empty Resource if detection fails.\n */\nclass GcpDetector implements ResourceDetector {\n detect(_config?: ResourceDetectionConfig): DetectedResource {\n const attributes = context.with(suppressTracing(context.active()), () =>\n this._getAttributes()\n );\n return { attributes };\n }\n\n /**\n * Asynchronously gather GCP cloud metadata.\n */\n private _getAttributes(): DetectedResourceAttributes {\n const isAvail = gcpMetadata.isAvailable();\n\n const attributes: DetectedResourceAttributes = {\n [SEMRESATTRS_CLOUD_PROVIDER]: (async () => {\n return (await isAvail) ? CLOUDPROVIDERVALUES_GCP : undefined;\n })(),\n [SEMRESATTRS_CLOUD_ACCOUNT_ID]: this._getProjectId(isAvail),\n [SEMRESATTRS_HOST_ID]: this._getInstanceId(isAvail),\n [SEMRESATTRS_HOST_NAME]: this._getHostname(isAvail),\n [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: this._getZone(isAvail),\n };\n\n // Add resource attributes for K8s.\n if (process.env.KUBERNETES_SERVICE_HOST) {\n attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = this._getClusterName(isAvail);\n attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = (async () => {\n return (await isAvail) ? process.env.NAMESPACE : undefined;\n })();\n attributes[SEMRESATTRS_K8S_POD_NAME] = (async () => {\n return (await isAvail) ? process.env.HOSTNAME : undefined;\n })();\n attributes[SEMRESATTRS_CONTAINER_NAME] = (async () => {\n return (await isAvail) ? process.env.CONTAINER_NAME : undefined;\n })();\n }\n\n return attributes;\n }\n\n /** Gets project id from GCP project metadata. */\n private async _getProjectId(\n isAvail: Promise<boolean>\n ): Promise<string | undefined> {\n if (!(await isAvail)) {\n return undefined;\n }\n try {\n return await gcpMetadata.project('project-id');\n } catch {\n return '';\n }\n }\n\n /** Gets instance id from GCP instance metadata. */\n private async _getInstanceId(\n isAvail: Promise<boolean>\n ): Promise<string | undefined> {\n if (!(await isAvail)) {\n return undefined;\n }\n try {\n const id = await gcpMetadata.instance('id');\n return id.toString();\n } catch {\n return '';\n }\n }\n\n /** Gets zone from GCP instance metadata. */\n private async _getZone(\n isAvail: Promise<boolean>\n ): Promise<string | undefined> {\n if (!(await isAvail)) {\n return undefined;\n }\n try {\n const zoneId = await gcpMetadata.instance('zone');\n if (zoneId) {\n return zoneId.split('/').pop();\n }\n return '';\n } catch {\n return '';\n }\n }\n\n /** Gets cluster name from GCP instance metadata. */\n private async _getClusterName(\n isAvail: Promise<boolean>\n ): Promise<string | undefined> {\n if (!(await isAvail)) {\n return undefined;\n }\n try {\n return await gcpMetadata.instance('attributes/cluster-name');\n } catch {\n return '';\n }\n }\n\n /** Gets hostname from GCP instance metadata. */\n private async _getHostname(\n isAvail: Promise<boolean>\n ): Promise<string | undefined> {\n if (!(await isAvail)) {\n return undefined;\n }\n try {\n return await gcpMetadata.instance('hostname');\n } catch {\n return '';\n }\n }\n}\n\nexport const gcpDetector = new GcpDetector();\n"]}
|
|
1
|
+
{"version":3,"file":"GcpDetector.js","sourceRoot":"","sources":["../../../src/detectors/GcpDetector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAEH,4CAA6C;AAC7C,8CAAsD;AACtD,8EAmB6C;AAG7C,wDAOkC;AAClC,yCAAyC;AACzC,+BAA+B;AAC/B,6BAA6B;AAC7B,6BAA6B;AAC7B,6BAA6B;AAE7B,MAAM,eAAe,GAAG;IACtB,iDAA0B;IAC1B,0DAAmC;IACnC,+CAAwB;IACxB,mDAA4B;IAC5B,4CAAqB;IACrB,0CAAmB;IACnB,4CAAqB;IACrB,iDAA0B;IAC1B,mDAA4B;IAC5B,4CAAqB;IACrB,+CAAwB;IACxB,gDAAyB;CACjB,CAAC;AAQX,KAAK,UAAU,MAAM;IACnB,IAAI,CAAC,CAAC,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC,EAAE;QACnC,OAAO,IAAA,yBAAa,GAAE,CAAC;KACxB;IAED,uFAAuF;IACvF,0FAA0F;IAC1F,IAAI,MAAM,GAAG,CAAC,KAAK,EAAE,EAAE;QACrB,OAAO,MAAM,WAAW,EAAE,CAAC;KAC5B;SAAM,IAAI,MAAM,IAAI,CAAC,gBAAgB,EAAE,EAAE;QACxC,OAAO,MAAM,sBAAsB,EAAE,CAAC;KACvC;SAAM,IAAI,MAAM,IAAI,CAAC,UAAU,EAAE,EAAE;QAClC,OAAO,MAAM,gBAAgB,EAAE,CAAC;KACjC;SAAM,IAAI,MA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ACjC,MAAM,CACP,CAAC;QACF,OAAO,QAAQ,CAAC,UAAU,CAAC;IAC7B,CAAC;IAED,MAAM;QACJ,MAAM,eAAe,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAChD,MAAM,UAAU,GAAG,EAAgC,CAAC;QACpD,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAC7B,yEAAyE;YACzE,UAAU,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC9D,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,UAAU,EAAE,CAAC;IACxB,CAAC;CACF;AAnBD,kCAmBC;AAEY,QAAA,WAAW,GAAG,IAAI,WAAW,EAAE,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\nimport { context } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n CLOUDPLATFORMVALUES_GCP_APP_ENGINE,\n CLOUDPLATFORMVALUES_GCP_CLOUD_FUNCTIONS,\n CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,\n CLOUDPLATFORMVALUES_GCP_COMPUTE_ENGINE,\n CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,\n CLOUDPROVIDERVALUES_GCP,\n SEMRESATTRS_CLOUD_ACCOUNT_ID,\n SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,\n SEMRESATTRS_CLOUD_PLATFORM,\n SEMRESATTRS_CLOUD_PROVIDER,\n SEMRESATTRS_CLOUD_REGION,\n SEMRESATTRS_FAAS_INSTANCE,\n SEMRESATTRS_FAAS_NAME,\n SEMRESATTRS_FAAS_VERSION,\n SEMRESATTRS_HOST_ID,\n SEMRESATTRS_HOST_NAME,\n SEMRESATTRS_HOST_TYPE,\n SEMRESATTRS_K8S_CLUSTER_NAME,\n} from '@opentelemetry/semantic-conventions';\n\nimport { AttributeValue, Attributes } from '@opentelemetry/api';\nimport {\n DetectedResource,\n DetectedResourceAttributes,\n emptyResource,\n Resource,\n ResourceDetector,\n resourceFromAttributes,\n} from '@opentelemetry/resources';\nimport * as metadata from 'gcp-metadata';\nimport * as faas from './faas';\nimport * as gae from './gae';\nimport * as gce from './gce';\nimport * as gke from './gke';\n\nconst ATTRIBUTE_NAMES = [\n SEMRESATTRS_CLOUD_PLATFORM,\n SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,\n SEMRESATTRS_CLOUD_REGION,\n SEMRESATTRS_K8S_CLUSTER_NAME,\n SEMRESATTRS_HOST_TYPE,\n SEMRESATTRS_HOST_ID,\n SEMRESATTRS_HOST_NAME,\n SEMRESATTRS_CLOUD_PROVIDER,\n SEMRESATTRS_CLOUD_ACCOUNT_ID,\n SEMRESATTRS_FAAS_NAME,\n SEMRESATTRS_FAAS_VERSION,\n SEMRESATTRS_FAAS_INSTANCE,\n] as const;\n\n// Ensure that all resource keys are accounted for in ATTRIBUTE_NAMES\ntype GcpResourceAttributeName = (typeof ATTRIBUTE_NAMES)[number];\ntype GcpResourceAttributes = Partial<\n Record<GcpResourceAttributeName, AttributeValue>\n>;\n\nasync function detect(): Promise<Resource> {\n if (!(await metadata.isAvailable())) {\n return emptyResource();\n }\n\n // Note the order of these if checks is significant with more specific resources coming\n // first. E.g. Cloud Functions gen2 are executed in Cloud Run so it must be checked first.\n if (await gke.onGke()) {\n return await gkeResource();\n } else if (await faas.onCloudFunctions()) {\n return await cloudFunctionsResource();\n } else if (await faas.onCloudRun()) {\n return await cloudRunResource();\n } else if (await gae.onAppEngine()) {\n return await gaeResource();\n } else if (await gce.onGce()) {\n return await gceResource();\n }\n\n return emptyResource();\n}\n\nasync function gkeResource(): Promise<Resource> {\n const [zoneOrRegion, k8sClusterName, hostId] = await Promise.all([\n gke.availabilityZoneOrRegion(),\n gke.clusterName(),\n gke.hostId(),\n ]);\n\n return await makeResource({\n [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_KUBERNETES_ENGINE,\n [zoneOrRegion.type === 'zone'\n ? SEMRESATTRS_CLOUD_AVAILABILITY_ZONE\n : SEMRESATTRS_CLOUD_REGION]: zoneOrRegion.value,\n [SEMRESATTRS_K8S_CLUSTER_NAME]: k8sClusterName,\n [SEMRESATTRS_HOST_ID]: hostId,\n });\n}\n\nasync function cloudRunResource(): Promise<Resource> {\n const [faasName, faasVersion, faasInstance, faasCloudRegion] =\n await Promise.all([\n faas.faasName(),\n faas.faasVersion(),\n faas.faasInstance(),\n faas.faasCloudRegion(),\n ]);\n\n return await makeResource({\n [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_CLOUD_RUN,\n [SEMRESATTRS_FAAS_NAME]: faasName,\n [SEMRESATTRS_FAAS_VERSION]: faasVersion,\n [SEMRESATTRS_FAAS_INSTANCE]: faasInstance,\n [SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,\n });\n}\n\nasync function cloudFunctionsResource(): Promise<Resource> {\n const [faasName, faasVersion, faasInstance, faasCloudRegion] =\n await Promise.all([\n faas.faasName(),\n faas.faasVersion(),\n faas.faasInstance(),\n faas.faasCloudRegion(),\n ]);\n\n return await makeResource({\n [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_CLOUD_FUNCTIONS,\n [SEMRESATTRS_FAAS_NAME]: faasName,\n [SEMRESATTRS_FAAS_VERSION]: faasVersion,\n [SEMRESATTRS_FAAS_INSTANCE]: faasInstance,\n [SEMRESATTRS_CLOUD_REGION]: faasCloudRegion,\n });\n}\n\nasync function gaeResource(): Promise<Resource> {\n let zone, region;\n if (await gae.onAppEngineStandard()) {\n [zone, region] = await Promise.all([\n gae.standardAvailabilityZone(),\n gae.standardCloudRegion(),\n ]);\n } else {\n ({ zone, region } = await gce.availabilityZoneAndRegion());\n }\n const [faasName, faasVersion, faasInstance] = await Promise.all([\n gae.serviceName(),\n gae.serviceVersion(),\n gae.serviceInstance(),\n ]);\n\n return await makeResource({\n [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_APP_ENGINE,\n [SEMRESATTRS_FAAS_NAME]: faasName,\n [SEMRESATTRS_FAAS_VERSION]: faasVersion,\n [SEMRESATTRS_FAAS_INSTANCE]: faasInstance,\n [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zone,\n [SEMRESATTRS_CLOUD_REGION]: region,\n });\n}\n\nasync function gceResource(): Promise<Resource> {\n const [zoneAndRegion, hostType, hostId, hostName] = await Promise.all([\n gce.availabilityZoneAndRegion(),\n gce.hostType(),\n gce.hostId(),\n gce.hostName(),\n ]);\n\n return await makeResource({\n [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_GCP_COMPUTE_ENGINE,\n [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: zoneAndRegion.zone,\n [SEMRESATTRS_CLOUD_REGION]: zoneAndRegion.region,\n [SEMRESATTRS_HOST_TYPE]: hostType,\n [SEMRESATTRS_HOST_ID]: hostId,\n [SEMRESATTRS_HOST_NAME]: hostName,\n });\n}\n\nasync function makeResource(attrs: GcpResourceAttributes): Promise<Resource> {\n const project = await metadata.project<string>('project-id');\n\n return resourceFromAttributes({\n [SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_GCP,\n [SEMRESATTRS_CLOUD_ACCOUNT_ID]: project,\n ...attrs,\n } satisfies GcpResourceAttributes);\n}\n\n/**\n * Google Cloud resource detector which populates attributes based on the environment this\n * process is running in. If not on GCP, returns an empty resource.\n */\nexport class GcpDetector implements ResourceDetector {\n private async _asyncAttributes(): Promise<Attributes> {\n const resource = await context.with(\n suppressTracing(context.active()),\n detect\n );\n return resource.attributes;\n }\n\n detect(): DetectedResource {\n const asyncAttributes = this._asyncAttributes();\n const attributes = {} as DetectedResourceAttributes;\n ATTRIBUTE_NAMES.forEach(name => {\n // Each resource attribute is determined asynchronously in _gatherData().\n attributes[name] = asyncAttributes.then(data => data[name]);\n });\n\n return { attributes };\n }\n}\n\nexport const gcpDetector = new GcpDetector();\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare function onCloudRun(): Promise<boolean>;
|
|
2
|
+
export declare function onCloudFunctions(): Promise<boolean>;
|
|
3
|
+
/**
|
|
4
|
+
* The name of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or {@link
|
|
5
|
+
* onCloudFunctions()} is true before calling this, or it may throw exceptions.
|
|
6
|
+
*/
|
|
7
|
+
export declare function faasName(): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* The version/revision of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or
|
|
10
|
+
* {@link onCloudFunctions()} is true before calling this, or it may throw exceptions.
|
|
11
|
+
*/
|
|
12
|
+
export declare function faasVersion(): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* The ID for the running instance of a Cloud Run or Cloud Function. Check that {@link
|
|
15
|
+
* onCloudRun()} or {@link onCloudFunctions()} is true before calling this, or it may throw
|
|
16
|
+
* exceptions.
|
|
17
|
+
*/
|
|
18
|
+
export declare function faasInstance(): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* The cloud region where the running instance of a Cloud Run or Cloud Function is located.
|
|
21
|
+
* Check that {@link onCloudRun()} or {@link onCloudFunctions()} is true before calling this,
|
|
22
|
+
* or it may throw exceptions.
|
|
23
|
+
*/
|
|
24
|
+
export declare function faasCloudRegion(): Promise<string>;
|
|
25
|
+
//# sourceMappingURL=faas.d.ts.map
|
|
@@ -0,0 +1,84 @@
|
|
|
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.faasCloudRegion = exports.faasInstance = exports.faasVersion = exports.faasName = exports.onCloudFunctions = exports.onCloudRun = void 0;
|
|
20
|
+
/**
|
|
21
|
+
* Implementation in this file copied from
|
|
22
|
+
* https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/v1.8.0/detectors/gcp/faas.go
|
|
23
|
+
*/
|
|
24
|
+
const metadata = require("gcp-metadata");
|
|
25
|
+
const ID_METADATA_ATTR = 'id';
|
|
26
|
+
const CLOUD_RUN_CONFIG_ENV = 'K_CONFIGURATION';
|
|
27
|
+
const CLOUD_FUNCTION_TARGET_ENV = 'FUNCTION_TARGET';
|
|
28
|
+
const FAAS_SERVICE_ENV = 'K_SERVICE';
|
|
29
|
+
const FAAS_REVISION_ENV = 'K_REVISION';
|
|
30
|
+
const REGION_METADATA_ATTR = 'region';
|
|
31
|
+
async function onCloudRun() {
|
|
32
|
+
return process.env[CLOUD_RUN_CONFIG_ENV] !== undefined;
|
|
33
|
+
}
|
|
34
|
+
exports.onCloudRun = onCloudRun;
|
|
35
|
+
async function onCloudFunctions() {
|
|
36
|
+
return process.env[CLOUD_FUNCTION_TARGET_ENV] !== undefined;
|
|
37
|
+
}
|
|
38
|
+
exports.onCloudFunctions = onCloudFunctions;
|
|
39
|
+
/**
|
|
40
|
+
* The name of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or {@link
|
|
41
|
+
* onCloudFunctions()} is true before calling this, or it may throw exceptions.
|
|
42
|
+
*/
|
|
43
|
+
async function faasName() {
|
|
44
|
+
return lookupEnv(FAAS_SERVICE_ENV);
|
|
45
|
+
}
|
|
46
|
+
exports.faasName = faasName;
|
|
47
|
+
/**
|
|
48
|
+
* The version/revision of the Cloud Run or Cloud Function. Check that {@link onCloudRun()} or
|
|
49
|
+
* {@link onCloudFunctions()} is true before calling this, or it may throw exceptions.
|
|
50
|
+
*/
|
|
51
|
+
async function faasVersion() {
|
|
52
|
+
return lookupEnv(FAAS_REVISION_ENV);
|
|
53
|
+
}
|
|
54
|
+
exports.faasVersion = faasVersion;
|
|
55
|
+
/**
|
|
56
|
+
* The ID for the running instance of a Cloud Run or Cloud Function. Check that {@link
|
|
57
|
+
* onCloudRun()} or {@link onCloudFunctions()} is true before calling this, or it may throw
|
|
58
|
+
* exceptions.
|
|
59
|
+
*/
|
|
60
|
+
async function faasInstance() {
|
|
61
|
+
// May be a bignumber.js BigNumber which can just be converted with toString(). See
|
|
62
|
+
// https://github.com/googleapis/gcp-metadata#take-care-with-large-number-valued-properties
|
|
63
|
+
const id = await metadata.instance(ID_METADATA_ATTR);
|
|
64
|
+
return id.toString();
|
|
65
|
+
}
|
|
66
|
+
exports.faasInstance = faasInstance;
|
|
67
|
+
/**
|
|
68
|
+
* The cloud region where the running instance of a Cloud Run or Cloud Function is located.
|
|
69
|
+
* Check that {@link onCloudRun()} or {@link onCloudFunctions()} is true before calling this,
|
|
70
|
+
* or it may throw exceptions.
|
|
71
|
+
*/
|
|
72
|
+
async function faasCloudRegion() {
|
|
73
|
+
const region = await metadata.instance(REGION_METADATA_ATTR);
|
|
74
|
+
return region.slice(region.lastIndexOf('/') + 1);
|
|
75
|
+
}
|
|
76
|
+
exports.faasCloudRegion = faasCloudRegion;
|
|
77
|
+
function lookupEnv(key) {
|
|
78
|
+
const val = process.env[key];
|
|
79
|
+
if (val === undefined) {
|
|
80
|
+
throw new Error(`Environment variable ${key} not found`);
|
|
81
|
+
}
|
|
82
|
+
return val;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=faas.js.map
|