diff --git a/benchmark/propagator.js b/benchmark/propagator.js index 6f547743d1..e3e5617523 100644 --- a/benchmark/propagator.js +++ b/benchmark/propagator.js @@ -4,11 +4,12 @@ const benchmark = require('./benchmark'); const opentelemetry = require('../packages/opentelemetry-core'); const api = require('../packages/opentelemetry-api'); const { Context } = require('../packages/opentelemetry-context-base'); +const { B3Propagator } = require('../packages/opentelemetry-propagator-b3'); const setups = [ { name: 'B3Propagator', - propagator: new opentelemetry.B3Propagator(), + propagator: new B3Propagator(), injectCarrier: {}, extractCarrier: { 'x-b3-traceid': 'd4cda95b652f4a1592b449d5929fda1b', diff --git a/examples/tracer-web/examples/fetch/index.js b/examples/tracer-web/examples/fetch/index.js index 2ed363453a..6717136bee 100644 --- a/examples/tracer-web/examples/fetch/index.js +++ b/examples/tracer-web/examples/fetch/index.js @@ -5,7 +5,7 @@ import { CollectorTraceExporter } from '@opentelemetry/exporter-collector'; import { WebTracerProvider } from '@opentelemetry/web'; import { FetchPlugin } from '@opentelemetry/plugin-fetch'; import { ZoneContextManager } from '@opentelemetry/context-zone'; -import { B3Propagator } from '@opentelemetry/core'; +import { B3Propagator } from '@opentelemetry/propagator-b3'; const provider = new WebTracerProvider({ plugins: [ diff --git a/examples/tracer-web/examples/xml-http-request/index.js b/examples/tracer-web/examples/xml-http-request/index.js index fb1a6529c8..d0a148e7e3 100644 --- a/examples/tracer-web/examples/xml-http-request/index.js +++ b/examples/tracer-web/examples/xml-http-request/index.js @@ -3,7 +3,7 @@ import { WebTracerProvider } from '@opentelemetry/web'; import { XMLHttpRequestPlugin } from '@opentelemetry/plugin-xml-http-request'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import { CollectorTraceExporter } from '@opentelemetry/exporter-collector'; -import { B3Propagator } from '@opentelemetry/core'; +import { B3Propagator } from '@opentelemetry/propagator-b3'; const providerWithZone = new WebTracerProvider({ plugins: [ diff --git a/examples/tracer-web/package.json b/examples/tracer-web/package.json index 82ce7823c2..f999bfdd49 100644 --- a/examples/tracer-web/package.json +++ b/examples/tracer-web/package.json @@ -39,6 +39,7 @@ "@opentelemetry/exporter-collector": "^0.12.0", "@opentelemetry/exporter-zipkin": "^0.12.0", "@opentelemetry/metrics": "^0.12.0", + "@opentelemetry/propagator-b3": "^0.12.0", "@opentelemetry/plugin-document-load": "^0.9.0", "@opentelemetry/plugin-fetch": "^0.12.0", "@opentelemetry/plugin-user-interaction": "^0.9.0", diff --git a/packages/opentelemetry-api/README.md b/packages/opentelemetry-api/README.md index b824c4ae36..e85eac8c25 100644 --- a/packages/opentelemetry-api/README.md +++ b/packages/opentelemetry-api/README.md @@ -106,7 +106,7 @@ Because the npm installer and node module resolution algorithm could potentially If you prefer to choose your own propagator or context manager, you may pass an options object into the `tracerProvider.register()` method. Omitted or `undefined` options will be replaced by a default value and `null` values will be skipped. ```javascript -const { B3Propagator } = require("@opentelemetry/core"); +const { B3Propagator } = require("@opentelemetry/propagator-b3"); tracerProvider.register({ // Use B3 Propagation diff --git a/packages/opentelemetry-core/README.md b/packages/opentelemetry-core/README.md index 5f60e17947..1b0c37196e 100644 --- a/packages/opentelemetry-core/README.md +++ b/packages/opentelemetry-core/README.md @@ -14,7 +14,6 @@ This package provides default implementations of the OpenTelemetry API for trace - [Built-in Implementations](#built-in-implementations) - [Built-in Propagators](#built-in-propagators) - [HttpTraceContext Propagator](#httptracecontext-propagator) - - [B3 Propagator](#b3-propagator) - [Composite Propagator](#composite-propagator) - [Correlation Context Propagator](#correlation-context-propagator) - [Built-in Sampler](#built-in-sampler) @@ -38,18 +37,6 @@ const { HttpTraceContext } = require("@opentelemetry/core"); api.propagation.setGlobalPropagator(new HttpTraceContext()); ``` -#### B3 Propagator - -This is propagator for the B3 HTTP header format, which sends a `SpanContext` on the wire in an HTTP request, allowing other services to create spans with the right context. Based on: - -```js -const api = require("@opentelemetry/api"); -const { B3Propagator } = require("@opentelemetry/core"); - -/* Set Global Propagator */ -api.propagation.setGlobalPropagator(new B3Propagator()); -``` - #### Composite Propagator Combines multiple propagators into a single propagator. diff --git a/packages/opentelemetry-core/package.json b/packages/opentelemetry-core/package.json index 138cffd786..fef4f8e1ae 100644 --- a/packages/opentelemetry-core/package.json +++ b/packages/opentelemetry-core/package.json @@ -52,6 +52,7 @@ "access": "public" }, "devDependencies": { + "@opentelemetry/propagator-b3": "^0.12.0", "@types/mocha": "8.0.2", "@types/node": "14.0.27", "@types/semver": "7.3.2", diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index d3a84ee651..e3a836e1b2 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -23,9 +23,6 @@ export * from './common/time'; export * from './common/types'; export * from './ExportResult'; export * from './version'; -export * from './context/propagation/B3Propagator'; -export * from './context/propagation/B3SinglePropagator'; -export * from './context/propagation/B3MultiPropagator'; export * from './context/propagation/composite'; export * from './context/propagation/HttpTraceContext'; export * from './context/propagation/types'; diff --git a/packages/opentelemetry-core/test/context/composite.test.ts b/packages/opentelemetry-core/test/context/composite.test.ts index eccf66e9de..b54c9859a6 100644 --- a/packages/opentelemetry-core/test/context/composite.test.ts +++ b/packages/opentelemetry-core/test/context/composite.test.ts @@ -34,7 +34,7 @@ import { X_B3_SAMPLED, X_B3_SPAN_ID, X_B3_TRACE_ID, -} from '../../src/context/propagation/B3MultiPropagator'; +} from '@opentelemetry/propagator-b3'; import { TRACE_PARENT_HEADER, TRACE_STATE_HEADER, diff --git a/packages/opentelemetry-plugin-fetch/package.json b/packages/opentelemetry-plugin-fetch/package.json index 3493f3ef00..93d501be07 100644 --- a/packages/opentelemetry-plugin-fetch/package.json +++ b/packages/opentelemetry-plugin-fetch/package.json @@ -46,6 +46,7 @@ "devDependencies": { "@babel/core": "7.11.1", "@opentelemetry/context-zone": "^0.12.0", + "@opentelemetry/propagator-b3": "^0.12.0", "@opentelemetry/tracing": "^0.12.0", "@types/mocha": "8.0.2", "@types/node": "14.0.27", diff --git a/packages/opentelemetry-plugin-fetch/test/fetch.test.ts b/packages/opentelemetry-plugin-fetch/test/fetch.test.ts index 39ca5ab8f3..647537f6c3 100644 --- a/packages/opentelemetry-plugin-fetch/test/fetch.test.ts +++ b/packages/opentelemetry-plugin-fetch/test/fetch.test.ts @@ -15,6 +15,13 @@ */ import * as api from '@opentelemetry/api'; import * as core from '@opentelemetry/core'; +import { + B3Propagator, + B3InjectEncoding, + X_B3_TRACE_ID, + X_B3_SPAN_ID, + X_B3_SAMPLED, +} from '@opentelemetry/propagator-b3'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import * as tracing from '@opentelemetry/tracing'; import { @@ -219,8 +226,8 @@ describe('fetch', () => { before(() => { api.propagation.setGlobalPropagator( - new core.B3Propagator({ - injectEncoding: core.B3InjectEncoding.MULTI_HEADER, + new B3Propagator({ + injectEncoding: B3InjectEncoding.MULTI_HEADER, }) ); }); @@ -449,26 +456,26 @@ describe('fetch', () => { it('should set trace headers', () => { const span: api.Span = exportSpy.args[1][0][0]; assert.strictEqual( - lastResponse.headers[core.X_B3_TRACE_ID], + lastResponse.headers[X_B3_TRACE_ID], span.context().traceId, - `trace header '${core.X_B3_TRACE_ID}' not set` + `trace header '${X_B3_TRACE_ID}' not set` ); assert.strictEqual( - lastResponse.headers[core.X_B3_SPAN_ID], + lastResponse.headers[X_B3_SPAN_ID], span.context().spanId, - `trace header '${core.X_B3_SPAN_ID}' not set` + `trace header '${X_B3_SPAN_ID}' not set` ); assert.strictEqual( - lastResponse.headers[core.X_B3_SAMPLED], + lastResponse.headers[X_B3_SAMPLED], String(span.context().traceFlags), - `trace header '${core.X_B3_SAMPLED}' not set` + `trace header '${X_B3_SAMPLED}' not set` ); }); it('should set trace headers with a request object', () => { const r = new Request('url'); window.fetch(r); - assert.ok(typeof r.headers.get(core.X_B3_TRACE_ID) === 'string'); + assert.ok(typeof r.headers.get(X_B3_TRACE_ID) === 'string'); }); it('should NOT clear the resources', () => { @@ -486,19 +493,19 @@ describe('fetch', () => { }); it('should NOT set trace headers', () => { assert.strictEqual( - lastResponse.headers[core.X_B3_TRACE_ID], + lastResponse.headers[X_B3_TRACE_ID], undefined, - `trace header '${core.X_B3_TRACE_ID}' should not be set` + `trace header '${X_B3_TRACE_ID}' should not be set` ); assert.strictEqual( - lastResponse.headers[core.X_B3_SPAN_ID], + lastResponse.headers[X_B3_SPAN_ID], undefined, - `trace header '${core.X_B3_SPAN_ID}' should not be set` + `trace header '${X_B3_SPAN_ID}' should not be set` ); assert.strictEqual( - lastResponse.headers[core.X_B3_SAMPLED], + lastResponse.headers[X_B3_SAMPLED], undefined, - `trace header '${core.X_B3_SAMPLED}' should not be set` + `trace header '${X_B3_SAMPLED}' should not be set` ); }); }); diff --git a/packages/opentelemetry-plugin-xml-http-request/package.json b/packages/opentelemetry-plugin-xml-http-request/package.json index 193bdb3391..5075e15c4d 100644 --- a/packages/opentelemetry-plugin-xml-http-request/package.json +++ b/packages/opentelemetry-plugin-xml-http-request/package.json @@ -46,6 +46,7 @@ "devDependencies": { "@babel/core": "7.11.1", "@opentelemetry/context-zone": "^0.12.0", + "@opentelemetry/propagator-b3": "^0.12.0", "@opentelemetry/tracing": "^0.12.0", "@types/mocha": "8.0.2", "@types/node": "14.0.27", diff --git a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts index 44c636d719..eb769068c8 100644 --- a/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts @@ -15,16 +15,18 @@ */ import * as api from '@opentelemetry/api'; import { - B3Propagator, LogLevel, otperformance as performance, - X_B3_SAMPLED, - X_B3_SPAN_ID, - X_B3_TRACE_ID, isWrapped, NoopLogger, - B3InjectEncoding, } from '@opentelemetry/core'; +import { + B3Propagator, + B3InjectEncoding, + X_B3_SAMPLED, + X_B3_SPAN_ID, + X_B3_TRACE_ID, +} from '@opentelemetry/propagator-b3'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import * as tracing from '@opentelemetry/tracing'; import { HttpAttribute } from '@opentelemetry/semantic-conventions'; diff --git a/packages/opentelemetry-propagator-b3/.eslintignore b/packages/opentelemetry-propagator-b3/.eslintignore new file mode 100644 index 0000000000..378eac25d3 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/.eslintignore @@ -0,0 +1 @@ +build diff --git a/packages/opentelemetry-propagator-b3/.eslintrc.js b/packages/opentelemetry-propagator-b3/.eslintrc.js new file mode 100644 index 0000000000..9dfe62f9b8 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/.eslintrc.js @@ -0,0 +1,9 @@ +module.exports = { + "env": { + "mocha": true, + "commonjs": true, + "node": true, + "browser": true + }, + ...require('../../eslint.config.js') +} diff --git a/packages/opentelemetry-propagator-b3/.npmignore b/packages/opentelemetry-propagator-b3/.npmignore new file mode 100644 index 0000000000..9505ba9450 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/.npmignore @@ -0,0 +1,4 @@ +/bin +/coverage +/doc +/test diff --git a/packages/opentelemetry-propagator-b3/LICENSE b/packages/opentelemetry-propagator-b3/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/packages/opentelemetry-propagator-b3/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/packages/opentelemetry-propagator-b3/README.md b/packages/opentelemetry-propagator-b3/README.md new file mode 100644 index 0000000000..afb8f3f6e7 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/README.md @@ -0,0 +1,172 @@ +# OpenTelemetry Propagator B3 + +[![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] + +The OpenTelemetry b3 propagator package provides multiple propagator +implementations for systems using the b3 context format. See the +[b3 specification][b3-spec] for complete details. + +## B3 Formats + +Single-Header Format: + +```bash +b3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} +``` + +Multi-Header Format: + +```bash +X-B3-TraceId: {TraceId} +X-B3-SpanId: {SpanId} +X-B3-ParentSpanId: {ParentSpanId} +X-B3-Sampled: {SamplingState} +``` + +- {TraceId} + + - Required + - Encoded as 32 or 16 lower-hex characters + - 16 character traceIds will be converted to 32 characters by left-padding + with 0s to conform with the [OpenTelemetry specification][otel-spec-id-format] + +- {SpanId} + + - Required + - Encoded as 16 lower-hex characters + +- {ParentSpanId} + + - Optional + - Used to support the Zipkin functionality where the client and server spans + that make up an HTTP request share the same id + - Not propagated by this library + +- {SamplingState} - Single-header + + - Optional + - Valid values + - 1 - Accept + - 0 - Deny + - d - Debug + - Absent - Defer sampling decision + +- {SamplingState} - Multi-header + + - Optional + - Valid values + - 1 - Accept + - 0 - Deny + +- {Flags} - Multi-header + - Optional + - Debug is encoded as `X-B3-Flags`: 1. Absent or any other value can be ignored. Debug implies an accept decision, so don't also send the `X-B3-Sampled` header. + +## Propagator Implementations + +### B3Propagator + +The default `B3Propagator` implements b3 propagation according to the +[OpenTelemetry specification][otel-b3-requirements]. It extracts b3 context +from multi and single header encodings and injects context using the +single-header b3 encoding. The inject encoding can be changed to multi-header +via configuration. + +Example usage (default): + +```javascript +const api = require('@opentelemetry/api'); +const { B3Propagator } = require('@opentelemetry/propagator-b3'); + +api.propagation.setGlobalPropagator(new B3Propagator()); +``` + +Example usage (specify inject encoding): + +```javascript +const api = require('@opentelemetry/api'); +const { B3Propagator } = require('@opentelemetry/propagator-b3'); + +api.propagation.setGlobalPropagator( + new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }) +); +``` + +### B3SinglePropagator + +If a distributed system only needs support for the b3 single-header +encoding it can use the `B3SinglePropagator` directly. + +Example usage: + +```javascript +const api = require('@opentelemetry/api'); +const { B3SinglePropagator } = require('@opentelemetry/propagator-b3'); + +api.propagation.setGlobalPropagator(new B3SinglePropagator()); +``` + +### B3MultiPropagator + +If a distributed system only needs support for the b3 multi-header +encoding it can use the `B3MultiPropagator` directly. + +Example usage: + +```javascript +const api = require('@opentelemetry/api'); +const { B3MultiPropagator } = require('@opentelemetry/propagator-b3'); + +api.propagation.setGlobalPropagator(new B3MultiPropagator()); +``` + +### CompositePropagator + +If a distributed system needs to support both single and multiple header +encodings for inject and extract the `B3SinglePropagator` and +`B3MultiPropagator` can be used in conjunction with a `CompositePropagator`. + +Example usage: + +```javascript +const api = require('@opentelemetry/api'); +const { CompositePropagator } = require('@opentelemetry/core'); +const { + B3SinglePropagator, + B3MultiPropagator, +} = require('@opentelemetry/propagator-b3'); + +api.propagation.setGlobalPropagator( + new CompositePropagator({ + propagators: [new B3SinglePropagator(), new B3MultiPropagator()], + }) +); +``` + +## 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.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/status.svg?path=packages/opentelemetry-propagator-jaeger +[dependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-propagator-jaeger +[devdependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-propagator-jaeger +[devdependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-propagator-jaeger&type=dev +[npm-url]: https://www.npmjs.com/package/@opentelemetry/propagator-jaeger +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fpropagator-jaeger.svg +[b3-spec]: https://github.com/openzipkin/b3-propagation +[otel-b3-requirements]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/context/api-propagators.md#b3-requirements +[otel-spec-id-format]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#retrieving-the-traceid-and-spanid diff --git a/packages/opentelemetry-propagator-b3/package.json b/packages/opentelemetry-propagator-b3/package.json new file mode 100644 index 0000000000..149596873b --- /dev/null +++ b/packages/opentelemetry-propagator-b3/package.json @@ -0,0 +1,61 @@ +{ + "name": "@opentelemetry/propagator-b3", + "version": "0.12.0", + "description": "OpenTelemetry B3 propagator provides context propagation for systems that are using the B3 header format", + "main": "build/src/index.js", + "types": "build/src/index.d.ts", + "repository": "open-telemetry/opentelemetry-js", + "scripts": { + "test": "nyc ts-mocha -p tsconfig.json test/**/*.test.ts", + "codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../", + "build": "npm run compile", + "clean": "rimraf build/*", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "precompile": "tsc --version", + "version:update": "node ../../scripts/version-update.js", + "compile": "npm run version:update && tsc -p .", + "prepare": "npm run compile", + "watch": "tsc -w" + }, + "keywords": [ + "opentelemetry", + "nodejs", + "tracing", + "profiling", + "monitoring", + "b3" + ], + "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.12.0" + }, + "devDependencies": { + "@opentelemetry/context-base": "^0.12.0", + "@types/mocha": "8.0.3", + "@types/node": "14.0.27", + "codecov": "3.7.2", + "gts": "2.0.2", + "istanbul-instrumenter-loader": "3.0.1", + "mocha": "7.2.0", + "nyc": "15.1.0", + "rimraf": "3.0.2", + "ts-loader": "8.0.4", + "ts-mocha": "7.0.0", + "typescript": "3.9.7" + } +} diff --git a/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts b/packages/opentelemetry-propagator-b3/src/B3MultiPropagator.ts similarity index 98% rename from packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts rename to packages/opentelemetry-propagator-b3/src/B3MultiPropagator.ts index d93489d41d..007390a497 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3MultiPropagator.ts +++ b/packages/opentelemetry-propagator-b3/src/B3MultiPropagator.ts @@ -26,7 +26,7 @@ import { TextMapSetter, TraceFlags, } from '@opentelemetry/api'; -import { B3_DEBUG_FLAG_KEY } from './b3-common'; +import { B3_DEBUG_FLAG_KEY } from './common'; /* b3 multi-header keys */ export const X_B3_TRACE_ID = 'x-b3-traceid'; diff --git a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts b/packages/opentelemetry-propagator-b3/src/B3Propagator.ts similarity index 90% rename from packages/opentelemetry-core/src/context/propagation/B3Propagator.ts rename to packages/opentelemetry-propagator-b3/src/B3Propagator.ts index a2b227f83d..8d5565589d 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3Propagator.ts +++ b/packages/opentelemetry-propagator-b3/src/B3Propagator.ts @@ -22,17 +22,7 @@ import { } from '@opentelemetry/api'; import { B3MultiPropagator } from './B3MultiPropagator'; import { B3SinglePropagator, B3_CONTEXT_HEADER } from './B3SinglePropagator'; - -/** Enumeraion of B3 inject encodings */ -export enum B3InjectEncoding { - SINGLE_HEADER, - MULTI_HEADER, -} - -/** Configuration for the B3Propagator */ -export interface B3PropagatorConfig { - injectEncoding?: B3InjectEncoding; -} +import { B3InjectEncoding, B3PropagatorConfig } from './types'; /** * Propagator that extracts B3 context in both single and multi-header variants, diff --git a/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts b/packages/opentelemetry-propagator-b3/src/B3SinglePropagator.ts similarity index 98% rename from packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts rename to packages/opentelemetry-propagator-b3/src/B3SinglePropagator.ts index 53444a1da9..2a3a81656f 100644 --- a/packages/opentelemetry-core/src/context/propagation/B3SinglePropagator.ts +++ b/packages/opentelemetry-propagator-b3/src/B3SinglePropagator.ts @@ -26,7 +26,7 @@ import { TextMapSetter, TraceFlags, } from '@opentelemetry/api'; -import { B3_DEBUG_FLAG_KEY } from './b3-common'; +import { B3_DEBUG_FLAG_KEY } from './common'; /** B3 single-header name */ export const B3_CONTEXT_HEADER = 'b3'; diff --git a/packages/opentelemetry-core/src/context/propagation/b3-common.ts b/packages/opentelemetry-propagator-b3/src/common.ts similarity index 100% rename from packages/opentelemetry-core/src/context/propagation/b3-common.ts rename to packages/opentelemetry-propagator-b3/src/common.ts diff --git a/packages/opentelemetry-propagator-b3/src/index.ts b/packages/opentelemetry-propagator-b3/src/index.ts new file mode 100644 index 0000000000..3fb23f4fda --- /dev/null +++ b/packages/opentelemetry-propagator-b3/src/index.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. + */ + +export * from './B3Propagator'; +export * from './B3SinglePropagator'; +export * from './B3MultiPropagator'; +export * from './types'; diff --git a/packages/opentelemetry-propagator-b3/src/types.ts b/packages/opentelemetry-propagator-b3/src/types.ts new file mode 100644 index 0000000000..45dcce0b05 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/src/types.ts @@ -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 + * + * 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. + */ + +/** Enumeration of B3 inject encodings */ +export enum B3InjectEncoding { + SINGLE_HEADER, + MULTI_HEADER, +} + +/** Configuration for the B3Propagator */ +export interface B3PropagatorConfig { + injectEncoding?: B3InjectEncoding; +} diff --git a/packages/opentelemetry-propagator-b3/src/version.ts b/packages/opentelemetry-propagator-b3/src/version.ts new file mode 100644 index 0000000000..707690f363 --- /dev/null +++ b/packages/opentelemetry-propagator-b3/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.12.0'; diff --git a/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts b/packages/opentelemetry-propagator-b3/test/B3MultiPropagator.test.ts similarity index 97% rename from packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts rename to packages/opentelemetry-propagator-b3/test/B3MultiPropagator.test.ts index 97149b4b89..5866a3d262 100644 --- a/packages/opentelemetry-core/test/context/B3MultiPropagator.test.ts +++ b/packages/opentelemetry-propagator-b3/test/B3MultiPropagator.test.ts @@ -24,7 +24,6 @@ import { } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; -import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; import { B3MultiPropagator, X_B3_FLAGS, @@ -32,8 +31,8 @@ import { X_B3_SAMPLED, X_B3_SPAN_ID, X_B3_TRACE_ID, -} from '../../src/context/propagation/B3MultiPropagator'; -import { TraceState } from '../../src/trace/TraceState'; +} from '../src/B3MultiPropagator'; +import { B3_DEBUG_FLAG_KEY } from '../src/common'; describe('B3MultiPropagator', () => { const b3Propagator = new B3MultiPropagator(); @@ -70,7 +69,20 @@ describe('B3MultiPropagator', () => { traceId: 'd4cda95b652f4a1592b449d5929fda1b', spanId: '6e0c63257de34c92', traceFlags: TraceFlags.NONE, - traceState: new TraceState('foo=bar,baz=qux'), + traceState: { + get: (key: string) => { + return undefined; + }, + set: function (key: string, value: string) { + return this; + }, + unset: function (key: string) { + return this; + }, + serialize: () => { + return 'foo=bar,baz=quux'; + }, + }, isRemote: false, }; diff --git a/packages/opentelemetry-core/test/context/B3Propagator.test.ts b/packages/opentelemetry-propagator-b3/test/B3Propagator.test.ts similarity index 94% rename from packages/opentelemetry-core/test/context/B3Propagator.test.ts rename to packages/opentelemetry-propagator-b3/test/B3Propagator.test.ts index 5964714bb8..67cd188f0b 100644 --- a/packages/opentelemetry-core/test/context/B3Propagator.test.ts +++ b/packages/opentelemetry-propagator-b3/test/B3Propagator.test.ts @@ -24,16 +24,14 @@ import { setExtractedSpanContext, } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; -import { - B3Propagator, - B3InjectEncoding, -} from '../../src/context/propagation/B3Propagator'; -import { B3_CONTEXT_HEADER } from '../../src/context/propagation/B3SinglePropagator'; +import { B3Propagator } from '../src/B3Propagator'; +import { B3InjectEncoding } from '../src/types'; +import { B3_CONTEXT_HEADER } from '../src/B3SinglePropagator'; import { X_B3_SAMPLED, X_B3_SPAN_ID, X_B3_TRACE_ID, -} from '../../src/context/propagation/B3MultiPropagator'; +} from '../src/B3MultiPropagator'; describe('B3Propagator', () => { let propagator: B3Propagator; diff --git a/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts b/packages/opentelemetry-propagator-b3/test/B3SinglePropagator.test.ts similarity index 98% rename from packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts rename to packages/opentelemetry-propagator-b3/test/B3SinglePropagator.test.ts index 60fcf051f0..c45191031e 100644 --- a/packages/opentelemetry-core/test/context/B3SinglePropagator.test.ts +++ b/packages/opentelemetry-propagator-b3/test/B3SinglePropagator.test.ts @@ -26,11 +26,11 @@ import { } from '@opentelemetry/api'; import { ROOT_CONTEXT } from '@opentelemetry/context-base'; import * as assert from 'assert'; -import { B3_DEBUG_FLAG_KEY } from '../../src/context/propagation/b3-common'; import { B3SinglePropagator, B3_CONTEXT_HEADER, -} from '../../src/context/propagation/B3SinglePropagator'; +} from '../src/B3SinglePropagator'; +import { B3_DEBUG_FLAG_KEY } from '../src/common'; describe('B3SinglePropagator', () => { const propagator = new B3SinglePropagator(); diff --git a/packages/opentelemetry-propagator-b3/tsconfig.json b/packages/opentelemetry-propagator-b3/tsconfig.json new file mode 100644 index 0000000000..a2042cd68b --- /dev/null +++ b/packages/opentelemetry-propagator-b3/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.base", + "compilerOptions": { + "rootDir": ".", + "outDir": "build" + }, + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/packages/opentelemetry-web/package.json b/packages/opentelemetry-web/package.json index e10ef5685b..2fa66276d4 100644 --- a/packages/opentelemetry-web/package.json +++ b/packages/opentelemetry-web/package.json @@ -45,6 +45,7 @@ "devDependencies": { "@babel/core": "7.11.1", "@opentelemetry/context-zone": "^0.12.0", + "@opentelemetry/propagator-b3": "^0.12.0", "@opentelemetry/resources": "^0.12.0", "@types/jquery": "3.5.1", "@types/mocha": "8.0.2", diff --git a/packages/opentelemetry-web/test/WebTracerProvider.test.ts b/packages/opentelemetry-web/test/WebTracerProvider.test.ts index e10c80405c..00a3d2a6b9 100644 --- a/packages/opentelemetry-web/test/WebTracerProvider.test.ts +++ b/packages/opentelemetry-web/test/WebTracerProvider.test.ts @@ -17,7 +17,8 @@ import { context } from '@opentelemetry/api'; import { ContextManager } from '@opentelemetry/context-base'; import { ZoneContextManager } from '@opentelemetry/context-zone'; -import { B3Propagator, BasePlugin, NoopLogger } from '@opentelemetry/core'; +import { BasePlugin, NoopLogger } from '@opentelemetry/core'; +import { B3Propagator } from '@opentelemetry/propagator-b3'; import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; import { Span, Tracer } from '@opentelemetry/tracing'; import * as assert from 'assert';