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(grouping): Add metrics for issue merging and unmerging #52919

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/sentry/api/endpoints/group_hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sentry.api.serializers import EventSerializer, serialize
from sentry.models import GroupHash
from sentry.tasks.unmerge import unmerge
from sentry.utils import metrics
from sentry.utils.snuba import raw_query


Expand Down Expand Up @@ -61,6 +62,13 @@ def delete(self, request: Request, group) -> Response:
if not hash_list:
return Response()

metrics.incr(
"grouping.unmerge_issues",
Copy link
Member

Choose a reason for hiding this comment

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

I'm really interested on knowing how many people actually unmerge.

sample_rate=1.0,
# We assume that if someone's merged groups, they were all from the same platform
tags={"platform": group.platform or "unknown"},
)

unmerge.delay(
group.project_id, group.id, None, hash_list, request.user.id if request.user else None
)
Expand Down
23 changes: 23 additions & 0 deletions src/sentry/api/helpers/group_index/update.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import re
from collections import defaultdict
from datetime import datetime, timedelta
from typing import Any, Dict, Mapping, MutableMapping, Sequence
from urllib.parse import urlparse

import rest_framework
from django.db import IntegrityError, transaction
Expand Down Expand Up @@ -650,6 +652,27 @@ def update_groups(
if len(projects) > 1:
return Response({"detail": "Merging across multiple projects is not supported"})

referer = urlparse(request.META.get("HTTP_REFERER", "")).path
issue_stream_regex = r"^(\/organizations\/[^\/]+)?\/issues\/$"
similar_issues_tab_regex = r"^(\/organizations\/[^\/]+)?\/issues\/\d+\/similar\/$"

metrics.incr(
"grouping.merge_issues",
sample_rate=1.0,
tags={
# We assume that if someone's merging groups, they're from the same platform
"platform": group_list[0].platform or "unknown",
# TODO: It's probably cleaner to just send this value from the front end
"referer": (
"issue stream"
if re.search(issue_stream_regex, referer)
else "similar issues tab"
if re.search(similar_issues_tab_regex, referer)
else "unknown"
),
},
)

result["merge"] = handle_merge(group_list, project_lookup, acting_user)

inbox = result.get("inbox", None)
Expand Down
14 changes: 11 additions & 3 deletions tests/sentry/api/endpoints/test_group_hashes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
from unittest.mock import patch
from urllib.parse import urlencode

from sentry.eventstream.snuba import SnubaEventStream
Expand Down Expand Up @@ -94,7 +95,7 @@ def test_return_multiple_hashes(self):
def test_unmerge(self):
self.login_as(user=self.user)

group = self.create_group()
group = self.create_group(platform="javascript")
lobsterkatie marked this conversation as resolved.
Show resolved Hide resolved

hashes = [
GroupHash.objects.create(project=group.project, group=group, hash=hash)
Expand All @@ -108,5 +109,12 @@ def test_unmerge(self):
]
)

response = self.client.delete(url, format="json")
assert response.status_code == 202, response.content
with patch("sentry.api.endpoints.group_hashes.metrics.incr") as mock_metrics_incr:
response = self.client.delete(url, format="json")

assert response.status_code == 202, response.content
mock_metrics_incr.assert_any_call(
"grouping.unmerge_issues",
sample_rate=1.0,
tags={"platform": "javascript"},
)
98 changes: 97 additions & 1 deletion tests/sentry/api/helpers/test_group_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import pytest
from django.http import QueryDict
Expand Down Expand Up @@ -264,6 +264,102 @@ def test_ignore_with_substatus_archived_until_escalating(self, send_robust: Mock
assert not GroupInbox.objects.filter(group=group).exists()


class MergeGroupsTest(TestCase):
@patch("sentry.api.helpers.group_index.update.handle_merge")
def test_simple(self, mock_handle_merge: MagicMock):
group_ids = [self.create_group().id, self.create_group().id]
project = self.project

request = self.make_request(method="PUT")
request.user = self.user
request.data = {"merge": 1}
request.GET = {"id": group_ids, "project": [project.id]}

update_groups(request, group_ids, [project], self.organization.id, search_fn=Mock())

call_args = mock_handle_merge.call_args.args

assert len(call_args) == 3
# Have to convert to ids because first argument is a queryset
assert [group.id for group in call_args[0]] == group_ids
assert call_args[1] == {project.id: project}
assert call_args[2] == self.user

@patch("sentry.api.helpers.group_index.update.handle_merge")
def test_multiple_projects(self, mock_handle_merge: MagicMock):
project1 = self.create_project()
project2 = self.create_project()
projects = [project1, project2]
project_ids = [project.id for project in projects]

group_ids = [
self.create_group(project1).id,
self.create_group(project2).id,
]

request = self.make_request(method="PUT")
request.user = self.user
request.data = {"merge": 1}
request.GET = {"id": group_ids, "project": project_ids}

response = update_groups(
request, group_ids, projects, self.organization.id, search_fn=Mock()
)

assert response.data == {"detail": "Merging across multiple projects is not supported"}
assert mock_handle_merge.call_count == 0

def test_metrics(self):
for referer, expected_referer_tag in [
("https://sentry.io/organizations/dogsaregreat/issues/", "issue stream"),
("https://dogsaregreat.sentry.io/issues/", "issue stream"),
(
"https://sentry.io/organizations/dogsaregreat/issues/12311121/similar/",
"similar issues tab",
),
(
"https://dogsaregreat.sentry.io/issues/12311121/similar/",
"similar issues tab",
),
(
"https://sentry.io/organizations/dogsaregreat/some/other/path/",
"unknown",
),
(
"https://dogsaregreat.sentry.io/some/other/path/",
"unknown",
),
(
"",
"unknown",
),
]:

group_ids = [
self.create_group(platform="javascript").id,
self.create_group(platform="javascript").id,
]
project = self.project

request = self.make_request(method="PUT")
request.user = self.user
request.data = {"merge": 1}
request.GET = {"id": group_ids, "project": [project.id]}
request.META = {"HTTP_REFERER": referer}

with patch("sentry.api.helpers.group_index.update.metrics.incr") as mock_metrics_incr:
update_groups(request, group_ids, [project], self.organization.id, search_fn=Mock())

mock_metrics_incr.assert_any_call(
"grouping.merge_issues",
sample_rate=1.0,
tags={
"platform": "javascript",
"referer": expected_referer_tag,
},
)


class TestHandleIsSubscribed(TestCase):
def setUp(self) -> None:
self.group = self.create_group()
Expand Down