Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion requirements-base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ rfc3986-validator>=0.1.1
sentry-arroyo>=2.19.9
sentry-kafka-schemas>=1.1.1
sentry-ophio==1.0.0
sentry-protos>=0.1.61
sentry-protos>=0.1.62
sentry-redis-tools>=0.1.7
sentry-relay>=0.9.6
sentry-sdk[http2]>=2.22.0
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev-frozen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ sentry-forked-djangorestframework-stubs==3.15.3.post1
sentry-forked-email-reply-parser==0.5.12.post1
sentry-kafka-schemas==1.1.1
sentry-ophio==1.0.0
sentry-protos==0.1.61
sentry-protos==0.1.62
sentry-redis-tools==0.1.7
sentry-relay==0.9.6
sentry-sdk==2.22.0
Expand Down
2 changes: 1 addition & 1 deletion requirements-frozen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ sentry-arroyo==2.19.9
sentry-forked-email-reply-parser==0.5.12.post1
sentry-kafka-schemas==1.1.1
sentry-ophio==1.0.0
sentry-protos==0.1.61
sentry-protos==0.1.62
sentry-redis-tools==0.1.7
sentry-relay==0.9.6
sentry-sdk==2.22.0
Expand Down
79 changes: 79 additions & 0 deletions src/sentry/api/endpoints/project_trace_item_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import uuid

from google.protobuf.json_format import MessageToDict
from google.protobuf.timestamp_pb2 import Timestamp as ProtoTimestamp
from rest_framework.request import Request
from rest_framework.response import Response
from sentry_protos.snuba.v1.endpoint_trace_item_details_pb2 import TraceItemDetailsRequest
from sentry_protos.snuba.v1.request_common_pb2 import TRACE_ITEM_TYPE_LOG, RequestMeta

from sentry.api.api_owners import ApiOwner
from sentry.api.api_publish_status import ApiPublishStatus
from sentry.api.base import region_silo_endpoint
from sentry.api.bases.project import ProjectEndpoint
from sentry.api.exceptions import BadRequest
from sentry.models.project import Project
from sentry.snuba.referrer import Referrer
from sentry.utils import snuba_rpc


def convert_rpc_attribute_to_json(source: dict) -> dict:
for k, v in source.items():
if k.startswith("val"):
return {"type": k[3:].lower(), "value": v}
raise BadRequest(f"unknown field in protobuf: {source}")


@region_silo_endpoint
class ProjectTraceItemDetailsEndpoint(ProjectEndpoint):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this base class guarantee the project ACL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yup

owner = ApiOwner.PERFORMANCE
publish_status = {
"GET": ApiPublishStatus.EXPERIMENTAL,
}

@staticmethod
def get(request: Request, project: Project, item_id: str) -> Response:
"""
Retrieve a Trace Item for a project.

For example, you might ask 'give me all the details about the span/log with id 01234567'
"""
dataset = request.GET.get("dataset")
referrer = request.GET.get("referrer", Referrer.API_ORGANIZATION_TRACE_ITEM_DETAILS.value)
if dataset == "ourlogs":
trace_item_type = TRACE_ITEM_TYPE_LOG
else:
raise BadRequest(detail=f"Unknown dataset: '{dataset}'")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe test this branch (can do it later too not super important)


start_timestamp_proto = ProtoTimestamp()
start_timestamp_proto.FromSeconds(0)

end_timestamp_proto = ProtoTimestamp()
end_timestamp_proto.GetCurrentTime()
Comment on lines +51 to +52

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the timestamp is user submitted, we can't guarantee the window of GetCurrentTime is correct, but this should work for now.


req = TraceItemDetailsRequest(
item_id=item_id,
meta=RequestMeta(
organization_id=project.organization.id,
cogs_category="events_analytics_platform",
referrer=referrer,
project_ids=[project.id],
start_timestamp=start_timestamp_proto,
end_timestamp=end_timestamp_proto,
trace_item_type=trace_item_type,
request_id=str(uuid.uuid4()),
),
)

