UNPKG

7.76 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var noop = require("./noop");
4/**
5 * Span represents a logical unit of work as part of a broader Trace. Examples
6 * of span might include remote procedure calls or a in-process function calls
7 * to sub-components. A Trace has a single, top-level "root" Span that in turn
8 * may have zero or more child Spans, which in turn may have children.
9 */
10var Span = /** @class */ (function () {
11 function Span() {
12 }
13 // ---------------------------------------------------------------------- //
14 // OpenTracing API methods
15 // ---------------------------------------------------------------------- //
16 /**
17 * Returns the SpanContext object associated with this Span.
18 *
19 * @return {SpanContext}
20 */
21 Span.prototype.context = function () {
22 return this._context();
23 };
24 /**
25 * Returns the Tracer object used to create this Span.
26 *
27 * @return {Tracer}
28 */
29 Span.prototype.tracer = function () {
30 return this._tracer();
31 };
32 /**
33 * Sets the string name for the logical operation this span represents.
34 *
35 * @param {string} name
36 */
37 Span.prototype.setOperationName = function (name) {
38 this._setOperationName(name);
39 return this;
40 };
41 /**
42 * Sets a key:value pair on this Span that also propagates to future
43 * children of the associated Span.
44 *
45 * setBaggageItem() enables powerful functionality given a full-stack
46 * opentracing integration (e.g., arbitrary application data from a web
47 * client can make it, transparently, all the way into the depths of a
48 * storage system), and with it some powerful costs: use this feature with
49 * care.
50 *
51 * IMPORTANT NOTE #1: setBaggageItem() will only propagate baggage items to
52 * *future* causal descendants of the associated Span.
53 *
54 * IMPORTANT NOTE #2: Use this thoughtfully and with care. Every key and
55 * value is copied into every local *and remote* child of the associated
56 * Span, and that can add up to a lot of network and cpu overhead.
57 *
58 * @param {string} key
59 * @param {string} value
60 */
61 Span.prototype.setBaggageItem = function (key, value) {
62 this._setBaggageItem(key, value);
63 return this;
64 };
65 /**
66 * Returns the value for a baggage item given its key.
67 *
68 * @param {string} key
69 * The key for the given trace attribute.
70 * @return {string}
71 * String value for the given key, or undefined if the key does not
72 * correspond to a set trace attribute.
73 */
74 Span.prototype.getBaggageItem = function (key) {
75 return this._getBaggageItem(key);
76 };
77 /**
78 * Adds a single tag to the span. See `addTags()` for details.
79 *
80 * @param {string} key
81 * @param {any} value
82 */
83 Span.prototype.setTag = function (key, value) {
84 var _a;
85 // NOTE: the call is normalized to a call to _addTags()
86 this._addTags((_a = {}, _a[key] = value, _a));
87 return this;
88 };
89 /**
90 * Adds the given key value pairs to the set of span tags.
91 *
92 * Multiple calls to addTags() results in the tags being the superset of
93 * all calls.
94 *
95 * The behavior of setting the same key multiple times on the same span
96 * is undefined.
97 *
98 * The supported type of the values is implementation-dependent.
99 * Implementations are expected to safely handle all types of values but
100 * may choose to ignore unrecognized / unhandle-able values (e.g. objects
101 * with cyclic references, function objects).
102 *
103 * @return {[type]} [description]
104 */
105 Span.prototype.addTags = function (keyValueMap) {
106 this._addTags(keyValueMap);
107 return this;
108 };
109 /**
110 * Add a log record to this Span, optionally at a user-provided timestamp.
111 *
112 * For example:
113 *
114 * span.log({
115 * size: rpc.size(), // numeric value
116 * URI: rpc.URI(), // string value
117 * payload: rpc.payload(), // Object value
118 * "keys can be arbitrary strings": rpc.foo(),
119 * });
120 *
121 * span.log({
122 * "error.description": someError.description(),
123 * }, someError.timestampMillis());
124 *
125 * @param {object} keyValuePairs
126 * An object mapping string keys to arbitrary value types. All
127 * Tracer implementations should support bool, string, and numeric
128 * value types, and some may also support Object values.
129 * @param {number} timestamp
130 * An optional parameter specifying the timestamp in milliseconds
131 * since the Unix epoch. Fractional values are allowed so that
132 * timestamps with sub-millisecond accuracy can be represented. If
133 * not specified, the implementation is expected to use its notion
134 * of the current time of the call.
135 */
136 Span.prototype.log = function (keyValuePairs, timestamp) {
137 this._log(keyValuePairs, timestamp);
138 return this;
139 };
140 /**
141 * DEPRECATED
142 */
143 Span.prototype.logEvent = function (eventName, payload) {
144 return this._log({ event: eventName, payload: payload });
145 };
146 /**
147 * Sets the end timestamp and finalizes Span state.
148 *
149 * With the exception of calls to Span.context() (which are always allowed),
150 * finish() must be the last call made to any span instance, and to do
151 * otherwise leads to undefined behavior.
152 *
153 * @param {number} finishTime
154 * Optional finish time in milliseconds as a Unix timestamp. Decimal
155 * values are supported for timestamps with sub-millisecond accuracy.
156 * If not specified, the current time (as defined by the
157 * implementation) will be used.
158 */
159 Span.prototype.finish = function (finishTime) {
160 this._finish(finishTime);
161 // Do not return `this`. The Span generally should not be used after it
162 // is finished so chaining is not desired in this context.
163 };
164 // ---------------------------------------------------------------------- //
165 // Derived classes can choose to implement the below
166 // ---------------------------------------------------------------------- //
167 // By default returns a no-op SpanContext.
168 Span.prototype._context = function () {
169 return noop.spanContext;
170 };
171 // By default returns a no-op tracer.
172 //
173 // The base class could store the tracer that created it, but it does not
174 // in order to ensure the no-op span implementation has zero members,
175 // which allows V8 to aggressively optimize calls to such objects.
176 Span.prototype._tracer = function () {
177 return noop.tracer;
178 };
179 // By default does nothing
180 Span.prototype._setOperationName = function (name) {
181 };
182 // By default does nothing
183 Span.prototype._setBaggageItem = function (key, value) {
184 };
185 // By default does nothing
186 Span.prototype._getBaggageItem = function (key) {
187 return undefined;
188 };
189 // By default does nothing
190 //
191 // NOTE: both setTag() and addTags() map to this function. keyValuePairs
192 // will always be an associative array.
193 Span.prototype._addTags = function (keyValuePairs) {
194 };
195 // By default does nothing
196 Span.prototype._log = function (keyValuePairs, timestamp) {
197 };
198 // By default does nothing
199 //
200 // finishTime is expected to be either a number or undefined.
201 Span.prototype._finish = function (finishTime) {
202 };
203 return Span;
204}());
205exports.Span = Span;
206exports.default = Span;
207//# sourceMappingURL=span.js.map
\No newline at end of file