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(eap): implement backend for "what tag values exist" RPC #6304

Merged
merged 9 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from snuba.web.rpc.exceptions import BadSnubaRPCRequestException


def tags_list_query(
def trace_items_attributes_query(
request: TraceItemAttributesRequest, _timer: Optional[Timer] = None
) -> TraceItemAttributesResponse:
if request.type == AttributeKey.Type.TYPE_STRING:
Expand Down
115 changes: 115 additions & 0 deletions snuba/web/rpc/trace_item_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import uuid
colin-sentry marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime

from google.protobuf.json_format import MessageToDict
from sentry_protos.snuba.v1alpha.endpoint_tags_list_pb2 import (
AttributeValuesRequest,
AttributeValuesResponse,
)

from snuba.attribution.appid import AppID
from snuba.attribution.attribution_info import AttributionInfo
from snuba.datasets.entities.entity_key import EntityKey
from snuba.datasets.entities.factory import get_entity
from snuba.datasets.pluggable_dataset import PluggableDataset
from snuba.query import OrderBy, OrderByDirection, SelectedExpression
from snuba.query.data_source.simple import Entity
from snuba.query.dsl import Functions as f
from snuba.query.dsl import column, literal, literals_array
from snuba.query.logical import Query
from snuba.query.query_settings import HTTPQuerySettings
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.exceptions import BadSnubaRPCRequestException


def _build_query(request: AttributeValuesRequest) -> Query:
if request.limit > 1000:
raise BadSnubaRPCRequestException("Limit can be at most 1000")

entity = Entity(
key=EntityKey("spans_str_attrs"),
schema=get_entity(EntityKey("spans_str_attrs")).get_data_model(),
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())

res = Query(
from_clause=entity,
selected_columns=[
SelectedExpression(
name="attr_value", expression=column("attr_value", alias="attr_value")
),
],
condition=base_conditions_and(
request.meta,
f.equals(column("attr_key"), literal(request.name)),
# multiSearchAny has special treatment with ngram bloom filters
# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#functions-support
f.multiSearchAny(
column("attr_value"),
literals_array(None, [literal(request.value_substring_match)]),
),
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what happens when request.value_substring_match is empty but when that happens we should just return any result. Perhaps this condition can be optional

Copy link
Member Author

Choose a reason for hiding this comment

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

if it's empty this condition is a no-op since every string contains the empty string

),
groupby=[column("attr_value", alias="attr_value")],
order_by=[
OrderBy(
direction=OrderByDirection.ASC, expression=column("organization_id")
),
OrderBy(direction=OrderByDirection.ASC, expression=column("attr_key")),
OrderBy(direction=OrderByDirection.ASC, expression=column("attr_value")),
],
limit=request.limit,
colin-sentry marked this conversation as resolved.
Show resolved Hide resolved
offset=request.offset,
)
treeify_or_and_conditions(res)
return res


def _build_snuba_request(
request: AttributeValuesRequest,
) -> SnubaRequest:
return SnubaRequest(
id=str(uuid.uuid4()),
original_body=MessageToDict(request),
query=_build_query(request),
query_settings=HTTPQuerySettings(),
attribution_info=AttributionInfo(
referrer=request.meta.referrer,
team="eap",
feature="eap",
tenant_ids={
"organization_id": request.meta.organization_id,
"referrer": request.meta.referrer,
},
app_id=AppID("eap"),
parent_api="trace_item_values",
),
)


def trace_item_values_query(
request: AttributeValuesRequest, timer: Timer | None = None
) -> AttributeValuesResponse:
timer = timer or Timer("trace_item_values")
snuba_request = _build_snuba_request(request)
res = run_query(
dataset=PluggableDataset(name="eap", all_entities=[]),
request=snuba_request,
timer=timer,
)
return AttributeValuesResponse(
values=[r["attr_value"] for r in res.result.get("data", [])]
)
10 changes: 8 additions & 2 deletions snuba/web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
)
from sentry_protos.snuba.v1alpha.endpoint_span_samples_pb2 import SpanSamplesRequest
from sentry_protos.snuba.v1alpha.endpoint_tags_list_pb2 import (
AttributeValuesRequest,
TraceItemAttributesRequest,
)
from werkzeug import Response as WerkzeugResponse
Expand Down Expand Up @@ -85,8 +86,9 @@
from snuba.web.query import parse_and_run_query
from snuba.web.rpc.exceptions import BadSnubaRPCRequestException
from snuba.web.rpc.span_samples import span_samples_query as span_samples_query
from snuba.web.rpc.tags_list import tags_list_query
from snuba.web.rpc.timeseries import timeseries_query as timeseries_query
from snuba.web.rpc.trace_item_attributes import trace_items_attributes_query
from snuba.web.rpc.trace_item_values import trace_item_values_query
from snuba.writer import BatchWriterEncoderWrapper, WriterTableRow

logger = logging.getLogger("snuba.api")
Expand Down Expand Up @@ -290,7 +292,11 @@ def rpc(*, name: str, timer: Timer) -> Response:
] = {
"AggregateBucketRequest": (timeseries_query, AggregateBucketRequest),
"SpanSamplesRequest": (span_samples_query, SpanSamplesRequest),
"TraceItemAttributesRequest": (tags_list_query, TraceItemAttributesRequest),
"TraceItemAttributesRequest": (
trace_items_attributes_query,
TraceItemAttributesRequest,
),
"AttributeValuesRequest": (trace_item_values_query, AttributeValuesRequest),
}
try:
endpoint, req_class = rpcs[name]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from snuba.datasets.storages.factory import get_storage
from snuba.datasets.storages.storage_key import StorageKey
from snuba.web.rpc.tags_list import tags_list_query
from snuba.web.rpc.trace_item_attributes import trace_items_attributes_query
from tests.base import BaseApiTest
from tests.helpers import write_raw_unprocessed_events

