diff --git a/propagators/opentelemetry-propagator-opentracing/.eslintignore b/propagators/opentelemetry-propagator-opentracing/.eslintignore new file mode 100644 index 0000000000..378eac25d3 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/.eslintignore @@ -0,0 +1 @@ +build diff --git a/propagators/opentelemetry-propagator-opentracing/.eslintrc.js b/propagators/opentelemetry-propagator-opentracing/.eslintrc.js new file mode 100644 index 0000000000..9dfe62f9b8 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/.eslintrc.js @@ -0,0 +1,9 @@ +module.exports = { + "env": { + "mocha": true, + "commonjs": true, + "node": true, + "browser": true + }, + ...require('../../eslint.config.js') +} diff --git a/propagators/opentelemetry-propagator-opentracing/.npmignore b/propagators/opentelemetry-propagator-opentracing/.npmignore new file mode 100644 index 0000000000..9505ba9450 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/.npmignore @@ -0,0 +1,4 @@ +/bin +/coverage +/doc +/test diff --git a/propagators/opentelemetry-propagator-opentracing/LICENSE b/propagators/opentelemetry-propagator-opentracing/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/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 [yyyy] [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/propagators/opentelemetry-propagator-opentracing/README.md b/propagators/opentelemetry-propagator-opentracing/README.md new file mode 100644 index 0000000000..aad326b39f --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/README.md @@ -0,0 +1,70 @@ +# OpenTelemetry Propagator OpenTracing + +[![Gitter chat][gitter-image]][gitter-url] +[![NPM Published Version][npm-img]][npm-url] +[![dependencies][dependencies-image]][dependencies-url] +[![devDependencies][devdependencies-image]][devdependencies-url] +[![Apache License][license-image]][license-image] + +## OpenTracing Format + +| Header Name | Description | Required | +| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | --------------------- | +| `ot-tracer-traceid` | uint64 encoded as a string of 16 hex characters | yes | +| `ot-tracer-spanid` | uint64 encoded as a string of 16 hex characters | yes | +| `ot-tracer-sampled` | boolean encoded as a string with the values `'true'` or `'false'` | no | +| `ot-baggage-*` | repeated string to string key-value baggage items; keys are prefixed with `ot-baggage-` and the corresponding value is the raw string. | if baggage is present | + +### Interop and trace ids + +The OpenTracing propagation format expects trace ids to be 64-bits. In order to +interop with OpenTelemetry, trace ids need to be truncated to 64-bits before +sending them on the wire. When truncating, the least significant (right-most) +bits MUST be retained. For example, a trace id of +`3c3039f4d78d5c02ee8e3e41b17ce105` would be truncated to `ee8e3e41b17ce105`. + +### Baggage Notes + +Baggage keys and values are validated according to [rfc7230][rfc7230-url]. Any +keys or values that would result in invalid HTTP headers will be silently +dropped during inject. + +OpenTelemetry Baggage is represented as multiple headers where the +names are carrier dependent. For this reason, they are omitted from the `fields` +method. This behavior should be taken into account if your application relies +on the `fields` functionality. See the [specification][fields-spec-url] for +more details. + +## Example Usage + +```javascript +const api = require('@opentelemetry/api'); +const { + OpenTracingPropagator, +} = require('@opentelemetry/propagator-opentracing'); + +api.propagation.setGlobalPropagator(new OpenTracingPropagator()); +``` + +## Useful links + +- For more information on OpenTelemetry, visit: +- For more about OpenTelemetry JavaScript: +- For help or feedback on this project, join us on [gitter][gitter-url] + +## License + +Apache 2.0 - See [LICENSE][license-url] for more information. + +[gitter-image]: https://badges.gitter.im/open-telemetry/opentelemetry-js-contrib.svg +[gitter-url]: https://gitter.im/open-telemetry/opentelemetry-node?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge +[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/master/LICENSE +[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat +[dependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib/status.svg?path=packages/opentelemetry-propagator-opentracing +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-propagator-opentracing +[devdependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib/dev-status.svg?path=packages/opentelemetry-propagator-opentracing +[devdependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js-contrib?path=packages%2Fopentelemetry-propagator-opentracing&type=dev +[npm-url]: https://www.npmjs.com/package/@opentelemetry/propagator-opentracing +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fpropagator-opentracing.svg +[rfc7230-url]: https://tools.ietf.org/html/rfc7230#section-3.2 +[fields-spec-url]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/context/api-propagators.md#fields diff --git a/propagators/opentelemetry-propagator-opentracing/karma.conf.js b/propagators/opentelemetry-propagator-opentracing/karma.conf.js new file mode 100644 index 0000000000..f1a1f3a7e8 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/karma.conf.js @@ -0,0 +1,26 @@ +/*! + * Copyright The 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 + * + * 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. + */ + +const karmaWebpackConfig = require('../../karma.webpack'); +const karmaBaseConfig = require('../../karma.base'); + +module.exports = config => { + config.set( + Object.assign({}, karmaBaseConfig, { + webpack: karmaWebpackConfig, + }) + ); +}; diff --git a/propagators/opentelemetry-propagator-opentracing/package.json b/propagators/opentelemetry-propagator-opentracing/package.json new file mode 100644 index 0000000000..d4ceb14355 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/package.json @@ -0,0 +1,67 @@ +{ + "name": "@opentelemetry/propagator-opentracing", + "version": "0.13.0", + "description": "OpenTelemetry propagator OpenTracing provides context propagation for systems that are using the OpenTracing header format", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "repository": "open-telemetry/opentelemetry-js-contrib", + "scripts": { + "compile": "tsc --build", + "clean": "tsc --build --clean", + "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts", + "test:browser": "nyc karma start --single-run", + "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "version": "node ../../scripts/version-update.js", + "watch": "tsc --build --watch" + }, + "keywords": [ + "opentelemetry", + "nodejs", + "tracing", + "profiling", + "monitoring", + "opentracing", + "propagator" + ], + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "engines": { + "node": ">=8.0.0" + }, + "files": [ + "build/src/**/*.js", + "build/src/**/*.js.map", + "build/src/**/*.d.ts", + "LICENSE", + "README.md" + ], + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@opentelemetry/api": "^0.15.0" + }, + "devDependencies": { + "@types/mocha": "8.2.0", + "@types/node": "14.14.12", + "@types/webpack-env": "1.16.0", + "codecov": "3.8.1", + "gts": "2.0.2", + "istanbul-instrumenter-loader": "3.0.1", + "karma": "5.2.3", + "karma-chrome-launcher": "3.1.0", + "karma-coverage-istanbul-reporter": "3.0.3", + "karma-mocha": "2.0.1", + "karma-spec-reporter": "0.0.32", + "karma-webpack": "4.0.2", + "mocha": "7.2.0", + "nyc": "15.1.0", + "rimraf": "3.0.2", + "ts-loader": "8.0.12", + "ts-mocha": "8.0.0", + "typescript": "3.9.7", + "webpack": "4.46.0" + } +} diff --git a/propagators/opentelemetry-propagator-opentracing/src/OpenTracingPropagator.ts b/propagators/opentelemetry-propagator-opentracing/src/OpenTracingPropagator.ts new file mode 100644 index 0000000000..7d8399101d --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/src/OpenTracingPropagator.ts @@ -0,0 +1,129 @@ +/* + * Copyright The 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 { + Baggage, + Context, + getBaggage, + getSpanContext, + isSpanContextValid, + isValidSpanId, + isValidTraceId, + setBaggage, + setSpanContext, + TextMapGetter, + TextMapPropagator, + TextMapSetter, + TraceFlags, +} from '@opentelemetry/api'; + +/** OT header keys */ +export const OT_TRACE_ID_HEADER = 'ot-tracer-traceid'; +export const OT_SPAN_ID_HEADER = 'ot-tracer-spanid'; +export const OT_SAMPLED_HEADER = 'ot-tracer-sampled'; +export const OT_BAGGAGE_PREFIX = 'ot-baggage-'; + +const FIELDS = [OT_TRACE_ID_HEADER, OT_SPAN_ID_HEADER, OT_SAMPLED_HEADER]; +const PADDING = '0'.repeat(16); + +function readHeader( + carrier: unknown, + getter: TextMapGetter, + key: string +): string { + let header = getter.get(carrier, key); + if (Array.isArray(header)) [header] = header; + return header || ''; +} + +const VALID_HEADER_NAME_CHARS = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/; + +function isValidHeaderName(name: string): boolean { + return VALID_HEADER_NAME_CHARS.test(name); +} + +const INVALID_HEADER_VALUE_CHARS = /[^\t\x20-\x7e\x80-\xff]/; + +function isValidHeaderValue(value: string): boolean { + return !INVALID_HEADER_VALUE_CHARS.test(value); +} + +/** + * Propagator for the OpenTracing HTTP format. + */ +export class OpenTracingPropagator implements TextMapPropagator { + inject(context: Context, carrier: unknown, setter: TextMapSetter) { + const spanContext = getSpanContext(context); + if (!spanContext || !isSpanContextValid(spanContext)) return; + + setter.set(carrier, OT_TRACE_ID_HEADER, spanContext.traceId.substr(16)); + setter.set(carrier, OT_SPAN_ID_HEADER, spanContext.spanId); + setter.set( + carrier, + OT_SAMPLED_HEADER, + spanContext.traceFlags === TraceFlags.SAMPLED ? 'true' : 'false' + ); + + const baggage = getBaggage(context); + if (!baggage) return; + + Object.entries(baggage).forEach(([k, v]) => { + if (!isValidHeaderName(k) || !isValidHeaderValue(v.value)) return; + setter.set(carrier, `${OT_BAGGAGE_PREFIX}${k}`, v.value); + }); + } + + extract(context: Context, carrier: unknown, getter: TextMapGetter): Context { + let traceId = readHeader(carrier, getter, OT_TRACE_ID_HEADER); + if (traceId.length == 16) traceId = `${PADDING}${traceId}`; + const spanId = readHeader(carrier, getter, OT_SPAN_ID_HEADER); + const sampled = readHeader(carrier, getter, OT_SAMPLED_HEADER); + const traceFlags = + sampled === 'true' ? TraceFlags.SAMPLED : TraceFlags.NONE; + + if (isValidTraceId(traceId) && isValidSpanId(spanId)) { + context = setSpanContext(context, { + traceId, + spanId, + isRemote: true, + traceFlags, + }); + + const baggage: Baggage = getBaggage(context) || {}; + + getter.keys(carrier).forEach(k => { + if (!k.startsWith(OT_BAGGAGE_PREFIX)) return; + const value = readHeader(carrier, getter, k); + baggage[k.substr(OT_BAGGAGE_PREFIX.length)] = { value }; + }); + + if (Object.keys(baggage).length > 0) { + context = setBaggage(context, baggage); + } + } + + return context; + } + + /** + * Note: fields does not include baggage headers as they are dependent on + * carrier instance. Attempting to reuse a carrier by clearing fields could + * result in a memory leak. + */ + fields(): string[] { + return FIELDS.slice(); + } +} diff --git a/propagators/opentelemetry-propagator-opentracing/src/index.ts b/propagators/opentelemetry-propagator-opentracing/src/index.ts new file mode 100644 index 0000000000..849bcd4a5c --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/src/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright The 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 * from './OpenTracingPropagator'; diff --git a/propagators/opentelemetry-propagator-opentracing/src/version.ts b/propagators/opentelemetry-propagator-opentracing/src/version.ts new file mode 100644 index 0000000000..db447568f3 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/src/version.ts @@ -0,0 +1,18 @@ +/* + * Copyright The 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.13.0'; diff --git a/propagators/opentelemetry-propagator-opentracing/test/OpenTracingPropagator.test.ts b/propagators/opentelemetry-propagator-opentracing/test/OpenTracingPropagator.test.ts new file mode 100644 index 0000000000..47338c3b31 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/test/OpenTracingPropagator.test.ts @@ -0,0 +1,346 @@ +/* + * Copyright The 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 { + defaultTextMapGetter, + defaultTextMapSetter, + INVALID_SPANID, + INVALID_TRACEID, + getSpanContext, + setSpanContext, + SpanContext, + TraceFlags, + Baggage, + setBaggage, + getBaggage, + ROOT_CONTEXT, +} from '@opentelemetry/api'; +import * as assert from 'assert'; +import { + OpenTracingPropagator, + OT_TRACE_ID_HEADER, + OT_SPAN_ID_HEADER, + OT_SAMPLED_HEADER, + OT_BAGGAGE_PREFIX, +} from '../src/OpenTracingPropagator'; + +describe('OpenTracingPropagator', () => { + const propagator = new OpenTracingPropagator(); + let carrier: { [key: string]: unknown }; + + beforeEach(() => { + carrier = {}; + }); + + describe('.inject()', () => { + it('truncates trace id to 64bits', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.NONE, + }; + + propagator.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + + assert.strictEqual(carrier[OT_TRACE_ID_HEADER], '64fe8b2a57d3eff7'); + }); + + it('injects context with sampled trace flags', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.SAMPLED, + }; + + propagator.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + + assert.strictEqual(carrier[OT_TRACE_ID_HEADER], '64fe8b2a57d3eff7'); + assert.strictEqual(carrier[OT_SPAN_ID_HEADER], 'e457b5a2e4d86bd1'); + assert.strictEqual(carrier[OT_SAMPLED_HEADER], 'true'); + }); + + it('injects context with unspecified trace flags', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.NONE, + }; + + propagator.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + + assert.strictEqual(carrier[OT_TRACE_ID_HEADER], '64fe8b2a57d3eff7'); + assert.strictEqual(carrier[OT_SPAN_ID_HEADER], 'e457b5a2e4d86bd1'); + assert.strictEqual(carrier[OT_SAMPLED_HEADER], 'false'); + }); + + it('no-ops if traceid invalid', () => { + const spanContext: SpanContext = { + traceId: INVALID_TRACEID, + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.SAMPLED, + }; + + propagator.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + + assert.strictEqual(carrier[OT_TRACE_ID_HEADER], undefined); + assert.strictEqual(carrier[OT_SPAN_ID_HEADER], undefined); + assert.strictEqual(carrier[OT_SAMPLED_HEADER], undefined); + }); + + it('no-ops if spanid invalid', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: INVALID_SPANID, + traceFlags: TraceFlags.SAMPLED, + }; + + propagator.inject( + setSpanContext(ROOT_CONTEXT, spanContext), + carrier, + defaultTextMapSetter + ); + + assert.strictEqual(carrier[OT_TRACE_ID_HEADER], undefined); + assert.strictEqual(carrier[OT_SPAN_ID_HEADER], undefined); + assert.strictEqual(carrier[OT_SAMPLED_HEADER], undefined); + }); + + it('injects baggage', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.SAMPLED, + }; + + let context = setSpanContext(ROOT_CONTEXT, spanContext); + + const baggage: Baggage = { + foo: { value: 'bar' }, + bar: { value: 'baz' }, + }; + + context = setBaggage(context, baggage); + + propagator.inject(context, carrier, defaultTextMapSetter); + + assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}foo`], 'bar'); + assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz'); + }); + + it('omits baggage items with invalid keys', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.SAMPLED, + }; + + let context = setSpanContext(ROOT_CONTEXT, spanContext); + + const baggage: Baggage = { + fθθ: { value: 'bar' }, + bar: { value: 'baz' }, + }; + + context = setBaggage(context, baggage); + + propagator.inject(context, carrier, defaultTextMapSetter); + assert.ok(!(`${OT_BAGGAGE_PREFIX}fθθ` in carrier)); + assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz'); + }); + + it('omits baggage items with invalid values', () => { + const spanContext: SpanContext = { + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + spanId: 'e457b5a2e4d86bd1', + traceFlags: TraceFlags.SAMPLED, + }; + + let context = setSpanContext(ROOT_CONTEXT, spanContext); + + const baggage: Baggage = { + foo: { value: 'bαr' }, + bar: { value: 'baz' }, + }; + + context = setBaggage(context, baggage); + + propagator.inject(context, carrier, defaultTextMapSetter); + assert.ok(!(`${OT_BAGGAGE_PREFIX}foo` in carrier)); + assert.strictEqual(carrier[`${OT_BAGGAGE_PREFIX}bar`], 'baz'); + }); + }); + + describe('.extract', () => { + it('extracts context with traceid, spanid, sampled true', () => { + carrier = { + [OT_TRACE_ID_HEADER]: '80f198ee56343ba864fe8b2a57d3eff7', + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'true', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + + assert.deepStrictEqual(extractedSpanContext, { + spanId: 'e457b5a2e4d86bd1', + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + isRemote: true, + traceFlags: TraceFlags.SAMPLED, + }); + }); + + it('extracts context with traceid, spanid, sampled false', () => { + carrier = { + [OT_TRACE_ID_HEADER]: '80f198ee56343ba864fe8b2a57d3eff7', + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'false', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + + assert.deepStrictEqual(extractedSpanContext, { + spanId: 'e457b5a2e4d86bd1', + traceId: '80f198ee56343ba864fe8b2a57d3eff7', + isRemote: true, + traceFlags: TraceFlags.NONE, + }); + }); + + it('converts 8-byte traceid', () => { + carrier = { + [OT_TRACE_ID_HEADER]: '4aaba1a52cf8ee09', + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'false', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + assert.deepStrictEqual(extractedSpanContext, { + spanId: 'e457b5a2e4d86bd1', + traceId: '00000000000000004aaba1a52cf8ee09', + isRemote: true, + traceFlags: TraceFlags.NONE, + }); + }); + + it('handles malformed traceid', () => { + carrier = { + [OT_TRACE_ID_HEADER]: 'abc123', + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'false', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + assert.deepStrictEqual(undefined, extractedSpanContext); + }); + + it('handles malformed spanid', () => { + carrier = { + [OT_TRACE_ID_HEADER]: '0f198ee56343ba864fe8b2a57d3eff7', + [OT_SPAN_ID_HEADER]: 'abc123', + [OT_SAMPLED_HEADER]: 'false', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + assert.deepStrictEqual(undefined, extractedSpanContext); + }); + + it('handles invalid traceid', () => { + carrier = { + [OT_TRACE_ID_HEADER]: INVALID_TRACEID, + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'false', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const extractedSpanContext = getSpanContext(context); + assert.deepStrictEqual(undefined, extractedSpanContext); + }); + + it('extracts baggage', () => { + carrier = { + [OT_TRACE_ID_HEADER]: '80f198ee56343ba864fe8b2a57d3eff7', + [OT_SPAN_ID_HEADER]: 'e457b5a2e4d86bd1', + [OT_SAMPLED_HEADER]: 'false', + [`${OT_BAGGAGE_PREFIX}foo`]: 'bar', + [`${OT_BAGGAGE_PREFIX}bar`]: 'baz', + }; + + const context = propagator.extract( + ROOT_CONTEXT, + carrier, + defaultTextMapGetter + ); + + const baggage = getBaggage(context); + + assert.ok(baggage); + assert.deepStrictEqual(baggage, { + foo: { value: 'bar' }, + bar: { value: 'baz' }, + }); + }); + }); +}); diff --git a/propagators/opentelemetry-propagator-opentracing/test/index-webpack.ts b/propagators/opentelemetry-propagator-opentracing/test/index-webpack.ts new file mode 100644 index 0000000000..061a48ccfa --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/test/index-webpack.ts @@ -0,0 +1,20 @@ +/* + * Copyright The 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. + */ +const testsContext = require.context('.', true, /test$/); +testsContext.keys().forEach(testsContext); + +const srcContext = require.context('.', true, /src$/); +srcContext.keys().forEach(srcContext); diff --git a/propagators/opentelemetry-propagator-opentracing/tsconfig.json b/propagators/opentelemetry-propagator-opentracing/tsconfig.json new file mode 100644 index 0000000000..4078877ce6 --- /dev/null +++ b/propagators/opentelemetry-propagator-opentracing/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +}