Repository navigation
feat(ourlogs): Add a trace item details endpoint + tests #86235
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
| 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}'") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 |
||
| for attr in resp["attributes"] | ||
| }, | ||
| } | ||
|
|
||
| return Response(resp_dict) | ||
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup