1 | ;
|
2 | /*
|
3 | * Copyright The OpenTelemetry Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * https://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.CompositePropagator = void 0;
|
19 | const api_1 = require("@opentelemetry/api");
|
20 | /** Combines multiple propagators into a single propagator. */
|
21 | class CompositePropagator {
|
22 | /**
|
23 | * Construct a composite propagator from a list of propagators.
|
24 | *
|
25 | * @param [config] Configuration object for composite propagator
|
26 | */
|
27 | constructor(config = {}) {
|
28 | var _a;
|
29 | this._propagators = (_a = config.propagators) !== null && _a !== void 0 ? _a : [];
|
30 | this._fields = Array.from(new Set(this._propagators
|
31 | // older propagators may not have fields function, null check to be sure
|
32 | .map(p => (typeof p.fields === 'function' ? p.fields() : []))
|
33 | .reduce((x, y) => x.concat(y), [])));
|
34 | }
|
35 | /**
|
36 | * Run each of the configured propagators with the given context and carrier.
|
37 | * Propagators are run in the order they are configured, so if multiple
|
38 | * propagators write the same carrier key, the propagator later in the list
|
39 | * will "win".
|
40 | *
|
41 | * @param context Context to inject
|
42 | * @param carrier Carrier into which context will be injected
|
43 | */
|
44 | inject(context, carrier, setter) {
|
45 | for (const propagator of this._propagators) {
|
46 | try {
|
47 | propagator.inject(context, carrier, setter);
|
48 | }
|
49 | catch (err) {
|
50 | api_1.diag.warn(`Failed to inject with ${propagator.constructor.name}. Err: ${err.message}`);
|
51 | }
|
52 | }
|
53 | }
|
54 | /**
|
55 | * Run each of the configured propagators with the given context and carrier.
|
56 | * Propagators are run in the order they are configured, so if multiple
|
57 | * propagators write the same context key, the propagator later in the list
|
58 | * will "win".
|
59 | *
|
60 | * @param context Context to add values to
|
61 | * @param carrier Carrier from which to extract context
|
62 | */
|
63 | extract(context, carrier, getter) {
|
64 | return this._propagators.reduce((ctx, propagator) => {
|
65 | try {
|
66 | return propagator.extract(ctx, carrier, getter);
|
67 | }
|
68 | catch (err) {
|
69 | api_1.diag.warn(`Failed to inject with ${propagator.constructor.name}. Err: ${err.message}`);
|
70 | }
|
71 | return ctx;
|
72 | }, context);
|
73 | }
|
74 | fields() {
|
75 | // return a new array so our fields cannot be modified
|
76 | return this._fields.slice();
|
77 | }
|
78 | }
|
79 | exports.CompositePropagator = CompositePropagator;
|
80 | //# sourceMappingURL=composite.js.map |
\ | No newline at end of file |