@opentelemetry/resource-detector-gcp 0.33.0 → 0.34.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.
@@ -1,20 +1,15 @@
1
- import { DetectorSync, ResourceDetectionConfig, IResource } from '@opentelemetry/resources';
1
+ import { ResourceDetectionConfig, ResourceDetector, DetectedResource } from '@opentelemetry/resources';
2
2
  /**
3
3
  * The GcpDetector can be used to detect if a process is running in the Google
4
4
  * Cloud Platform and return a {@link Resource} populated with metadata about
5
5
  * the instance. Returns an empty Resource if detection fails.
6
6
  */
7
- declare class GcpDetector implements DetectorSync {
8
- detect(_config?: ResourceDetectionConfig): IResource;
7
+ declare class GcpDetector implements ResourceDetector {
8
+ detect(_config?: ResourceDetectionConfig): DetectedResource;
9
9
  /**
10
- * Attempts to connect and obtain instance configuration data from the GCP metadata service.
11
- * If the connection is successful it returns a promise containing a {@link ResourceAttributes}
12
- * object with instance metadata. Returns a promise containing an
13
- * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
10
+ * Asynchronously gather GCP cloud metadata.
14
11
  */
15
12
  private _getAttributes;
16
- /** Add resource attributes for K8s */
17
- private _addK8sAttributes;
18
13
  /** Gets project id from GCP project metadata. */
19
14
  private _getProjectId;
20
15
  /** Gets instance id from GCP instance metadata. */
@@ -13,208 +13,114 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
- var __generator = (this && this.__generator) || function (thisArg, body) {
26
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
- function verb(n) { return function (v) { return step([n, v]); }; }
29
- function step(op) {
30
- if (f) throw new TypeError("Generator is already executing.");
31
- while (_) try {
32
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
- if (y = 0, t) op = [op[0] & 2, t.value];
34
- switch (op[0]) {
35
- case 0: case 1: t = op; break;
36
- case 4: _.label++; return { value: op[1], done: false };
37
- case 5: _.label++; y = op[1]; op = [0]; continue;
38
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
- default:
40
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
- if (t[2]) _.ops.pop();
45
- _.trys.pop(); continue;
46
- }
47
- op = body.call(thisArg, _);
48
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
- }
51
- };
52
16
  import * as gcpMetadata from 'gcp-metadata';
53
- import { context, diag } from '@opentelemetry/api';
17
+ import { context } from '@opentelemetry/api';
54
18
  import { suppressTracing } from '@opentelemetry/core';
55
- import { Resource, } from '@opentelemetry/resources';
56
- import { getEnv } from '@opentelemetry/core';
57
19
  import { CLOUDPROVIDERVALUES_GCP, SEMRESATTRS_CLOUD_ACCOUNT_ID, SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, SEMRESATTRS_CLOUD_PROVIDER, SEMRESATTRS_CONTAINER_NAME, SEMRESATTRS_HOST_ID, SEMRESATTRS_HOST_NAME, SEMRESATTRS_K8S_CLUSTER_NAME, SEMRESATTRS_K8S_NAMESPACE_NAME, SEMRESATTRS_K8S_POD_NAME, } from '@opentelemetry/semantic-conventions';
58
20
  /**
59
21
  * The GcpDetector can be used to detect if a process is running in the Google
60
22
  * Cloud Platform and return a {@link Resource} populated with metadata about
61
23
  * the instance. Returns an empty Resource if detection fails.
62
24
  */
63
- var GcpDetector = /** @class */ (function () {
64
- function GcpDetector() {
25
+ class GcpDetector {
26
+ detect(_config) {
27
+ const attributes = context.with(suppressTracing(context.active()), () => this._getAttributes());
28
+ return { attributes };
65
29
  }
66
- GcpDetector.prototype.detect = function (_config) {
67
- var _this = this;
68
- var attributes = context.with(suppressTracing(context.active()), function () {
69
- return _this._getAttributes();
70
- });
71
- return new Resource({}, attributes);
72
- };
73
30
  /**
74
- * Attempts to connect and obtain instance configuration data from the GCP metadata service.
75
- * If the connection is successful it returns a promise containing a {@link ResourceAttributes}
76
- * object with instance metadata. Returns a promise containing an
77
- * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
31
+ * Asynchronously gather GCP cloud metadata.
78
32
  */
79
- GcpDetector.prototype._getAttributes = function () {
80
- return __awaiter(this, void 0, void 0, function () {
81
- var _a, projectId, instanceId, zoneId, clusterName, hostname, attributes;
82
- return __generator(this, function (_b) {
83
- switch (_b.label) {
84
- case 0: return [4 /*yield*/, gcpMetadata.isAvailable()];
85
- case 1:
86
- if (!(_b.sent())) {
87
- diag.debug('GcpDetector failed: GCP Metadata unavailable.');
88
- return [2 /*return*/, {}];
89
- }
90
- return [4 /*yield*/, Promise.all([
91
- this._getProjectId(),
92
- this._getInstanceId(),
93
- this._getZone(),
94
- this._getClusterName(),
95
- this._getHostname(),
96
- ])];
97
- case 2:
98
- _a = _b.sent(), projectId = _a[0], instanceId = _a[1], zoneId = _a[2], clusterName = _a[3], hostname = _a[4];
99
- attributes = {};
100
- attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID] = projectId;
101
- attributes[SEMRESATTRS_HOST_ID] = instanceId;
102
- attributes[SEMRESATTRS_HOST_NAME] = hostname;
103
- attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;
104
- attributes[SEMRESATTRS_CLOUD_PROVIDER] = CLOUDPROVIDERVALUES_GCP;
105
- if (getEnv().KUBERNETES_SERVICE_HOST)
106
- this._addK8sAttributes(attributes, clusterName);
107
- return [2 /*return*/, attributes];
108
- }
109
- });
110
- });
111
- };
112
- /** Add resource attributes for K8s */
113
- GcpDetector.prototype._addK8sAttributes = function (attributes, clusterName) {
114
- var env = getEnv();
115
- attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;
116
- attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = env.NAMESPACE;
117
- attributes[SEMRESATTRS_K8S_POD_NAME] = env.HOSTNAME;
118
- attributes[SEMRESATTRS_CONTAINER_NAME] = env.CONTAINER_NAME;
119
- };
33
+ _getAttributes() {
34
+ const isAvail = gcpMetadata.isAvailable();
35
+ const attributes = {
36
+ [SEMRESATTRS_CLOUD_PROVIDER]: (async () => {
37
+ return (await isAvail) ? CLOUDPROVIDERVALUES_GCP : undefined;
38
+ })(),
39
+ [SEMRESATTRS_CLOUD_ACCOUNT_ID]: this._getProjectId(isAvail),
40
+ [SEMRESATTRS_HOST_ID]: this._getInstanceId(isAvail),
41
+ [SEMRESATTRS_HOST_NAME]: this._getHostname(isAvail),
42
+ [SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: this._getZone(isAvail),
43
+ };
44
+ // Add resource attributes for K8s.
45
+ if (process.env.KUBERNETES_SERVICE_HOST) {
46
+ attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = this._getClusterName(isAvail);
47
+ attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = (async () => {
48
+ return (await isAvail) ? process.env.NAMESPACE : undefined;
49
+ })();
50
+ attributes[SEMRESATTRS_K8S_POD_NAME] = (async () => {
51
+ return (await isAvail) ? process.env.HOSTNAME : undefined;
52
+ })();
53
+ attributes[SEMRESATTRS_CONTAINER_NAME] = (async () => {
54
+ return (await isAvail) ? process.env.CONTAINER_NAME : undefined;
55
+ })();
56
+ }
57
+ return attributes;
58
+ }
120
59
  /** Gets project id from GCP project metadata. */
121
- GcpDetector.prototype._getProjectId = function () {
122
- return __awaiter(this, void 0, void 0, function () {
123
- var _a;
124
- return __generator(this, function (_b) {
125
- switch (_b.label) {
126
- case 0:
127
- _b.trys.push([0, 2, , 3]);
128
- return [4 /*yield*/, gcpMetadata.project('project-id')];
129
- case 1: return [2 /*return*/, _b.sent()];
130
- case 2:
131
- _a = _b.sent();
132
- return [2 /*return*/, ''];
133
- case 3: return [2 /*return*/];
134
- }
135
- });
136
- });
137
- };
60
+ async _getProjectId(isAvail) {
61
+ if (!(await isAvail)) {
62
+ return undefined;
63
+ }
64
+ try {
65
+ return await gcpMetadata.project('project-id');
66
+ }
67
+ catch {
68
+ return '';
69
+ }
70
+ }
138
71
  /** Gets instance id from GCP instance metadata. */
139
- GcpDetector.prototype._getInstanceId = function () {
140
- return __awaiter(this, void 0, void 0, function () {
141
- var id, _a;
142
- return __generator(this, function (_b) {
143
- switch (_b.label) {
144
- case 0:
145
- _b.trys.push([0, 2, , 3]);
146
- return [4 /*yield*/, gcpMetadata.instance('id')];
147
- case 1:
148
- id = _b.sent();
149
- return [2 /*return*/, id.toString()];
150
- case 2:
151
- _a = _b.sent();
152
- return [2 /*return*/, ''];
153
- case 3: return [2 /*return*/];
154
- }
155
- });
156
- });
157
- };
72
+ async _getInstanceId(isAvail) {
73
+ if (!(await isAvail)) {
74
+ return undefined;
75
+ }
76
+ try {
77
+ const id = await gcpMetadata.instance('id');
78
+ return id.toString();
79
+ }
80
+ catch {
81
+ return '';
82
+ }
83
+ }
158
84
  /** Gets zone from GCP instance metadata. */
159
- GcpDetector.prototype._getZone = function () {
160
- return __awaiter(this, void 0, void 0, function () {
161
- var zoneId, _a;
162
- return __generator(this, function (_b) {
163
- switch (_b.label) {
164
- case 0:
165
- _b.trys.push([0, 2, , 3]);
166
- return [4 /*yield*/, gcpMetadata.instance('zone')];
167
- case 1:
168
- zoneId = _b.sent();
169
- if (zoneId) {
170
- return [2 /*return*/, zoneId.split('/').pop()];
171
- }
172
- return [2 /*return*/, ''];
173
- case 2:
174
- _a = _b.sent();
175
- return [2 /*return*/, ''];
176
- case 3: return [2 /*return*/];
177
- }
178
- });
179
- });
180
- };
85
+ async _getZone(isAvail) {
86
+ if (!(await isAvail)) {
87
+ return undefined;
88
+ }
89
+ try {
90
+ const zoneId = await gcpMetadata.instance('zone');
91
+ if (zoneId) {
92
+ return zoneId.split('/').pop();
93
+ }
94
+ return '';
95
+ }
96
+ catch {
97
+ return '';
98
+ }
99
+ }
181
100
  /** Gets cluster name from GCP instance metadata. */
182
- GcpDetector.prototype._getClusterName = function () {
183
- return __awaiter(this, void 0, void 0, function () {
184
- var _a;
185
- return __generator(this, function (_b) {
186
- switch (_b.label) {
187
- case 0:
188
- _b.trys.push([0, 2, , 3]);
189
- return [4 /*yield*/, gcpMetadata.instance('attributes/cluster-name')];
190
- case 1: return [2 /*return*/, _b.sent()];
191
- case 2:
192
- _a = _b.sent();
193
- return [2 /*return*/, ''];
194
- case 3: return [2 /*return*/];
195
- }
196
- });
197
- });
198
- };
101
+ async _getClusterName(isAvail) {
102
+ if (!(await isAvail)) {
103
+ return undefined;
104
+ }
105
+ try {
106
+ return await gcpMetadata.instance('attributes/cluster-name');
107
+ }
108
+ catch {
109
+ return '';
110
+ }
111
+ }
199
112
  /** Gets hostname from GCP instance metadata. */
200
- GcpDetector.prototype._getHostname = function () {
201
- return __awaiter(this, void 0, void 0, function () {
202
- var _a;
203
- return __generator(this, function (_b) {
204
- switch (_b.label) {
205
- case 0:
206
- _b.trys.push([0, 2, , 3]);
207
- return [4 /*yield*/, gcpMetadata.instance('hostname')];
208
- case 1: return [2 /*return*/, _b.sent()];
209
- case 2:
210
- _a = _b.sent();
211
- return [2 /*return*/, ''];
212
- case 3: return [2 /*return*/];
213
- }
214
- });
215
- });
216
- };
217
- return GcpDetector;
218
- }());
219
- export var gcpDetector = new GcpDetector();
113
+ async _getHostname(isAvail) {
114
+ if (!(await isAvail)) {
115
+ return undefined;
116
+ }
117
+ try {
118
+ return await gcpMetadata.instance('hostname');
119
+ }
120
+ catch {
121
+ return '';
122
+ }
123
+ }
124
+ }
125
+ export const gcpDetector = new GcpDetector();
220
126
  //# sourceMappingURL=GcpDetector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"GcpDetector.js","sourceRoot":"","sources":["../../../src/detectors/GcpDetector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,OAAO,KAAK,WAAW,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAGL,QAAQ,GAGT,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EACL,uBAAuB,EACvB,4BAA4B,EAC5B,mCAAmC,EACnC,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,EACrB,4BAA4B,EAC5B,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,qCAAqC,CAAC;AAE7C;;;;GAIG;AACH;IAAA;IAwGA,CAAC;IAvGC,4BAAM,GAAN,UAAO,OAAiC;QAAxC,iBAKC;QAJC,IAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE;YACjE,OAAA,KAAI,CAAC,cAAc,EAAE;QAArB,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AAAD,CAAC,AAxGD,IAwGC;AAED,MAAM,CAAC,IAAM,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, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n DetectorSync,\n ResourceDetectionConfig,\n Resource,\n ResourceAttributes,\n IResource,\n} from '@opentelemetry/resources';\nimport { getEnv } from '@opentelemetry/core';\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 DetectorSync {\n detect(_config?: ResourceDetectionConfig): IResource {\n const attributes = context.with(suppressTracing(context.active()), () =>\n this._getAttributes()\n );\n return new Resource({}, attributes);\n }\n\n /**\n * Attempts to connect and obtain instance configuration data from the GCP metadata service.\n * If the connection is successful it returns a promise containing a {@link ResourceAttributes}\n * object with instance metadata. Returns a promise containing an\n * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.\n */\n private async _getAttributes(): Promise<ResourceAttributes> {\n if (!(await gcpMetadata.isAvailable())) {\n diag.debug('GcpDetector failed: GCP Metadata unavailable.');\n return {};\n }\n\n const [projectId, instanceId, zoneId, clusterName, hostname] =\n await Promise.all([\n this._getProjectId(),\n this._getInstanceId(),\n this._getZone(),\n this._getClusterName(),\n this._getHostname(),\n ]);\n\n const attributes: ResourceAttributes = {};\n attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID] = projectId;\n attributes[SEMRESATTRS_HOST_ID] = instanceId;\n attributes[SEMRESATTRS_HOST_NAME] = hostname;\n attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;\n attributes[SEMRESATTRS_CLOUD_PROVIDER] = CLOUDPROVIDERVALUES_GCP;\n\n if (getEnv().KUBERNETES_SERVICE_HOST)\n this._addK8sAttributes(attributes, clusterName);\n\n return attributes;\n }\n\n /** Add resource attributes for K8s */\n private _addK8sAttributes(\n attributes: ResourceAttributes,\n clusterName: string\n ): void {\n const env = getEnv();\n\n attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;\n attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = env.NAMESPACE;\n attributes[SEMRESATTRS_K8S_POD_NAME] = env.HOSTNAME;\n attributes[SEMRESATTRS_CONTAINER_NAME] = env.CONTAINER_NAME;\n }\n\n /** Gets project id from GCP project metadata. */\n private async _getProjectId(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,WAAW,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAOtD,OAAO,EACL,uBAAuB,EACvB,4BAA4B,EAC5B,mCAAmC,EACnC,0BAA0B,EAC1B,0BAA0B,EAC1B,mBAAmB,EACnB,qBAAqB,EACrB,4BAA4B,EAC5B,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,qCAAqC,CAAC;AAE7C;;;;GAIG;AACH,MAAM,WAAW;IACf,MAAM,CAAC,OAAiC;QACtC,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,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,0BAA0B,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE;gBACxC,OAAO,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC,SAAS,CAAC;YAC/D,CAAC,CAAC,EAAE;YACJ,CAAC,4BAA4B,CAAC,EAAE,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AAC,QAAQ,CAAC,UAAU,CAAC,CAAC;SAC/C;QAAC,MAAM;YACN,OAAO,EAAE,CAAC;SACX;IACH,CAAC;CACF;AAED,MAAM,CAAC,MAAM,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,2 +1,2 @@
1
- export * from './GcpDetector';
1
+ export { gcpDetector } from './GcpDetector';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -13,5 +13,5 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export * from './GcpDetector';
16
+ export { gcpDetector } from './GcpDetector';
17
17
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,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\nexport * from './GcpDetector';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,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\nexport { gcpDetector } from './GcpDetector';\n"]}
@@ -1,3 +1,3 @@
1
- export * from './detectors';
1
+ export { gcpDetector } from './detectors';
2
2
  export { resetIsAvailableCache } from 'gcp-metadata';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -13,7 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export * from './detectors';
16
+ export { gcpDetector } from './detectors';
17
17
  // Internal - used for tests only
18
18
  export { resetIsAvailableCache } from 'gcp-metadata';
19
19
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,aAAa,CAAC;AAE5B,iCAAiC;AACjC,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,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\nexport * from './detectors';\n\n// Internal - used for tests only\nexport { resetIsAvailableCache } from 'gcp-metadata';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,iCAAiC;AACjC,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,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\nexport { gcpDetector } from './detectors';\n\n// Internal - used for tests only\nexport { resetIsAvailableCache } from 'gcp-metadata';\n"]}
@@ -1,20 +1,15 @@
1
- import { DetectorSync, ResourceDetectionConfig, IResource } from '@opentelemetry/resources';
1
+ import { ResourceDetectionConfig, ResourceDetector, DetectedResource } from '@opentelemetry/resources';
2
2
  /**
3
3
  * The GcpDetector can be used to detect if a process is running in the Google
4
4
  * Cloud Platform and return a {@link Resource} populated with metadata about
5
5
  * the instance. Returns an empty Resource if detection fails.
6
6
  */
7
- declare class GcpDetector implements DetectorSync {
8
- detect(_config?: ResourceDetectionConfig): IResource;
7
+ declare class GcpDetector implements ResourceDetector {
8
+ detect(_config?: ResourceDetectionConfig): DetectedResource;
9
9
  /**
10
- * Attempts to connect and obtain instance configuration data from the GCP metadata service.
11
- * If the connection is successful it returns a promise containing a {@link ResourceAttributes}
12
- * object with instance metadata. Returns a promise containing an
13
- * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
10
+ * Asynchronously gather GCP cloud metadata.
14
11
  */
15
12
  private _getAttributes;
16
- /** Add resource attributes for K8s */
17
- private _addK8sAttributes;
18
13
  /** Gets project id from GCP project metadata. */
19
14
  private _getProjectId;
20
15
  /** Gets instance id from GCP instance metadata. */
@@ -19,8 +19,6 @@ exports.gcpDetector = void 0;
19
19
  const gcpMetadata = require("gcp-metadata");
20
20
  const api_1 = require("@opentelemetry/api");
21
21
  const core_1 = require("@opentelemetry/core");
22
- const resources_1 = require("@opentelemetry/resources");
23
- const core_2 = require("@opentelemetry/core");
24
22
  const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
25
23
  /**
26
24
  * The GcpDetector can be used to detect if a process is running in the Google
@@ -30,65 +28,67 @@ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
30
28
  class GcpDetector {
31
29
  detect(_config) {
32
30
  const attributes = api_1.context.with((0, core_1.suppressTracing)(api_1.context.active()), () => this._getAttributes());
33
- return new resources_1.Resource({}, attributes);
31
+ return { attributes };
34
32
  }
35
33
  /**
36
- * Attempts to connect and obtain instance configuration data from the GCP metadata service.
37
- * If the connection is successful it returns a promise containing a {@link ResourceAttributes}
38
- * object with instance metadata. Returns a promise containing an
39
- * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
34
+ * Asynchronously gather GCP cloud metadata.
40
35
  */
41
- async _getAttributes() {
42
- if (!(await gcpMetadata.isAvailable())) {
43
- api_1.diag.debug('GcpDetector failed: GCP Metadata unavailable.');
44
- return {};
45
- }
46
- const [projectId, instanceId, zoneId, clusterName, hostname] = await Promise.all([
47
- this._getProjectId(),
48
- this._getInstanceId(),
49
- this._getZone(),
50
- this._getClusterName(),
51
- this._getHostname(),
52
- ]);
53
- const attributes = {};
54
- attributes[semantic_conventions_1.SEMRESATTRS_CLOUD_ACCOUNT_ID] = projectId;
55
- attributes[semantic_conventions_1.SEMRESATTRS_HOST_ID] = instanceId;
56
- attributes[semantic_conventions_1.SEMRESATTRS_HOST_NAME] = hostname;
57
- attributes[semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;
58
- attributes[semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER] = semantic_conventions_1.CLOUDPROVIDERVALUES_GCP;
59
- if ((0, core_2.getEnv)().KUBERNETES_SERVICE_HOST)
60
- this._addK8sAttributes(attributes, clusterName);
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
+ }
61
60
  return attributes;
62
61
  }
63
- /** Add resource attributes for K8s */
64
- _addK8sAttributes(attributes, clusterName) {
65
- const env = (0, core_2.getEnv)();
66
- attributes[semantic_conventions_1.SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;
67
- attributes[semantic_conventions_1.SEMRESATTRS_K8S_NAMESPACE_NAME] = env.NAMESPACE;
68
- attributes[semantic_conventions_1.SEMRESATTRS_K8S_POD_NAME] = env.HOSTNAME;
69
- attributes[semantic_conventions_1.SEMRESATTRS_CONTAINER_NAME] = env.CONTAINER_NAME;
70
- }
71
62
  /** Gets project id from GCP project metadata. */
72
- async _getProjectId() {
63
+ async _getProjectId(isAvail) {
64
+ if (!(await isAvail)) {
65
+ return undefined;
66
+ }
73
67
  try {
74
68
  return await gcpMetadata.project('project-id');
75
69
  }
76
- catch (_a) {
70
+ catch {
77
71
  return '';
78
72
  }
79
73
  }
80
74
  /** Gets instance id from GCP instance metadata. */
81
- async _getInstanceId() {
75
+ async _getInstanceId(isAvail) {
76
+ if (!(await isAvail)) {
77
+ return undefined;
78
+ }
82
79
  try {
83
80
  const id = await gcpMetadata.instance('id');
84
81
  return id.toString();
85
82
  }
86
- catch (_a) {
83
+ catch {
87
84
  return '';
88
85
  }
89
86
  }
90
87
  /** Gets zone from GCP instance metadata. */
91
- async _getZone() {
88
+ async _getZone(isAvail) {
89
+ if (!(await isAvail)) {
90
+ return undefined;
91
+ }
92
92
  try {
93
93
  const zoneId = await gcpMetadata.instance('zone');
94
94
  if (zoneId) {
@@ -96,25 +96,31 @@ class GcpDetector {
96
96
  }
97
97
  return '';
98
98
  }
99
- catch (_a) {
99
+ catch {
100
100
  return '';
101
101
  }
102
102
  }
103
103
  /** Gets cluster name from GCP instance metadata. */
104
- async _getClusterName() {
104
+ async _getClusterName(isAvail) {
105
+ if (!(await isAvail)) {
106
+ return undefined;
107
+ }
105
108
  try {
106
109
  return await gcpMetadata.instance('attributes/cluster-name');
107
110
  }
108
- catch (_a) {
111
+ catch {
109
112
  return '';
110
113
  }
111
114
  }
112
115
  /** Gets hostname from GCP instance metadata. */
113
- async _getHostname() {
116
+ async _getHostname(isAvail) {
117
+ if (!(await isAvail)) {
118
+ return undefined;
119
+ }
114
120
  try {
115
121
  return await gcpMetadata.instance('hostname');
116
122
  }
117
- catch (_a) {
123
+ catch {
118
124
  return '';
119
125
  }
120
126
  }
@@ -1 +1 @@
1
- {"version":3,"file":"GcpDetector.js","sourceRoot":"","sources":["../../../src/detectors/GcpDetector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;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AAC;SAC9D;QAAC,WAAM;YACN,OAAO,EAAE,CAAC;SACX;IACH,CAAC;IAED,gDAAgD;IACxC,KAAK,CAAC,YAAY;QACxB,IAAI;YACF,OAAO,MAAM,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;SAC/C;QAAC,WAAM;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, diag } from '@opentelemetry/api';\nimport { suppressTracing } from '@opentelemetry/core';\nimport {\n DetectorSync,\n ResourceDetectionConfig,\n Resource,\n ResourceAttributes,\n IResource,\n} from '@opentelemetry/resources';\nimport { getEnv } from '@opentelemetry/core';\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 DetectorSync {\n detect(_config?: ResourceDetectionConfig): IResource {\n const attributes = context.with(suppressTracing(context.active()), () =>\n this._getAttributes()\n );\n return new Resource({}, attributes);\n }\n\n /**\n * Attempts to connect and obtain instance configuration data from the GCP metadata service.\n * If the connection is successful it returns a promise containing a {@link ResourceAttributes}\n * object with instance metadata. Returns a promise containing an\n * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.\n */\n private async _getAttributes(): Promise<ResourceAttributes> {\n if (!(await gcpMetadata.isAvailable())) {\n diag.debug('GcpDetector failed: GCP Metadata unavailable.');\n return {};\n }\n\n const [projectId, instanceId, zoneId, clusterName, hostname] =\n await Promise.all([\n this._getProjectId(),\n this._getInstanceId(),\n this._getZone(),\n this._getClusterName(),\n this._getHostname(),\n ]);\n\n const attributes: ResourceAttributes = {};\n attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID] = projectId;\n attributes[SEMRESATTRS_HOST_ID] = instanceId;\n attributes[SEMRESATTRS_HOST_NAME] = hostname;\n attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;\n attributes[SEMRESATTRS_CLOUD_PROVIDER] = CLOUDPROVIDERVALUES_GCP;\n\n if (getEnv().KUBERNETES_SERVICE_HOST)\n this._addK8sAttributes(attributes, clusterName);\n\n return attributes;\n }\n\n /** Add resource attributes for K8s */\n private _addK8sAttributes(\n attributes: ResourceAttributes,\n clusterName: string\n ): void {\n const env = getEnv();\n\n attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;\n attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = env.NAMESPACE;\n attributes[SEMRESATTRS_K8S_POD_NAME] = env.HOSTNAME;\n attributes[SEMRESATTRS_CONTAINER_NAME] = env.CONTAINER_NAME;\n }\n\n /** Gets project id from GCP project metadata. */\n private async _getProjectId(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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(): Promise<string> {\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;;;;;;;;;;;;;;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,aAAa,CAAC,OAAO,CAAC;YAC3D,CAAC,0CAAmB,CAAC,EA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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,2 +1,2 @@
1
- export * from './GcpDetector';
1
+ export { gcpDetector } from './GcpDetector';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -14,16 +14,8 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
- };
27
17
  Object.defineProperty(exports, "__esModule", { value: true });
28
- __exportStar(require("./GcpDetector"), exports);
18
+ exports.gcpDetector = void 0;
19
+ var GcpDetector_1 = require("./GcpDetector");
20
+ Object.defineProperty(exports, "gcpDetector", { enumerable: true, get: function () { return GcpDetector_1.gcpDetector; } });
29
21
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;AAEH,gDAA8B","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\nexport * from './GcpDetector';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/detectors/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,6CAA4C;AAAnC,0GAAA,WAAW,OAAA","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\nexport { gcpDetector } from './GcpDetector';\n"]}
@@ -1,3 +1,3 @@
1
- export * from './detectors';
1
+ export { gcpDetector } from './detectors';
2
2
  export { resetIsAvailableCache } from 'gcp-metadata';
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -14,19 +14,10 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
- };
27
17
  Object.defineProperty(exports, "__esModule", { value: true });
28
- exports.resetIsAvailableCache = void 0;
29
- __exportStar(require("./detectors"), exports);
18
+ exports.resetIsAvailableCache = exports.gcpDetector = void 0;
19
+ var detectors_1 = require("./detectors");
20
+ Object.defineProperty(exports, "gcpDetector", { enumerable: true, get: function () { return detectors_1.gcpDetector; } });
30
21
  // Internal - used for tests only
31
22
  var gcp_metadata_1 = require("gcp-metadata");
32
23
  Object.defineProperty(exports, "resetIsAvailableCache", { enumerable: true, get: function () { return gcp_metadata_1.resetIsAvailableCache; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;;;;;;;;AAEH,8CAA4B;AAE5B,iCAAiC;AACjC,6CAAqD;AAA5C,qHAAA,qBAAqB,OAAA","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\nexport * from './detectors';\n\n// Internal - used for tests only\nexport { resetIsAvailableCache } from 'gcp-metadata';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,yCAA0C;AAAjC,wGAAA,WAAW,OAAA;AAEpB,iCAAiC;AACjC,6CAAqD;AAA5C,qHAAA,qBAAqB,OAAA","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\nexport { gcpDetector } from './detectors';\n\n// Internal - used for tests only\nexport { resetIsAvailableCache } from 'gcp-metadata';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentelemetry/resource-detector-gcp",
3
- "version": "0.33.0",
3
+ "version": "0.34.0",
4
4
  "description": "OpenTelemetry SDK resource detector for GCP",
5
5
  "main": "build/src/index.js",
6
6
  "module": "build/esm/index.js",
@@ -27,7 +27,7 @@
27
27
  "author": "OpenTelemetry Authors",
28
28
  "license": "Apache-2.0",
29
29
  "engines": {
30
- "node": ">=14"
30
+ "node": "^18.19.0 || >=20.6.0"
31
31
  },
32
32
  "files": [
33
33
  "build/src/**/*.js",
@@ -42,26 +42,27 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@opentelemetry/api": "^1.0.0",
45
- "@opentelemetry/contrib-test-utils": "^0.45.0",
46
- "@opentelemetry/instrumentation-http": "^0.57.0",
47
- "@types/mocha": "8.2.3",
45
+ "@opentelemetry/contrib-test-utils": "^0.46.0",
46
+ "@opentelemetry/instrumentation-http": "^0.200.0",
47
+ "@opentelemetry/sdk-trace-base": "^2.0.0",
48
+ "@types/mocha": "10.0.10",
48
49
  "@types/node": "18.18.14",
49
50
  "@types/semver": "7.5.8",
50
51
  "nock": "13.3.3",
51
52
  "nyc": "15.1.0",
52
53
  "rimraf": "5.0.10",
53
- "typescript": "4.4.4"
54
+ "typescript": "5.0.4"
54
55
  },
55
56
  "peerDependencies": {
56
57
  "@opentelemetry/api": "^1.0.0"
57
58
  },
58
59
  "dependencies": {
59
- "@opentelemetry/core": "^1.0.0",
60
- "@opentelemetry/resources": "^1.10.0",
60
+ "@opentelemetry/core": "^2.0.0",
61
+ "@opentelemetry/resources": "^2.0.0",
61
62
  "@opentelemetry/semantic-conventions": "^1.27.0",
62
63
  "gcp-metadata": "^6.0.0"
63
64
  },
64
65
  "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-gcp#readme",
65
66
  "sideEffects": false,
66
- "gitHead": "bcf1da7964e72c47e127cfc3f615c79da3c5dc30"
67
+ "gitHead": "32abc4c3c01d0e78e10022c74b6805b06e0e1fe7"
67
68
  }