|
| 1 | +/* |
| 2 | + * Copyright 2018 Expedia, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {Codex, Propagator, PropagatorOpts} from './propagator'; |
| 18 | +import SpanContext from '../span_context'; |
| 19 | +import DefaultCodex from './default_codex'; |
| 20 | + |
| 21 | +export default class TextMapPropagator implements Propagator { |
| 22 | + _opts: PropagatorOpts; |
| 23 | + _codex: Codex; |
| 24 | + |
| 25 | + constructor(codex: Codex = new DefaultCodex(), opts: PropagatorOpts = new PropagatorOpts()) { |
| 26 | + this._opts = opts; |
| 27 | + this._codex = codex; |
| 28 | + } |
| 29 | + |
| 30 | + inject(spanContext: SpanContext, carrier: any): void { |
| 31 | + carrier[this._opts.traceIdKey()] = spanContext.traceId(); |
| 32 | + carrier[this._opts.spanIdKey()] = spanContext.spanId(); |
| 33 | + carrier[this._opts.parentSpanIdKey()] = spanContext.parentSpanId(); |
| 34 | + |
| 35 | + const baggage = spanContext.baggage(); |
| 36 | + for (const key in baggage) { |
| 37 | + if (baggage.hasOwnProperty(key)) { |
| 38 | + carrier[`${this._opts.baggageKeyPrefix()}${key}`] = this._codex.encode(spanContext.baggage()[key]); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + extract(carrier: any): SpanContext { |
| 44 | + const baggage = {}; |
| 45 | + for (const key in carrier) { |
| 46 | + if (carrier.hasOwnProperty(key) && key.indexOf(this._opts.baggageKeyPrefix()) === 0) { |
| 47 | + const keySansPrefix = key.substring(this._opts.baggageKeyPrefix().length); |
| 48 | + baggage[keySansPrefix] = this._codex.decode(carrier[key]); |
| 49 | + } |
| 50 | + } |
| 51 | + return new SpanContext(carrier[this._opts.traceIdKey()], carrier[this._opts.spanIdKey()], carrier[this._opts.parentSpanIdKey()], baggage); |
| 52 | + } |
| 53 | +} |
0 commit comments