-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
feat(insights): Update backend performance score function to handle missing vitals #82750
Merged
edwardgou-sentry
merged 4 commits into
master
from
egou/feat/performance-score-functions-missing-vitals
Jan 2, 2025
+135
−6
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
980bdeb
Updates the performance score function to account for missing webvita…
edwardgou-sentry 5615ad4
feature flag check
edwardgou-sentry 3bf1e7b
Merge branch 'master' into egou/feat/performance-score-functions-miss…
edwardgou-sentry 2cd5a0c
add doc strings
edwardgou-sentry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from django.utils.functional import cached_property | ||
from snuba_sdk import Column, Condition, Function, Op, OrderBy | ||
|
||
from sentry import features | ||
from sentry.api.event_search import SearchFilter | ||
from sentry.exceptions import IncompatibleMetricsQuery, InvalidSearchQuery | ||
from sentry.search.events import constants, fields | ||
|
@@ -1531,6 +1532,12 @@ def _resolve_web_vital_score_function( | |
args: Mapping[str, str | Column | SelectType | int | float], | ||
alias: str | None, | ||
) -> SelectType: | ||
"""Returns the normalized score (0.0-1.0) for a given web vital. | ||
This function exists because we don't store a metric for the normalized score. | ||
The normalized score is calculated by dividing the sum of measurements.score.* by the sum of measurements.score.weight.* | ||
|
||
To calculate the total performance score, see _resolve_total_performance_score_function. | ||
""" | ||
column = args["column"] | ||
metric_id = args["metric_id"] | ||
|
||
|
@@ -1835,6 +1842,14 @@ def _resolve_total_performance_score_function( | |
_: Mapping[str, str | Column | SelectType | int | float], | ||
alias: str | None, | ||
) -> SelectType: | ||
"""Returns the total performance score based on a page/site's web vitals. | ||
This function is calculated by: | ||
the summation of (normalized_vital_score * weight) for each vital, divided by the sum of all weights | ||
- normalized_vital_score is the 0.0-1.0 score for each individual vital | ||
- weight is the 0.0-1.0 weight for each individual vital (this is a constant value stored in constants.WEB_VITALS_PERFORMANCE_SCORE_WEIGHTS) | ||
- if all webvitals have data, then the sum of all weights is 1 | ||
- normalized_vital_score is obtained through _resolve_web_vital_score_function (see docstring on that function for more details) | ||
""" | ||
vitals = ["lcp", "fcp", "cls", "ttfb", "inp"] | ||
scores = { | ||
vital: Function( | ||
|
@@ -1853,9 +1868,38 @@ def _resolve_total_performance_score_function( | |
for vital in vitals | ||
} | ||
|
||
weights = { | ||
vital: Function( | ||
"if", | ||
[ | ||
Function( | ||
"isZeroOrNull", | ||
[ | ||
Function( | ||
"countIf", | ||
[ | ||
Column("value"), | ||
Function( | ||
"equals", | ||
[ | ||
Column("metric_id"), | ||
self.resolve_metric(f"measurements.score.{vital}"), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
), | ||
0, | ||
constants.WEB_VITALS_PERFORMANCE_SCORE_WEIGHTS[vital], | ||
], | ||
) | ||
for vital in vitals | ||
} | ||
|
||
# TODO: Is there a way to sum more than 2 values at once? | ||
return Function( | ||
"plus", | ||
"divide", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We divide by the maximum possible weight (which will be some value between 0 - 1.0 depending on which vitals are present). This will scale up the result if there are any vitals missing. |
||
[ | ||
Function( | ||
"plus", | ||
|
@@ -1866,17 +1910,54 @@ def _resolve_total_performance_score_function( | |
Function( | ||
"plus", | ||
[ | ||
scores["lcp"], | ||
scores["fcp"], | ||
Function( | ||
"plus", | ||
[ | ||
scores["lcp"], | ||
scores["fcp"], | ||
], | ||
), | ||
scores["cls"], | ||
], | ||
), | ||
scores["cls"], | ||
scores["ttfb"], | ||
], | ||
), | ||
scores["ttfb"], | ||
scores["inp"], | ||
], | ||
), | ||
scores["inp"], | ||
( | ||
Function( | ||
"plus", | ||
[ | ||
Function( | ||
"plus", | ||
[ | ||
Function( | ||
"plus", | ||
[ | ||
Function( | ||
"plus", | ||
[ | ||
weights["lcp"], | ||
weights["fcp"], | ||
], | ||
), | ||
weights["cls"], | ||
], | ||
), | ||
weights["ttfb"], | ||
], | ||
), | ||
weights["inp"], | ||
], | ||
) | ||
if features.has( | ||
"organizations:performance-vitals-handle-missing-webvitals", | ||
self.builder.params.organization, | ||
) | ||
else 1 | ||
), | ||
], | ||
alias, | ||
) | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both a 0 or a null value represent a missing vital score?
This makes sense to me, because you can't ever have an instantaneous load time for any vital metric.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
isZeroOrNull
condition is ran on thecount
of the metric so we're really just checking for the existence of the vital, not the actual value. This is false if anlcp
with value of0ms
is reported, because the count is > 0 in this case.You're also right, it shouldn't really be possible for load time vitals to be 0ms. It might be possible for cls to be 0 though, and that would depend on if the sdk reports it.
Not sure if there's an easier clickhouse or snuba function to do this check, but this works. We'll see if there are any suggestions