Expand Down Expand Up @@ -135,7 +135,7 @@ def test_simple_case_str(self, setup_teardown: Any) -> None:
offset=0,
type=AttributeKey.Type.TYPE_STRING,
)
response = tags_list_query(message)
response = trace_items_attributes_query(message)
assert response.tags == [
TraceItemAttributesResponse.Tag(
name=f"a_tag_{i:03}", type=AttributeKey.Type.TYPE_STRING
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_simple_case_float(self, setup_teardown: Any) -> None:
offset=0,
type=AttributeKey.Type.TYPE_FLOAT,
)
response = tags_list_query(message)
response = trace_items_attributes_query(message)
assert response.tags == [
TraceItemAttributesResponse.Tag(
name=f"b_measurement_{i:03}", type=AttributeKey.Type.TYPE_FLOAT
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_with_offset(self, setup_teardown: Any) -> None:
offset=10,
type=AttributeKey.Type.TYPE_FLOAT,
)
response = tags_list_query(message)
response = trace_items_attributes_query(message)
assert response.tags == [
TraceItemAttributesResponse.Tag(
name="b_measurement_010", type=AttributeKey.Type.TYPE_FLOAT
Expand Down
127 changes: 127 additions & 0 deletions tests/web/rpc/test_trace_item_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import uuid
colin-sentry marked this conversation as resolved.
Show resolved Hide resolved
from datetime import UTC, datetime, timedelta
from typing import Any, Mapping

import pytest
from google.protobuf.timestamp_pb2 import Timestamp
from sentry_protos.snuba.v1alpha.endpoint_tags_list_pb2 import AttributeValuesRequest
from sentry_protos.snuba.v1alpha.request_common_pb2 import RequestMeta

from snuba.datasets.storages.factory import get_storage
from snuba.datasets.storages.storage_key import StorageKey
from snuba.web.rpc.trace_item_values import trace_item_values_query
from tests.base import BaseApiTest
from tests.helpers import write_raw_unprocessed_events

BASE_TIME = datetime.utcnow().replace(minute=0, second=0, microsecond=0) - timedelta(
minutes=180
)
COMMON_META = RequestMeta(
project_ids=[1, 2, 3],
organization_id=1,
cogs_category="something",
referrer="something",
start_timestamp=Timestamp(
seconds=int(
datetime(
year=BASE_TIME.year,
month=BASE_TIME.month,
day=BASE_TIME.day,
tzinfo=UTC,
).timestamp()
)
),
end_timestamp=Timestamp(
seconds=int(
datetime(
year=BASE_TIME.year,
month=BASE_TIME.month,
day=BASE_TIME.day + 1,
tzinfo=UTC,
).timestamp()
)
),
)


def gen_message(tags: Mapping[str, str]) -> Mapping[str, Any]:
return {
"description": "/api/0/relays/projectconfigs/",
"duration_ms": 152,
"event_id": "d826225de75d42d6b2f01b957d51f18f",
"exclusive_time_ms": 0.228,
"is_segment": True,
"data": {},
"measurements": {},
"organization_id": 1,
"origin": "auto.http.django",
"project_id": 1,
"received": 1721319572.877828,
"retention_days": 90,
"segment_id": "8873a98879faf06d",
"sentry_tags": {},
"span_id": uuid.uuid4().hex,
"tags": tags,
"trace_id": uuid.uuid4().hex,
"start_timestamp_ms": int(BASE_TIME.timestamp() * 1000),
"start_timestamp_precise": BASE_TIME.timestamp(),
"end_timestamp_precise": BASE_TIME.timestamp() + 1,
}


@pytest.fixture(autouse=True)
def setup_teardown(clickhouse_db: None, redis_db: None) -> None:
spans_storage = get_storage(StorageKey("eap_spans"))
messages = [
gen_message({"tag1": "herp", "tag2": "herp"}),
gen_message({"tag1": "herpderp", "tag2": "herp"}),
gen_message({"tag1": "durp", "tag3": "herp"}),
gen_message({"tag1": "blah", "tag2": "herp"}),
gen_message({"tag1": "derpderp", "tag2": "derp"}),
gen_message({"tag2": "hehe"}),
gen_message({"tag1": "some_last_value"}),
]
write_raw_unprocessed_events(spans_storage, messages) # type: ignore


@pytest.mark.clickhouse_db
@pytest.mark.redis_db
class TestTraceItemAttributes(BaseApiTest):
def test_basic(self) -> None:
ts = Timestamp()
ts.GetCurrentTime()
message = AttributeValuesRequest(
meta=COMMON_META,
name="tag1",
limit=10,
offset=20,
)
response = self.app.post(
"/rpc/AttributeValuesRequest", data=message.SerializeToString()
)
assert response.status_code == 200

def test_simple_case(self, setup_teardown: Any) -> None:
message = AttributeValuesRequest(meta=COMMON_META, limit=5, name="tag1")
response = trace_item_values_query(message)
assert response.values == ["blah", "derpderp", "durp", "herp", "herpderp"]

def test_offset(self, setup_teardown: Any) -> None:
message = AttributeValuesRequest(
meta=COMMON_META, limit=5, offset=1, name="tag1"
)
response = trace_item_values_query(message)
assert response.values == [
"derpderp",
"durp",
"herp",
"herpderp",
"some_last_value",
]

def test_with_value_filter(self, setup_teardown: Any) -> None:
message = AttributeValuesRequest(
meta=COMMON_META, limit=5, name="tag1", value_substring_match="erp"
)
response = trace_item_values_query(message)
assert response.values == ["derpderp", "herp", "herpderp"]
Loading