1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import * as api from '@opentelemetry/api';
|
17 | import { isAttributeValue, hrTime, hrTimeDuration, isTimeInput, timeInputToHrTime, } from '@opentelemetry/core';
|
18 | import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
|
19 | import { ExceptionEventName } from './enums';
|
20 |
|
21 |
|
22 |
|
23 | var Span = (function () {
|
24 |
|
25 | function Span(parentTracer, context, spanName, spanContext, kind, parentSpanId, links, startTime) {
|
26 | if (links === void 0) { links = []; }
|
27 | if (startTime === void 0) { startTime = hrTime(); }
|
28 | this.attributes = {};
|
29 | this.links = [];
|
30 | this.events = [];
|
31 | this.status = {
|
32 | code: api.SpanStatusCode.UNSET,
|
33 | };
|
34 | this.endTime = [0, 0];
|
35 | this._ended = false;
|
36 | this._duration = [-1, -1];
|
37 | this.name = spanName;
|
38 | this._spanContext = spanContext;
|
39 | this.parentSpanId = parentSpanId;
|
40 | this.kind = kind;
|
41 | this.links = links;
|
42 | this.startTime = timeInputToHrTime(startTime);
|
43 | this.resource = parentTracer.resource;
|
44 | this.instrumentationLibrary = parentTracer.instrumentationLibrary;
|
45 | this._spanLimits = parentTracer.getSpanLimits();
|
46 | this._spanProcessor = parentTracer.getActiveSpanProcessor();
|
47 | this._spanProcessor.onStart(this, context);
|
48 | }
|
49 | Span.prototype.spanContext = function () {
|
50 | return this._spanContext;
|
51 | };
|
52 | Span.prototype.setAttribute = function (key, value) {
|
53 | if (value == null || this._isSpanEnded())
|
54 | return this;
|
55 | if (key.length === 0) {
|
56 | api.diag.warn("Invalid attribute key: " + key);
|
57 | return this;
|
58 | }
|
59 | if (!isAttributeValue(value)) {
|
60 | api.diag.warn("Invalid attribute value set for key: " + key);
|
61 | return this;
|
62 | }
|
63 | if (Object.keys(this.attributes).length >=
|
64 | this._spanLimits.attributeCountLimit &&
|
65 | !Object.prototype.hasOwnProperty.call(this.attributes, key)) {
|
66 | return this;
|
67 | }
|
68 | this.attributes[key] = value;
|
69 | return this;
|
70 | };
|
71 | Span.prototype.setAttributes = function (attributes) {
|
72 | for (var _i = 0, _a = Object.entries(attributes); _i < _a.length; _i++) {
|
73 | var _b = _a[_i], k = _b[0], v = _b[1];
|
74 | this.setAttribute(k, v);
|
75 | }
|
76 | return this;
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Span.prototype.addEvent = function (name, attributesOrStartTime, startTime) {
|
86 | if (this._isSpanEnded())
|
87 | return this;
|
88 | if (this.events.length >= this._spanLimits.eventCountLimit) {
|
89 | api.diag.warn('Dropping extra events.');
|
90 | this.events.shift();
|
91 | }
|
92 | if (isTimeInput(attributesOrStartTime)) {
|
93 | if (typeof startTime === 'undefined') {
|
94 | startTime = attributesOrStartTime;
|
95 | }
|
96 | attributesOrStartTime = undefined;
|
97 | }
|
98 | if (typeof startTime === 'undefined') {
|
99 | startTime = hrTime();
|
100 | }
|
101 | this.events.push({
|
102 | name: name,
|
103 | attributes: attributesOrStartTime,
|
104 | time: timeInputToHrTime(startTime),
|
105 | });
|
106 | return this;
|
107 | };
|
108 | Span.prototype.setStatus = function (status) {
|
109 | if (this._isSpanEnded())
|
110 | return this;
|
111 | this.status = status;
|
112 | return this;
|
113 | };
|
114 | Span.prototype.updateName = function (name) {
|
115 | if (this._isSpanEnded())
|
116 | return this;
|
117 | this.name = name;
|
118 | return this;
|
119 | };
|
120 | Span.prototype.end = function (endTime) {
|
121 | if (endTime === void 0) { endTime = hrTime(); }
|
122 | if (this._isSpanEnded()) {
|
123 | api.diag.error('You can only call end() on a span once.');
|
124 | return;
|
125 | }
|
126 | this._ended = true;
|
127 | this.endTime = timeInputToHrTime(endTime);
|
128 | this._duration = hrTimeDuration(this.startTime, this.endTime);
|
129 | if (this._duration[0] < 0) {
|
130 | api.diag.warn('Inconsistent start and end time, startTime > endTime', this.startTime, this.endTime);
|
131 | }
|
132 | this._spanProcessor.onEnd(this);
|
133 | };
|
134 | Span.prototype.isRecording = function () {
|
135 | return this._ended === false;
|
136 | };
|
137 | Span.prototype.recordException = function (exception, time) {
|
138 | if (time === void 0) { time = hrTime(); }
|
139 | var attributes = {};
|
140 | if (typeof exception === 'string') {
|
141 | attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception;
|
142 | }
|
143 | else if (exception) {
|
144 | if (exception.code) {
|
145 | attributes[SemanticAttributes.EXCEPTION_TYPE] = exception.code.toString();
|
146 | }
|
147 | else if (exception.name) {
|
148 | attributes[SemanticAttributes.EXCEPTION_TYPE] = exception.name;
|
149 | }
|
150 | if (exception.message) {
|
151 | attributes[SemanticAttributes.EXCEPTION_MESSAGE] = exception.message;
|
152 | }
|
153 | if (exception.stack) {
|
154 | attributes[SemanticAttributes.EXCEPTION_STACKTRACE] = exception.stack;
|
155 | }
|
156 | }
|
157 |
|
158 | if (attributes[SemanticAttributes.EXCEPTION_TYPE] ||
|
159 | attributes[SemanticAttributes.EXCEPTION_MESSAGE]) {
|
160 | this.addEvent(ExceptionEventName, attributes, time);
|
161 | }
|
162 | else {
|
163 | api.diag.warn("Failed to record an exception " + exception);
|
164 | }
|
165 | };
|
166 | Object.defineProperty(Span.prototype, "duration", {
|
167 | get: function () {
|
168 | return this._duration;
|
169 | },
|
170 | enumerable: false,
|
171 | configurable: true
|
172 | });
|
173 | Object.defineProperty(Span.prototype, "ended", {
|
174 | get: function () {
|
175 | return this._ended;
|
176 | },
|
177 | enumerable: false,
|
178 | configurable: true
|
179 | });
|
180 | Span.prototype._isSpanEnded = function () {
|
181 | if (this._ended) {
|
182 | api.diag.warn('Can not execute the operation on ended Span {traceId: %s, spanId: %s}', this._spanContext.traceId, this._spanContext.spanId);
|
183 | }
|
184 | return this._ended;
|
185 | };
|
186 | return Span;
|
187 | }());
|
188 | export { Span };
|
189 |
|
\ | No newline at end of file |