From 2da2cebd9e71e5cc4ada7091fb9ecda7828df836 Mon Sep 17 00:00:00 2001 From: Roch Devost Date: Wed, 29 Jan 2020 11:50:28 -0500 Subject: [PATCH] remove several unnecessary dependencies in the browser (#797) --- LICENSE-3rdparty.csv | 4 +-- package.json | 4 +-- packages/datadog-plugin-fetch/src/index.js | 1 + .../src/index.js | 1 + packages/dd-trace/browser.js | 5 +++- .../dd-trace/src/exporters/browser/index.js | 24 +++++++++++------ packages/dd-trace/src/id.js | 7 ++--- packages/dd-trace/src/log.js | 15 +++++++++-- packages/dd-trace/src/opentracing/span.js | 7 +++-- .../dd-trace/src/platform/browser/tags.js | 5 +--- packages/dd-trace/src/priority_sampler.js | 21 ++++++++------- packages/dd-trace/test/browser.test.js | 27 ++++++++++++++----- webpack.config.js | 4 --- 13 files changed, 80 insertions(+), 45 deletions(-) diff --git a/LICENSE-3rdparty.csv b/LICENSE-3rdparty.csv index 9a51ad07b0c..d656976954b 100644 --- a/LICENSE-3rdparty.csv +++ b/LICENSE-3rdparty.csv @@ -1,16 +1,14 @@ Component,Origin,License,Copyright require,@types/node,MIT,Copyright Authors -require,bowser,MIT,Copyright 2015 Dustin Diaz require,container-info,MIT,Copyright 2018 Stephen Belanger +require,core-js,MIT,Copyright 2014-2020 Denis Pushkarev require,hdr-histogram-js,BSD-2-Clause,Copyright 2016 Alexandre Victoor require,int64-buffer,MIT,Copyright 2015-2016 Yusuke Kawasaki require,koalas,MIT,Copyright 2013-2017 Brian Woodward require,limiter,MIT,Copyright 2011 John Hurliman require,lodash.kebabcase,MIT,Copyright JS Foundation and other contributors -require,lodash.memoize,MIT,Copyright JS Foundation and other contributors require,lodash.pick,MIT,Copyright JS Foundation and other contributors require,lodash.sortby,MIT,Copyright JS Foundation and other contributors -require,lodash.truncate,MIT,Copyright JS Foundation and other contributors require,lodash.uniq,MIT,Copyright JS Foundation and other contributors require,methods,MIT,Copyright 2013-2014 TJ Holowaychuk require,module-details-from-path,MIT,Copyright 2016 Thomas Watson Steen diff --git a/package.json b/package.json index fe2f4c630f0..6fed600e0f1 100644 --- a/package.json +++ b/package.json @@ -50,17 +50,15 @@ }, "dependencies": { "@types/node": "^10.12.18", - "bowser": "^2.5.3", "container-info": "^1.0.1", + "core-js": "^3.6.0", "hdr-histogram-js": "^1.1.4", "int64-buffer": "^0.1.9", "koalas": "^1.0.2", "limiter": "^1.1.4", "lodash.kebabcase": "^4.1.1", - "lodash.memoize": "^4.1.2", "lodash.pick": "^4.4.0", "lodash.sortby": "^4.7.0", - "lodash.truncate": "^4.4.2", "lodash.uniq": "^4.5.0", "methods": "^1.1.2", "module-details-from-path": "^1.0.3", diff --git a/packages/datadog-plugin-fetch/src/index.js b/packages/datadog-plugin-fetch/src/index.js index 04c8042760e..189ba0d766e 100644 --- a/packages/datadog-plugin-fetch/src/index.js +++ b/packages/datadog-plugin-fetch/src/index.js @@ -1,5 +1,6 @@ 'use strict' +const URL = require('url-parse') const { Reference, REFERENCE_CHILD_OF } = require('opentracing') const { REFERENCE_NOOP } = require('../../dd-trace/src/constants') const tx = require('../../dd-trace/src/plugins/util/http') diff --git a/packages/datadog-plugin-xmlhttprequest/src/index.js b/packages/datadog-plugin-xmlhttprequest/src/index.js index 763681a4de0..c1cbfc85cf3 100644 --- a/packages/datadog-plugin-xmlhttprequest/src/index.js +++ b/packages/datadog-plugin-xmlhttprequest/src/index.js @@ -1,5 +1,6 @@ 'use strict' +const URL = require('url-parse') const { Reference, REFERENCE_CHILD_OF } = require('opentracing') const { REFERENCE_NOOP } = require('../../dd-trace/src/constants') const tx = require('../../dd-trace/src/plugins/util/http') diff --git a/packages/dd-trace/browser.js b/packages/dd-trace/browser.js index 82e16128b80..e4995b2f66e 100644 --- a/packages/dd-trace/browser.js +++ b/packages/dd-trace/browser.js @@ -1,6 +1,9 @@ 'use strict' -require('whatwg-fetch') // TODO: remove depenency +require('core-js/stable/object/assign') // TODO: remove dependency +require('core-js/stable/set') // TODO: remove dependency +require('core-js/stable/map') // TODO: remove dependency +require('core-js/stable/typed-array') // TODO: remove dependency const platform = require('./src/platform') const browser = require('./src/platform/browser') diff --git a/packages/dd-trace/src/exporters/browser/index.js b/packages/dd-trace/src/exporters/browser/index.js index 2fa5bac1475..0c50c40bf6f 100644 --- a/packages/dd-trace/src/exporters/browser/index.js +++ b/packages/dd-trace/src/exporters/browser/index.js @@ -43,19 +43,27 @@ class BrowserExporter { this._flushing = true const url = `${this._url.href}/v1/input/${this._clientToken}` - const method = 'POST' const body = this._queue.join(DELIMITER) - const keepalive = true - const mode = 'no-cors' - const done = () => { - this._flushing = false - } this._queue = [] this._size = 0 - window.fetch(url, { body, method, keepalive, mode }) - .then(done, done) + send(url, body, () => { + this._flushing = false + }) + } +} + +function send (url, body, callback) { + if (window.fetch) { + window.fetch(url, { body, method: 'POST', keepalive: true, mode: 'no-cors' }) + .then(callback, callback) + } else { + const req = new XMLHttpRequest() + + req.open('POST', url, true) + req.addEventListener('loadend', callback) + req.send(body) } } diff --git a/packages/dd-trace/src/id.js b/packages/dd-trace/src/id.js index 360ded6542d..9dcfe92850b 100644 --- a/packages/dd-trace/src/id.js +++ b/packages/dd-trace/src/id.js @@ -13,6 +13,9 @@ if (platform.crypto) { platform.crypto.getRandomValues(seed) } +const map = Array.prototype.map +const pad = byte => `${byte < 16 ? '0' : ''}${byte.toString(16)}` + // Internal representation of a trace or span ID. class Identifier { constructor (value, radix) { @@ -113,9 +116,7 @@ function toNumberString (buffer, radix) { // Convert a buffer to a hexadecimal string. function toHexString (buffer) { - return Array.from(buffer) - .map(byte => byte.toString(16).padStart(2, '0')) - .join('') + return map.call(buffer, pad).join('') } // Simple pseudo-random 64-bit ID generator. diff --git a/packages/dd-trace/src/log.js b/packages/dd-trace/src/log.js index 2185599d0d1..3b6fea46dbc 100644 --- a/packages/dd-trace/src/log.js +++ b/packages/dd-trace/src/log.js @@ -1,7 +1,5 @@ 'use strict' -const memoize = require('lodash.memoize') - const _default = { debug: message => console.log(message), /* eslint-disable-line no-console */ error: err => console.error(err) /* eslint-disable-line no-console */ @@ -32,6 +30,19 @@ const _checkLogLevel = (logLevel) => { return _logLevels[_defaultLogLevel] } +const memoize = func => { + const cache = {} + const memoized = function (key) { + if (!cache[key]) { + cache[key] = func.apply(this, arguments) + } + + return cache[key] + } + + return memoized +} + const log = { use (logger) { if (logger && logger.debug instanceof Function && logger.error instanceof Function) { diff --git a/packages/dd-trace/src/opentracing/span.js b/packages/dd-trace/src/opentracing/span.js index 06a10e2524a..2b1929fbc1e 100644 --- a/packages/dd-trace/src/opentracing/span.js +++ b/packages/dd-trace/src/opentracing/span.js @@ -2,7 +2,6 @@ const opentracing = require('opentracing') const Span = opentracing.Span -const truncate = require('lodash.truncate') const SpanContext = require('./span_context') const platform = require('../platform') const constants = require('../constants') @@ -39,13 +38,17 @@ class DatadogSpan extends Span { toString () { const spanContext = this.context() + const resourceName = spanContext._tags['resource.name'] + const resource = resourceName.length > 100 + ? `${resourceName.substring(0, 97)}...` + : resourceName const json = JSON.stringify({ traceId: spanContext._traceId, spanId: spanContext._spanId, parentId: spanContext._parentId, service: spanContext._tags['service.name'], name: spanContext._name, - resource: truncate(spanContext._tags['resource.name'], { length: 100 }) + resource }) return `Span${json}` diff --git a/packages/dd-trace/src/platform/browser/tags.js b/packages/dd-trace/src/platform/browser/tags.js index dc4c48cbbc5..7f880916023 100644 --- a/packages/dd-trace/src/platform/browser/tags.js +++ b/packages/dd-trace/src/platform/browser/tags.js @@ -1,11 +1,8 @@ 'use strict' -const bowser = require('bowser/bundled') -const navigator = bowser.parse(window.navigator.userAgent) - module.exports = () => { const rum = window.DD_RUM const context = rum && rum.getInternalContext && rum.getInternalContext() - return { navigator, ...context } + return { ...context } } diff --git a/packages/dd-trace/src/priority_sampler.js b/packages/dd-trace/src/priority_sampler.js index 5e143808014..83bc4d4a409 100644 --- a/packages/dd-trace/src/priority_sampler.js +++ b/packages/dd-trace/src/priority_sampler.js @@ -20,13 +20,6 @@ const AUTO_KEEP = ext.priority.AUTO_KEEP const USER_KEEP = ext.priority.USER_KEEP const DEFAULT_KEY = 'service:,env:' -const priorities = new Set([ - USER_REJECT, - AUTO_REJECT, - AUTO_KEEP, - USER_KEEP -]) - class PrioritySampler { constructor (env, { sampleRate, rateLimit = 100, rules = [] } = {}) { this._env = env @@ -76,7 +69,15 @@ class PrioritySampler { } validate (samplingPriority) { - return priorities.has(samplingPriority) + switch (samplingPriority) { + case USER_REJECT: + case USER_KEEP: + case AUTO_REJECT: + case AUTO_KEEP: + return true + default: + return false + } } _getContext (span) { @@ -131,7 +132,9 @@ class PrioritySampler { } _findRule (context) { - return this._rules.find(rule => this._matchRule(context, rule)) + for (let i = 0, l = this._rules.length; i < l; i++) { + if (this._matchRule(context, this._rules[i])) return this._rules[i] + } } _matchRule (context, rule) { diff --git a/packages/dd-trace/test/browser.test.js b/packages/dd-trace/test/browser.test.js index 67961fff8b5..bce4ce412a1 100644 --- a/packages/dd-trace/test/browser.test.js +++ b/packages/dd-trace/test/browser.test.js @@ -12,6 +12,7 @@ const createEvent = (name) => { describe('dd-trace', () => { let tracer + let fetch beforeEach(() => { tracer = window.ddtrace.tracer @@ -21,19 +22,33 @@ describe('dd-trace', () => { }) }) - afterEach(() => { - window.fetch.restore && window.fetch.restore() - }) + if (window.fetch) { + beforeEach(() => { + fetch = sinon.stub(window, 'fetch').returns({ + then: resolve => resolve() + }) + }) - it('should record and send a trace to the agent', () => { - sinon.stub(window, 'fetch').returns(Promise.resolve()) + afterEach(() => { + window.fetch && window.fetch.restore() + }) + } else { + beforeEach(() => { + fetch = sinon.stub(window.XMLHttpRequest.prototype, 'send') + }) + afterEach(() => { + window.XMLHttpRequest.prototype.restore && window.XMLHttpRequest.prototype.restore() + }) + } + + it('should record and send a trace to the agent', () => { const span = tracer.startSpan('test.request') span.finish() window.dispatchEvent(createEvent('visibilitychange')) - expect(window.fetch).to.have.been.called + window.fetch && expect(fetch).to.have.been.called }) }) diff --git a/webpack.config.js b/webpack.config.js index f25c95cc7ae..d2175fb3f44 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,10 +6,6 @@ const base = { 'dd-trace': './browser.js' }, module: { - noParse: [ - /node_modules\/zone\.js/, - /node_modules\/bowser/ - ], rules: [{ loader: 'babel-loader' }]