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

Conversation

vigneshshanmugam
Copy link
Member

@vigneshshanmugam vigneshshanmugam requested a review from hmdhk May 25, 2020 13:55
@apmmachine
Copy link
Contributor

apmmachine commented May 25, 2020

💚 Build Succeeded

Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: [Pull request #796 updated]

  • Start Time: 2020-05-27T12:45:15.396+0000

  • Duration: 92 min 20 sec

Test stats 🧪

Test Results
Failed 0
Passed 932
Skipped 10
Total 942

Steps errors

Expand to view the steps failures

  • Name: Bundlesize

    • Description: #!/bin/bash set -o pipefail npm run bundlesize|tee bundlesize.txt

    • Duration: 1 min 31 sec

    • Start Time: 2020-05-27T12:52:41.934+0000

    • log

  • Name: Start Elastic Stack 7.7.0 - @elastic/apm-rum - none

    • Description:

    • Duration: 4 min 2 sec

    • Start Time: 2020-05-27T13:01:16.757+0000

    • log

  • Name: Error signal

    • Description: The test failed

    • Duration: 0 min 0 sec

    • Start Time: 2020-05-27T13:40:05.141+0000

    • log

@codecov-commenter
Copy link

codecov-commenter commented May 25, 2020

Codecov Report

Merging #796 into master will increase coverage by 0.52%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #796      +/-   ##
==========================================
+ Coverage   92.13%   92.66%   +0.52%     
==========================================
  Files          50       50              
  Lines        2289     2290       +1     
  Branches      457      456       -1     
==========================================
+ Hits         2109     2122      +13     
+ Misses        177      165      -12     
  Partials        3        3              
Impacted Files Coverage Δ
packages/rum-core/src/index.js 100.00% <ø> (ø)
packages/rum/src/apm-base.js 99.05% <ø> (ø)
packages/rum-core/src/bootstrap.js 66.66% <100.00%> (+51.28%) ⬆️
packages/rum-core/src/common/polyfills.js 100.00% <100.00%> (+33.33%) ⬆️
packages/rum-core/src/common/url.js 98.61% <100.00%> (ø)
packages/rum-core/src/common/utils.js 94.90% <100.00%> (+1.31%) ⬆️
packages/rum/src/index.js 100.00% <100.00%> (ø)

@vigneshshanmugam
Copy link
Member Author

@jahtalab Can i get your review before I do the rebase, that wont have a huge change as it would be simply point to rum-core.

Copy link
Contributor

@hmdhk hmdhk left a comment

Choose a reason for hiding this comment

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

@vigneshshanmugam , I think the approach taken here is fine but IMO, it doesn't address the core issue which is the fact that the agent is loaded in the page twice, we need to fix that! One idea is to stop having dependency from the framework specific packages to the base package and instead use window.elasticApm

packages/rum/src/index.js Outdated Show resolved Hide resolved
@vigneshshanmugam
Copy link
Member Author

agent is loaded in the page twice

This is a common problem and we have to deal with it as you said. There are couple of options.

1. Exposing apmBase, apm from @elastic/apm-rum-*(react/angular/vue) packages

This would let users to use to initialise the agent from the imports of a single package and would remove the need to import @elastic/apm-rum for initialisation purpose alone. This is already done in VuePlugin and AngularService but react package doesn't expose it.

2. Relying on window.elasticApm in framework integrations

This has huge disadvantages.

  • We expect the users to load apmbase before the framework package which would result in window.elasticApm not being found if any of the modules was downloaded async and we cannot dynamically import if the package isn't found.

Looking at both options, I would say No.1 is an easy solution and best approach. I have created an issue stating the above reasons #799.

In any case, this PR is not related to how exports are used. Instead it fixes the issue if the user imported our packages by mistake multiple times and our library has to deal with it.

@vigneshshanmugam vigneshshanmugam requested a review from hmdhk May 27, 2020 10:41
Copy link
Contributor

@hmdhk hmdhk left a comment

Choose a reason for hiding this comment

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

Thanks @vigneshshanmugam for opening the issue, we should address the core issue to prevent this. But we can merge this one as a quick fix.

packages/rum-core/src/common/bootstrap.js Outdated Show resolved Hide resolved
* 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}

packages/rum/test/specs/index.spec.js Show resolved Hide resolved
@vigneshshanmugam vigneshshanmugam requested a review from hmdhk May 27, 2020 12:46
@hmdhk hmdhk merged commit d585324 into elastic:master May 28, 2020
@vigneshshanmugam vigneshshanmugam deleted the single-instance branch May 28, 2020 08:23
vigneshshanmugam added a commit that referenced this pull request May 28, 2020
* fix(rum): use single instance of apm across all packages

* chore: address review and add test
v1v added a commit to v1v/apm-agent-rum-js that referenced this pull request Jul 3, 2020
* upstream/master: (23 commits)
  feat(rum-core): capture XHR/Fetch spans using resource timing (elastic#825)
  docs: update set-up.asciidoc (elastic#814)
  chore: remove compressed size gh workflow (elastic#828)
  feat: use page visibilityState for browser responsiveness check (elastic#813)
  ci(jenkins): report bundlesize as a GitHub comment (elastic#826)
  docs: release notes for 5.2.1 (elastic#824)
  chore(release): publish
  fix(rum-core): protect aganist buggy navigation timing data (elastic#819)
  fix(rum-core): protect aganist buggy navigation timing data (elastic#819)
  chore(rum-core): use startTime for LCP marks (elastic#815)
  fix(rum-core): capture tbt after all task entries are observed (elastic#803)
  feat(rum-react): use correct path when route is path array (elastic#800)
  ci: enable benchmark on a PR basis (elastic#812)
  ci: use dockerLogs step (elastic#810)
  fix: env var invalid type (elastic#809)
  fix: workarount for elastic/beats#18858 (elastic#807)
  docs: add release notes for 5.2.0 (elastic#801)
  chore(release): publish
  fix(rum-core): consider user defined type of high precedence (elastic#798)
  fix(rum): use single instance of apm across all packages (elastic#796)
  ...
David-Development pushed a commit to David-Development/apm-agent-rum-js that referenced this pull request Oct 20, 2021
* fix(rum): use single instance of apm across all packages

* chore: address review and add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bundle should not replace elasticApm if its present on window
4 participants