This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
span.ts
224 lines (201 loc) · 7.4 KB
/
span.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
import * as noop from './noop';
import SpanContext from './span_context';
import Tracer from './tracer';
/**
* Span represents a logical unit of work as part of a broader Trace. Examples
* of span might include remote procedure calls or a in-process function calls
* to sub-components. A Trace has a single, top-level "root" Span that in turn
* may have zero or more child Spans, which in turn may have children.
*/
export class Span {
// ---------------------------------------------------------------------- //
// OpenTracing API methods
// ---------------------------------------------------------------------- //
/**
* Returns the SpanContext object associated with this Span.
*
* @return {SpanContext}
*/
context(): SpanContext {
return this._context();
}
/**
* Returns the Tracer object used to create this Span.
*
* @return {Tracer}
*/
tracer(): Tracer {
return this._tracer();
}
/**
* Sets the string name for the logical operation this span represents.
*
* @param {string} name
*/
setOperationName(name: string): this {
this._setOperationName(name);
return this;
}
/**
* Sets a key:value pair on this Span that also propagates to future
* children of the associated Span.
*
* setBaggageItem() enables powerful functionality given a full-stack
* opentracing integration (e.g., arbitrary application data from a web
* client can make it, transparently, all the way into the depths of a
* storage system), and with it some powerful costs: use this feature with
* care.
*
* IMPORTANT NOTE #1: setBaggageItem() will only propagate baggage items to
* *future* causal descendants of the associated Span.
*
* IMPORTANT NOTE #2: Use this thoughtfully and with care. Every key and
* value is copied into every local *and remote* child of the associated
* Span, and that can add up to a lot of network and cpu overhead.
*
* @param {string} key
* @param {string} value
*/
setBaggageItem(key: string, value: string): this {
this._setBaggageItem(key, value);
return this;
}
/**
* Returns the value for a baggage item given its key.
*
* @param {string} key
* The key for the given trace attribute.
* @return {string}
* String value for the given key, or undefined if the key does not
* correspond to a set trace attribute.
*/
getBaggageItem(key: string): string | undefined {
return this._getBaggageItem(key);
}
/**
* Adds a single tag to the span. See `addTags()` for details.
*
* @param {string} key
* @param {any} value
*/
setTag(key: string, value: any): this {
// NOTE: the call is normalized to a call to _addTags()
this._addTags({ [key]: value });
return this;
}
/**
* Adds the given key value pairs to the set of span tags.
*
* Multiple calls to addTags() results in the tags being the superset of
* all calls.
*
* The behavior of setting the same key multiple times on the same span
* is undefined.
*
* The supported type of the values is implementation-dependent.
* Implementations are expected to safely handle all types of values but
* may choose to ignore unrecognized / unhandle-able values (e.g. objects
* with cyclic references, function objects).
*
* @return {[type]} [description]
*/
addTags(keyValueMap: { [key: string]: any }): this {
this._addTags(keyValueMap);
return this;
}
/**
* Add a log record to this Span, optionally at a user-provided timestamp.
*
* For example:
*
* span.log({
* size: rpc.size(), // numeric value
* URI: rpc.URI(), // string value
* payload: rpc.payload(), // Object value
* "keys can be arbitrary strings": rpc.foo(),
* });
*
* span.log({
* "error.description": someError.description(),
* }, someError.timestampMillis());
*
* @param {object} keyValuePairs
* An object mapping string keys to arbitrary value types. All
* Tracer implementations should support bool, string, and numeric
* value types, and some may also support Object values.
* @param {number} timestamp
* An optional parameter specifying the timestamp in milliseconds
* since the Unix epoch. Fractional values are allowed so that
* timestamps with sub-millisecond accuracy can be represented. If
* not specified, the implementation is expected to use its notion
* of the current time of the call.
*/
log(keyValuePairs: { [key: string]: any }, timestamp?: number): this {
this._log(keyValuePairs, timestamp);
return this;
}
/**
* DEPRECATED
*/
logEvent(eventName: string, payload: any): void {
return this._log({ event: eventName, payload });
}
/**
* Sets the end timestamp and finalizes Span state.
*
* With the exception of calls to Span.context() (which are always allowed),
* finish() must be the last call made to any span instance, and to do
* otherwise leads to undefined behavior.
*
* @param {number} finishTime
* Optional finish time in milliseconds as a Unix timestamp. Decimal
* values are supported for timestamps with sub-millisecond accuracy.
* If not specified, the current time (as defined by the
* implementation) will be used.
*/
finish(finishTime?: number): void {
this._finish(finishTime);
// Do not return `this`. The Span generally should not be used after it
// is finished so chaining is not desired in this context.
}
// ---------------------------------------------------------------------- //
// Derived classes can choose to implement the below
// ---------------------------------------------------------------------- //
// By default returns a no-op SpanContext.
protected _context(): SpanContext {
return noop.spanContext!;
}
// By default returns a no-op tracer.
//
// The base class could store the tracer that created it, but it does not
// in order to ensure the no-op span implementation has zero members,
// which allows V8 to aggressively optimize calls to such objects.
protected _tracer(): Tracer {
return noop.tracer!;
}
// By default does nothing
protected _setOperationName(name: string): void {
}
// By default does nothing
protected _setBaggageItem(key: string, value: string): void {
}
// By default does nothing
protected _getBaggageItem(key: string): string | undefined {
return undefined;
}
// By default does nothing
//
// NOTE: both setTag() and addTags() map to this function. keyValuePairs
// will always be an associative array.
protected _addTags(keyValuePairs: { [key: string]: any }): void {
}
// By default does nothing
protected _log(keyValuePairs: { [key: string]: any }, timestamp?: number): void {
}
// By default does nothing
//
// finishTime is expected to be either a number or undefined.
protected _finish(finishTime?: number): void {
}
}
export default Span;