UNPKG

3.79 kBJavaScriptView Raw
1"use strict";
2/* eslint-disable import/no-extraneous-dependencies */
3var __extends = (this && this.__extends) || (function () {
4 var extendStatics = function (d, b) {
5 extendStatics = Object.setPrototypeOf ||
6 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
8 return extendStatics(d, b);
9 };
10 return function (d, b) {
11 extendStatics(d, b);
12 function __() { this.constructor = d; }
13 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14 };
15})();
16Object.defineProperty(exports, "__esModule", { value: true });
17var opentracing = require("../index");
18var mock_context_1 = require("./mock_context");
19/**
20 * OpenTracing Span implementation designed for use in unit tests.
21 */
22var MockSpan = /** @class */ (function (_super) {
23 __extends(MockSpan, _super);
24 //------------------------------------------------------------------------//
25 // MockSpan-specific
26 //------------------------------------------------------------------------//
27 function MockSpan(tracer) {
28 var _this = _super.call(this) || this;
29 _this._mockTracer = tracer;
30 _this._uuid = _this._generateUUID();
31 _this._startMs = Date.now();
32 _this._finishMs = 0;
33 _this._operationName = '';
34 _this._tags = {};
35 _this._logs = [];
36 return _this;
37 }
38 //------------------------------------------------------------------------//
39 // OpenTracing implementation
40 //------------------------------------------------------------------------//
41 MockSpan.prototype._context = function () {
42 return new mock_context_1.default(this);
43 };
44 MockSpan.prototype._setOperationName = function (name) {
45 this._operationName = name;
46 };
47 MockSpan.prototype._addTags = function (set) {
48 var keys = Object.keys(set);
49 for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
50 var key = keys_1[_i];
51 this._tags[key] = set[key];
52 }
53 };
54 MockSpan.prototype._log = function (fields, timestamp) {
55 this._logs.push({
56 fields: fields,
57 timestamp: timestamp
58 });
59 };
60 MockSpan.prototype._finish = function (finishTime) {
61 this._finishMs = finishTime || Date.now();
62 };
63 MockSpan.prototype.uuid = function () {
64 return this._uuid;
65 };
66 MockSpan.prototype.operationName = function () {
67 return this._operationName;
68 };
69 MockSpan.prototype.durationMs = function () {
70 return this._finishMs - this._startMs;
71 };
72 MockSpan.prototype.tags = function () {
73 return this._tags;
74 };
75 MockSpan.prototype.tracer = function () {
76 return this._mockTracer;
77 };
78 MockSpan.prototype._generateUUID = function () {
79 var p0 = ("00000000" + Math.abs((Math.random() * 0xFFFFFFFF) | 0).toString(16)).substr(-8);
80 var p1 = ("00000000" + Math.abs((Math.random() * 0xFFFFFFFF) | 0).toString(16)).substr(-8);
81 return "" + p0 + p1;
82 };
83 MockSpan.prototype.addReference = function (ref) {
84 };
85 /**
86 * Returns a simplified object better for console.log()'ing.
87 */
88 MockSpan.prototype.debug = function () {
89 var obj = {
90 uuid: this._uuid,
91 operation: this._operationName,
92 millis: [this._finishMs - this._startMs, this._startMs, this._finishMs]
93 };
94 if (Object.keys(this._tags).length) {
95 obj.tags = this._tags;
96 }
97 return obj;
98 };
99 return MockSpan;
100}(opentracing.Span));
101exports.MockSpan = MockSpan;
102exports.default = MockSpan;
103//# sourceMappingURL=mock_span.js.map
\No newline at end of file