-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(metrics): Write all session metrics in test [INGEST-386] (#28667)
Write more session metrics to snuba in metrics test case, mimicking the extract_session_metrics function in relay. This prepares for https://getsentry.atlassian.net/browse/INGEST-386.
- Loading branch information
Showing
3 changed files
with
99 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,64 @@ | ||
from typing import Optional | ||
import itertools | ||
from collections import defaultdict | ||
from typing import DefaultDict, Dict, Optional | ||
|
||
from sentry.models import Organization | ||
|
||
from .base import StringIndexer, UseCase | ||
|
||
_STRINGS = { | ||
"abnormal": 0, | ||
"crashed": 1, | ||
"environment": 2, | ||
"errored": 3, | ||
"healthy": 4, | ||
"production": 5, | ||
"release": 6, | ||
"session.duration": 7, | ||
"session.status": 8, | ||
"session": 9, | ||
"staging": 10, | ||
"user": 11, | ||
"init": 12, | ||
} | ||
_REVERSE = {v: k for k, v in _STRINGS.items()} | ||
|
||
|
||
class MockIndexer(StringIndexer): | ||
""" | ||
Mock string indexer | ||
""" | ||
_STRINGS = ( | ||
"abnormal", | ||
"crashed", | ||
"environment", | ||
"errored", | ||
"healthy", | ||
"production", | ||
"release", | ||
"session.duration", | ||
"session.status", | ||
"session", | ||
"staging", | ||
"user", | ||
"init", | ||
"session.error", | ||
) | ||
|
||
|
||
class SimpleIndexer(StringIndexer): | ||
|
||
"""Simple indexer with in-memory store. Do not use in production.""" | ||
|
||
def __init__(self) -> None: | ||
self._counter = itertools.count() | ||
self._strings: DefaultDict[str, int] = defaultdict(self._counter.__next__) | ||
self._reverse: Dict[int, str] = {} | ||
|
||
def record(self, organization: Organization, use_case: UseCase, string: str) -> int: | ||
"""Mock indexer cannot record.""" | ||
raise NotImplementedError() | ||
# NOTE: Ignores ``use_case`` for simplicity. | ||
return self._record(string) | ||
|
||
def resolve(self, organization: Organization, use_case: UseCase, string: str) -> Optional[int]: | ||
# NOTE: Ignores ``use_case`` for simplicity. | ||
return _STRINGS.get(string) | ||
return self._strings.get(string) | ||
|
||
def reverse_resolve( | ||
self, organization: Organization, use_case: UseCase, id: int | ||
) -> Optional[str]: | ||
# NOTE: Ignores ``use_case`` for simplicity. | ||
return _REVERSE.get(id) | ||
return self._reverse.get(id) | ||
|
||
def _record(self, string: str) -> int: | ||
index = self._strings[string] | ||
self._reverse[index] = string | ||
return index | ||
|
||
|
||
class MockIndexer(SimpleIndexer): | ||
""" | ||
Mock string indexer. Comes with a prepared set of strings. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
for string in _STRINGS: | ||
self._record(string) |
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