1 | /**
|
2 | * Copyright 2015 Google Inc. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | ;
|
18 |
|
19 | /**
|
20 | * Creates a trace span object.
|
21 | * @constructor
|
22 | */
|
23 | function TraceSpan(name, spanId, parentSpanId) {
|
24 | this.name = name;
|
25 | this.parentSpanId = parentSpanId;
|
26 | this.spanId = spanId;
|
27 | this.kind = 'RPC_CLIENT';
|
28 | this.labels = {};
|
29 | this.startTime = (new Date()).toISOString();
|
30 | this.endTime = '';
|
31 | }
|
32 |
|
33 |
|
34 | /**
|
35 | * Sets or updates a label value.
|
36 | * @param {string} key The label key to set.
|
37 | * @param {string} value The new value of the label.
|
38 | */
|
39 | TraceSpan.prototype.setLabel = function(key, value) {
|
40 | this.labels[key] = '' + value;
|
41 | };
|
42 |
|
43 |
|
44 | /**
|
45 | * Closes the span, which just means assigning an end time.
|
46 | */
|
47 | TraceSpan.prototype.close = function() {
|
48 | this.endTime = (new Date()).toISOString();
|
49 | };
|
50 |
|
51 |
|
52 | /**
|
53 | * Checks whether or not this span has been closed.
|
54 | * @returns {boolean} True if the span is closed, false otherwise.
|
55 | */
|
56 | TraceSpan.prototype.isClosed = function() {
|
57 | return !!this.endTime;
|
58 | };
|
59 |
|
60 |
|
61 | /**
|
62 | * Export TraceSpan.
|
63 | */
|
64 | module.exports = TraceSpan;
|