forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspan-data.js
146 lines (133 loc) · 4.56 KB
/
span-data.js
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
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var TraceSpan = require('./trace-span.js');
var TraceLabels = require('./trace-labels.js');
// Auto-incrementing integer
var uid = 1;
/**
* Creates a trace context object.
* @param {Trace} trace The object holding the spans comprising this trace.
* @param {string} name The name of the span.
* @param {number} parentSpanId The id of the parent span, 0 for root spans.
* @param {boolean} isRoot Whether this is a root span.
* @param {number=} time The time when the associated TraceSpan begins.
* @constructor
*/
function SpanData(agent, trace, name, parentSpanId, isRoot, time) {
var spanId = uid++;
this.agent = agent;
this.span = new TraceSpan(name, spanId, parentSpanId, time);
this.trace = trace;
this.isRoot = isRoot;
trace.spans.push(this.span);
if (agent.config().stackTraceLimit > 0) {
// This is a mechanism to get the structured stack trace out of V8.
// prepareStackTrace is called th first time the Error#stack property is
// accessed. The original behavior is to format the stack as an exception
// throw, which is not what we like. We customize it.
//
// See: https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi
//
var origLimit = Error.stackTraceLimit;
Error.stackTraceLimit = agent.config().stackTraceLimit;
var origPrepare = Error.prepareStackTrace;
Error.prepareStackTrace = function(error, structured) {
return structured;
};
var e = {};
Error.captureStackTrace(e, SpanData);
var stackFrames = [];
e.stack.forEach(function(callSite) {
var functionName = callSite.getFunctionName();
var methodName = callSite.getMethodName();
var name = (methodName && functionName) ?
functionName + ' [as ' + methodName + ']' :
functionName || methodName || '<anonymous function>';
stackFrames.push(new StackFrame(undefined, name,
callSite.getFileName(), callSite.getLineNumber(),
callSite.getColumnNumber()));
});
this.span.setLabel(TraceLabels.STACK_TRACE_DETAILS_KEY,
JSON.stringify({stack_frame: stackFrames}));
Error.stackTraceLimit = origLimit;
Error.prepareStackTrace = origPrepare;
}
}
/**
* Creates a child span of this span.
* @param name The name of the child span.
* @returns {SpanData} The new child trace span data.
*/
SpanData.prototype.createChildSpanData = function(name) {
return new SpanData(this.agent, this.trace, name, this.span.spanId, false);
};
SpanData.prototype.addLabel = function(key, value) {
this.span.setLabel(key, value);
};
/**
* Closes the span and queues it for publishing if it is a root.
*/
SpanData.prototype.close = function() {
this.span.close();
if (this.isRoot) {
this.agent.logger.info('Writing root span');
this.agent.traceWriter.writeSpan(this);
}
};
/**
* Trace API expects stack frames to be a JSON string with the following
* structure:
* STACK_TRACE := { "stack_frame" : [ FRAMES ] }
* FRAMES := { "class_name" : CLASS_NAME, "file_name" : FILE_NAME,
* "line_number" : LINE_NUMBER, "method_name" : METHOD_NAME }*
*
* While the API doesn't expect a columnNumber at this point, it does accept,
* and ignore it.
*
* @param {string|undefined} className
* @param {string|undefined} methodName
* @param {string|undefined} fileName
* @param {number|undefined} lineNumber
* @param {number|undefined} columnNumber
* @constructor @private
*/
function StackFrame(className, methodName, fileName, lineNumber, columnNumber) {
if (className) {
this.class_name = className;
}
if (methodName) {
this.method_name = methodName;
}
if (fileName) {
this.file_name = fileName;
}
if (typeof lineNumber === 'number') {
this.line_number = lineNumber;
}
if (typeof columnNumber === 'number') {
this.column_number = columnNumber;
}
}
SpanData.nullSpan = {
createChildSpanData: function() { return SpanData.nullSpan; },
addLabel: function() {},
close: function() {}
};
/**
* Export SpanData.
*/
module.exports = SpanData;