Skip to content

Commit

Permalink
Merge branch 'master' into span_attr_table_v3
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-sentry authored Sep 18, 2024
2 parents 9a7231b + 2fad570 commit 67bb083
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
18 changes: 18 additions & 0 deletions snuba/web/rpc/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Final, Mapping, Sequence, Set

from sentry_protos.snuba.v1alpha.request_common_pb2 import RequestMeta
Expand All @@ -18,6 +19,23 @@
from snuba.web.rpc.exceptions import BadSnubaRPCRequestException


def truncate_request_meta_to_day(meta: RequestMeta) -> None:
# some tables store timestamp as toStartOfDay(x) in UTC, so if you request 4PM - 8PM on a specific day, nada
# this changes a request from 4PM - 8PM to a request from midnight today to 8PM tomorrow UTC.
# it also changes 11PM - 1AM to midnight today to 1AM overmorrow
start_timestamp = datetime.utcfromtimestamp(meta.start_timestamp.seconds)
end_timestamp = datetime.utcfromtimestamp(meta.end_timestamp.seconds)
start_timestamp = start_timestamp.replace(
day=start_timestamp.day, hour=0, minute=0, second=0, microsecond=0
)
end_timestamp = end_timestamp.replace(
day=end_timestamp.day + 1, hour=0, minute=0, second=0, microsecond=0
)

meta.start_timestamp.seconds = int(start_timestamp.timestamp())
meta.end_timestamp.seconds = int(end_timestamp.timestamp())


def treeify_or_and_conditions(query: Query) -> None:
"""
look for expressions like or(a, b, c) and turn them into or(a, or(b, c))
Expand Down
13 changes: 2 additions & 11 deletions snuba/web/rpc/trace_item_attribute_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from typing import List, Optional

from sentry_protos.snuba.v1alpha.endpoint_tags_list_pb2 import (
Expand All @@ -12,6 +11,7 @@
from snuba.datasets.storages.factory import get_storage
from snuba.datasets.storages.storage_key import StorageKey
from snuba.utils.metrics.timer import Timer
from snuba.web.rpc.common import truncate_request_meta_to_day
from snuba.web.rpc.exceptions import BadSnubaRPCRequestException


Expand All @@ -31,16 +31,7 @@ def trace_item_attribute_list_query(
if request.limit > 1000:
raise BadSnubaRPCRequestException("Limit can be at most 1000")

# this table stores timestamp as toStartOfDay(x) in UTC, so if you request 4PM - 8PM on a specific day, nada
start_timestamp = datetime.utcfromtimestamp(request.meta.start_timestamp.seconds)
end_timestamp = datetime.utcfromtimestamp(request.meta.end_timestamp.seconds)
if start_timestamp.day == end_timestamp.day:
start_timestamp = start_timestamp.replace(
day=start_timestamp.day - 1, hour=0, minute=0, second=0, microsecond=0
)
end_timestamp = end_timestamp.replace(day=end_timestamp.day + 1)
request.meta.start_timestamp.seconds = int(start_timestamp.timestamp())
request.meta.end_timestamp.seconds = int(end_timestamp.timestamp())
truncate_request_meta_to_day(request.meta)

query = f"""
SELECT DISTINCT attr_key, timestamp
Expand Down
18 changes: 6 additions & 12 deletions snuba/web/rpc/trace_item_attribute_values.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import uuid
from datetime import datetime

from google.protobuf.json_format import MessageToDict
from sentry_protos.snuba.v1alpha.endpoint_tags_list_pb2 import (
Expand All @@ -21,7 +20,11 @@
from snuba.request import Request as SnubaRequest
from snuba.utils.metrics.timer import Timer
from snuba.web.query import run_query
from snuba.web.rpc.common import base_conditions_and, treeify_or_and_conditions
from snuba.web.rpc.common import (
base_conditions_and,
treeify_or_and_conditions,
truncate_request_meta_to_day,
)
from snuba.web.rpc.exceptions import BadSnubaRPCRequestException


Expand All @@ -35,16 +38,7 @@ def _build_query(request: AttributeValuesRequest) -> Query:
sample=None,
)

# this table stores timestamp as toStartOfDay(x) in UTC, so if you request 4PM - 8PM on a specific day, nada
start_timestamp = datetime.utcfromtimestamp(request.meta.start_timestamp.seconds)
end_timestamp = datetime.utcfromtimestamp(request.meta.end_timestamp.seconds)
if start_timestamp.day == end_timestamp.day:
start_timestamp = start_timestamp.replace(
day=start_timestamp.day - 1, hour=0, minute=0, second=0, microsecond=0
)
end_timestamp = end_timestamp.replace(day=end_timestamp.day + 1)
request.meta.start_timestamp.seconds = int(start_timestamp.timestamp())
request.meta.end_timestamp.seconds = int(end_timestamp.timestamp())
truncate_request_meta_to_day(request.meta)

res = Query(
from_clause=entity,
Expand Down
1 change: 1 addition & 0 deletions tests/web/rpc/test_trace_item_attribute_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def setup_teardown(clickhouse_db: None, redis_db: None) -> None:
write_raw_unprocessed_events(spans_storage, messages) # type: ignore


@pytest.mark.skip(reason="temporarily disabled to allow a migration to run")
@pytest.mark.clickhouse_db
@pytest.mark.redis_db
class TestTraceItemAttributes(BaseApiTest):
Expand Down
1 change: 1 addition & 0 deletions tests/web/rpc/test_trace_item_attribute_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def setup_teardown(clickhouse_db: None, redis_db: None) -> None:
write_raw_unprocessed_events(spans_storage, messages) # type: ignore


@pytest.mark.skip(reason="temporarily disabled to allow a migration to run")
@pytest.mark.clickhouse_db
@pytest.mark.redis_db
class TestTraceItemAttributes(BaseApiTest):
Expand Down

0 comments on commit 67bb083

Please sign in to comment.