-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(rum): Add measurements support and web vitals #2909
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
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
49db0be
feat(rum): Add measurements support and web vitals
dashed f26f152
Update packages/tracing/src/browser/metrics.ts
dashed 242a71d
remove some @ts-ignore
dashed 0f06193
Measurements are only available for pageload transactions
dashed 892e0ec
add enableMeasurements option to browser tracing options
dashed 951c8b9
lint fix
dashed 2b8f766
fix test
dashed d696e62
rename mark.fid_start to mark.fid
dashed 29c4b52
set enableMeasurements to be true by default
dashed 01f1117
remove enableMeasurements option
dashed 4f194a7
Merge branch 'master' into add-web-vitals
dashed cad03a0
Update packages/tracing/src/browser/metrics.ts
dashed 646ebea
Update packages/tracing/src/browser/metrics.ts
dashed 375e7cc
Update packages/tracing/src/browser/metrics.ts
dashed bc7e0cc
Update packages/tracing/src/browser/metrics.ts
dashed 2f1821b
save some bytes
dashed 688c985
delete old lcp implementation
dashed c1b72ae
share firstHiddenTime code
dashed 8c81de5
feature-detect FID and LCP
dashed 8cc49f8
construction transaction event beforehand and add measurements condit…
dashed 9eca498
yarn fix
dashed 7f88469
ref: Restore blank line
rhcarvalho 2318bfb
capture final lcp on hidden
dashed 1dd9e83
Merge branch 'add-web-vitals' of github.com:getsentry/sentry-javascri…
dashed d5f0a5e
always disconnect the performance observer for FID
dashed b2671c3
vendor FID and LCP implementations from https://github.com/GoogleChro…
dashed 6564a13
update ts on web-vitals impl
dashed 4928ed3
use getLCP function
dashed d803955
remove _forceLCP
dashed 18e4060
use getFID function
dashed 7a40c5a
fix import
dashed 6665ab6
rm unused interface
dashed f13c9ae
add missing file
dashed 0700577
more fixes
dashed 4ef5c48
fix log
dashed 7b71679
add web-vitals README
dashed b0522af
Merge branch 'master' into add-web-vitals
dashed d8c89d3
Merge branch 'master' into add-web-vitals
dashed 7006bc8
Merge branch 'master' into add-web-vitals
dashed 3cb996b
note the SHA commit used to vendor web-vitals
dashed 7801463
Merge branch 'master' into add-web-vitals
dashed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# web-vitals | ||
|
||
> A modular library for measuring the [Web Vitals](https://web.dev/vitals/) metrics on real users. | ||
|
||
This was vendored from: https://github.com/GoogleChrome/web-vitals | ||
rhcarvalho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The commit SHA used is: [56c736b7c4e80f295bc8a98017671c95231fa225](https://github.com/GoogleChrome/web-vitals/tree/56c736b7c4e80f295bc8a98017671c95231fa225) | ||
|
||
Current vendored web vitals are: | ||
|
||
- LCP (Largest Contentful Paint) | ||
- FID (First Input Delay) | ||
|
||
# License | ||
|
||
[Apache 2.0](https://github.com/GoogleChrome/web-vitals/blob/master/LICENSE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { bindReporter } from './lib/bindReporter'; | ||
import { getFirstHidden } from './lib/getFirstHidden'; | ||
import { initMetric } from './lib/initMetric'; | ||
import { observe, PerformanceEntryHandler } from './lib/observe'; | ||
import { onHidden } from './lib/onHidden'; | ||
import { ReportHandler } from './types'; | ||
|
||
interface FIDPolyfillCallback { | ||
(value: number, event: Event): void; | ||
} | ||
|
||
interface FIDPolyfill { | ||
onFirstInputDelay: (onReport: FIDPolyfillCallback) => void; | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
perfMetrics: FIDPolyfill; | ||
} | ||
} | ||
|
||
// https://wicg.github.io/event-timing/#sec-performance-event-timing | ||
interface PerformanceEventTiming extends PerformanceEntry { | ||
processingStart: DOMHighResTimeStamp; | ||
cancelable?: boolean; | ||
target?: Element; | ||
} | ||
|
||
export const getFID = (onReport: ReportHandler): void => { | ||
const metric = initMetric('FID'); | ||
const firstHidden = getFirstHidden(); | ||
|
||
const entryHandler = (entry: PerformanceEventTiming): void => { | ||
// Only report if the page wasn't hidden prior to the first input. | ||
if (entry.startTime < firstHidden.timeStamp) { | ||
metric.value = entry.processingStart - entry.startTime; | ||
metric.entries.push(entry); | ||
metric.isFinal = true; | ||
report(); | ||
} | ||
}; | ||
|
||
const po = observe('first-input', entryHandler as PerformanceEntryHandler); | ||
const report = bindReporter(onReport, metric, po); | ||
|
||
if (po) { | ||
onHidden(() => { | ||
po.takeRecords().map(entryHandler as PerformanceEntryHandler); | ||
po.disconnect(); | ||
}, true); | ||
} else { | ||
if (window.perfMetrics && window.perfMetrics.onFirstInputDelay) { | ||
window.perfMetrics.onFirstInputDelay((value: number, event: Event) => { | ||
// Only report if the page wasn't hidden prior to the first input. | ||
if (event.timeStamp < firstHidden.timeStamp) { | ||
metric.value = value; | ||
metric.isFinal = true; | ||
metric.entries = [ | ||
{ | ||
entryType: 'first-input', | ||
name: event.type, | ||
target: event.target, | ||
cancelable: event.cancelable, | ||
startTime: event.timeStamp, | ||
processingStart: event.timeStamp + value, | ||
} as PerformanceEventTiming, | ||
]; | ||
report(); | ||
} | ||
}); | ||
} | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { bindReporter } from './lib/bindReporter'; | ||
import { getFirstHidden } from './lib/getFirstHidden'; | ||
import { initMetric } from './lib/initMetric'; | ||
import { observe, PerformanceEntryHandler } from './lib/observe'; | ||
import { onHidden } from './lib/onHidden'; | ||
import { whenInput } from './lib/whenInput'; | ||
import { ReportHandler } from './types'; | ||
|
||
export const getLCP = (onReport: ReportHandler, reportAllChanges = false): void => { | ||
const metric = initMetric('LCP'); | ||
const firstHidden = getFirstHidden(); | ||
|
||
let report: ReturnType<typeof bindReporter>; | ||
|
||
const entryHandler = (entry: PerformanceEntry): void => { | ||
// The startTime attribute returns the value of the renderTime if it is not 0, | ||
// and the value of the loadTime otherwise. | ||
const value = entry.startTime; | ||
|
||
// If the page was hidden prior to paint time of the entry, | ||
// ignore it and mark the metric as final, otherwise add the entry. | ||
if (value < firstHidden.timeStamp) { | ||
metric.value = value; | ||
metric.entries.push(entry); | ||
} else { | ||
metric.isFinal = true; | ||
} | ||
|
||
report(); | ||
}; | ||
|
||
const po = observe('largest-contentful-paint', entryHandler); | ||
|
||
if (po) { | ||
report = bindReporter(onReport, metric, po, reportAllChanges); | ||
|
||
const onFinal = (): void => { | ||
if (!metric.isFinal) { | ||
po.takeRecords().map(entryHandler as PerformanceEntryHandler); | ||
metric.isFinal = true; | ||
report(); | ||
} | ||
}; | ||
|
||
void whenInput().then(onFinal); | ||
onHidden(onFinal, true); | ||
} | ||
}; |
45 changes: 45 additions & 0 deletions
45
packages/tracing/src/browser/web-vitals/lib/bindReporter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Metric, ReportHandler } from '../types'; | ||
|
||
export const bindReporter = ( | ||
callback: ReportHandler, | ||
metric: Metric, | ||
po: PerformanceObserver | undefined, | ||
observeAllUpdates?: boolean, | ||
): (() => void) => { | ||
let prevValue: number; | ||
return () => { | ||
if (po && metric.isFinal) { | ||
po.disconnect(); | ||
} | ||
if (metric.value >= 0) { | ||
if (observeAllUpdates || metric.isFinal || document.visibilityState === 'hidden') { | ||
metric.delta = metric.value - (prevValue || 0); | ||
|
||
// Report the metric if there's a non-zero delta, if the metric is | ||
// final, or if no previous value exists (which can happen in the case | ||
// of the document becoming hidden when the metric value is 0). | ||
// See: https://github.com/GoogleChrome/web-vitals/issues/14 | ||
if (metric.delta || metric.isFinal || prevValue === undefined) { | ||
callback(metric); | ||
prevValue = metric.value; | ||
} | ||
} | ||
} | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.