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

feat(anomaly detection): warn users if less than one week of historical data is available (backend) #75214

Merged
merged 9 commits into from
Aug 5, 2024

Conversation

mifu67
Copy link
Contributor

@mifu67 mifu67 commented Jul 30, 2024

If snuba returns fewer than 7 days worth of historical data, then surface a warning.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jul 30, 2024
@mifu67 mifu67 force-pushed the mifu67/alert-creation-warning branch from ef6f4b2 to 051a28f Compare July 30, 2024 00:04
@mifu67 mifu67 closed this Jul 30, 2024
@mifu67 mifu67 deleted the mifu67/alert-creation-warning branch July 30, 2024 00:06
@mifu67 mifu67 restored the mifu67/alert-creation-warning branch July 30, 2024 00:07
@mifu67 mifu67 reopened this Jul 30, 2024
@mifu67
Copy link
Contributor Author

mifu67 commented Jul 30, 2024

ack accidentally branched off of a branch ;_;

Copy link

codecov bot commented Jul 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.18%. Comparing base (97bd9c7) to head (777553f).
Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #75214   +/-   ##
=======================================
  Coverage   78.18%   78.18%           
=======================================
  Files        6808     6808           
  Lines      302899   302924   +25     
  Branches    52128    52134    +6     
=======================================
+ Hits       236818   236850   +32     
+ Misses      59692    59685    -7     
  Partials     6389     6389           
Files Coverage Δ
...cidents/endpoints/organization_alert_rule_index.py 98.01% <100.00%> (+0.06%) ⬆️
src/sentry/seer/anomaly_detection/store_data.py 83.33% <100.00%> (+4.95%) ⬆️

... and 11 files with indirect coverage changes

@mifu67 mifu67 requested a review from ceorourke July 31, 2024 20:09
@mifu67 mifu67 changed the title feat(anomaly detection): warn users if less than one week of historical data is available feat(anomaly detection): warn users if less than one week of historical data is available (backend) Jul 31, 2024
@mifu67 mifu67 marked this pull request as ready for review July 31, 2024 21:41
@mifu67 mifu67 requested a review from a team as a code owner July 31, 2024 21:41
Copy link
Contributor

@nhsiehgit nhsiehgit left a comment

Choose a reason for hiding this comment

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

few comments

if not indices_with_results:
return start, end
else:
start = indices_with_results[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh grab the first and the last because the data is already ordered is that right?
Maybe we should add some quick validations in here for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we're looping through the data in order, so start is guaranteed to be <= end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't be able to test the validation—should I still add it?

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a quick assertion here so that it would raise an exception 🤷

could just be a quick assert start < end
This way it will raise in case this is ever false

src/sentry/seer/anomaly_detection/store_data.py Outdated Show resolved Hide resolved
MIN_DAYS = 7
data_start_index, data_end_index = get_start_and_end(historical_data)
if data_start_index == -1:
resp.status = 202
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok - RE: comments above, lets just throw an exception if there's not enough data.
This should be a loud failure, rather than a seeming success

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that we do create the alert rule—it just won't detect any anomalies until Seer has at least a week of data. Should we still have an exception in this case?

Copy link
Member

Choose a reason for hiding this comment

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

^^ Yes this is just a warning to the user that it can't fire for some period of time until we've collected the data. We still want to create it and then we'll be periodically sending updated data to Seer

Copy link
Contributor

Choose a reason for hiding this comment

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

got it, that makes sense!

Commented above - rather than modifying the response and returning the response,
Thoughts on simply returning a boolean for successful backfill or not?

we're not utilizing the rest of the response, and modifying the response is unexpected (for ex. mocking seer response w/ 200, but expecting a 202?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can do that

maybeeee?

backend stuff

smol change

logic fixed (tests are gonna fail don't come for me)

buggies

fix stuff and add test

cleanup

test fixed (?)

fix failing test and address some comments

another test

fix up more tests
@mifu67 mifu67 force-pushed the mifu67/alert-creation-warning branch from 777553f to 9e2ebcf Compare August 1, 2024 22:42
@mifu67 mifu67 marked this pull request as draft August 2, 2024 18:10
@mifu67 mifu67 marked this pull request as ready for review August 2, 2024 18:12
Copy link
Contributor

@nhsiehgit nhsiehgit left a comment

Choose a reason for hiding this comment

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

few comments!

@@ -106,6 +106,7 @@ def create_metric_alert(self, request, organization, project=None):
},
data=data,
)

Copy link
Contributor

Choose a reason for hiding this comment

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

🤷

src/sentry/incidents/logic.py Outdated Show resolved Hide resolved
@@ -652,7 +652,10 @@ def create_alert_rule(
)

try:
Copy link
Contributor

Choose a reason for hiding this comment

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

👌

So we've covered create_alert_rule,
Lets also be sure to capture update_alert_rule as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll work in that in the next PR after Colleen's changes to update_alert_rule land

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave a TODO in the update method so we don't forget it?

src/sentry/incidents/subscription_processor.py Outdated Show resolved Hide resolved
if not indices_with_results:
return start, end
else:
start = indices_with_results[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a quick assertion here so that it would raise an exception 🤷

could just be a quick assert start < end
This way it will raise in case this is ever false

src/sentry/seer/anomaly_detection/store_data.py Outdated Show resolved Hide resolved
MIN_DAYS = 7
data_start_index, data_end_index = get_start_and_end(historical_data)
if data_start_index == -1:
resp.status = 202
Copy link
Contributor

Choose a reason for hiding this comment

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

got it, that makes sense!

Commented above - rather than modifying the response and returning the response,
Thoughts on simply returning a boolean for successful backfill or not?

we're not utilizing the rest of the response, and modifying the response is unexpected (for ex. mocking seer response w/ 200, but expecting a 202?)

self.alert_rule.save()
else:
# we don't need to check if the alert should fire if the alert can't fire yet
continue
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to continue iterating through this - like would there be a situation in which the next potential_anomaly would change the result of self.has_enough_data()? If not, we can just break here (which I realize is also a little silly since we expect one anomaly, but 🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, but I also don't know, which is why I did continue instead of break. If only one anomaly is returned, like how it's spec'd in Seer, then they accomplish the same thing. I think continue is just a little safer.

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Would Seer ever actually return any potential anomalies if we don't have enough data?
This check may actually be unnecessary? (but safe to have anyways)

Copy link
Contributor

@nhsiehgit nhsiehgit left a comment

Choose a reason for hiding this comment

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

hell yea!

a few nit comments, but this is looking sharp!

@@ -652,7 +652,10 @@ def create_alert_rule(
)

try:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave a TODO in the update method so we don't forget it?

src/sentry/incidents/logic.py Show resolved Hide resolved
self.alert_rule.save()
else:
# we don't need to check if the alert should fire if the alert can't fire yet
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Would Seer ever actually return any potential anomalies if we don't have enough data?
This check may actually be unnecessary? (but safe to have anyways)

src/sentry/seer/anomaly_detection/store_data.py Outdated Show resolved Hide resolved
src/sentry/seer/anomaly_detection/store_data.py Outdated Show resolved Hide resolved
src/sentry/seer/anomaly_detection/store_data.py Outdated Show resolved Hide resolved
@mifu67 mifu67 merged commit c8103fe into master Aug 5, 2024
48 checks passed
@mifu67 mifu67 deleted the mifu67/alert-creation-warning branch August 5, 2024 17:33
Copy link

sentry-io bot commented Aug 6, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ TimeoutError /api/0/organizations/{organization_id_or_slug}/... View Issue

Did you find this useful? React with a 👍 or 👎

@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants