-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(issues-platform): Start writing occurrences to platform (#45712)
Use system and project options set in #45709 Fixes #43975 --------- Co-authored-by: Dan Fuller <[email protected]>
- Loading branch information
Showing
37 changed files
with
394 additions
and
11 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from atexit import register | ||
from collections import deque | ||
from concurrent import futures | ||
from concurrent.futures import Future | ||
from typing import Deque | ||
|
||
from arroyo import Topic | ||
from arroyo.backends.kafka import KafkaPayload, KafkaProducer, build_kafka_configuration | ||
from arroyo.types import BrokerValue | ||
from django.conf import settings | ||
|
||
from sentry.issues.issue_occurrence import IssueOccurrence | ||
from sentry.utils import json | ||
from sentry.utils.kafka_config import get_kafka_producer_cluster_options | ||
|
||
occurrence_producer = None | ||
|
||
|
||
def get_occurrence_producer() -> KafkaProducer: | ||
global occurrence_producer | ||
if occurrence_producer is None: | ||
cluster_name = settings.KAFKA_TOPICS[settings.KAFKA_INGEST_OCCURRENCES]["cluster"] | ||
producer_config = get_kafka_producer_cluster_options(cluster_name) | ||
producer_config.pop("compression.type", None) | ||
producer_config.pop("message.max.bytes", None) | ||
occurrence_producer = KafkaProducer( | ||
build_kafka_configuration(default_config=producer_config) | ||
) | ||
return occurrence_producer | ||
|
||
|
||
def produce_occurrence_to_kafka(occurrence: IssueOccurrence) -> None: | ||
if settings.SENTRY_EVENTSTREAM != "sentry.eventstream.kafka.KafkaEventStream": | ||
# If we're not running Kafka then we're just in dev. Skip producing here and just log for | ||
# debugging. | ||
logging.info( | ||
"Attempted to produce occurrence to Kafka, but Kafka isn't running", | ||
extra={"occurrence": occurrence}, | ||
) | ||
return | ||
payload = KafkaPayload(None, json.dumps(occurrence.to_dict()).encode("utf-8"), []) | ||
occurrence_producer = get_occurrence_producer() | ||
future = occurrence_producer.produce(Topic(settings.KAFKA_INGEST_OCCURRENCES), payload) | ||
|
||
track_occurrence_producer_futures(future) | ||
|
||
|
||
occurrence_producer_futures: Deque[Future[BrokerValue[KafkaPayload]]] = deque() | ||
|
||
|
||
def track_occurrence_producer_futures(future: Future[BrokerValue[KafkaPayload]]) -> None: | ||
global occurrence_producer_futures | ||
occurrence_producer_futures.append(future) | ||
if len(occurrence_producer_futures) >= settings.SENTRY_ISSUE_PLATFORM_FUTURES_MAX_LIMIT: | ||
try: | ||
future = occurrence_producer_futures.popleft() | ||
future.result() | ||
except IndexError: | ||
return | ||
|
||
|
||
def handle_occurrence_producer() -> None: | ||
futures.wait(occurrence_producer_futures) | ||
if occurrence_producer: | ||
occurrence_producer.close() | ||
|
||
|
||
register(handle_occurrence_producer) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Union | ||
|
||
from sentry import options | ||
from sentry.issues.grouptype import ( | ||
GroupCategory, | ||
PerformanceNPlusOneGroupType, | ||
get_group_type_by_type_id, | ||
) | ||
from sentry.issues.issue_occurrence import IssueOccurrence, IssueOccurrenceData | ||
from sentry.models import Project | ||
from sentry.models.group import Group | ||
from sentry.utils.performance_issues.performance_problem import PerformanceProblem | ||
|
||
|
||
def can_create_group( | ||
entity: Union[IssueOccurrence, IssueOccurrenceData, PerformanceProblem, Group], project: Project | ||
) -> bool: | ||
if isinstance(entity, dict): | ||
group_type = get_group_type_by_type_id(entity["type"]) | ||
elif isinstance(entity, Group): | ||
group_type = entity.issue_type | ||
else: | ||
group_type = entity.type | ||
return bool( | ||
group_type.category != GroupCategory.PERFORMANCE.value | ||
or ( | ||
# create N+1 db query issues first | ||
group_type.type_id == PerformanceNPlusOneGroupType.type_id | ||
# system-wide option | ||
and options.get("performance.issues.create_issues_through_platform", False) | ||
# more-granular per-project option | ||
and project.get_option("sentry:performance_issue_create_issue_through_platform", False) | ||
) | ||
) | ||
|
||
|
||
def write_occurrence_to_platform(performance_problem: PerformanceProblem, project: Project) -> bool: | ||
return bool( | ||
# handle only N+1 db query detector first | ||
performance_problem.type.type_id == PerformanceNPlusOneGroupType.type_id | ||
# system-wide option | ||
and options.get("performance.issues.send_to_issues_platform", False) | ||
# more-granular per-project option | ||
and project.get_option("sentry:performance_issue_send_to_issues_platform", False) | ||
) |
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
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
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
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
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
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
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.