resp = MessageToDict(snuba_rpc.trace_item_details_rpc(req))

resp_dict = {
"itemId": resp["itemId"],
"timestamp": resp["timestamp"],
"attributes": {
attr["name"]: convert_rpc_attribute_to_json(attr["value"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👏

for attr in resp["attributes"]
},
}

return Response(resp_dict)
6 changes: 6 additions & 0 deletions src/sentry/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@
from .endpoints.project_tags import ProjectTagsEndpoint
from .endpoints.project_team_details import ProjectTeamDetailsEndpoint
from .endpoints.project_teams import ProjectTeamsEndpoint
from .endpoints.project_trace_item_details import ProjectTraceItemDetailsEndpoint
from .endpoints.project_transaction_names import ProjectTransactionNamesCluster
from .endpoints.project_transaction_threshold import ProjectTransactionThresholdEndpoint
from .endpoints.project_transaction_threshold_override import (
Expand Down Expand Up @@ -2239,6 +2240,11 @@ def create_group_urls(name_prefix: str) -> list[URLPattern | URLResolver]:
ProjectEnvironmentDetailsEndpoint.as_view(),
name="sentry-api-0-project-environment-details",
),
re_path(
r"^(?P<organization_id_or_slug>[^\/]+)/(?P<project_id_or_slug>[^\/]+)/trace-items/(?P<item_id>(?:[A-Fa-f0-9]+))/$",
ProjectTraceItemDetailsEndpoint.as_view(),
name="sentry-api-0-project-trace-item-details",
),
re_path(
r"^(?P<organization_id_or_slug>[^\/]+)/(?P<project_id_or_slug>[^\/]+)/events/$",
ProjectEventsEndpoint.as_view(),
Expand Down
1 change: 1 addition & 0 deletions src/sentry/snuba/referrer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Referrer(StrEnum):
API_ISSUES_ISSUE_EVENTS = "api.issues.issue_events"
API_ISSUES_RELATED_ISSUES = "api.issues.related_issues"
API_METRICS_TOTALS = "api.metrics.totals"
API_ORGANIZATION_TRACE_ITEM_DETAILS = "api.organization-trace-item-details"
API_ORGANIZATION_EVENT_STATS_FIND_TOPN = "api.organization-event-stats.find-topn"
API_ORGANIZATION_EVENT_STATS_METRICS_ENHANCED = "api.organization-event-stats.metrics-enhanced"
API_ORGANIZATION_EVENT_STATS = "api.organization-event-stats"
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/testutils/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3341,10 +3341,10 @@ def create_span(
class _OptionalOurLogData(TypedDict, total=False):
body: str
trace_id: str
span_id: str
severity_text: str
severity_number: int
trace_flags: int
item_id: int


class OurLogTestCase(BaseTestCase):
Expand Down
19 changes: 19 additions & 0 deletions src/sentry/utils/snuba_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sentry_sdk.scope
import urllib3
from google.protobuf.message import Message as ProtobufMessage
from rest_framework.exceptions import NotFound
from sentry_protos.snuba.v1.endpoint_create_subscription_pb2 import (
CreateSubscriptionRequest,
CreateSubscriptionResponse,
Expand All @@ -23,6 +24,10 @@
TraceItemAttributeValuesRequest,
TraceItemAttributeValuesResponse,
)
from sentry_protos.snuba.v1.endpoint_trace_item_details_pb2 import (
TraceItemDetailsRequest,
TraceItemDetailsResponse,
)
from sentry_protos.snuba.v1.endpoint_trace_item_stats_pb2 import (
TraceItemStatsRequest,
TraceItemStatsResponse,
Expand Down Expand Up @@ -167,6 +172,18 @@ def trace_item_stats_rpc(req: TraceItemStatsRequest) -> TraceItemStatsResponse:
return response


def trace_item_details_rpc(req: TraceItemDetailsRequest) -> TraceItemDetailsResponse:
"""
An RPC which requests all of the details about a specific trace item.
For example, you might say "give me all of the attributes for the log with id 1234" or
"give me all of the attributes for the span with id 12345 and trace_id 34567"
"""
resp = _make_rpc_request("EndpointTraceItemDetails", "v1", req.meta.referrer, req)
response = TraceItemDetailsResponse()
response.ParseFromString(resp.data)
return response


def rpc(
req: SnubaRPCRequest,
resp_type: type[RPCResponseType],
Expand Down Expand Up @@ -261,6 +278,8 @@ def _make_rpc_request(
error.ParseFromString(http_resp.data)
if SNUBA_INFO:
log_snuba_info(f"{referrer}.error:\n{error}")
if http_resp.status == 404:
raise NotFound() from SnubaRPCError(error)
raise SnubaRPCError(error)
return http_resp

Expand Down
99 changes: 99 additions & 0 deletions tests/snuba/api/endpoints/test_project_trace_item_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import uuid

from django.urls import reverse

from sentry.testutils.cases import APITestCase, OurLogTestCase, SnubaTestCase
from sentry.testutils.helpers.datetime import before_now


class ProjectEventDetailsTest(APITestCase, SnubaTestCase, OurLogTestCase):
def setUp(self):
super().setUp()
self.login_as(user=self.user)
self.features = {
"organizations:ourlogs-enabled": True,
}

def test_simple(self):
one_min_ago = before_now(minutes=1)
trace_id = str(uuid.uuid4())
logs = [
self.create_ourlog(
{
"body": "foo",
"trace_id": trace_id,
},
attributes={
"str_attr": {
"string_value": "1",
},
"int_attr": {"int_value": 2},
"float_attr": {
"double_value": 3.0,
},
"bool_attr": {
"bool_value": True,
},
},
timestamp=one_min_ago,
),
]
self.store_ourlogs(logs)
item_list_url = reverse(
"sentry-api-0-organization-events",
kwargs={
"organization_id_or_slug": self.project.organization.slug,
},
)
with self.feature(self.features):
item_list_response = self.client.get(
item_list_url,
{
"field": ["log.body", "sentry.item_id"],
"query": "",
"orderby": "sentry.item_id",
"project": self.project.id,
"dataset": "ourlogs",
"useRpc": "1",
},
)
assert item_list_response.data is not None
item_id = item_list_response.data["data"][0]["sentry.item_id"]

item_details_url = reverse(
"sentry-api-0-project-trace-item-details",
kwargs={
"item_id": item_id,
"project_id_or_slug": self.project.slug,
"organization_id_or_slug": self.project.organization.slug,
},
)
with self.feature(self.features):
trace_details_response = self.client.get(
item_details_url + "?dataset=ourlogs", format="json"
)

assert trace_details_response.status_code == 200, trace_details_response.content
assert trace_details_response.data == {
"attributes": {
"bool_attr": {
"value": True,
"type": "bool",
},
"float_attr": {"value": 3.0, "type": "float"},
"int_attr": {"value": "2", "type": "int"},
"sentry.body": {"value": "foo", "type": "str"},
"sentry.item_type": {"value": "3", "type": "int"},
"sentry.organization_id": {
"value": str(self.project.organization.id),
"type": "int",
},
"sentry.project_id": {"value": str(self.project.id), "type": "int"},
"sentry.severity_number": {"value": "0", "type": "int"},
"sentry.severity_text": {"value": "INFO", "type": "str"},
"sentry.trace_id": {"value": trace_id, "type": "str"},
"str_attr": {"value": "1", "type": "str"},
},
"itemId": item_id,
"timestamp": one_min_ago.replace(microsecond=0, tzinfo=None).isoformat() + "Z",
}, trace_details_response.data