UNPKG

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