Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rum): use single instance of apm across all packages #796

Merged
merged 2 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,17 @@
*
*/

import { isPlatformSupported } from './common/utils'
import { patchAll } from './common/patching'
import { bootstrapMetrics } from './performance-monitoring/metrics'
import { isPlatformSupported, isBrowser } from './utils'
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
import { patchAll } from './patching'
import { bootstrapMetrics } from '../performance-monitoring/metrics'

let alreadyBootstrap = false
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
let enabled = false

export function bootstrap() {
if (alreadyBootstrap) {
return enabled
}
alreadyBootstrap = true

if (isPlatformSupported()) {
patchAll()
bootstrapMetrics()
enabled = true
} else if (typeof window !== 'undefined') {
} else if (isBrowser) {
/**
* Print this error message only on the browser console
* on unsupported browser versions
Expand Down
3 changes: 2 additions & 1 deletion packages/rum-core/src/common/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
*/

import PromisePollyfill from 'promise-polyfill'
import { isBrowser } from './utils'

/**
* Use the globally available promise if it exists and
* fallback to using the polyfilled Promise
*/
let local = {}
if (typeof window !== 'undefined') {
if (isBrowser) {
local = window
} else if (typeof self !== 'undefined') {
local = self
Expand Down
4 changes: 3 additions & 1 deletion packages/rum-core/src/common/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
*
*/

import { isBrowser } from './utils'

/**
* Add default ports for other protocols(ws, wss) after
* RUM agent starts instrumenting those
Expand Down Expand Up @@ -197,7 +199,7 @@ class Url {

getLocation() {
var globalVar = {}
if (typeof window !== 'undefined') {
if (isBrowser) {
globalVar = window
}

Expand Down
11 changes: 5 additions & 6 deletions packages/rum-core/src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import { Promise } from './polyfills'

const slice = [].slice
const PERF =
typeof window !== 'undefined' && typeof performance !== 'undefined'
? performance
: {}
const isBrowser = typeof window !== 'undefined'
const PERF = isBrowser && typeof performance !== 'undefined' ? performance : {}

function isCORSSupported() {
var xhr = new window.XMLHttpRequest()
Expand Down Expand Up @@ -135,7 +133,7 @@ function checkSameOrigin(source, target) {

function isPlatformSupported() {
return (
typeof window !== 'undefined' &&
isBrowser &&
typeof Array.prototype.forEach === 'function' &&
typeof JSON.stringify === 'function' &&
typeof Function.bind === 'function' &&
Expand Down Expand Up @@ -390,5 +388,6 @@ export {
find,
removeInvalidChars,
PERF,
isPerfTimelineSupported
isPerfTimelineSupported,
isBrowser
}
6 changes: 4 additions & 2 deletions packages/rum-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import { ServiceFactory } from './common/service-factory'
import {
isPlatformSupported,
scheduleMicroTask,
scheduleMacroTask
scheduleMacroTask,
isBrowser
} from './common/utils'
import { patchAll, patchEventHandler } from './common/patching'
import {
Expand All @@ -43,8 +44,8 @@ import {
} from './common/constants'
import { getInstrumentationFlags } from './common/instrument'
import afterFrame from './common/after-frame'
import { bootstrap } from './common/bootstrap'
import { createTracer } from './opentracing'
import { bootstrap } from './bootstrap'

function createServiceFactory() {
registerPerfServices()
Expand All @@ -59,6 +60,7 @@ export {
patchAll,
patchEventHandler,
isPlatformSupported,
isBrowser,
getInstrumentationFlags,
createTracer,
scheduleMicroTask,
Expand Down
42 changes: 42 additions & 0 deletions packages/rum-core/test/common/bootstrap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import { bootstrap } from '../../src/common/bootstrap'

describe('bootstrap', function() {
it('should log warning on unsupported environments', () => {
// Pass unsupported check
const nowFn = window.performance.now
window.performance.now = undefined

spyOn(console, 'log')
bootstrap()

expect(console.log).toHaveBeenCalledWith(
'[Elastic APM] platform is not supported!'
)
window.performance.now = nowFn
})
})
4 changes: 1 addition & 3 deletions packages/rum/src/apm-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
APM_SERVER
} from '@elastic/apm-rum-core'

class ApmBase {
export default class ApmBase {
constructor(serviceFactory, disable) {
this._disable = disable
this.serviceFactory = serviceFactory
Expand Down Expand Up @@ -277,5 +277,3 @@ class ApmBase {
configService.addFilter(fn)
}
}

export default ApmBase
29 changes: 23 additions & 6 deletions packages/rum/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,34 @@
*
*/

import { createServiceFactory, bootstrap } from '@elastic/apm-rum-core'
import {
createServiceFactory,
bootstrap,
isBrowser
} from '@elastic/apm-rum-core'
import ApmBase from './apm-base'

const enabled = bootstrap()
const serviceFactory = createServiceFactory()
const apmBase = new ApmBase(serviceFactory, !enabled)
/**
* Use a single instance of ApmBase across all instance of the agent
* including the instanes used in framework specific integrations
*/
function getApmBase() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is there any reason for having a function here instead of just having on the top level?
It seems to me the logic would be simpler if we remove this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a personal preference. It looked a bit cleaner than having to reassign apmBase like this

let apmBase

if (isBrowser && window.elasticApm) {
  apmBase = window.elasticApm
} else {
  const enabled = bootstrap()
  const serviceFactory = createServiceFactory()
  apmBase = new ApmBase(serviceFactory, !enabled)

  if (isBrowser) {
    window.elasticApm = apmBase
  }
}
export {apmBase}

if (isBrowser && window.elasticApm) {
return window.elasticApm
}
const enabled = bootstrap()
const serviceFactory = createServiceFactory()
const apmBase = new ApmBase(serviceFactory, !enabled)

if (typeof window !== 'undefined') {
window.elasticApm = apmBase
if (isBrowser) {
window.elasticApm = apmBase
}

return apmBase
}

const apmBase = getApmBase()

const init = apmBase.init.bind(apmBase)

export default init
Expand Down
16 changes: 0 additions & 16 deletions packages/rum/test/specs/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ describe('index', function() {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout
})

it('should log only on browser environments', () => {
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
// Pass unsupported check
const nowFn = window.performance.now
window.performance.now = undefined

spyOn(console, 'log')

require('../../src/')

expect(console.log).toHaveBeenCalledWith(
'[Elastic APM] platform is not supported!'
)

window.performance.now = nowFn
})

it('should init ApmBase', function(done) {
var apmServer = apmBase.serviceFactory.getService('ApmServer')
if (globalConfig.useMocks) {
Expand Down