From a880b3519b74ffedf7e88738bd468db830ed7e74 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Mon, 3 Apr 2023 14:43:58 -0700 Subject: [PATCH 01/28] create starter file --- src/sentry/sentry_metrics/generic_metrics.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/sentry/sentry_metrics/generic_metrics.py diff --git a/src/sentry/sentry_metrics/generic_metrics.py b/src/sentry/sentry_metrics/generic_metrics.py new file mode 100644 index 00000000000000..8c9d56406ce964 --- /dev/null +++ b/src/sentry/sentry_metrics/generic_metrics.py @@ -0,0 +1,11 @@ +# import register_use_case + +# def emit_counter() -> MetricCounter +# build the Kafka payload +# verify the use case is valid +# send to DummyBackend + +# def emit_set() -> MetricSet + + +# def emit_distribution() -> MetricDistribution From 6332cdc4388171cc731757953ee855bcbb55da93 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 4 Apr 2023 10:03:42 -0700 Subject: [PATCH 02/28] scaffold for interface --- src/sentry/sentry_metrics/generic_metrics.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sentry/sentry_metrics/generic_metrics.py b/src/sentry/sentry_metrics/generic_metrics.py index 8c9d56406ce964..ba9fb7d12dcca6 100644 --- a/src/sentry/sentry_metrics/generic_metrics.py +++ b/src/sentry/sentry_metrics/generic_metrics.py @@ -1,3 +1,5 @@ +from typing import Any + # import register_use_case # def emit_counter() -> MetricCounter @@ -9,3 +11,15 @@ # def emit_distribution() -> MetricDistribution + + +class MetricsInterface: + def __init__(self, metrics: Any) -> None: + # internally used metrics backend + self.__metrics = metrics + + # def emit_counter(): + + # def emit_set(): + + # def emit_distribution(): From a195e70b86741da854ea39352a57d60f398fa040 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 4 Apr 2023 10:52:13 -0700 Subject: [PATCH 03/28] add use case registry, first attempt --- .../sentry_metrics/use_case_registry.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/sentry/sentry_metrics/use_case_registry.py diff --git a/src/sentry/sentry_metrics/use_case_registry.py b/src/sentry/sentry_metrics/use_case_registry.py new file mode 100644 index 00000000000000..305956f96d1fc1 --- /dev/null +++ b/src/sentry/sentry_metrics/use_case_registry.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from typing import Any, Iterator + +_HARDCODED_USE_CASES = {"PERFORMANCE": "performance"} + +_REGISTERED_USE_CASES: dict[str, str] = {} + + +class _UseCaseID(type): + def __getattr__(self, attr: str) -> UseCaseID: + if attr not in _HARDCODED_USE_CASES and attr not in _REGISTERED_USE_CASES: + raise AttributeError(attr) + + return UseCaseID(attr.lower()) + + def __iter__(self) -> Iterator[UseCaseID]: + return iter( + UseCaseID(value) + for value in { + **_HARDCODED_USE_CASES, + **_REGISTERED_USE_CASES, + }.values() + ) + + +class UseCaseID(metaclass=_UseCaseID): + def __init__(self, value: str): + self.value = value + + def __hash__(self) -> int: + return hash(self.value) + + def __eq__(self, other: Any) -> bool: + return isinstance(other, UseCaseID) and other.value == self.value + + def __repr__(self) -> str: + return f"UseCaseID.{self.value.upper()}" + + +def register_use_case(key: str) -> UseCaseID: + _REGISTERED_USE_CASES[key.upper()] = key.lower() + return UseCaseID(key) From 05952a252993515a90ae4b1abcbfad61ae5da9d1 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 4 Apr 2023 11:26:09 -0700 Subject: [PATCH 04/28] create base class for interface --- src/sentry/sentry_metrics/generic_metrics.py | 35 ++++++++------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/sentry/sentry_metrics/generic_metrics.py b/src/sentry/sentry_metrics/generic_metrics.py index ba9fb7d12dcca6..e2f1ab14240533 100644 --- a/src/sentry/sentry_metrics/generic_metrics.py +++ b/src/sentry/sentry_metrics/generic_metrics.py @@ -1,25 +1,18 @@ -from typing import Any +from sentry_metrics.use_case_registry import UseCaseID -# import register_use_case -# def emit_counter() -> MetricCounter -# build the Kafka payload -# verify the use case is valid -# send to DummyBackend +class GenericMetricsInterface: + def emit_counter( + use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags + ): + raise NotImplementedError -# def emit_set() -> MetricSet + def emit_set( + use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags + ): + raise NotImplementedError - -# def emit_distribution() -> MetricDistribution - - -class MetricsInterface: - def __init__(self, metrics: Any) -> None: - # internally used metrics backend - self.__metrics = metrics - - # def emit_counter(): - - # def emit_set(): - - # def emit_distribution(): + def emit_distribution( + use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags + ): + raise NotImplementedError From 8558b04e614e3aac1fcccaf38cc82fcd27227b8c Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 4 Apr 2023 13:14:16 -0700 Subject: [PATCH 05/28] update enum class --- src/sentry/sentry_metrics/kafka_generic_metrics.py | 0 src/sentry/sentry_metrics/use_case_registry.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 src/sentry/sentry_metrics/kafka_generic_metrics.py diff --git a/src/sentry/sentry_metrics/kafka_generic_metrics.py b/src/sentry/sentry_metrics/kafka_generic_metrics.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/src/sentry/sentry_metrics/use_case_registry.py b/src/sentry/sentry_metrics/use_case_registry.py index 305956f96d1fc1..21988d8589c13b 100644 --- a/src/sentry/sentry_metrics/use_case_registry.py +++ b/src/sentry/sentry_metrics/use_case_registry.py @@ -26,6 +26,12 @@ def __iter__(self) -> Iterator[UseCaseID]: class UseCaseID(metaclass=_UseCaseID): def __init__(self, value: str): + if ( + value not in _HARDCODED_USE_CASES.values() + and value not in _REGISTERED_USE_CASES.values() + ): + raise ValueError("Passed use case has not been registered") + self.value = value def __hash__(self) -> int: From 525049f170f2a84c22217778b5b8dac2f281bf9d Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 11:34:55 -0700 Subject: [PATCH 06/28] stub class for generic metrics backend/interface --- src/sentry/sentry_metrics/generic_metrics.py | 18 --------- .../sentry_metrics/kafka_generic_metrics.py | 0 .../sentry_metrics/kafka_metrics_interface.py | 40 +++++++++++++++++++ .../sentry_metrics/metrics_interface.py | 35 ++++++++++++++++ 4 files changed, 75 insertions(+), 18 deletions(-) delete mode 100644 src/sentry/sentry_metrics/generic_metrics.py delete mode 100644 src/sentry/sentry_metrics/kafka_generic_metrics.py create mode 100644 src/sentry/sentry_metrics/kafka_metrics_interface.py create mode 100644 src/sentry/sentry_metrics/metrics_interface.py diff --git a/src/sentry/sentry_metrics/generic_metrics.py b/src/sentry/sentry_metrics/generic_metrics.py deleted file mode 100644 index e2f1ab14240533..00000000000000 --- a/src/sentry/sentry_metrics/generic_metrics.py +++ /dev/null @@ -1,18 +0,0 @@ -from sentry_metrics.use_case_registry import UseCaseID - - -class GenericMetricsInterface: - def emit_counter( - use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags - ): - raise NotImplementedError - - def emit_set( - use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags - ): - raise NotImplementedError - - def emit_distribution( - use_case_id: UseCaseID, org_id: int, project_id: int, metric_name: str, value: int, tags - ): - raise NotImplementedError diff --git a/src/sentry/sentry_metrics/kafka_generic_metrics.py b/src/sentry/sentry_metrics/kafka_generic_metrics.py deleted file mode 100644 index e69de29bb2d1d6..00000000000000 diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py new file mode 100644 index 00000000000000..12588c26f0423e --- /dev/null +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -0,0 +1,40 @@ +from typing import Mapping + +from sentry_metrics.metrics_interface import GenericMetricsBackend + +from sentry.sentry_metrics.use_case_registry import UseCaseID + + +class KafkaGenericMetricsBackend(GenericMetricsBackend): + def __init__(self) -> None: + super().__init__() + + def counter( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + pass + + def set( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + pass + + def distribution( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + pass diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py new file mode 100644 index 00000000000000..4be8136792834b --- /dev/null +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -0,0 +1,35 @@ +from typing import Mapping + +from sentry_metrics.use_case_registry import UseCaseID + + +class GenericMetricsBackend: + def counter( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + raise NotImplementedError + + def set( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + raise NotImplementedError + + def distribution( + use_case_id: UseCaseID, + org_id: int, + project_id: int, + metric_name: str, + value: int, + tags: Mapping[str, str], + ): + raise NotImplementedError From 2d7b9b68dc9d6f8ec349106ce38114b628a82950 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 11:36:42 -0700 Subject: [PATCH 07/28] renames --- src/sentry/sentry_metrics/kafka_metrics_interface.py | 2 +- .../{use_case_registry.py => use_case_id_registry.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/sentry/sentry_metrics/{use_case_registry.py => use_case_id_registry.py} (100%) diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py index 12588c26f0423e..ace78837067cae 100644 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -2,7 +2,7 @@ from sentry_metrics.metrics_interface import GenericMetricsBackend -from sentry.sentry_metrics.use_case_registry import UseCaseID +from sentry.sentry_metrics.use_case_id_registry import UseCaseID class KafkaGenericMetricsBackend(GenericMetricsBackend): diff --git a/src/sentry/sentry_metrics/use_case_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py similarity index 100% rename from src/sentry/sentry_metrics/use_case_registry.py rename to src/sentry/sentry_metrics/use_case_id_registry.py From cf44cafe24bb23c19a7b2913aa4312f9452b5264 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 11:40:51 -0700 Subject: [PATCH 08/28] fix typing --- src/sentry/sentry_metrics/kafka_metrics_interface.py | 6 +++--- src/sentry/sentry_metrics/metrics_interface.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py index ace78837067cae..f57ec114bae793 100644 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -16,7 +16,7 @@ def counter( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: pass def set( @@ -26,7 +26,7 @@ def set( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: pass def distribution( @@ -36,5 +36,5 @@ def distribution( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: pass diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 4be8136792834b..9606e6a87d31fb 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,6 +1,6 @@ from typing import Mapping -from sentry_metrics.use_case_registry import UseCaseID +from sentry_metrics.use_case_id_registry import UseCaseID class GenericMetricsBackend: @@ -11,7 +11,7 @@ def counter( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: raise NotImplementedError def set( @@ -21,7 +21,7 @@ def set( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: raise NotImplementedError def distribution( @@ -31,5 +31,5 @@ def distribution( metric_name: str, value: int, tags: Mapping[str, str], - ): + ) -> None: raise NotImplementedError From f342ded5d017e0242e0b960cc4a0a9c304268ec9 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 11:51:06 -0700 Subject: [PATCH 09/28] fix typing --- src/sentry/sentry_metrics/kafka_metrics_interface.py | 3 +-- src/sentry/sentry_metrics/metrics_interface.py | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py index f57ec114bae793..56080dcf51d1a5 100644 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -1,8 +1,7 @@ from typing import Mapping from sentry_metrics.metrics_interface import GenericMetricsBackend - -from sentry.sentry_metrics.use_case_id_registry import UseCaseID +from sentry_metrics.use_case_id_registry import UseCaseID class KafkaGenericMetricsBackend(GenericMetricsBackend): diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 9606e6a87d31fb..14837cc7bb9659 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -12,7 +12,7 @@ def counter( value: int, tags: Mapping[str, str], ) -> None: - raise NotImplementedError + raise NotImplementedError() def set( use_case_id: UseCaseID, @@ -22,7 +22,7 @@ def set( value: int, tags: Mapping[str, str], ) -> None: - raise NotImplementedError + raise NotImplementedError() def distribution( use_case_id: UseCaseID, @@ -32,4 +32,4 @@ def distribution( value: int, tags: Mapping[str, str], ) -> None: - raise NotImplementedError + raise NotImplementedError() From ce1e05cc1acd71c7eb2562edcfcddff831e4f994 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 12:44:48 -0700 Subject: [PATCH 10/28] fix typing --- src/sentry/sentry_metrics/kafka_metrics_interface.py | 4 ++-- src/sentry/sentry_metrics/metrics_interface.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py index 56080dcf51d1a5..d9baae75d05372 100644 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -1,7 +1,7 @@ from typing import Mapping -from sentry_metrics.metrics_interface import GenericMetricsBackend -from sentry_metrics.use_case_id_registry import UseCaseID +from sentry.sentry_metrics.metrics_interface import GenericMetricsBackend +from sentry.sentry_metrics.use_case_id_registry import UseCaseID class KafkaGenericMetricsBackend(GenericMetricsBackend): diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 14837cc7bb9659..fc574ea9194ad0 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,6 +1,6 @@ from typing import Mapping -from sentry_metrics.use_case_id_registry import UseCaseID +from sentry.sentry_metrics.use_case_id_registry import UseCaseID class GenericMetricsBackend: From 5fa2aafffec4aac33f7f8c19687fe5a198a646c3 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 12:48:29 -0700 Subject: [PATCH 11/28] fix typign again --- src/sentry/sentry_metrics/kafka_metrics_interface.py | 3 +++ src/sentry/sentry_metrics/metrics_interface.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py index d9baae75d05372..e0dda1a9124d90 100644 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ b/src/sentry/sentry_metrics/kafka_metrics_interface.py @@ -9,6 +9,7 @@ def __init__(self) -> None: super().__init__() def counter( + self, use_case_id: UseCaseID, org_id: int, project_id: int, @@ -19,6 +20,7 @@ def counter( pass def set( + self, use_case_id: UseCaseID, org_id: int, project_id: int, @@ -29,6 +31,7 @@ def set( pass def distribution( + self, use_case_id: UseCaseID, org_id: int, project_id: int, diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index fc574ea9194ad0..b3f936529b4380 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -5,6 +5,7 @@ class GenericMetricsBackend: def counter( + self, use_case_id: UseCaseID, org_id: int, project_id: int, @@ -15,6 +16,7 @@ def counter( raise NotImplementedError() def set( + self, use_case_id: UseCaseID, org_id: int, project_id: int, @@ -25,6 +27,7 @@ def set( raise NotImplementedError() def distribution( + self, use_case_id: UseCaseID, org_id: int, project_id: int, From 0c21461cc5294ae4f65397733437fb45d728431a Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 14:28:49 -0700 Subject: [PATCH 12/28] make abstract class --- src/sentry/sentry_metrics/metrics_interface.py | 6 +++++- src/sentry/sentry_metrics/use_case_id_registry.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index b3f936529b4380..465115bb1e69df 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,9 +1,11 @@ +import abc from typing import Mapping from sentry.sentry_metrics.use_case_id_registry import UseCaseID -class GenericMetricsBackend: +class GenericMetricsBackend(metaclass=abc.ABCMeta): + @abc.abstractmethod def counter( self, use_case_id: UseCaseID, @@ -15,6 +17,7 @@ def counter( ) -> None: raise NotImplementedError() + @abc.abstractmethod def set( self, use_case_id: UseCaseID, @@ -26,6 +29,7 @@ def set( ) -> None: raise NotImplementedError() + @abc.abstractmethod def distribution( self, use_case_id: UseCaseID, diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index 21988d8589c13b..e25bdeaf2f49bf 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -47,3 +47,8 @@ def __repr__(self) -> str: def register_use_case(key: str) -> UseCaseID: _REGISTERED_USE_CASES[key.upper()] = key.lower() return UseCaseID(key) + + +register_use_case("riya") + +r = UseCaseID.RIYA From 565411e8b2fe34bc9b5e63b5867581ad200b114a Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 22:44:32 -0700 Subject: [PATCH 13/28] add use case key mapping --- src/sentry/sentry_metrics/use_case_id_registry.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index e25bdeaf2f49bf..77b34e63c298e6 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -1,8 +1,10 @@ from __future__ import annotations -from typing import Any, Iterator +from typing import Any, Iterator, Mapping, Set -_HARDCODED_USE_CASES = {"PERFORMANCE": "performance"} +from sentry.sentry_metrics.configuration import UseCaseKey + +_HARDCODED_USE_CASES = {"PERFORMANCE": "performance", "RELEASE_HEALTH": "release-health"} _REGISTERED_USE_CASES: dict[str, str] = {} @@ -49,6 +51,7 @@ def register_use_case(key: str) -> UseCaseID: return UseCaseID(key) -register_use_case("riya") - -r = UseCaseID.RIYA +METRIC_PATH_MAPPING: Mapping[UseCaseKey, Set[UseCaseID]] = { + UseCaseKey.RELEASE_HEALTH: {UseCaseID.RELEASE_HEALTH}, + UseCaseKey.PERFORMANCE: {UseCaseID.PERFORMANCE}, +} From c6032fdc124890b1f45c54fbf0c7e0673c0e4b2b Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 5 Apr 2023 22:55:59 -0700 Subject: [PATCH 14/28] add mapping function --- src/sentry/sentry_metrics/use_case_id_registry.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index 77b34e63c298e6..40c443eaf05346 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -55,3 +55,9 @@ def register_use_case(key: str) -> UseCaseID: UseCaseKey.RELEASE_HEALTH: {UseCaseID.RELEASE_HEALTH}, UseCaseKey.PERFORMANCE: {UseCaseID.PERFORMANCE}, } + + +def get_metric_path_from_usecase(use_case: UseCaseID) -> UseCaseKey: + for metric_path_key, use_case_list in METRIC_PATH_MAPPING.items(): + if use_case in use_case_list: + return metric_path_key From dd51b80fcb25619e119757430e0a479417885cda Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Thu, 6 Apr 2023 12:56:14 -0700 Subject: [PATCH 15/28] fix typing --- src/sentry/sentry_metrics/use_case_id_registry.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index 40c443eaf05346..e70892023032e2 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Iterator, Mapping, Set +from typing import Any, Iterator, Mapping, Optional, Set from sentry.sentry_metrics.configuration import UseCaseKey @@ -57,7 +57,8 @@ def register_use_case(key: str) -> UseCaseID: } -def get_metric_path_from_usecase(use_case: UseCaseID) -> UseCaseKey: +def get_metric_path_from_usecase(use_case: UseCaseID) -> Optional[UseCaseKey]: for metric_path_key, use_case_list in METRIC_PATH_MAPPING.items(): if use_case in use_case_list: return metric_path_key + return None From 076c2f32065bef0723e828eb1ef55a2f287789d0 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Thu, 6 Apr 2023 13:37:31 -0700 Subject: [PATCH 16/28] remove hardcoded dict --- src/sentry/sentry_metrics/use_case_id_registry.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index e70892023032e2..63a48621493858 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -4,14 +4,15 @@ from sentry.sentry_metrics.configuration import UseCaseKey -_HARDCODED_USE_CASES = {"PERFORMANCE": "performance", "RELEASE_HEALTH": "release-health"} - -_REGISTERED_USE_CASES: dict[str, str] = {} +_REGISTERED_USE_CASES: dict[str, str] = { + "PERFORMANCE": "performance", + "RELEASE_HEALTH": "release-health", +} class _UseCaseID(type): def __getattr__(self, attr: str) -> UseCaseID: - if attr not in _HARDCODED_USE_CASES and attr not in _REGISTERED_USE_CASES: + if attr not in _REGISTERED_USE_CASES: raise AttributeError(attr) return UseCaseID(attr.lower()) @@ -20,7 +21,6 @@ def __iter__(self) -> Iterator[UseCaseID]: return iter( UseCaseID(value) for value in { - **_HARDCODED_USE_CASES, **_REGISTERED_USE_CASES, }.values() ) @@ -28,10 +28,7 @@ def __iter__(self) -> Iterator[UseCaseID]: class UseCaseID(metaclass=_UseCaseID): def __init__(self, value: str): - if ( - value not in _HARDCODED_USE_CASES.values() - and value not in _REGISTERED_USE_CASES.values() - ): + if value not in _REGISTERED_USE_CASES.values(): raise ValueError("Passed use case has not been registered") self.value = value From faa5db4c2f1c02623fe8e985cb90971bd72e8ac5 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Thu, 6 Apr 2023 14:03:36 -0700 Subject: [PATCH 17/28] use regular ABC --- src/sentry/sentry_metrics/metrics_interface.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 465115bb1e69df..56e811d732e1a4 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,11 +1,11 @@ -import abc +from abc import ABC, abstractmethod from typing import Mapping from sentry.sentry_metrics.use_case_id_registry import UseCaseID -class GenericMetricsBackend(metaclass=abc.ABCMeta): - @abc.abstractmethod +class GenericMetricsBackend(ABC): + @abstractmethod def counter( self, use_case_id: UseCaseID, @@ -17,7 +17,7 @@ def counter( ) -> None: raise NotImplementedError() - @abc.abstractmethod + @abstractmethod def set( self, use_case_id: UseCaseID, @@ -29,7 +29,7 @@ def set( ) -> None: raise NotImplementedError() - @abc.abstractmethod + @abstractmethod def distribution( self, use_case_id: UseCaseID, From 73cb703e661eab2de57be729b07fe89d3dca0dd7 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Fri, 7 Apr 2023 16:22:14 -0700 Subject: [PATCH 18/28] remove kafka interface for now --- .../sentry_metrics/kafka_metrics_interface.py | 42 ------------------- .../sentry_metrics/metrics_interface.py | 4 +- 2 files changed, 2 insertions(+), 44 deletions(-) delete mode 100644 src/sentry/sentry_metrics/kafka_metrics_interface.py diff --git a/src/sentry/sentry_metrics/kafka_metrics_interface.py b/src/sentry/sentry_metrics/kafka_metrics_interface.py deleted file mode 100644 index e0dda1a9124d90..00000000000000 --- a/src/sentry/sentry_metrics/kafka_metrics_interface.py +++ /dev/null @@ -1,42 +0,0 @@ -from typing import Mapping - -from sentry.sentry_metrics.metrics_interface import GenericMetricsBackend -from sentry.sentry_metrics.use_case_id_registry import UseCaseID - - -class KafkaGenericMetricsBackend(GenericMetricsBackend): - def __init__(self) -> None: - super().__init__() - - def counter( - self, - use_case_id: UseCaseID, - org_id: int, - project_id: int, - metric_name: str, - value: int, - tags: Mapping[str, str], - ) -> None: - pass - - def set( - self, - use_case_id: UseCaseID, - org_id: int, - project_id: int, - metric_name: str, - value: int, - tags: Mapping[str, str], - ) -> None: - pass - - def distribution( - self, - use_case_id: UseCaseID, - org_id: int, - project_id: int, - metric_name: str, - value: int, - tags: Mapping[str, str], - ) -> None: - pass diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 56e811d732e1a4..214adf9b7e7aeb 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Mapping +from typing import Mapping, Union from sentry.sentry_metrics.use_case_id_registry import UseCaseID @@ -36,7 +36,7 @@ def distribution( org_id: int, project_id: int, metric_name: str, - value: int, + value: Union[int, float], tags: Mapping[str, str], ) -> None: raise NotImplementedError() From 593174842efab211b939363b0becbde2cd7b9f4e Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Fri, 7 Apr 2023 16:27:37 -0700 Subject: [PATCH 19/28] add sequence, float --- src/sentry/sentry_metrics/metrics_interface.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 214adf9b7e7aeb..d576159924ae35 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Mapping, Union +from typing import Mapping, Sequence, Union from sentry.sentry_metrics.use_case_id_registry import UseCaseID @@ -24,7 +24,7 @@ def set( org_id: int, project_id: int, metric_name: str, - value: int, + value: Sequence[Union[int, float]], tags: Mapping[str, str], ) -> None: raise NotImplementedError() @@ -36,7 +36,7 @@ def distribution( org_id: int, project_id: int, metric_name: str, - value: Union[int, float], + value: Sequence[Union[int, float]], tags: Mapping[str, str], ) -> None: raise NotImplementedError() From 63af3430b511d906be46e1cd58ddd8e2c7736daf Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Mon, 10 Apr 2023 13:14:30 -0700 Subject: [PATCH 20/28] add string to sets --- src/sentry/sentry_metrics/metrics_interface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index d576159924ae35..fef53bd93b1d5f 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -24,7 +24,7 @@ def set( org_id: int, project_id: int, metric_name: str, - value: Sequence[Union[int, float]], + value: Sequence[Union[int, str]], tags: Mapping[str, str], ) -> None: raise NotImplementedError() From 4fb4c296d41b2e9a002029b49636ad9f4ba1b74e Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Mon, 10 Apr 2023 14:36:24 -0700 Subject: [PATCH 21/28] fix typing --- src/sentry/sentry_metrics/metrics_interface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index fef53bd93b1d5f..748a7d57d53d35 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -12,7 +12,7 @@ def counter( org_id: int, project_id: int, metric_name: str, - value: int, + value: Union[int, float], tags: Mapping[str, str], ) -> None: raise NotImplementedError() From f2dc61758900b8a75d9c45d2b8fcdae6051a66b9 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Mon, 10 Apr 2023 14:52:42 -0700 Subject: [PATCH 22/28] change class to enum --- .../sentry_metrics/use_case_id_registry.py | 68 +++++++------------ 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index 63a48621493858..bab050327a6931 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -1,61 +1,43 @@ from __future__ import annotations -from typing import Any, Iterator, Mapping, Optional, Set +from enum import Enum +from typing import Mapping from sentry.sentry_metrics.configuration import UseCaseKey -_REGISTERED_USE_CASES: dict[str, str] = { - "PERFORMANCE": "performance", - "RELEASE_HEALTH": "release-health", -} - - -class _UseCaseID(type): - def __getattr__(self, attr: str) -> UseCaseID: - if attr not in _REGISTERED_USE_CASES: - raise AttributeError(attr) - - return UseCaseID(attr.lower()) - def __iter__(self) -> Iterator[UseCaseID]: - return iter( - UseCaseID(value) - for value in { - **_REGISTERED_USE_CASES, - }.values() - ) +class UseCaseID(Enum): + PERFORMANCE = "performance" + RELEASE_HEALTH = "release-health" -class UseCaseID(metaclass=_UseCaseID): - def __init__(self, value: str): - if value not in _REGISTERED_USE_CASES.values(): - raise ValueError("Passed use case has not been registered") +# UseCaseKey will be renamed to MetricPathKey +METRIC_PATH_MAPPING: Mapping[UseCaseID, UseCaseKey] = { + UseCaseID.RELEASE_HEALTH: UseCaseKey.RELEASE_HEALTH, + UseCaseID.PERFORMANCE: UseCaseKey.PERFORMANCE, +} - self.value = value - def __hash__(self) -> int: - return hash(self.value) +def get_metric_path_from_usecase(use_case: UseCaseID) -> UseCaseKey: + return METRIC_PATH_MAPPING[use_case] - def __eq__(self, other: Any) -> bool: - return isinstance(other, UseCaseID) and other.value == self.value - def __repr__(self) -> str: - return f"UseCaseID.{self.value.upper()}" +NAMESPACE_MAPPING: Mapping[UseCaseID, str] = { + UseCaseID.RELEASE_HEALTH: "sessions", + UseCaseID.PERFORMANCE: "transactions", +} -def register_use_case(key: str) -> UseCaseID: - _REGISTERED_USE_CASES[key.upper()] = key.lower() - return UseCaseID(key) +def get_namespace_from_usecase(use_case: UseCaseID) -> str: + if use_case in NAMESPACE_MAPPING: + return NAMESPACE_MAPPING[use_case] + return use_case.value -METRIC_PATH_MAPPING: Mapping[UseCaseKey, Set[UseCaseID]] = { - UseCaseKey.RELEASE_HEALTH: {UseCaseID.RELEASE_HEALTH}, - UseCaseKey.PERFORMANCE: {UseCaseID.PERFORMANCE}, -} +def get_usecase_from_namespace(namespace: str) -> UseCaseID: + for use_case in NAMESPACE_MAPPING: + if NAMESPACE_MAPPING[use_case] == namespace: + return use_case -def get_metric_path_from_usecase(use_case: UseCaseID) -> Optional[UseCaseKey]: - for metric_path_key, use_case_list in METRIC_PATH_MAPPING.items(): - if use_case in use_case_list: - return metric_path_key - return None + return UseCaseID(namespace) From b0d389b9b320ba8133a3ad38b01a2fa9958d2498 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 11 Apr 2023 08:59:55 -0700 Subject: [PATCH 23/28] remove str --- src/sentry/sentry_metrics/metrics_interface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 748a7d57d53d35..ecc135829a5104 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -24,7 +24,7 @@ def set( org_id: int, project_id: int, metric_name: str, - value: Sequence[Union[int, str]], + value: Sequence[int], tags: Mapping[str, str], ) -> None: raise NotImplementedError() From 1cc5cdad570fd439f305a245b589f4d0b7c3be89 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 11 Apr 2023 12:15:01 -0700 Subject: [PATCH 24/28] add unit and docstring --- .../sentry_metrics/metrics_interface.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index ecc135829a5104..ed5a99b56abb0f 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Mapping, Sequence, Union +from typing import Mapping, Optional, Sequence, Union from sentry.sentry_metrics.use_case_id_registry import UseCaseID @@ -13,8 +13,15 @@ def counter( project_id: int, metric_name: str, value: Union[int, float], + unit: Optional[str], tags: Mapping[str, str], ) -> None: + + """ + Used for emitting a counter metric. Ensure that the use_case_id + passed in has been registered in the UseCaseID enum. + """ + raise NotImplementedError() @abstractmethod @@ -25,8 +32,14 @@ def set( project_id: int, metric_name: str, value: Sequence[int], + unit: Optional[str], tags: Mapping[str, str], ) -> None: + + """ + Used for emitting a set metric. Can support a sequence of values. Ensure that the use_case_id + passed in has been registered in the UseCaseID enum. + """ raise NotImplementedError() @abstractmethod @@ -37,6 +50,12 @@ def distribution( project_id: int, metric_name: str, value: Sequence[Union[int, float]], + unit: Optional[str], tags: Mapping[str, str], ) -> None: + + """ + Used for emitting a distribution metric. Can support a sequence of values. Ensure that the use_case_id + passed in has been registered in the UseCaseID enum. + """ raise NotImplementedError() From 7486191e4bc5aae3a40b488cd793c55fb07c4b8a Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 11 Apr 2023 12:15:39 -0700 Subject: [PATCH 25/28] fix spacing --- src/sentry/sentry_metrics/metrics_interface.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index ed5a99b56abb0f..5851225574cdcb 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -37,8 +37,8 @@ def set( ) -> None: """ - Used for emitting a set metric. Can support a sequence of values. Ensure that the use_case_id - passed in has been registered in the UseCaseID enum. + Used for emitting a set metric. Can support a sequence of values. + Ensure that the use_case_id passed in has been registered in the UseCaseID enum. """ raise NotImplementedError() @@ -55,7 +55,7 @@ def distribution( ) -> None: """ - Used for emitting a distribution metric. Can support a sequence of values. Ensure that the use_case_id - passed in has been registered in the UseCaseID enum. + Used for emitting a distribution metric. Can support a sequence of values. + Ensure that the use_case_id passed in has been registered in the UseCaseID enum. """ raise NotImplementedError() From 5db95d7fcf2f1003deff3f028607996105a5b8f3 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Tue, 11 Apr 2023 13:22:26 -0700 Subject: [PATCH 26/28] change docstrings --- src/sentry/sentry_metrics/metrics_interface.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/sentry/sentry_metrics/metrics_interface.py b/src/sentry/sentry_metrics/metrics_interface.py index 5851225574cdcb..fbca6050d4cf89 100644 --- a/src/sentry/sentry_metrics/metrics_interface.py +++ b/src/sentry/sentry_metrics/metrics_interface.py @@ -18,8 +18,9 @@ def counter( ) -> None: """ - Used for emitting a counter metric. Ensure that the use_case_id - passed in has been registered in the UseCaseID enum. + Used for emitting a counter metric for internal use cases only. + Ensure that the use_case_id passed in has been registered + in the UseCaseID enum. """ raise NotImplementedError() @@ -37,8 +38,9 @@ def set( ) -> None: """ - Used for emitting a set metric. Can support a sequence of values. - Ensure that the use_case_id passed in has been registered in the UseCaseID enum. + Used for emitting a set metric for internal use cases only. Can support + a sequence of values. Ensure that the use_case_id passed in has + been registered in the UseCaseID enum. """ raise NotImplementedError() @@ -55,7 +57,8 @@ def distribution( ) -> None: """ - Used for emitting a distribution metric. Can support a sequence of values. - Ensure that the use_case_id passed in has been registered in the UseCaseID enum. + Used for emitting a distribution metric for internal use cases only. Can + support a sequence of values. Ensure that the use_case_id passed in + has been registered in the UseCaseID enum. """ raise NotImplementedError() From e6e01f2f2162e0d22b8d8aeb771c0a05620fba1f Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 12 Apr 2023 11:12:40 -0700 Subject: [PATCH 27/28] remove use case to namespace mapping --- .../sentry_metrics/use_case_id_registry.py | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index bab050327a6931..b01468d1ba06f5 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -20,24 +20,3 @@ class UseCaseID(Enum): def get_metric_path_from_usecase(use_case: UseCaseID) -> UseCaseKey: return METRIC_PATH_MAPPING[use_case] - - -NAMESPACE_MAPPING: Mapping[UseCaseID, str] = { - UseCaseID.RELEASE_HEALTH: "sessions", - UseCaseID.PERFORMANCE: "transactions", -} - - -def get_namespace_from_usecase(use_case: UseCaseID) -> str: - if use_case in NAMESPACE_MAPPING: - return NAMESPACE_MAPPING[use_case] - - return use_case.value - - -def get_usecase_from_namespace(namespace: str) -> UseCaseID: - for use_case in NAMESPACE_MAPPING: - if NAMESPACE_MAPPING[use_case] == namespace: - return use_case - - return UseCaseID(namespace) From 7f20f172d1a5fb61f749275c6b29a45203140639 Mon Sep 17 00:00:00 2001 From: Riya Chakraborty Date: Wed, 12 Apr 2023 11:15:09 -0700 Subject: [PATCH 28/28] change use case id to be the same as namespace always --- src/sentry/sentry_metrics/use_case_id_registry.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index b01468d1ba06f5..619c59417401b7 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -7,14 +7,14 @@ class UseCaseID(Enum): - PERFORMANCE = "performance" - RELEASE_HEALTH = "release-health" + TRANSACTIONS = "transactions" + SESSIONS = "sessions" # UseCaseKey will be renamed to MetricPathKey METRIC_PATH_MAPPING: Mapping[UseCaseID, UseCaseKey] = { - UseCaseID.RELEASE_HEALTH: UseCaseKey.RELEASE_HEALTH, - UseCaseID.PERFORMANCE: UseCaseKey.PERFORMANCE, + UseCaseID.SESSIONS: UseCaseKey.RELEASE_HEALTH, + UseCaseID.TRANSACTIONS: UseCaseKey.PERFORMANCE, }