UNPKG

rsocket-rpc-tracing

Version:

RSocket JavaScript RPC Tracing Support

111 lines (87 loc) 3.04 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSpanSingle = createSpanSingle; var _Single = require('rsocket-flowable/build/Single'); var _opentracing = require('opentracing'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function createSpanSingle(single, tracer, name, context, metadata) { for (var _len = arguments.length, tags = Array(_len > 5 ? _len - 5 : 0), _key = 5; _key < _len; _key++) { tags[_key - 5] = arguments[_key]; } return new _Single.Single(function (subscriber) { var spanSubscriber = new (Function.prototype.bind.apply(SpanSingleSubscriber, [null].concat([subscriber, tracer, name, context, metadata], tags)))(); single.subscribe(spanSubscriber); }); } var SpanSingleSubscriber = function () { function SpanSingleSubscriber(subscriber, tracer, name, context, metadata) { _classCallCheck(this, SpanSingleSubscriber); this._subscriber = subscriber; this._tracer = tracer; var options = {}; if (context) { options.childOf = context; } for (var _len2 = arguments.length, tags = Array(_len2 > 5 ? _len2 - 5 : 0), _key2 = 5; _key2 < _len2; _key2++) { tags[_key2 - 5] = arguments[_key2]; } if (tags) { var finalTags = {}; tags.forEach(function (tag) { Object.keys(tag).forEach(function (key) { finalTags[key] = tag[key]; }); }); options.tags = finalTags; } //Not supported at this time. // if (references) { // options.references = references; // } // options.startTime = Date.now() * 1000; this._span = this._tracer.startSpan(name, options); this._tracer.inject(this._span.context(), _opentracing.FORMAT_TEXT_MAP, metadata === undefined || metadata === null ? {} : metadata); } SpanSingleSubscriber.prototype.cleanup = function cleanup() { this._span.finish(); }; SpanSingleSubscriber.prototype.onSubscribe = function onSubscribe(cancel) { var _this = this; this._cancel = cancel; this._span.log('onSubscribe', timeInMicros()); this._subscriber.onSubscribe(function () { _this.cancel(); }); }; SpanSingleSubscriber.prototype.cancel = function cancel() { try { this._span.log('cancel', timeInMicros()); this._cancel && this._cancel(); } finally { this.cleanup(); } }; SpanSingleSubscriber.prototype.onError = function onError(error) { try { this._span.log('onError', timeInMicros()); this._subscriber.onError(error); } finally { this.cleanup(); } }; SpanSingleSubscriber.prototype.onComplete = function onComplete(value) { try { this._span.log('onComplete', timeInMicros()); this._subscriber.onComplete(value); } finally { this.cleanup(); } }; return SpanSingleSubscriber; }(); function timeInMicros() { return Date.now() * 1000 /*microseconds*/; }