-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(spans-migration): self hosted spans migration for discover saved queries #106018
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
Open
nikkikapadia
wants to merge
9
commits into
master
Choose a base branch
from
nikki/discover-self-hosted-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4925f4f
self hosted spans migration for discover saved queries
nikkikapadia 0deaddf
Merge branch 'master' into nikki/discover-self-hosted-migration
nikkikapadia a638d64
update migration
nikkikapadia 3bd17f7
add in hint column
nikkikapadia 7b30bd0
Merge branch 'master' into nikki/discover-self-hosted-migration
nikkikapadia e3ee06f
add codeowner coverage
nikkikapadia 3929c59
add more detail
nikkikapadia 73c3d40
cursor fixes:
nikkikapadia 8320b3a
Merge branch 'master' into nikki/discover-self-hosted-migration
nikkikapadia 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 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
98 changes: 98 additions & 0 deletions
98
src/sentry/migrations/1064_discover_to_explore_queries_self_hosted.py
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,98 @@ | ||
| # Generated by Django 5.2.8 on 2025-12-01 16:35 | ||
|
|
||
|
|
||
| import sentry_sdk | ||
| from django.db import migrations | ||
| from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
| from django.db.migrations.state import StateApps | ||
|
|
||
| from sentry.discover.models import DiscoverSavedQuery, DiscoverSavedQueryProject | ||
| from sentry.explore.models import ExploreSavedQueryProject | ||
| from sentry.explore.translation.discover_translation import ( | ||
| translate_discover_query_to_explore_query, | ||
| ) | ||
| from sentry.models.dashboard_widget import TypesClass | ||
| from sentry.new_migrations.migrations import CheckedMigration | ||
| from sentry.utils.query import RangeQuerySetWrapperWithProgressBar | ||
|
|
||
|
|
||
| class DiscoverSavedQueryTypes(TypesClass): | ||
| DISCOVER = 0 | ||
| ERROR_EVENTS = 1 | ||
| """ | ||
| Error side of the split from Discover. | ||
| """ | ||
| TRANSACTION_LIKE = 2 | ||
| """ | ||
| This targets transaction-like data from the split from discover. | ||
| """ | ||
|
|
||
| TYPES = [ | ||
| (DISCOVER, "discover"), | ||
| (ERROR_EVENTS, "error-events"), | ||
| (TRANSACTION_LIKE, "transaction-like"), | ||
| ] | ||
| TYPE_NAMES = [t[1] for t in TYPES] | ||
|
|
||
|
|
||
| def migrate_transactions_discover_queries_self_hosted( | ||
| apps: StateApps, schema_editor: BaseDatabaseSchemaEditor | ||
| ) -> None: | ||
| queryset = DiscoverSavedQuery.objects.filter( | ||
| dataset=DiscoverSavedQueryTypes.TRANSACTION_LIKE | ||
| ).select_related("organization") | ||
|
|
||
| for discover_query in RangeQuerySetWrapperWithProgressBar(queryset): | ||
| try: | ||
| new_explore_query = translate_discover_query_to_explore_query(discover_query) | ||
| if not new_explore_query.projects.exists(): | ||
| discover_projects_qs = DiscoverSavedQueryProject.objects.filter( | ||
| discover_saved_query_id=discover_query.id | ||
| ) | ||
| projects_to_create = [ | ||
| ExploreSavedQueryProject( | ||
| explore_saved_query_id=new_explore_query.id, | ||
| project_id=discover_project.project_id, | ||
| ) | ||
| for discover_project in discover_projects_qs | ||
| ] | ||
| ExploreSavedQueryProject.objects.bulk_create(projects_to_create) | ||
| except Exception as e: | ||
| sentry_sdk.capture_exception(e) | ||
|
|
||
|
|
||
| class Migration(CheckedMigration): | ||
| # This flag is used to mark that a migration shouldn't be automatically run in production. | ||
| # This should only be used for operations where it's safe to run the migration after your | ||
| # code has deployed. So this should not be used for most operations that alter the schema | ||
| # of a table. | ||
| # Here are some things that make sense to mark as post deployment: | ||
| # - Large data migrations. Typically we want these to be run manually so that they can be | ||
| # monitored and not block the deploy for a long period of time while they run. | ||
| # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to | ||
| # run this outside deployments so that we don't block them. Note that while adding an index | ||
| # is a schema change, it's completely safe to run the operation after the code has deployed. | ||
| # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment | ||
|
|
||
| is_post_deployment = True | ||
|
|
||
| dependencies = [ | ||
| ("sentry", "1063_remove_customdynamicsamplingrule"), | ||
| ("discover", "0003_discover_json_field"), | ||
| ("explore", "0006_add_changed_reason_field_explore"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| migrate_transactions_discover_queries_self_hosted, | ||
| reverse_code=migrations.RunPython.noop, | ||
| hints={ | ||
| "tables": [ | ||
| "sentry_discoversavedquery", | ||
| "sentry_discoversavedqueryproject", | ||
| "explore_exploresavedqueryproject", | ||
| "explore_exploresavedquery", | ||
| ] | ||
| }, | ||
| ) | ||
| ] | ||
99 changes: 99 additions & 0 deletions
99
tests/sentry/migrations/test_1064_discover_to_explore_queries_self_hosted.py
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,99 @@ | ||
| from sentry.discover.models import ( | ||
| DiscoverSavedQuery, | ||
| DiscoverSavedQueryProject, | ||
| DiscoverSavedQueryTypes, | ||
| ) | ||
| from sentry.explore.models import ExploreSavedQueryDataset | ||
| from sentry.testutils.cases import SnubaTestCase, TestMigrations | ||
|
|
||
|
|
||
| class MigrateDiscoverQueriesToExploreQueriesSelfHostedTest(TestMigrations, SnubaTestCase): | ||
| migrate_from = "1063_remove_customdynamicsamplingrule" | ||
| migrate_to = "1064_discover_to_explore_queries_self_hosted" | ||
|
|
||
| def setup_before_migration(self, apps): | ||
| User = apps.get_model("sentry", "User") | ||
|
|
||
| self.user = User.objects.create(email="test@sentry.io") | ||
| self.org = self.create_organization(name="Test org", slug="test-org") | ||
| self.project = self.create_project(organization=self.org) | ||
| self.project_ids = [self.project.id] | ||
|
|
||
| self.query = {"fields": ["title", "count()"], "query": ""} | ||
| self.discover_query_with_fields_to_translate = DiscoverSavedQuery.objects.create( | ||
| organization=self.org, | ||
| name="Test query", | ||
| query=self.query, | ||
| dataset=DiscoverSavedQueryTypes.TRANSACTION_LIKE, | ||
| ) | ||
| for project_id in self.project_ids: | ||
| DiscoverSavedQueryProject.objects.create( | ||
| discover_saved_query=self.discover_query_with_fields_to_translate, | ||
| project_id=project_id, | ||
| ) | ||
|
|
||
| self.discover_query_without_projects = DiscoverSavedQuery.objects.create( | ||
| organization=self.org, | ||
| name="Test query", | ||
| query=self.query, | ||
| dataset=DiscoverSavedQueryTypes.TRANSACTION_LIKE, | ||
| ) | ||
|
|
||
| self.dropped_fields_query = { | ||
| "fields": ["id", "title", "count_web_vitals(measurements.lcp,good)"] | ||
| } | ||
| self.discover_query_dropped_fields = DiscoverSavedQuery.objects.create( | ||
| organization=self.org, | ||
| name="Test query", | ||
| query=self.dropped_fields_query, | ||
| dataset=DiscoverSavedQueryTypes.TRANSACTION_LIKE, | ||
| ) | ||
|
|
||
| self.discover_query_errors = DiscoverSavedQuery.objects.create( | ||
| organization=self.org, | ||
| name="Test query", | ||
| query={}, | ||
| dataset=DiscoverSavedQueryTypes.ERROR_EVENTS, | ||
| ) | ||
|
|
||
| def test(self): | ||
| self.discover_query_with_fields_to_translate.refresh_from_db() | ||
| self.discover_query_without_projects.refresh_from_db() | ||
| self.discover_query_dropped_fields.refresh_from_db() | ||
| self.discover_query_errors.refresh_from_db() | ||
|
|
||
| # All TRANSACTION_LIKE queries should get explore queries, even without projects | ||
| # (no projects means "all projects") | ||
| assert self.discover_query_with_fields_to_translate.explore_query is not None | ||
| assert self.discover_query_without_projects.explore_query is not None | ||
| assert self.discover_query_dropped_fields.explore_query is not None | ||
| # ERRORS queries should NOT get explore queries | ||
| assert self.discover_query_errors.explore_query is None | ||
|
|
||
| explore_query_with_translated_fields = ( | ||
| self.discover_query_with_fields_to_translate.explore_query | ||
| ) | ||
| explore_query_dropped_fields = self.discover_query_dropped_fields.explore_query | ||
| explore_query_no_projects = self.discover_query_without_projects.explore_query | ||
|
|
||
| assert ( | ||
| explore_query_with_translated_fields.dataset == ExploreSavedQueryDataset.SEGMENT_SPANS | ||
| ) | ||
| assert explore_query_dropped_fields.dataset == ExploreSavedQueryDataset.SEGMENT_SPANS | ||
| assert explore_query_no_projects.dataset == ExploreSavedQueryDataset.SEGMENT_SPANS | ||
|
|
||
| assert explore_query_with_translated_fields.query["query"][0]["fields"] == [ | ||
| "id", | ||
| "transaction", | ||
| ] | ||
| assert explore_query_with_translated_fields.query["query"][0]["aggregateField"] == [ | ||
| {"yAxes": ["count(span.duration)"], "chartType": 2} | ||
| ] | ||
| assert explore_query_with_translated_fields.query["query"][0]["query"] == "is_transaction:1" | ||
| assert explore_query_with_translated_fields.projects.count() == len(self.project_ids) | ||
|
|
||
| assert explore_query_dropped_fields.query["query"][0]["fields"] == ["id", "transaction"] | ||
| assert explore_query_dropped_fields.query["query"][0]["aggregateField"] == [] | ||
| assert explore_query_dropped_fields.query["query"][0]["query"] == "is_transaction:1" | ||
|
|
||
| assert explore_query_no_projects.projects.count() == 0 |
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.