diff --git a/packages/opentelemetry-base/src/index.ts b/packages/opentelemetry-base/src/index.ts index 67a64d0e0e..a6120e227b 100644 --- a/packages/opentelemetry-base/src/index.ts +++ b/packages/opentelemetry-base/src/index.ts @@ -14,5 +14,4 @@ * limitations under the License. */ -export * from './resources/Resource'; export * from './ExportResult'; diff --git a/packages/opentelemetry-resources/.npmignore b/packages/opentelemetry-resources/.npmignore new file mode 100644 index 0000000000..9505ba9450 --- /dev/null +++ b/packages/opentelemetry-resources/.npmignore @@ -0,0 +1,4 @@ +/bin +/coverage +/doc +/test diff --git a/packages/opentelemetry-resources/LICENSE b/packages/opentelemetry-resources/LICENSE new file mode 100644 index 0000000000..b0e74c7d15 --- /dev/null +++ b/packages/opentelemetry-resources/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [2020] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/opentelemetry-resources/package.json b/packages/opentelemetry-resources/package.json new file mode 100644 index 0000000000..33608a0599 --- /dev/null +++ b/packages/opentelemetry-resources/package.json @@ -0,0 +1,59 @@ +{ + "name": "@opentelemetry/resources", + "version": "0.4.0", + "description": "OpenTelemetry SDK resources", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "repository": "open-telemetry/opentelemetry-js", + "scripts": { + "lint": "gts check", + "lint:fix": "gts fix", + "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'", + "tdd": "npm run test -- --watch-extensions ts --watch", + "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "clean": "rimraf build/*", + "precompile": "tsc --version", + "version:update": "node ../../scripts/version-update.js", + "compile": "npm run version:update && tsc -p .", + "prepare": "npm run compile" + }, + "keywords": [ + "opentelemetry", + "nodejs", + "resources", + "stats", + "profiling" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "engines": { + "node": ">=8.0.0" + }, + "files": [ + "build/src/**/*.js", + "build/src/**/*.d.ts", + "doc", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "@types/mocha": "^5.2.7", + "@types/node": "^12.6.9", + "codecov": "^3.6.1", + "gts": "^1.1.0", + "mocha": "^6.2.0", + "nyc": "^14.1.1", + "rimraf": "^3.0.0", + "ts-mocha": "^6.0.0", + "ts-node": "^8.6.2", + "tslint-consistent-codestyle": "^1.16.0", + "tslint-microsoft-contrib": "^6.2.0", + "typescript": "3.7.2" + }, + "dependencies": { + "@opentelemetry/api": "^0.4.0" + } +} diff --git a/packages/opentelemetry-base/src/resources/Resource.ts b/packages/opentelemetry-resources/src/Resource.ts similarity index 89% rename from packages/opentelemetry-base/src/resources/Resource.ts rename to packages/opentelemetry-resources/src/Resource.ts index 89deb077d3..eb35658207 100644 --- a/packages/opentelemetry-base/src/resources/Resource.ts +++ b/packages/opentelemetry-resources/src/Resource.ts @@ -1,5 +1,5 @@ /*! - * Copyright 2019, OpenTelemetry Authors + * Copyright 2020, OpenTelemetry Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +22,10 @@ export class Resource { constructor( /** * A dictionary of labels with string keys and values that provide information - * about the entity. + * about the entity as numbers, strings or booleans * TODO: Consider to add check/validation on labels. */ - readonly labels: { [key: string]: string } + readonly labels: { [key: string]: number | string | boolean } ) {} /** diff --git a/packages/opentelemetry-resources/src/constants.ts b/packages/opentelemetry-resources/src/constants.ts new file mode 100644 index 0000000000..33a5639336 --- /dev/null +++ b/packages/opentelemetry-resources/src/constants.ts @@ -0,0 +1,125 @@ +/** + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Semantic conventions for Resources + * https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-resource-semantic-conventions.md + */ + +/** Attributes defining a running environment (e.g. Cloud, Data Center). */ +export const CLOUD_RESOURCE = { + /** Name of the cloud provider. Example values are aws, azure, gcp. */ + PROVIDER: 'cloud.provider', + + /** The cloud account id used to identify different entities. */ + ACCOUNT_ID: 'cloud.account.id', + + /** A specific geographical location where different entities can run. */ + REGION: 'cloud.region', + + /** Zones are a sub set of the region connected through low-latency links. */ + ZONE: 'cloud.zone', +}; + +/** + * Attributes defining a compute unit (e.g. Container, Process, Lambda + * Function). + * */ +export const CONTAINER_RESOURCE = { + /** The container name. */ + NAME: 'container.name', + + /** The name of the image the container was built on. */ + IMAGE_NAME: 'container.image.name', + + /** The container image tag. */ + IMAGE_TAG: 'container.image.tag', +}; + +/** Attributes defining a computing instance (e.g. host). */ +export const HOST_RESOURCE = { + /** + * Hostname of the host. It contains what the hostname command returns on the + * host machine. + */ + HOSTNAME: 'host.hostname', + + /** + * Unique host id. For Cloud this must be the instance_id assigned by the + * cloud provider + */ + ID: 'host.id', + + /** + * Name of the host. It may contain what hostname returns on Unix systems, + * the fully qualified, or a name specified by the user. + */ + NAME: 'host.name', + + /** Type of host. For Cloud this must be the machine type.*/ + TYPE: 'host.type', + + /** Name of the VM image or OS install the host was instantiated from. */ + IMAGE_NAME: 'host.image.name', + + /** VM image id. For Cloud, this value is from the provider. */ + IMAGE_ID: 'host.image.id', + + /** The version string of the VM image */ + IMAGE_VERSION: 'host.image.version', +}; + +/** Attributes defining a deployment service (e.g. Kubernetes). */ +export const K8S_RESOURCE = { + /** The name of the cluster that the pod is running in. */ + CLUSTER_NAME: 'k8s.cluster.name', + + /** The name of the namespace that the pod is running in. */ + NAMESPACE_NAME: 'k8s.namespace.name', + + /** The name of the pod. */ + POD_NAME: 'k8s.pod.name', + + /** The name of the deployment. */ + DEPLOYMENT_NAME: 'k8s.deployment.name', +}; + +/** Attributes describing the telemetry library. */ +export const LIBRARY_RESOURCE = { + /** The name of the telemetry library. */ + NAME: 'library.name', + + /** The language of telemetry library and of the code instrumented with it. */ + LANGUAGE: 'library.language', + + /** The version string of the library. */ + VERSION: 'library.version', +}; + +/** Attributes describing a service instance. */ +export const SERVICE_RESOURCE = { + /** Logical name of the service. */ + NAME: 'service.name', + + /** A namespace for `service.name`. */ + NAMESPACE: 'service.namespace', + + /** The string ID of the service instance. */ + INSTANCE_ID: 'service.instance.id', + + /** The version string of the service API or implementation. */ + VERSION: 'service.version', +}; diff --git a/packages/opentelemetry-resources/src/index.ts b/packages/opentelemetry-resources/src/index.ts new file mode 100644 index 0000000000..c75f0f7daf --- /dev/null +++ b/packages/opentelemetry-resources/src/index.ts @@ -0,0 +1,18 @@ +/** + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { Resource } from './Resource'; +export * from './constants'; diff --git a/packages/opentelemetry-resources/src/version.ts b/packages/opentelemetry-resources/src/version.ts new file mode 100644 index 0000000000..f469c91367 --- /dev/null +++ b/packages/opentelemetry-resources/src/version.ts @@ -0,0 +1,18 @@ +/*! + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// this is autogenerated file, see scripts/version-update.js +export const VERSION = '0.4.0'; diff --git a/packages/opentelemetry-base/test/resources/resource.test.ts b/packages/opentelemetry-resources/test/Resource.test.ts similarity index 85% rename from packages/opentelemetry-base/test/resources/resource.test.ts rename to packages/opentelemetry-resources/test/Resource.test.ts index 42a8a6cf7e..c15ba1c6cf 100644 --- a/packages/opentelemetry-base/test/resources/resource.test.ts +++ b/packages/opentelemetry-resources/test/Resource.test.ts @@ -1,5 +1,5 @@ /*! - * Copyright 2019, OpenTelemetry Authors + * Copyright 2020, OpenTelemetry Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ */ import * as assert from 'assert'; -import { Resource } from '../../src/resources/Resource'; +import { Resource } from '../src/Resource'; describe('Resource', () => { const resource1 = new Resource({ @@ -75,4 +75,15 @@ describe('Resource', () => { assert.strictEqual(Object.keys(actualResource.labels).length, 3); assert.deepStrictEqual(actualResource, resource1); }); + + it('should accept string, number, and boolean values', () => { + const resource = new Resource({ + 'custom.string': 'strvalue', + 'custom.number': 42, + 'custom.boolean': true, + }); + assert.equal(resource.labels['custom.string'], 'strvalue'); + assert.equal(resource.labels['custom.number'], 42); + assert.equal(resource.labels['custom.boolean'], true); + }); }); diff --git a/packages/opentelemetry-resources/test/resource-assertions.test.ts b/packages/opentelemetry-resources/test/resource-assertions.test.ts new file mode 100644 index 0000000000..4101624054 --- /dev/null +++ b/packages/opentelemetry-resources/test/resource-assertions.test.ts @@ -0,0 +1,180 @@ +/*! + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Resource } from '../src/Resource'; +import { + CLOUD_RESOURCE, + CONTAINER_RESOURCE, + HOST_RESOURCE, + K8S_RESOURCE, + LIBRARY_RESOURCE, + SERVICE_RESOURCE, +} from '../src/constants'; +import { + assertCloudResource, + assertContainerResource, + assertHostResource, + assertK8sResource, + assertLibraryResource, + assertServiceResource, +} from './util/resource-assertions'; + +describe('assertCloudResource', () => { + it('requires one cloud label', () => { + const resource = new Resource({ [CLOUD_RESOURCE.PROVIDER]: 'gcp' }); + assertCloudResource(resource, {}); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [CLOUD_RESOURCE.PROVIDER]: 'gcp', + [CLOUD_RESOURCE.ACCOUNT_ID]: 'opentelemetry', + [CLOUD_RESOURCE.REGION]: 'us-central1', + [CLOUD_RESOURCE.ZONE]: 'us-central1-a', + }); + assertCloudResource(resource, { + provider: 'gcp', + accountId: 'opentelemetry', + region: 'us-central1', + zone: 'us-central1-a', + }); + }); +}); + +describe('assertContainerResource', () => { + it('requires one container label', () => { + const resource = new Resource({ + [CONTAINER_RESOURCE.NAME]: 'opentelemetry-autoconf', + }); + assertContainerResource(resource, {}); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [CONTAINER_RESOURCE.NAME]: 'opentelemetry-autoconf', + [CONTAINER_RESOURCE.IMAGE_NAME]: 'gcr.io/opentelemetry/operator', + [CONTAINER_RESOURCE.IMAGE_TAG]: '0.1', + }); + assertContainerResource(resource, { + name: 'opentelemetry-autoconf', + imageName: 'gcr.io/opentelemetry/operator', + imageTag: '0.1', + }); + }); +}); + +describe('assertHostResource', () => { + it('requires one host label', () => { + const resource = new Resource({ + [HOST_RESOURCE.ID]: 'opentelemetry-test-id', + }); + assertHostResource(resource, {}); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [HOST_RESOURCE.HOSTNAME]: 'opentelemetry-test-hostname', + [HOST_RESOURCE.ID]: 'opentelemetry-test-id', + [HOST_RESOURCE.NAME]: 'opentelemetry-test-name', + [HOST_RESOURCE.TYPE]: 'n1-standard-1', + [HOST_RESOURCE.IMAGE_NAME]: + 'infra-ami-eks-worker-node-7d4ec78312, CentOS-8-x86_64-1905', + [HOST_RESOURCE.IMAGE_ID]: 'ami-07b06b442921831e5', + [HOST_RESOURCE.IMAGE_VERSION]: '0.1', + }); + assertHostResource(resource, { + hostName: 'opentelemetry-test-hostname', + id: 'opentelemetry-test-id', + name: 'opentelemetry-test-name', + hostType: 'n1-standard-1', + imageName: 'infra-ami-eks-worker-node-7d4ec78312, CentOS-8-x86_64-1905', + imageId: 'ami-07b06b442921831e5', + imageVersion: '0.1', + }); + }); +}); + +describe('assertK8sResource', () => { + it('requires one k8s label', () => { + const resource = new Resource({ + [K8S_RESOURCE.CLUSTER_NAME]: 'opentelemetry-cluster', + }); + assertK8sResource(resource, {}); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [K8S_RESOURCE.CLUSTER_NAME]: 'opentelemetry-cluster', + [K8S_RESOURCE.NAMESPACE_NAME]: 'default', + [K8S_RESOURCE.POD_NAME]: 'opentelemetry-pod-autoconf', + [K8S_RESOURCE.DEPLOYMENT_NAME]: 'opentelemetry', + }); + assertK8sResource(resource, { + clusterName: 'opentelemetry-cluster', + namespaceName: 'default', + podName: 'opentelemetry-pod-autoconf', + deploymentName: 'opentelemetry', + }); + }); +}); + +describe('assertLibraryResource', () => { + it('requires one library label', () => { + const resource = new Resource({ [LIBRARY_RESOURCE.NAME]: 'opentelemetry' }); + assertLibraryResource(resource, {}); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [LIBRARY_RESOURCE.NAME]: 'opentelemetry', + [LIBRARY_RESOURCE.LANGUAGE]: 'nodejs', + [LIBRARY_RESOURCE.VERSION]: '0.1.0', + }); + assertLibraryResource(resource, { + name: 'opentelemetry', + language: 'nodejs', + version: '0.1.0', + }); + }); +}); + +describe('assertServiceResource', () => { + it('validates required labels', () => { + const resource = new Resource({ + [SERVICE_RESOURCE.NAME]: 'shoppingcart', + [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', + }); + assertServiceResource(resource, { + name: 'shoppingcart', + instanceId: '627cc493-f310-47de-96bd-71410b7dec09', + }); + }); + + it('validates optional labels', () => { + const resource = new Resource({ + [SERVICE_RESOURCE.NAME]: 'shoppingcart', + [SERVICE_RESOURCE.INSTANCE_ID]: '627cc493-f310-47de-96bd-71410b7dec09', + [SERVICE_RESOURCE.NAMESPACE]: 'shop', + [SERVICE_RESOURCE.VERSION]: '0.1.0', + }); + assertServiceResource(resource, { + name: 'shoppingcart', + instanceId: '627cc493-f310-47de-96bd-71410b7dec09', + namespace: 'shop', + version: '0.1.0', + }); + }); +}); diff --git a/packages/opentelemetry-resources/test/util/resource-assertions.ts b/packages/opentelemetry-resources/test/util/resource-assertions.ts new file mode 100644 index 0000000000..d748229495 --- /dev/null +++ b/packages/opentelemetry-resources/test/util/resource-assertions.ts @@ -0,0 +1,259 @@ +/** + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as assert from 'assert'; +import { Resource } from '../../src/Resource'; +import { + CLOUD_RESOURCE, + CONTAINER_RESOURCE, + HOST_RESOURCE, + K8S_RESOURCE, + LIBRARY_RESOURCE, + SERVICE_RESOURCE, +} from '../../src/constants'; +/** + * Test utility method to validate a cloud resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertCloudResource = ( + resource: Resource, + validations: { + provider?: string; + accountId?: string; + region?: string; + zone?: string; + } +) => { + assertHasOneLabel(CLOUD_RESOURCE, resource); + if (validations.provider) + assert.strictEqual( + resource.labels[CLOUD_RESOURCE.PROVIDER], + validations.provider + ); + if (validations.accountId) + assert.strictEqual( + resource.labels[CLOUD_RESOURCE.ACCOUNT_ID], + validations.accountId + ); + if (validations.region) + assert.strictEqual( + resource.labels[CLOUD_RESOURCE.REGION], + validations.region + ); + if (validations.zone) + assert.strictEqual(resource.labels[CLOUD_RESOURCE.ZONE], validations.zone); +}; + +/** + * Test utility method to validate a container resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertContainerResource = ( + resource: Resource, + validations: { + name?: string; + imageName?: string; + imageTag?: string; + } +) => { + assertHasOneLabel(CONTAINER_RESOURCE, resource); + if (validations.name) + assert.strictEqual( + resource.labels[CONTAINER_RESOURCE.NAME], + validations.name + ); + if (validations.imageName) + assert.strictEqual( + resource.labels[CONTAINER_RESOURCE.IMAGE_NAME], + validations.imageName + ); + if (validations.imageTag) + assert.strictEqual( + resource.labels[CONTAINER_RESOURCE.IMAGE_TAG], + validations.imageTag + ); +}; + +/** + * Test utility method to validate a host resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertHostResource = ( + resource: Resource, + validations: { + hostName?: string; + id?: string; + name?: string; + hostType?: string; + imageName?: string; + imageId?: string; + imageVersion?: string; + } +) => { + assertHasOneLabel(HOST_RESOURCE, resource); + if (validations.hostName) + assert.strictEqual( + resource.labels[HOST_RESOURCE.HOSTNAME], + validations.hostName + ); + if (validations.id) + assert.strictEqual(resource.labels[HOST_RESOURCE.ID], validations.id); + if (validations.name) + assert.strictEqual(resource.labels[HOST_RESOURCE.NAME], validations.name); + if (validations.hostType) + assert.strictEqual( + resource.labels[HOST_RESOURCE.TYPE], + validations.hostType + ); + if (validations.imageName) + assert.strictEqual( + resource.labels[HOST_RESOURCE.IMAGE_NAME], + validations.imageName + ); + if (validations.imageId) + assert.strictEqual( + resource.labels[HOST_RESOURCE.IMAGE_ID], + validations.imageId + ); + if (validations.imageVersion) + assert.strictEqual( + resource.labels[HOST_RESOURCE.IMAGE_VERSION], + validations.imageVersion + ); +}; + +/** + * Test utility method to validate a K8s resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertK8sResource = ( + resource: Resource, + validations: { + clusterName?: string; + namespaceName?: string; + podName?: string; + deploymentName?: string; + } +) => { + assertHasOneLabel(K8S_RESOURCE, resource); + if (validations.clusterName) + assert.strictEqual( + resource.labels[K8S_RESOURCE.CLUSTER_NAME], + validations.clusterName + ); + if (validations.namespaceName) + assert.strictEqual( + resource.labels[K8S_RESOURCE.NAMESPACE_NAME], + validations.namespaceName + ); + if (validations.podName) + assert.strictEqual( + resource.labels[K8S_RESOURCE.POD_NAME], + validations.podName + ); + if (validations.deploymentName) + assert.strictEqual( + resource.labels[K8S_RESOURCE.DEPLOYMENT_NAME], + validations.deploymentName + ); +}; + +/** + * Test utility method to validate a library resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertLibraryResource = ( + resource: Resource, + validations: { + name?: string; + language?: string; + version?: string; + } +) => { + assertHasOneLabel(LIBRARY_RESOURCE, resource); + if (validations.name) + assert.strictEqual( + resource.labels[LIBRARY_RESOURCE.NAME], + validations.name + ); + if (validations.language) + assert.strictEqual( + resource.labels[LIBRARY_RESOURCE.LANGUAGE], + validations.language + ); + if (validations.version) + assert.strictEqual( + resource.labels[LIBRARY_RESOURCE.VERSION], + validations.version + ); +}; + +/** + * Test utility method to validate a service resource + * + * @param resource the Resource to validate + * @param validations validations for the resource labels + */ +export const assertServiceResource = ( + resource: Resource, + validations: { + name: string; + instanceId: string; + namespace?: string; + version?: string; + } +) => { + assert.strictEqual(resource.labels[SERVICE_RESOURCE.NAME], validations.name); + assert.strictEqual( + resource.labels[SERVICE_RESOURCE.INSTANCE_ID], + validations.instanceId + ); + if (validations.namespace) + assert.strictEqual( + resource.labels[SERVICE_RESOURCE.NAMESPACE], + validations.namespace + ); + if (validations.version) + assert.strictEqual( + resource.labels[SERVICE_RESOURCE.VERSION], + validations.version + ); +}; + +const assertHasOneLabel = ( + constants: { [key: string]: string }, + resource: Resource +): void => { + const hasOne = Object.values(constants).reduce( + (found, key) => found || resource.labels.hasOwnProperty(key), + false + ); + assert.ok( + hasOne, + 'Resource must have one of the following labels: ' + + Object.values(constants).join(', ') + ); +}; diff --git a/packages/opentelemetry-resources/tsconfig.json b/packages/opentelemetry-resources/tsconfig.json new file mode 100644 index 0000000000..e4b3b29e6a --- /dev/null +++ b/packages/opentelemetry-resources/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": ["src/**/*.ts", "test/**/*.ts"] +} diff --git a/packages/opentelemetry-resources/tslint.json b/packages/opentelemetry-resources/tslint.json new file mode 100644 index 0000000000..69d07efd8b --- /dev/null +++ b/packages/opentelemetry-resources/tslint.json @@ -0,0 +1,7 @@ +{ + "rulesDirectory": ["node_modules/tslint-microsoft-contrib"], + "extends": [ + "../../tslint.base.js", + "./node_modules/tslint-consistent-codestyle" + ] +}