123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590 |
- from __future__ import annotations
- import time
- from datetime import UTC, datetime, timedelta
- from unittest import mock
- import pytest
- from django.utils import timezone
- from sentry.release_health.base import OverviewStat
- from sentry.release_health.metrics import MetricsReleaseHealthBackend
- from sentry.release_health.sessions import SessionsReleaseHealthBackend
- from sentry.sentry_metrics.use_case_id_registry import UseCaseID
- from sentry.snuba.sessions import _make_stats
- from sentry.testutils.cases import BaseMetricsTestCase, SnubaTestCase, TestCase
- pytestmark = pytest.mark.sentry_metrics
- def parametrize_backend(cls):
- """
- hack to parametrize test-classes by backend. Ideally we'd move
- over to pytest-style tests so we can use `pytest.mark.parametrize`, but
- hopefully we won't have more than one backend in the future.
- """
- assert isinstance(cls.backend, SessionsReleaseHealthBackend)
- newcls = type(
- f"{cls.__name__}MetricsLayer",
- (BaseMetricsTestCase, cls),
- {
- "__doc__": f"Repeat tests from {cls} with metrics layer",
- "backend": MetricsReleaseHealthBackend(),
- "adjust_interval": True, # HACK interval adjustment for new MetricsLayer implementation
- },
- )
- globals()[newcls.__name__] = newcls
- return cls
- def format_timestamp(dt):
- if not isinstance(dt, datetime):
- dt = datetime.fromtimestamp(dt)
- return dt.strftime("%Y-%m-%dT%H:%M:%S+00:00")
- def make_24h_stats(ts, adjust_start=False):
- ret_val = _make_stats(datetime.fromtimestamp(ts, UTC), 3600, 24)
- if adjust_start:
- # HACK this adds another interval at the beginning in accordance with the new way of calculating intervals
- # https://www.notion.so/sentry/Metrics-Layer-get_intervals-bug-dce140607d054201a5e6629b070cb969
- ret_val.insert(0, [ret_val[0][0] - 3600, 0])
- return ret_val
- @parametrize_backend
- class SnubaSessionsTest(TestCase, SnubaTestCase):
- backend = SessionsReleaseHealthBackend()
- adjust_interval = False # HACK interval adjustment for new MetricsLayer implementation
- def setUp(self):
- super().setUp()
- self.received = time.time()
- self.session_started = time.time() // 60 * 60
- self.session_release = "foo@1.0.0"
- self.session_crashed_release = "foo@2.0.0"
- session_1 = "5d52fd05-fcc9-4bf3-9dc9-267783670341"
- session_2 = "5e910c1a-6941-460e-9843-24103fb6a63c"
- session_3 = "a148c0c5-06a2-423b-8901-6b43b812cf82"
- user_1 = "39887d89-13b2-4c84-8c23-5d13d2102666"
- self.store_session(
- self.build_session(
- distinct_id=user_1,
- session_id=session_1,
- status="exited",
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- self.store_session(
- self.build_session(
- distinct_id=user_1,
- session_id=session_2,
- release=self.session_release,
- environment="prod",
- duration=None,
- started=self.session_started,
- received=self.received,
- )
- )
- self.store_session(
- self.build_session(
- distinct_id=user_1,
- session_id=session_2,
- seq=1,
- duration=30,
- status="exited",
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- self.store_session(
- self.build_session(
- distinct_id=user_1,
- session_id=session_3,
- status="crashed",
- release=self.session_crashed_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- def test_get_oldest_health_data_for_releases(self):
- data = self.backend.get_oldest_health_data_for_releases(
- [(self.project.id, self.session_release)]
- )
- assert data == {
- (self.project.id, self.session_release): format_timestamp(
- self.session_started // 3600 * 3600
- )
- }
- def test_check_has_health_data(self):
- data = self.backend.check_has_health_data(
- [(self.project.id, self.session_release), (self.project.id, "dummy-release")]
- )
- assert data == {(self.project.id, self.session_release)}
- def test_check_has_health_data_without_releases_should_include_sessions_lte_90_days(self):
- """
- Test that ensures that `check_has_health_data` returns a set of projects that has health
- data within the last 90d if only a list of project ids is provided and any project with
- session data earlier than 90 days should be included
- """
- project2 = self.create_project(
- name="Bar2",
- slug="bar2",
- teams=[self.team],
- fire_project_created=True,
- organization=self.organization,
- )
- self.store_session(
- self.build_session(
- **{
- "project_id": project2.id,
- "org_id": project2.organization_id,
- "status": "exited",
- }
- )
- )
- data = self.backend.check_has_health_data([self.project.id, project2.id])
- assert data == {self.project.id, project2.id}
- def test_check_has_health_data_does_not_crash_when_sending_projects_list_as_set(self):
- data = self.backend.check_has_health_data({self.project.id})
- assert data == {self.project.id}
- def test_get_project_releases_by_stability(self):
- # Add an extra session with a different `distinct_id` so that sorting by users
- # is stable
- self.store_session(
- self.build_session(
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- for scope in "sessions", "users":
- data = self.backend.get_project_releases_by_stability(
- [self.project.id], offset=0, limit=100, scope=scope, stats_period="24h"
- )
- assert data == [
- (self.project.id, self.session_release),
- (self.project.id, self.session_crashed_release),
- ]
- def test_get_project_releases_by_stability_for_crash_free_sort(self):
- """
- Test that ensures that using crash free rate sort options, returns a list of ASC releases
- according to the chosen crash_free sort option
- """
- # add another user to session_release to make sure that they are sorted correctly
- self.store_session(
- self.build_session(
- status="exited",
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- for scope in "crash_free_sessions", "crash_free_users":
- data = self.backend.get_project_releases_by_stability(
- [self.project.id], offset=0, limit=100, scope=scope, stats_period="24h"
- )
- assert data == [
- (self.project.id, self.session_crashed_release),
- (self.project.id, self.session_release),
- ]
- def test_get_project_releases_by_stability_for_releases_with_users_data(self):
- """
- Test that ensures if releases contain no users data, then those releases should not be
- returned on `users` and `crash_free_users` sorts
- """
- self.store_session(
- self.build_session(
- distinct_id=None,
- release="release-with-no-users",
- environment="prod",
- started=self.session_started,
- received=self.received,
- )
- )
- data = self.backend.get_project_releases_by_stability(
- [self.project.id], offset=0, limit=100, scope="users", stats_period="24h"
- )
- assert set(data) == {
- (self.project.id, self.session_release),
- (self.project.id, self.session_crashed_release),
- }
- data = self.backend.get_project_releases_by_stability(
- [self.project.id], offset=0, limit=100, scope="crash_free_users", stats_period="24h"
- )
- assert set(data) == {
- (self.project.id, self.session_crashed_release),
- (self.project.id, self.session_release),
- }
- def test_get_release_adoption(self):
- data = self.backend.get_release_adoption(
- [
- (self.project.id, self.session_release),
- (self.project.id, self.session_crashed_release),
- (self.project.id, "dummy-release"),
- ]
- )
- assert data == {
- (self.project.id, self.session_release): {
- "sessions_24h": 2,
- "users_24h": 1,
- "adoption": 100.0,
- "sessions_adoption": 66.66666666666666,
- "project_sessions_24h": 3,
- "project_users_24h": 1,
- },
- (self.project.id, self.session_crashed_release): {
- "sessions_24h": 1,
- "users_24h": 1,
- "adoption": 100.0,
- "sessions_adoption": 33.33333333333333,
- "project_sessions_24h": 3,
- "project_users_24h": 1,
- },
- }
- def test_get_release_adoption_lowered(self):
- self.store_session(
- self.build_session(
- release=self.session_crashed_release,
- environment="prod",
- status="crashed",
- started=self.session_started,
- received=self.received,
- )
- )
- data = self.backend.get_release_adoption(
- [
- (self.project.id, self.session_release),
- (self.project.id, self.session_crashed_release),
- (self.project.id, "dummy-release"),
- ]
- )
- assert data == {
- (self.project.id, self.session_release): {
- "sessions_24h": 2,
- "users_24h": 1,
- "adoption": 50.0,
- "sessions_adoption": 50.0,
- "project_sessions_24h": 4,
- "project_users_24h": 2,
- },
- (self.project.id, self.session_crashed_release): {
- "sessions_24h": 2,
- "users_24h": 2,
- "adoption": 100.0,
- "sessions_adoption": 50.0,
- "project_sessions_24h": 4,
- "project_users_24h": 2,
- },
- }
- def test_fetching_release_sessions_time_bounds_for_different_release(self):
- """
- Test that ensures only session bounds for releases are calculated according
- to their respective release
- """
- # Same release session
- self.store_session(
- self.build_session(
- release=self.session_release,
- environment="prod",
- status="exited",
- started=self.session_started - 3600 * 2,
- received=self.received - 3600 * 2,
- )
- )
- # Different release session
- self.store_session(
- self.build_session(
- release=self.session_crashed_release,
- environment="prod",
- status="crashed",
- started=self.session_started - 3600 * 2,
- received=self.received - 3600 * 2,
- )
- )
- expected_formatted_lower_bound = (
- datetime.fromtimestamp(self.session_started - 3600 * 2)
- .replace(minute=0)
- .isoformat()[:19]
- + "Z"
- )
- expected_formatted_upper_bound = (
- datetime.fromtimestamp(self.session_started).replace(minute=0).isoformat()[:19] + "Z"
- )
- # Test for self.session_release
- data = self.backend.get_release_sessions_time_bounds(
- project_id=self.project.id,
- release=self.session_release,
- org_id=self.organization.id,
- environments=["prod"],
- )
- assert data == {
- "sessions_lower_bound": expected_formatted_lower_bound,
- "sessions_upper_bound": expected_formatted_upper_bound,
- }
- # Test for self.session_crashed_release
- data = self.backend.get_release_sessions_time_bounds(
- project_id=self.project.id,
- release=self.session_crashed_release,
- org_id=self.organization.id,
- environments=["prod"],
- )
- assert data == {
- "sessions_lower_bound": expected_formatted_lower_bound,
- "sessions_upper_bound": expected_formatted_upper_bound,
- }
- def test_fetching_release_sessions_time_bounds_for_different_release_with_no_sessions(self):
- """
- Test that ensures if no sessions are available for a specific release then the bounds
- should be returned as None
- """
- data = self.backend.get_release_sessions_time_bounds(
- project_id=self.project.id,
- release="different_release",
- org_id=self.organization.id,
- environments=["prod"],
- )
- assert data == {
- "sessions_lower_bound": None,
- "sessions_upper_bound": None,
- }
- def test_get_crash_free_breakdown(self):
- start = timezone.now() - timedelta(days=4)
- # it should work with and without environments
- for environments in [None, ["prod"]]:
- data = self.backend.get_crash_free_breakdown(
- project_id=self.project.id,
- release=self.session_release,
- start=start,
- environments=environments,
- )
- # Last returned date is generated within function, should be close to now:
- last_date = data[-1]["date"]
- assert timezone.now() - last_date < timedelta(seconds=1)
- assert data == [
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=1),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=2),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": 100.0,
- "crash_free_users": 100.0,
- "total_sessions": 2,
- "total_users": 1,
- "date": mock.ANY, # tested above
- },
- ]
- data = self.backend.get_crash_free_breakdown(
- project_id=self.project.id,
- release=self.session_crashed_release,
- start=start,
- environments=["prod"],
- )
- assert data == [
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=1),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=2),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": 0.0,
- "crash_free_users": 0.0,
- "total_sessions": 1,
- "total_users": 1,
- "date": mock.ANY,
- },
- ]
- data = self.backend.get_crash_free_breakdown(
- project_id=self.project.id,
- release="non-existing",
- start=start,
- environments=["prod"],
- )
- assert data == [
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=1),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=2),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "total_sessions": 0,
- "total_users": 0,
- "date": mock.ANY,
- },
- ]
- def test_basic_release_model_adoptions(self):
- """
- Test that the basic (project,release) data is returned
- """
- proj_id = self.project.id
- data = self.backend.get_changed_project_release_model_adoptions([proj_id])
- assert set(data) == {(proj_id, "foo@1.0.0"), (proj_id, "foo@2.0.0")}
- def test_old_release_model_adoptions(self):
- """
- Test that old entries (older that 72 h) are not returned
- """
- _100h = 100 * 60 * 60 # 100 hours in seconds
- proj_id = self.project.id
- self.store_session(
- self.build_session(
- release="foo@3.0.0",
- environment="prod",
- status="crashed",
- started=self.session_started - _100h,
- received=self.received - 3600 * 2,
- )
- )
- data = self.backend.get_changed_project_release_model_adoptions([proj_id])
- assert set(data) == {(proj_id, "foo@1.0.0"), (proj_id, "foo@2.0.0")}
- def test_multi_proj_release_model_adoptions(self):
- """Test that the api works with multiple projects"""
- proj_id = self.project.id
- new_proj_id = proj_id + 1
- self.store_session(
- self.build_session(
- project_id=new_proj_id,
- release="foo@3.0.0",
- environment="prod",
- status="crashed",
- started=self.session_started,
- received=self.received - 3600 * 2,
- )
- )
- data = self.backend.get_changed_project_release_model_adoptions([proj_id, new_proj_id])
- assert set(data) == {
- (proj_id, "foo@1.0.0"),
- (proj_id, "foo@2.0.0"),
- (new_proj_id, "foo@3.0.0"),
- }
- @staticmethod
- def _add_timestamps_to_series(series, start: datetime):
- one_day = 24 * 60 * 60
- day0 = one_day * int(start.timestamp() / one_day)
- def ts(days: int) -> int:
- return day0 + days * one_day
- return [(ts(i + 1), data) for i, data in enumerate(series)]
- def _test_get_project_release_stats(
- self, stat: OverviewStat, release: str, expected_series, expected_totals
- ):
- end = timezone.now()
- start = end - timedelta(days=4)
- stats, totals = self.backend.get_project_release_stats(
- self.project.id,
- release=release,
- stat=stat,
- rollup=86400,
- start=start,
- end=end,
- )
- # one system returns lists instead of tuples
- normed = [(ts, data) for ts, data in stats]
- assert normed == self._add_timestamps_to_series(expected_series, start)
- assert totals == expected_totals
- def test_get_project_release_stats_users(self):
- self._test_get_project_release_stats(
- "users",
- self.session_release,
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": 45.0,
- "duration_p90": 57.0,
- "users": 1,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 1,
- },
- ],
- {
- "users": 1,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 1,
- },
- )
- def test_get_project_release_stats_users_crashed(self):
- self._test_get_project_release_stats(
- "users",
- self.session_crashed_release,
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 1,
- "users_abnormal": 0,
- "users_crashed": 1,
- "users_errored": 0,
- "users_healthy": 0,
- },
- ],
- {
- "users": 1,
- "users_abnormal": 0,
- "users_crashed": 1,
- "users_errored": 0,
- "users_healthy": 0,
- },
- )
- def test_get_project_release_stats_sessions(self):
- self._test_get_project_release_stats(
- "sessions",
- self.session_release,
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": 45.0,
- "duration_p90": 57.0,
- "sessions": 2,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 2,
- },
- ],
- {
- "sessions": 2,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 2,
- },
- )
- def test_get_project_release_stats_sessions_crashed(self):
- self._test_get_project_release_stats(
- "sessions",
- self.session_crashed_release,
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 1,
- "sessions_abnormal": 0,
- "sessions_crashed": 1,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- ],
- {
- "sessions": 1,
- "sessions_abnormal": 0,
- "sessions_crashed": 1,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- )
- def test_get_project_release_stats_no_sessions(self):
- """
- Test still returning correct data when no sessions are available
- :return:
- """
- self._test_get_project_release_stats(
- "sessions",
- "INEXISTENT-RELEASE",
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- ],
- {
- "sessions": 0,
- "sessions_abnormal": 0,
- "sessions_crashed": 0,
- "sessions_errored": 0,
- "sessions_healthy": 0,
- },
- )
- def test_get_project_release_stats_no_users(self):
- self._test_get_project_release_stats(
- "users",
- "INEXISTENT-RELEASE",
- [
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- {
- "duration_p50": None,
- "duration_p90": None,
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- ],
- {
- "users": 0,
- "users_abnormal": 0,
- "users_crashed": 0,
- "users_errored": 0,
- "users_healthy": 0,
- },
- )
- @parametrize_backend
- class GetCrashFreeRateTestCase(TestCase, SnubaTestCase):
- """
- TestClass that tests that `get_current_and_previous_crash_free_rates` returns the correct
- `currentCrashFreeRate` and `previousCrashFreeRate` for each project
- TestData:
- Project 1:
- In the last 24h -> 2 Exited Sessions / 2 Total Sessions -> 100% Crash free rate
- In the previous 24h (>24h & <48h) -> 2 Exited + 1 Crashed Sessions / 3 Sessions -> 66.7%
- Project 2:
- In the last 24h -> 1 Exited + 1 Crashed / 2 Total Sessions -> 50% Crash free rate
- In the previous 24h (>24h & <48h) -> 0 Sessions -> None
- Project 3:
- In the last 24h -> 0 Sessions -> None
- In the previous 24h (>24h & <48h) -> 4 Exited + 1 Crashed / 5 Total Sessions -> 80%
- """
- backend = SessionsReleaseHealthBackend()
- def setUp(self):
- super().setUp()
- self.session_started = time.time() // 60 * 60
- self.session_started_gt_24_lt_48 = self.session_started - 30 * 60 * 60
- self.project2 = self.create_project(
- name="Bar2",
- slug="bar2",
- teams=[self.team],
- fire_project_created=True,
- organization=self.organization,
- )
- self.project3 = self.create_project(
- name="Bar3",
- slug="bar3",
- teams=[self.team],
- fire_project_created=True,
- organization=self.organization,
- )
- # Project 1
- for _ in range(0, 2):
- self.store_session(
- self.build_session(
- **{
- "project_id": self.project.id,
- "org_id": self.project.organization_id,
- "status": "exited",
- }
- )
- )
- for idx in range(0, 3):
- status = "exited"
- if idx == 2:
- status = "crashed"
- self.store_session(
- self.build_session(
- **{
- "project_id": self.project.id,
- "org_id": self.project.organization_id,
- "status": status,
- "started": self.session_started_gt_24_lt_48,
- }
- )
- )
- # Project 2
- for i in range(0, 2):
- status = "exited"
- if i == 1:
- status = "crashed"
- self.store_session(
- self.build_session(
- **{
- "project_id": self.project2.id,
- "org_id": self.project2.organization_id,
- "status": status,
- }
- )
- )
- # Project 3
- for i in range(0, 5):
- status = "exited"
- if i == 4:
- status = "crashed"
- self.store_session(
- self.build_session(
- **{
- "project_id": self.project3.id,
- "org_id": self.project3.organization_id,
- "status": status,
- "started": self.session_started_gt_24_lt_48,
- }
- )
- )
- def test_get_current_and_previous_crash_free_rates(self):
- now = timezone.now().replace(minute=15, second=23)
- last_24h_start = now - 24 * timedelta(hours=1)
- last_48h_start = now - 2 * 24 * timedelta(hours=1)
- data = self.backend.get_current_and_previous_crash_free_rates(
- org_id=self.organization.id,
- project_ids=[self.project.id, self.project2.id, self.project3.id],
- current_start=last_24h_start,
- current_end=now,
- previous_start=last_48h_start,
- previous_end=last_24h_start,
- rollup=3600,
- )
- assert data == {
- self.project.id: {
- "currentCrashFreeRate": 100,
- "previousCrashFreeRate": 66.66666666666667,
- },
- self.project2.id: {"currentCrashFreeRate": 50.0, "previousCrashFreeRate": None},
- self.project3.id: {"currentCrashFreeRate": None, "previousCrashFreeRate": 80.0},
- }
- def test_get_current_and_previous_crash_free_rates_with_zero_sessions(self):
- now = timezone.now().replace(minute=15, second=23)
- last_48h_start = now - 2 * 24 * timedelta(hours=1)
- last_72h_start = now - 3 * 24 * timedelta(hours=1)
- last_96h_start = now - 4 * 24 * timedelta(hours=1)
- data = self.backend.get_current_and_previous_crash_free_rates(
- org_id=self.organization.id,
- project_ids=[self.project.id],
- current_start=last_72h_start,
- current_end=last_48h_start,
- previous_start=last_96h_start,
- previous_end=last_72h_start,
- rollup=3600,
- )
- assert data == {
- self.project.id: {
- "currentCrashFreeRate": None,
- "previousCrashFreeRate": None,
- },
- }
- @parametrize_backend
- class GetProjectReleasesCountTest(TestCase, SnubaTestCase):
- backend = SessionsReleaseHealthBackend()
- def test_empty(self):
- # Test no errors when no session data
- org = self.create_organization()
- proj = self.create_project(organization=org)
- assert (
- self.backend.get_project_releases_count(
- org.id, [proj.id], "crash_free_users", stats_period="14d"
- )
- == 0
- )
- def test_with_other_metrics(self):
- if not self.backend.is_metrics_based():
- return
- assert isinstance(self, BaseMetricsTestCase)
- # Test no errors when no session data
- org = self.create_organization()
- proj = self.create_project(organization=org)
- # Insert a different set metric:
- for value in 1, 2, 3:
- self.store_metric(
- org_id=org.id,
- project_id=proj.id,
- name="foobarbaz", # any other metric ID
- timestamp=int(time.time()),
- tags={},
- type="set",
- value=value,
- use_case_id=UseCaseID.SESSIONS,
- )
- assert (
- self.backend.get_project_releases_count(
- org.id, [proj.id], "crash_free_users", stats_period="14d"
- )
- == 0
- )
- def test(self):
- project_release_1 = self.create_release(self.project)
- other_project = self.create_project()
- other_project_release_1 = self.create_release(other_project)
- self.bulk_store_sessions(
- [
- self.build_session(
- environment=self.environment.name, release=project_release_1.version
- ),
- self.build_session(
- environment="staging",
- project_id=other_project.id,
- release=other_project_release_1.version,
- ),
- ]
- )
- assert (
- self.backend.get_project_releases_count(
- self.organization.id, [self.project.id], "sessions"
- )
- == 1
- )
- assert (
- self.backend.get_project_releases_count(
- self.organization.id, [self.project.id], "users"
- )
- == 1
- )
- assert (
- self.backend.get_project_releases_count(
- self.organization.id, [self.project.id, other_project.id], "sessions"
- )
- == 2
- )
- assert (
- self.backend.get_project_releases_count(
- self.organization.id,
- [self.project.id, other_project.id],
- "users",
- )
- == 2
- )
- assert (
- self.backend.get_project_releases_count(
- self.organization.id,
- [self.project.id, other_project.id],
- "sessions",
- environments=[self.environment.name],
- )
- == 1
- )
- @parametrize_backend
- class CheckReleasesHaveHealthDataTest(TestCase, SnubaTestCase):
- backend = SessionsReleaseHealthBackend()
- def run_test(self, expected, projects, releases, start=None, end=None):
- if not start:
- start = datetime.now() - timedelta(days=1)
- if not end:
- end = datetime.now()
- assert self.backend.check_releases_have_health_data(
- self.organization.id,
- [p.id for p in projects],
- [r.version for r in releases],
- start,
- end,
- ) == {v.version for v in expected}
- def test_empty(self):
- # Test no errors when no session data
- project_release_1 = self.create_release(self.project)
- self.run_test([], [self.project], [project_release_1])
- def test(self):
- other_project = self.create_project()
- release_1 = self.create_release(
- self.project, version="1", additional_projects=[other_project]
- )
- release_2 = self.create_release(other_project, version="2")
- self.bulk_store_sessions(
- [
- self.build_session(release=release_1),
- self.build_session(project_id=other_project, release=release_1),
- self.build_session(project_id=other_project, release=release_2),
- ]
- )
- self.run_test([release_1], [self.project], [release_1])
- self.run_test([release_1], [self.project], [release_1, release_2])
- self.run_test([release_1], [other_project], [release_1])
- self.run_test([release_1, release_2], [other_project], [release_1, release_2])
- self.run_test([release_1, release_2], [self.project, other_project], [release_1, release_2])
- @parametrize_backend
- class CheckNumberOfSessions(TestCase, SnubaTestCase):
- backend = SessionsReleaseHealthBackend()
- def setUp(self):
- super().setUp()
- self.dev_env = self.create_environment(name="development", project=self.project)
- self.prod_env = self.create_environment(name="production", project=self.project)
- self.test_env = self.create_environment(name="test", project=self.project)
- self.another_project = self.create_project()
- self.third_project = self.create_project()
- # now_dt should be set to 17:40 of some day not in the future and (system time - now_dt)
- # must be less than 90 days for the metrics DB TTL
- ONE_DAY_AGO = timezone.now() - timedelta(days=1)
- self.now_dt = ONE_DAY_AGO.replace(hour=17, minute=40, second=0)
- self._5_min_ago_dt = self.now_dt - timedelta(minutes=5)
- self._30_min_ago_dt = self.now_dt - timedelta(minutes=30)
- self._1_h_ago_dt = self.now_dt - timedelta(hours=1)
- self._2_h_ago_dt = self.now_dt - timedelta(hours=2)
- self._3_h_ago_dt = self.now_dt - timedelta(hours=3)
- self.now = self.now_dt.timestamp()
- self._5_min_ago = self._5_min_ago_dt.timestamp()
- self._30_min_ago = self._30_min_ago_dt.timestamp()
- self._1_h_ago = self._1_h_ago_dt.timestamp()
- self._2_h_ago = self._2_h_ago_dt.timestamp()
- self._3_h_ago = self._3_h_ago_dt.timestamp()
- def test_no_sessions(self):
- """
- Tests that when there are no sessions the function behaves and returns 0
- """
- actual = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=None,
- rollup=60,
- start=self._30_min_ago_dt,
- end=self.now_dt,
- )
- assert 0 == actual
- def test_sessions_in_environment(self):
- """
- Tests that it correctly picks up the sessions for the selected environment
- in the selected time, not counting other environments and other times
- """
- dev = self.dev_env.name
- prod = self.prod_env.name
- self.bulk_store_sessions(
- [
- self.build_session(
- environment=dev, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(environment=prod, received=self._2_h_ago, started=self._2_h_ago),
- ]
- )
- actual = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=self.prod_env.id,
- rollup=60,
- start=self._1_h_ago_dt,
- end=self.now_dt,
- )
- assert actual == 2
- def test_environment_without_sessions(self):
- """
- We should get zero sessions, even if the environment name has not been indexed
- by the metrics indexer.
- """
- env_without_sessions = self.create_environment(
- name="this_has_no_sessions", project=self.project
- )
- self.bulk_store_sessions(
- [
- self.build_session(
- environment=self.prod_env.name,
- received=self._5_min_ago,
- started=self._5_min_ago,
- ),
- self.build_session(
- environment=None, received=self._5_min_ago, started=self._5_min_ago
- ),
- ]
- )
- count_env_all = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=None,
- rollup=60,
- start=self._1_h_ago_dt,
- end=self.now_dt,
- )
- assert count_env_all == 2
- count_env_new = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=env_without_sessions.id,
- rollup=60,
- start=self._1_h_ago_dt,
- end=self.now_dt,
- )
- assert count_env_new == 0
- def test_sessions_in_all_environments(self):
- """
- When the environment is not specified sessions from all environments are counted
- """
- dev = self.dev_env.name
- prod = self.prod_env.name
- self.bulk_store_sessions(
- [
- self.build_session(
- environment=dev, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(environment=prod, received=self._2_h_ago, started=self._2_h_ago),
- self.build_session(environment=dev, received=self._2_h_ago, started=self._2_h_ago),
- ]
- )
- actual = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=None,
- rollup=60,
- start=self._1_h_ago_dt,
- end=self.now_dt,
- )
- assert actual == 3
- def test_sessions_from_multiple_projects(self):
- """
- Only sessions from the specified project are considered
- """
- dev = self.dev_env.name
- prod = self.prod_env.name
- self.bulk_store_sessions(
- [
- self.build_session(
- environment=dev, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod,
- received=self._5_min_ago,
- project_id=self.another_project.id,
- started=self._5_min_ago,
- ),
- ]
- )
- actual = self.backend.get_project_sessions_count(
- project_id=self.project.id,
- environment_id=None,
- rollup=60,
- start=self._1_h_ago_dt,
- end=self.now_dt,
- )
- assert actual == 2
- def test_sessions_per_project_no_sessions(self):
- """
- Tests that no sessions are returned
- """
- actual = self.backend.get_num_sessions_per_project(
- project_ids=[self.project.id, self.another_project.id],
- environment_ids=None,
- rollup=60,
- start=self._30_min_ago_dt,
- end=self.now_dt,
- )
- assert [] == actual
- def test_sesions_per_project_multiple_projects(self):
- dev = self.dev_env.name
- prod = self.prod_env.name
- test = self.test_env.name
- p1 = self.project
- p2 = self.another_project
- p3 = self.third_project
- self.bulk_store_sessions(
- [
- # counted in p1
- self.build_session(
- environment=dev, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=prod, received=self._5_min_ago, started=self._5_min_ago
- ),
- self.build_session(
- environment=dev, received=self._30_min_ago, started=self._30_min_ago
- ),
- # ignored in p1
- # ignored env
- self.build_session(
- environment=test, received=self._30_min_ago, started=self._30_min_ago
- ),
- # too old
- self.build_session(environment=prod, received=self._3_h_ago, started=self._3_h_ago),
- # counted in p2
- self.build_session(
- environment=dev,
- received=self._5_min_ago,
- project_id=p2.id,
- started=self._5_min_ago,
- ),
- # ignored in p2
- # ignored env
- self.build_session(
- environment=test,
- received=self._5_min_ago,
- project_id=p2.id,
- started=self._5_min_ago,
- ),
- # too old
- self.build_session(
- environment=prod,
- received=self._3_h_ago,
- project_id=p2.id,
- started=self._3_h_ago,
- ),
- # ignored p3
- self.build_session(
- environment=dev,
- received=self._5_min_ago,
- project_id=p3.id,
- started=self._5_min_ago,
- ),
- ]
- )
- actual = self.backend.get_num_sessions_per_project(
- project_ids=[self.project.id, self.another_project.id],
- environment_ids=[self.dev_env.id, self.prod_env.id],
- rollup=60,
- start=self._2_h_ago_dt,
- end=self.now_dt,
- )
- assert set(actual) == {(p1.id, 3), (p2.id, 1)}
- eids_tests: tuple[list[int] | None, ...] = ([], None)
- for eids in eids_tests:
- actual = self.backend.get_num_sessions_per_project(
- project_ids=[self.project.id, self.another_project.id],
- environment_ids=eids,
- rollup=60,
- start=self._2_h_ago_dt,
- end=self.now_dt,
- )
- assert set(actual) == {(p1.id, 4), (p2.id, 2)}
- @parametrize_backend
- class InitWithoutUserTestCase(TestCase, SnubaTestCase):
- backend = SessionsReleaseHealthBackend()
- def setUp(self):
- super().setUp()
- self.received = time.time()
- self.session_started = time.time() // 60 * 60
- self.session_release = "foo@1.0.0"
- session_1 = "5d52fd05-fcc9-4bf3-9dc9-267783670341"
- session_2 = "5e910c1a-6941-460e-9843-24103fb6a63c"
- session_3 = "a148c0c5-06a2-423b-8901-6b43b812cf82"
- user_1 = "39887d89-13b2-4c84-8c23-5d13d2102666"
- user_2 = "39887d89-13b2-4c84-8c23-5d13d2102667"
- user_3 = "39887d89-13b2-4c84-8c23-5d13d2102668"
- self.bulk_store_sessions(
- [
- self.build_session(
- distinct_id=user_1,
- session_id=session_1,
- status="exited",
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- ),
- self.build_session(
- distinct_id=user_2,
- session_id=session_2,
- status="crashed",
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- ),
- # session_3 initial update: no user ID
- self.build_session(
- distinct_id=None,
- session_id=session_3,
- status="ok",
- seq=0,
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- ),
- # session_3 subsequent update: user ID is here!
- self.build_session(
- distinct_id=user_3,
- session_id=session_3,
- status="ok",
- seq=123,
- release=self.session_release,
- environment="prod",
- started=self.session_started,
- received=self.received,
- ),
- ]
- )
- def test_get_release_adoption(self):
- data = self.backend.get_release_adoption(
- [
- (self.project.id, self.session_release),
- ]
- )
- inner = data[(self.project.id, self.session_release)]
- assert inner["users_24h"] == 3
- def test_get_release_health_data_overview_users(self):
- data = self.backend.get_release_health_data_overview(
- [
- (self.project.id, self.session_release),
- ],
- summary_stats_period="24h",
- health_stats_period="24h",
- stat="users",
- )
- inner = data[(self.project.id, self.session_release)]
- assert inner["total_users"] == 3
- assert inner["crash_free_users"] == 66.66666666666667
- def test_get_crash_free_breakdown(self):
- start = timezone.now() - timedelta(days=4)
- data = self.backend.get_crash_free_breakdown(
- project_id=self.project.id,
- release=self.session_release,
- start=start,
- environments=["prod"],
- )
- # Last returned date is generated within function, should be close to now:
- last_date = data[-1]["date"]
- assert timezone.now() - last_date < timedelta(seconds=1)
- assert data == [
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=1),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": None,
- "crash_free_users": None,
- "date": start + timedelta(days=2),
- "total_sessions": 0,
- "total_users": 0,
- },
- {
- "crash_free_sessions": 66.66666666666667,
- "crash_free_users": 66.66666666666667,
- "total_sessions": 3,
- "total_users": 3,
- "date": mock.ANY, # tested above
- },
- ]
- def test_get_project_release_stats_users(self):
- end = timezone.now()
- start = end - timedelta(days=4)
- stats, totals = self.backend.get_project_release_stats(
- self.project.id,
- release=self.session_release,
- stat="users",
- rollup=86400,
- start=start,
- end=end,
- )
- assert stats[3][1] == {
- "duration_p50": 60.0,
- "duration_p90": 60.0,
- "users": 3,
- "users_abnormal": 0,
- "users_crashed": 1,
- "users_errored": 0,
- "users_healthy": 2,
- }
|