@opentelemetry/resource-detector-gcp 0.32.0 → 0.33.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ import { DetectorSync, ResourceDetectionConfig, IResource } from '@opentelemetry/resources';
2
+ /**
3
+ * The GcpDetector can be used to detect if a process is running in the Google
4
+ * Cloud Platform and return a {@link Resource} populated with metadata about
5
+ * the instance. Returns an empty Resource if detection fails.
6
+ */
7
+ declare class GcpDetector implements DetectorSync {
8
+ detect(_config?: ResourceDetectionConfig): IResource;
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.
14
+ */
15
+ private _getAttributes;
16
+ /** Add resource attributes for K8s */
17
+ private _addK8sAttributes;
18
+ /** Gets project id from GCP project metadata. */
19
+ private _getProjectId;
20
+ /** Gets instance id from GCP instance metadata. */
21
+ private _getInstanceId;
22
+ /** Gets zone from GCP instance metadata. */
23
+ private _getZone;
24
+ /** Gets cluster name from GCP instance metadata. */
25
+ private _getClusterName;
26
+ /** Gets hostname from GCP instance metadata. */
27
+ private _getHostname;
28
+ }
29
+ export declare const gcpDetector: GcpDetector;
30
+ export {};
31
+ //# sourceMappingURL=GcpDetector.d.ts.map
@@ -0,0 +1,219 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
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
+ import * as gcpMetadata from 'gcp-metadata';
53
+ import { context, diag } from '@opentelemetry/api';
54
+ import { suppressTracing } from '@opentelemetry/core';
55
+ import { Resource, } from '@opentelemetry/resources';
56
+ 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';
57
+ /**
58
+ * The GcpDetector can be used to detect if a process is running in the Google
59
+ * Cloud Platform and return a {@link Resource} populated with metadata about
60
+ * the instance. Returns an empty Resource if detection fails.
61
+ */
62
+ var GcpDetector = /** @class */ (function () {
63
+ function GcpDetector() {
64
+ }
65
+ GcpDetector.prototype.detect = function (_config) {
66
+ var _this = this;
67
+ var attributes = context.with(suppressTracing(context.active()), function () {
68
+ return _this._getAttributes();
69
+ });
70
+ return new Resource({}, attributes);
71
+ };
72
+ /**
73
+ * Attempts to connect and obtain instance configuration data from the GCP metadata service.
74
+ * If the connection is successful it returns a promise containing a {@link ResourceAttributes}
75
+ * object with instance metadata. Returns a promise containing an
76
+ * empty {@link ResourceAttributes} if the connection or parsing of the metadata fails.
77
+ */
78
+ GcpDetector.prototype._getAttributes = function () {
79
+ return __awaiter(this, void 0, void 0, function () {
80
+ var _a, projectId, instanceId, zoneId, clusterName, hostname, attributes;
81
+ return __generator(this, function (_b) {
82
+ switch (_b.label) {
83
+ case 0: return [4 /*yield*/, gcpMetadata.isAvailable()];
84
+ case 1:
85
+ if (!(_b.sent())) {
86
+ diag.debug('GcpDetector failed: GCP Metadata unavailable.');
87
+ return [2 /*return*/, {}];
88
+ }
89
+ return [4 /*yield*/, Promise.all([
90
+ this._getProjectId(),
91
+ this._getInstanceId(),
92
+ this._getZone(),
93
+ this._getClusterName(),
94
+ this._getHostname(),
95
+ ])];
96
+ case 2:
97
+ _a = _b.sent(), projectId = _a[0], instanceId = _a[1], zoneId = _a[2], clusterName = _a[3], hostname = _a[4];
98
+ attributes = {};
99
+ attributes[SEMRESATTRS_CLOUD_ACCOUNT_ID] = projectId;
100
+ attributes[SEMRESATTRS_HOST_ID] = instanceId;
101
+ attributes[SEMRESATTRS_HOST_NAME] = hostname;
102
+ attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;
103
+ attributes[SEMRESATTRS_CLOUD_PROVIDER] = CLOUDPROVIDERVALUES_GCP;
104
+ if (process.env.KUBERNETES_SERVICE_HOST)
105
+ this._addK8sAttributes(attributes, clusterName);
106
+ return [2 /*return*/, attributes];
107
+ }
108
+ });
109
+ });
110
+ };
111
+ /** Add resource attributes for K8s */
112
+ GcpDetector.prototype._addK8sAttributes = function (attributes, clusterName) {
113
+ var _a, _b, _c;
114
+ attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;
115
+ attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = (_a = process.env.NAMESPACE) !== null && _a !== void 0 ? _a : '';
116
+ attributes[SEMRESATTRS_K8S_POD_NAME] = (_b = process.env.HOSTNAME) !== null && _b !== void 0 ? _b : '';
117
+ attributes[SEMRESATTRS_CONTAINER_NAME] = (_c = process.env.CONTAINER_NAME) !== null && _c !== void 0 ? _c : '';
118
+ };
119
+ /** Gets project id from GCP project metadata. */
120
+ GcpDetector.prototype._getProjectId = function () {
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ var _a;
123
+ return __generator(this, function (_b) {
124
+ switch (_b.label) {
125
+ case 0:
126
+ _b.trys.push([0, 2, , 3]);
127
+ return [4 /*yield*/, gcpMetadata.project('project-id')];
128
+ case 1: return [2 /*return*/, _b.sent()];
129
+ case 2:
130
+ _a = _b.sent();
131
+ return [2 /*return*/, ''];
132
+ case 3: return [2 /*return*/];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ /** Gets instance id from GCP instance metadata. */
138
+ GcpDetector.prototype._getInstanceId = function () {
139
+ return __awaiter(this, void 0, void 0, function () {
140
+ var id, _a;
141
+ return __generator(this, function (_b) {
142
+ switch (_b.label) {
143
+ case 0:
144
+ _b.trys.push([0, 2, , 3]);
145
+ return [4 /*yield*/, gcpMetadata.instance('id')];
146
+ case 1:
147
+ id = _b.sent();
148
+ return [2 /*return*/, id.toString()];
149
+ case 2:
150
+ _a = _b.sent();
151
+ return [2 /*return*/, ''];
152
+ case 3: return [2 /*return*/];
153
+ }
154
+ });
155
+ });
156
+ };
157
+ /** Gets zone from GCP instance metadata. */
158
+ GcpDetector.prototype._getZone = function () {
159
+ return __awaiter(this, void 0, void 0, function () {
160
+ var zoneId, _a;
161
+ return __generator(this, function (_b) {
162
+ switch (_b.label) {
163
+ case 0:
164
+ _b.trys.push([0, 2, , 3]);
165
+ return [4 /*yield*/, gcpMetadata.instance('zone')];
166
+ case 1:
167
+ zoneId = _b.sent();
168
+ if (zoneId) {
169
+ return [2 /*return*/, zoneId.split('/').pop()];
170
+ }
171
+ return [2 /*return*/, ''];
172
+ case 2:
173
+ _a = _b.sent();
174
+ return [2 /*return*/, ''];
175
+ case 3: return [2 /*return*/];
176
+ }
177
+ });
178
+ });
179
+ };
180
+ /** Gets cluster name from GCP instance metadata. */
181
+ GcpDetector.prototype._getClusterName = function () {
182
+ return __awaiter(this, void 0, void 0, function () {
183
+ var _a;
184
+ return __generator(this, function (_b) {
185
+ switch (_b.label) {
186
+ case 0:
187
+ _b.trys.push([0, 2, , 3]);
188
+ return [4 /*yield*/, gcpMetadata.instance('attributes/cluster-name')];
189
+ case 1: return [2 /*return*/, _b.sent()];
190
+ case 2:
191
+ _a = _b.sent();
192
+ return [2 /*return*/, ''];
193
+ case 3: return [2 /*return*/];
194
+ }
195
+ });
196
+ });
197
+ };
198
+ /** Gets hostname from GCP instance metadata. */
199
+ GcpDetector.prototype._getHostname = function () {
200
+ return __awaiter(this, void 0, void 0, function () {
201
+ var _a;
202
+ return __generator(this, function (_b) {
203
+ switch (_b.label) {
204
+ case 0:
205
+ _b.trys.push([0, 2, , 3]);
206
+ return [4 /*yield*/, gcpMetadata.instance('hostname')];
207
+ case 1: return [2 /*return*/, _b.sent()];
208
+ case 2:
209
+ _a = _b.sent();
210
+ return [2 /*return*/, ''];
211
+ case 3: return [2 /*return*/];
212
+ }
213
+ });
214
+ });
215
+ };
216
+ return GcpDetector;
217
+ }());
218
+ export var gcpDetector = new GcpDetector();
219
+ //# sourceMappingURL=GcpDetector.js.map
@@ -0,0 +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,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;IAsGA,CAAC;IArGC,4BAAM,GAAN,UAAO,OAAiC;QAAxC,iBAKC;QAJC,IAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,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EW,qBAAM,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAA;4BAA7C,sBAAO,SAAsC,EAAC;;;wBAE9C,sBAAO,EAAE,EAAC;;;;;KAEb;IACH,kBAAC;AAAD,CAAC,AAtGD,IAsGC;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 {\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 (process.env.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 attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;\n attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = process.env.NAMESPACE ?? '';\n attributes[SEMRESATTRS_K8S_POD_NAME] = process.env.HOSTNAME ?? '';\n attributes[SEMRESATTRS_CONTAINER_NAME] = process.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"]}
@@ -0,0 +1,2 @@
1
+ export * from './GcpDetector';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export * from './GcpDetector';
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,3 @@
1
+ export * from './detectors';
2
+ export { resetIsAvailableCache } from 'gcp-metadata';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export * from './detectors';
17
+ // Internal - used for tests only
18
+ export { resetIsAvailableCache } from 'gcp-metadata';
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"]}
@@ -20,7 +20,6 @@ const gcpMetadata = require("gcp-metadata");
20
20
  const api_1 = require("@opentelemetry/api");
21
21
  const core_1 = require("@opentelemetry/core");
22
22
  const resources_1 = require("@opentelemetry/resources");
23
- const core_2 = require("@opentelemetry/core");
24
23
  const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
25
24
  /**
26
25
  * The GcpDetector can be used to detect if a process is running in the Google
@@ -56,17 +55,17 @@ class GcpDetector {
56
55
  attributes[semantic_conventions_1.SEMRESATTRS_HOST_NAME] = hostname;
57
56
  attributes[semantic_conventions_1.SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = zoneId;
58
57
  attributes[semantic_conventions_1.SEMRESATTRS_CLOUD_PROVIDER] = semantic_conventions_1.CLOUDPROVIDERVALUES_GCP;
59
- if ((0, core_2.getEnv)().KUBERNETES_SERVICE_HOST)
58
+ if (process.env.KUBERNETES_SERVICE_HOST)
60
59
  this._addK8sAttributes(attributes, clusterName);
61
60
  return attributes;
62
61
  }
63
62
  /** Add resource attributes for K8s */
64
63
  _addK8sAttributes(attributes, clusterName) {
65
- const env = (0, core_2.getEnv)();
64
+ var _a, _b, _c;
66
65
  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;
66
+ attributes[semantic_conventions_1.SEMRESATTRS_K8S_NAMESPACE_NAME] = (_a = process.env.NAMESPACE) !== null && _a !== void 0 ? _a : '';
67
+ attributes[semantic_conventions_1.SEMRESATTRS_K8S_POD_NAME] = (_b = process.env.HOSTNAME) !== null && _b !== void 0 ? _b : '';
68
+ attributes[semantic_conventions_1.SEMRESATTRS_CONTAINER_NAME] = (_c = process.env.CONTAINER_NAME) !== null && _c !== void 0 ? _c : '';
70
69
  }
71
70
  /** Gets project id from GCP project metadata. */
72
71
  async _getProjectId() {
@@ -1 +1 @@
1
- {"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C,KAAK,CAAC,eAAe;QAC3B,IAAI;YACF,OAAO,MAAM,WAAW,CAAC,QAAQ,CAAC,yBAAyB,CAAC,CAAC;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,4CAAmD;AACnD,8CAAsD;AACtD,wDAMkC;AAClC,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,IAAI,oBAAQ,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC;IAED;;;;;OAKG;IACK,KAAK,CAAC,cAAc;QAC1B,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,WAAW,EAAE,CAAC,EAAE;YACtC,UAAI,CAAC,KAAK,CAAC,+CAA+C,CAAC,CAAC;YAC5D,OAAO,EAAE,CAAC;SACX;QAED,MAAM,CAAC,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAA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;IAED,mDAAmD;IAC3C,KAAK,CAAC,cAAc;QAC1B,IAAI;YACF,MAAM,EAAE,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC5C,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC;SACtB;QAAC,WAAM;YACN,OAAO,EAAE,CAAC;SACX;IACH,CAAC;IAED,4CAA4C;IACpC,KAAK,CAAC,QAAQ;QACpB,IAAI;YACF,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAClD,IAAI,MAAM,EAAE;gBACV,OAAO,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;aAChC;YACD,OAAO,EAAE,CAAC;SACX;QAAC,WAAM;YACN,OAAO,EAAE,CAAC;SACX;IACH,CAAC;IAED,oDAAoD;IAC5C,KAAK,CAAC,eAAe;QAC3B,IAAI;YACF,OAAO,MAAM,WAAW,CAAC,QAAQ,CAAC,yBAAyB,CAAC,CAAC;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 {\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 (process.env.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 attributes[SEMRESATTRS_K8S_CLUSTER_NAME] = clusterName;\n attributes[SEMRESATTRS_K8S_NAMESPACE_NAME] = process.env.NAMESPACE ?? '';\n attributes[SEMRESATTRS_K8S_POD_NAME] = process.env.HOSTNAME ?? '';\n attributes[SEMRESATTRS_CONTAINER_NAME] = process.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"]}
package/package.json CHANGED
@@ -1,20 +1,21 @@
1
1
  {
2
2
  "name": "@opentelemetry/resource-detector-gcp",
3
- "version": "0.32.0",
3
+ "version": "0.33.1",
4
4
  "description": "OpenTelemetry SDK resource detector for GCP",
5
5
  "main": "build/src/index.js",
6
+ "module": "build/esm/index.js",
6
7
  "types": "build/src/index.d.ts",
7
8
  "repository": "open-telemetry/opentelemetry-js-contrib",
8
9
  "scripts": {
9
- "clean": "rimraf build/*",
10
- "compile": "tsc -p .",
10
+ "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json",
11
+ "compile": "tsc --build tsconfig.json tsconfig.esm.json",
11
12
  "lint": "eslint . --ext .ts",
12
13
  "lint:fix": "eslint . --ext .ts --fix",
13
14
  "prewatch": "npm run precompile",
14
15
  "prepublishOnly": "npm run compile",
15
16
  "test": "nyc mocha 'test/**/*.test.ts'",
16
17
  "tdd": "npm run test -- --watch-extensions ts --watch",
17
- "watch": "tsc -w"
18
+ "watch": "tsc --build --watch tsconfig.json tsconfig.esm.json"
18
19
  },
19
20
  "keywords": [
20
21
  "opentelemetry",
@@ -31,15 +32,18 @@
31
32
  "files": [
32
33
  "build/src/**/*.js",
33
34
  "build/src/**/*.js.map",
34
- "build/src/**/*.d.ts"
35
+ "build/src/**/*.d.ts",
36
+ "build/esm/**/*.js",
37
+ "build/esm/**/*.js.map",
38
+ "build/esm/**/*.d.ts"
35
39
  ],
36
40
  "publishConfig": {
37
41
  "access": "public"
38
42
  },
39
43
  "devDependencies": {
40
44
  "@opentelemetry/api": "^1.0.0",
41
- "@opentelemetry/contrib-test-utils": "^0.45.0",
42
- "@opentelemetry/instrumentation-http": "^0.57.0",
45
+ "@opentelemetry/contrib-test-utils": "^0.45.1",
46
+ "@opentelemetry/instrumentation-http": "^0.57.1",
43
47
  "@types/mocha": "8.2.3",
44
48
  "@types/node": "18.18.14",
45
49
  "@types/semver": "7.5.8",
@@ -58,5 +62,6 @@
58
62
  "gcp-metadata": "^6.0.0"
59
63
  },
60
64
  "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-gcp#readme",
61
- "gitHead": "3ef5205e5353010184c69ff7b9f0f4830f0932de"
65
+ "sideEffects": false,
66
+ "gitHead": "1eb77007669bae87fe5664d68ba6533b95275d52"
62
67
  }