UNPKG

lightstep-tracer

Version:

> ❗ **This instrumentation is no longer recommended**. Please review [documentation on setting up and configuring the OpenTelemetry Node.js Launcher](https://github.com/lightstep/otel-launcher-node) or [OpenTelemetry JS (Browser)](https://github.com/open-

200 lines (172 loc) 7.06 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } // eslint-disable-next-line import/extensions var optionsParser = require('./options_parser.js'); var util = require('./util'); var kRuntimeGUIDCookiePrefix = 'lightstep_guid'; var kSessionIDCookieKey = 'lightstep_session_id'; var kCookieTimeToLiveSeconds = 7 * 24 * 60 * 60; var nowMicrosImp = function () { // Is a hi-res timer available? if (window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart) { var start = performance.timing.navigationStart; return function () { return Math.floor((start + performance.now()) * 1000.0); }; } // The low-res timer is the best we can do return function () { return Date.now() * 1000.0; }; }(); var PlatformBrowser = /*#__PURE__*/function () { function PlatformBrowser() { _classCallCheck(this, PlatformBrowser); } _createClass(PlatformBrowser, [{ key: "name", value: function name() { return 'browser'; } }, { key: "nowMicros", value: function nowMicros() { return nowMicrosImp(); } // Return the GUID to use for the runtime. The intention is to reuse the // GUID so that logically a single browser session looks like a single // runtime. }, { key: "runtimeGUID", value: function runtimeGUID(groupName) { // Account for the groupName in the same that multiple apps or services // are running on the same domain (and should not share the same // runtime GUID). var cookieKey = encodeURIComponent("".concat(kRuntimeGUIDCookiePrefix, "/").concat(groupName)); var uuid = util.cookie(cookieKey) || this._generateLongUUID(); util.cookie(cookieKey, uuid, kCookieTimeToLiveSeconds, '/'); // Also create a session ID as well to give the server more information // to coordinate with. var sessionID = util.cookie(kSessionIDCookieKey) || this._generateLongUUID(); util.cookie(kSessionIDCookieKey, sessionID, kCookieTimeToLiveSeconds, '/'); return uuid; } }, { key: "generateUUID", value: function generateUUID() { return this._generateLongUUID(); } }, { key: "_generateLongUUID", value: function _generateLongUUID() { /* eslint-disable no-bitwise */ var p0 = "00000000".concat(Math.abs(Math.random() * 0xFFFFFFFF | 0).toString(16)).substr(-8); var p1 = "00000000".concat(Math.abs(Math.random() * 0xFFFFFFFF | 0).toString(16)).substr(-8); return "".concat(p0).concat(p1); /* eslint-enable no-bitwise */ } }, { key: "onBeforeExit", value: function onBeforeExit() { // This will result in the final report not being made in non-browser // environments like React Native. Flush should be called explicitly in // those environments if (util.isBrowser()) { var _window; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } (_window = window).addEventListener.apply(_window, ['beforeunload'].concat(args)); } } }, { key: "plugins", value: function plugins(opts) { return [require('../../../plugins/instrument_xhr'), require('../../../plugins/instrument_fetch'), require('../../../plugins/instrument_document_load')]; } }, { key: "options", value: function options(imp) { var tracerOpts = {}; var browserOpts = {}; optionsParser.parseScriptElementOptions(tracerOpts, browserOpts); optionsParser.parseURLQueryOptions(tracerOpts, browserOpts); return tracerOpts; } }, { key: "tracerTags", value: function tracerTags() { return { 'lightstep.tracer_platform': 'browser' }; } // There's no way to truly "fatal" on the browser; the best approximation // is an Error exception. }, { key: "fatal", value: function fatal(message) { throw new Error(message); } }, { key: "localStoreGet", value: function localStoreGet(key) { try { if (!window.sessionStorage) { return null; } } catch (_ignored) { // Accessing `sessionStorage` or `localStorage` in an `<iframe>` in Chrome throws when // the user setting "block third-party cookies and site data" is turned on. // // eslint-disable-next-line max-len // https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document return null; } try { return JSON.parse(sessionStorage.getItem("lightstep/".concat(key))); } catch (_ignored) { return null; } } }, { key: "localStoreSet", value: function localStoreSet(key, value) { try { if (!window.sessionStorage) { return; } } catch (_ignored) { // (See comment above) return; } try { sessionStorage.setItem("lightstep/".concat(key), JSON.stringify(value)); } catch (_ignored) { /* Ignored */ } } }], [{ key: "initLibrary", value: function initLibrary(lib) { var tracerOpts = {}; var browserOpts = {}; optionsParser.parseScriptElementOptions(tracerOpts, browserOpts); if (browserOpts.init_global_tracer) { PlatformBrowser.initGlobalTracer(lib, tracerOpts); } } }, { key: "initGlobalTracer", value: function initGlobalTracer(lib, opts) { if ((typeof window === "undefined" ? "undefined" : _typeof(window)) !== 'object') { return; } if (_typeof(window.opentracing) !== 'object') { return; } opentracing.initGlobalTracer(new lib.Tracer(opts)); // eslint-disable-line no-undef } }]); return PlatformBrowser; }(); module.exports = PlatformBrowser; //# sourceMappingURL=platform_browser.js.map