12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.flushAllTraces = exports.trace = exports.SpanStatus = void 0;
- var _report = require("./report");
- const NUM_OF_MICROSEC_IN_NANOSEC = BigInt("1000");
- let count = 0;
- const getId = ()=>{
- count++;
- return count;
- };
- var SpanStatus;
- exports.SpanStatus = SpanStatus;
- (function(SpanStatus) {
- SpanStatus[SpanStatus["Started"] = 0] = "Started";
- SpanStatus[SpanStatus["Stopped"] = 1] = "Stopped";
- })(SpanStatus || (exports.SpanStatus = SpanStatus = {}));
- class Span {
- constructor({ name , parentId , attrs , startTime }){
- this.name = name;
- this.parentId = parentId;
- this.duration = null;
- this.attrs = attrs ? {
- ...attrs
- } : {};
- this.status = 0;
- this.id = getId();
- this._start = startTime || process.hrtime.bigint();
-
-
-
-
-
- this.now = Date.now();
- }
-
-
-
-
- stop(stopTime) {
- const end = stopTime || process.hrtime.bigint();
- const duration = (end - this._start) / NUM_OF_MICROSEC_IN_NANOSEC;
- this.status = 1;
- if (duration > Number.MAX_SAFE_INTEGER) {
- throw new Error(`Duration is too long to express as float64: ${duration}`);
- }
- const timestamp = this._start / NUM_OF_MICROSEC_IN_NANOSEC;
- _report.reporter.report(this.name, Number(duration), Number(timestamp), this.id, this.parentId, this.attrs, this.now);
- }
- traceChild(name, attrs) {
- return new Span({
- name,
- parentId: this.id,
- attrs
- });
- }
- manualTraceChild(name, startTime, stopTime, attrs) {
- const span = new Span({
- name,
- parentId: this.id,
- attrs,
- startTime
- });
- span.stop(stopTime);
- }
- setAttribute(key, value) {
- this.attrs[key] = String(value);
- }
- traceFn(fn) {
- try {
- return fn();
- } finally{
- this.stop();
- }
- }
- async traceAsyncFn(fn) {
- try {
- return await fn();
- } finally{
- this.stop();
- }
- }
- }
- exports.Span = Span;
- const trace = (name, parentId, attrs)=>{
- return new Span({
- name,
- parentId,
- attrs
- });
- };
- exports.trace = trace;
- const flushAllTraces = ()=>_report.reporter.flushAll();
- exports.flushAllTraces = flushAllTraces;
|