-
Notifications
You must be signed in to change notification settings - Fork 821
/
shim.ts
379 lines (333 loc) · 10.2 KB
/
shim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* 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 * as api from '@opentelemetry/api';
import * as opentracing from 'opentracing';
import {
SpanAttributes,
SpanAttributeValue,
SpanStatusCode,
TextMapPropagator,
} from '@opentelemetry/api';
function translateReferences(references: opentracing.Reference[]): api.Link[] {
const links: api.Link[] = [];
for (const reference of references) {
const context = reference.referencedContext();
if (context instanceof SpanContextShim) {
links.push({
context: (context as SpanContextShim).getSpanContext(),
attributes: { 'span.kind': reference.type() },
});
}
}
return links;
}
function translateSpanOptions(
options: opentracing.SpanOptions
): api.SpanOptions {
const opts: api.SpanOptions = {
startTime: options.startTime,
};
if (options.references) {
opts.links = translateReferences(options.references);
}
return opts;
}
function getContextWithParent(options: opentracing.SpanOptions) {
if (options.childOf) {
if (options.childOf instanceof SpanShim) {
return api.trace.setSpan(api.context.active(), options.childOf.getSpan());
} else if (options.childOf instanceof SpanContextShim) {
return api.trace.setSpanContext(
api.context.active(),
options.childOf.getSpanContext()
);
}
}
return api.context.active();
}
/**
* SpanContextShim wraps a {@link api.SpanContext} and implements the
* OpenTracing span context API.
*/
export class SpanContextShim extends opentracing.SpanContext {
private readonly _spanContext: api.SpanContext;
private _baggage: api.Baggage;
constructor(spanContext: api.SpanContext, baggage: api.Baggage) {
super();
this._spanContext = spanContext;
this._baggage = baggage;
}
/**
* Returns the underlying {@link api.SpanContext}
*/
getSpanContext(): api.SpanContext {
return this._spanContext;
}
/**
* Returns the underlying {@link api.Baggage}
*/
getBaggage(): api.Baggage {
return this._baggage;
}
/**
* Returns the trace ID as a string.
*/
override toTraceId(): string {
return this._spanContext.traceId;
}
/**
* Returns the span ID as a string.
*/
override toSpanId(): string {
return this._spanContext.spanId;
}
getBaggageItem(key: string): string | undefined {
return this._baggage.getEntry(key)?.value;
}
setBaggageItem(key: string, value: string) {
this._baggage = this._baggage.setEntry(key, { value });
}
}
/**
* TracerShim wraps a {@link api.Tracer} and implements the
* OpenTracing tracer API.
*/
export class TracerShim extends opentracing.Tracer {
private readonly _tracer: api.Tracer;
private readonly _propagators: ShimPropagators | undefined;
constructor(tracer: api.Tracer, propagators?: ShimPropagators) {
super();
this._tracer = tracer;
this._propagators = propagators;
}
override startSpan(
name: string,
options: opentracing.SpanOptions = {}
): opentracing.Span {
const span = this._tracer.startSpan(
name,
translateSpanOptions(options),
getContextWithParent(options)
);
let baggage: api.Baggage = api.propagation.createBaggage();
if (options.childOf instanceof SpanShim) {
const shimContext = options.childOf.context() as SpanContextShim;
baggage = shimContext.getBaggage();
} else if (options.childOf instanceof SpanContextShim) {
baggage = options.childOf.getBaggage();
}
if (options.tags) {
span.setAttributes(options.tags);
}
return new SpanShim(this, span, baggage);
}
override _inject(
spanContext: opentracing.SpanContext,
format: string,
carrier: unknown
): void {
const spanContextShim: SpanContextShim = spanContext as SpanContextShim;
const oTelSpanContext: api.SpanContext = spanContextShim.getSpanContext();
const oTelSpanBaggage: api.Baggage = spanContextShim.getBaggage();
if (!carrier || typeof carrier !== 'object') return;
if (format === opentracing.FORMAT_BINARY) {
api.diag.warn('OpentracingShim.inject() does not support FORMAT_BINARY');
// @todo: Implement binary format
return;
}
const propagator = this._getPropagator(format);
if (propagator !== undefined) {
const context = api.propagation.setBaggage(
api.trace.setSpanContext(api.ROOT_CONTEXT, oTelSpanContext),
oTelSpanBaggage
);
propagator.inject(context, carrier, api.defaultTextMapSetter);
}
}
override _extract(format: string, carrier: unknown): opentracing.SpanContext | null {
if (format === opentracing.FORMAT_BINARY) {
api.diag.warn('OpentracingShim.extract() does not support FORMAT_BINARY');
// @todo: Implement binary format
return null;
}
const propagator = this._getPropagator(format);
if (propagator !== undefined) {
const context: api.Context = propagator.extract(
api.ROOT_CONTEXT,
carrier,
api.defaultTextMapGetter
);
const spanContext = api.trace.getSpanContext(context);
const baggage = api.propagation.getBaggage(context);
if (!spanContext) {
return null;
}
return new SpanContextShim(spanContext, baggage || api.propagation.createBaggage());
}
return null;
}
private _getPropagator(format: string): api.TextMapPropagator | undefined {
switch (format) {
case opentracing.FORMAT_TEXT_MAP:
return this._propagators?.textMapPropagator ?? api.propagation;
case opentracing.FORMAT_HTTP_HEADERS:
return this._propagators?.httpHeadersPropagator ?? api.propagation;
default:
return;
}
}
}
/**
* SpanShim wraps an {@link api.Span} and implements the OpenTracing Span API
* around it.
*
**/
export class SpanShim extends opentracing.Span {
// _span is the original OpenTelemetry span that we are wrapping with
// an opentracing interface.
private readonly _span: api.Span;
private readonly _contextShim: SpanContextShim;
private readonly _tracerShim: TracerShim;
constructor(tracerShim: TracerShim, span: api.Span, baggage: api.Baggage) {
super();
this._span = span;
this._contextShim = new SpanContextShim(span.spanContext(), baggage);
this._tracerShim = tracerShim;
}
/**
* Returns a reference to the Span's context.
*
* @returns a {@link SpanContextShim} containing the underlying context.
*/
override context(): opentracing.SpanContext {
return this._contextShim;
}
/**
* Returns the {@link opentracing.Tracer} that created the span.
*/
override tracer(): opentracing.Tracer {
return this._tracerShim;
}
/**
* Updates the underlying span's name.
*
* @param name the Span name.
*/
override setOperationName(name: string): this {
this._span.updateName(name);
return this;
}
/**
* Finishes the span. Once the span is finished, no new updates can be applied
* to the span.
*
* @param finishTime An optional timestamp to explicitly set the span's end time.
*/
override finish(finishTime?: number): void {
this._span.end(finishTime);
}
/**
* Logs an event with an optional payload.
* @param eventName name of the event.
* @param payload an arbitrary object to be attached to the event.
*/
override logEvent(eventName: string, payload?: SpanAttributes): void {
this._span.addEvent(eventName, payload);
}
/**
* Logs a set of key value pairs. Since OpenTelemetry only supports events,
* the KV pairs are used as attributes on an event named "log".
*/
override log(keyValuePairs: SpanAttributes, _timestamp?: number): this {
// @todo: Handle timestamp
this._span.addEvent('log', keyValuePairs);
return this;
}
/**
* Adds a set of tags to the span.
* @param keyValueMap set of KV pairs representing tags
*/
override addTags(keyValueMap: SpanAttributes): this {
for (const [key, value] of Object.entries(keyValueMap)) {
if (this._setErrorAsSpanStatusCode(key, value)) {
continue;
}
if (value !== undefined) {
this._span.setAttribute(key, value);
}
}
return this;
}
/**
* Sets a tag on the span, updating the value if the key is already present
* on the span.
* @param key key for the tag
* @param value value for the tag
*/
override setTag(key: string, value: SpanAttributeValue): this {
if (this._setErrorAsSpanStatusCode(key, value)) {
return this;
}
this._span.setAttribute(key, value);
return this;
}
override getBaggageItem(key: string): string | undefined {
return this._contextShim.getBaggageItem(key);
}
override setBaggageItem(key: string, value: string): this {
this._contextShim.setBaggageItem(key, value);
return this;
}
/**
* Returns the underlying {@link api.Span} that the shim
* is wrapping.
*/
getSpan(): api.Span {
return this._span;
}
private _setErrorAsSpanStatusCode(
key: string,
value: SpanAttributeValue | undefined
): boolean {
if (key === opentracing.Tags.ERROR) {
const statusCode = SpanShim._mapErrorTag(value);
this._span.setStatus({ code: statusCode });
return statusCode !== SpanStatusCode.UNSET;
}
return false;
}
private static _mapErrorTag(
value: SpanAttributeValue | undefined
): SpanStatusCode {
switch (value) {
case true:
case 'true':
return SpanStatusCode.ERROR;
case false:
case 'false':
return SpanStatusCode.OK;
default:
return SpanStatusCode.UNSET;
}
}
}
/**
* Propagator configuration for the {@link TracerShim}
*/
export interface ShimPropagators {
textMapPropagator?: TextMapPropagator;
httpHeadersPropagator?: TextMapPropagator;
}