UNPKG

6.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Functions = require("./functions");
4var Noop = require("./noop");
5var span_1 = require("./span");
6/**
7 * Tracer is the entry-point between the instrumentation API and the tracing
8 * implementation.
9 *
10 * The default object acts as a no-op implementation.
11 *
12 * Note to implementators: derived classes can choose to directly implement the
13 * methods in the "OpenTracing API methods" section, or optionally the subset of
14 * underscore-prefixed methods to pick up the argument checking and handling
15 * automatically from the base class.
16 */
17var Tracer = /** @class */ (function () {
18 function Tracer() {
19 }
20 // ---------------------------------------------------------------------- //
21 // OpenTracing API methods
22 // ---------------------------------------------------------------------- //
23 /**
24 * Starts and returns a new Span representing a logical unit of work.
25 *
26 * For example:
27 *
28 * // Start a new (parentless) root Span:
29 * var parent = Tracer.startSpan('DoWork');
30 *
31 * // Start a new (child) Span:
32 * var child = Tracer.startSpan('load-from-db', {
33 * childOf: parent.context(),
34 * });
35 *
36 * // Start a new async (FollowsFrom) Span:
37 * var child = Tracer.startSpan('async-cache-write', {
38 * references: [
39 * opentracing.followsFrom(parent.context())
40 * ],
41 * });
42 *
43 * @param {string} name - the name of the operation (REQUIRED).
44 * @param {SpanOptions} [options] - options for the newly created span.
45 * @return {Span} - a new Span object.
46 */
47 Tracer.prototype.startSpan = function (name, options) {
48 if (options === void 0) { options = {}; }
49 // Convert options.childOf to fields.references as needed.
50 if (options.childOf) {
51 // Convert from a Span or a SpanContext into a Reference.
52 var childOf = Functions.childOf(options.childOf);
53 if (options.references) {
54 options.references.push(childOf);
55 }
56 else {
57 options.references = [childOf];
58 }
59 delete (options.childOf);
60 }
61 return this._startSpan(name, options);
62 };
63 /**
64 * Injects the given SpanContext instance for cross-process propagation
65 * within `carrier`. The expected type of `carrier` depends on the value of
66 * `format.
67 *
68 * OpenTracing defines a common set of `format` values (see
69 * FORMAT_TEXT_MAP, FORMAT_HTTP_HEADERS, and FORMAT_BINARY), and each has
70 * an expected carrier type.
71 *
72 * Consider this pseudocode example:
73 *
74 * var clientSpan = ...;
75 * ...
76 * // Inject clientSpan into a text carrier.
77 * var headersCarrier = {};
78 * Tracer.inject(clientSpan.context(), Tracer.FORMAT_HTTP_HEADERS, headersCarrier);
79 * // Incorporate the textCarrier into the outbound HTTP request header
80 * // map.
81 * Object.assign(outboundHTTPReq.headers, headersCarrier);
82 * // ... send the httpReq
83 *
84 * @param {SpanContext} spanContext - the SpanContext to inject into the
85 * carrier object. As a convenience, a Span instance may be passed
86 * in instead (in which case its .context() is used for the
87 * inject()).
88 * @param {string} format - the format of the carrier.
89 * @param {any} carrier - see the documentation for the chosen `format`
90 * for a description of the carrier object.
91 */
92 Tracer.prototype.inject = function (spanContext, format, carrier) {
93 // Allow the user to pass a Span instead of a SpanContext
94 if (spanContext instanceof span_1.default) {
95 spanContext = spanContext.context();
96 }
97 return this._inject(spanContext, format, carrier);
98 };
99 /**
100 * Returns a SpanContext instance extracted from `carrier` in the given
101 * `format`.
102 *
103 * OpenTracing defines a common set of `format` values (see
104 * FORMAT_TEXT_MAP, FORMAT_HTTP_HEADERS, and FORMAT_BINARY), and each has
105 * an expected carrier type.
106 *
107 * Consider this pseudocode example:
108 *
109 * // Use the inbound HTTP request's headers as a text map carrier.
110 * var headersCarrier = inboundHTTPReq.headers;
111 * var wireCtx = Tracer.extract(Tracer.FORMAT_HTTP_HEADERS, headersCarrier);
112 * var serverSpan = Tracer.startSpan('...', { childOf : wireCtx });
113 *
114 * @param {string} format - the format of the carrier.
115 * @param {any} carrier - the type of the carrier object is determined by
116 * the format.
117 * @return {SpanContext}
118 * The extracted SpanContext, or null if no such SpanContext could
119 * be found in `carrier`
120 */
121 Tracer.prototype.extract = function (format, carrier) {
122 return this._extract(format, carrier);
123 };
124 // ---------------------------------------------------------------------- //
125 // Derived classes can choose to implement the below
126 // ---------------------------------------------------------------------- //
127 // NOTE: the input to this method is *always* an associative array. The
128 // public-facing startSpan() method normalizes the arguments so that
129 // all N implementations do not need to worry about variations in the call
130 // signature.
131 //
132 // The default behavior returns a no-op span.
133 Tracer.prototype._startSpan = function (name, fields) {
134 return Noop.span;
135 };
136 // The default behavior is a no-op.
137 Tracer.prototype._inject = function (spanContext, format, carrier) {
138 };
139 // The default behavior is to return a no-op SpanContext.
140 Tracer.prototype._extract = function (format, carrier) {
141 return Noop.spanContext;
142 };
143 return Tracer;
144}());
145exports.Tracer = Tracer;
146exports.default = Tracer;
147//# sourceMappingURL=tracer.js.map
\No newline at end of file