-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(error-tracking): add selectable MCP event context #71111
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3a8b2ff
feat(error-tracking): add selectable MCP event context
hpouillot 7b88492
test(mcp): update unit test snapshots
tests-posthog[bot] 4656624
Merge branch 'master' into posthog-code/error-tracking-event-context
hpouillot 5254d30
feat(error-tracking): improve event correlation identifiers
hpouillot 075c421
Merge branch 'master' into posthog-code/error-tracking-event-context
hpouillot a52081f
feat(error-tracking): use include for event detail
hpouillot 37f6d13
Merge branch 'master' into posthog-code/error-tracking-event-context
hpouillot 186a2ea
fix(error-tracking): enforce property access on event queries
hpouillot 29e1195
test(mcp): update unit test snapshots
tests-posthog[bot] b3d3c63
fix(error-tracking): refine event context fields
hpouillot 8f247b1
chore: merge master
hpouillot 1123f75
fix(error-tracking): normalize handled event context
hpouillot eff7449
fix(error-tracking): include telemetry correlation context
hpouillot 7ea9610
chore: merge master
hpouillot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,10 +5,13 @@ | |
| import structlog | ||
| from drf_spectacular.utils import OpenApiResponse | ||
| from rest_framework import status, viewsets | ||
| from rest_framework.exceptions import ValidationError | ||
| from rest_framework.response import Response | ||
|
|
||
| from posthog.schema import DateRange, ErrorTrackingIssueAssignee, ErrorTrackingQuery, EventsQuery | ||
|
|
||
| from posthog.hogql.errors import ResolutionError | ||
|
|
||
| from posthog.api.mixins import ValidatedRequest, validated_request | ||
| from posthog.api.routing import TeamAndOrgViewSetMixin | ||
| from posthog.api.utils import action | ||
|
|
@@ -21,10 +24,11 @@ | |
| ) | ||
| from products.error_tracking.backend.facade.query_utils import ( | ||
| CONTEXT_EVENT_SELECTS, | ||
| EVENT_SELECTS, | ||
| DEFAULT_EVENT_CONTEXT_INCLUDES, | ||
| ISSUE_FIELDS, | ||
| LIST_ISSUE_FIELDS, | ||
| build_date_range, | ||
| build_event_selects, | ||
| build_fingerprint_event_where, | ||
| build_fingerprint_where, | ||
| build_impact, | ||
|
|
@@ -181,9 +185,14 @@ def issue(self, request: ValidatedRequest, **kwargs: object) -> Response: | |
| tags={"productKey": "error_tracking"}, | ||
| ) | ||
| with tags_context(product=Product.ERROR_TRACKING, feature=Feature.QUERY): | ||
| event_data = ( | ||
| EventsQueryRunner(team=self.team, query=context_event_query).calculate().model_dump(mode="json") | ||
| ) | ||
| try: | ||
| event_data = ( | ||
| EventsQueryRunner(team=self.team, query=context_event_query, user=request.user) | ||
| .calculate() | ||
| .model_dump(mode="json") | ||
| ) | ||
| except ResolutionError as error: | ||
| raise ValidationError(str(error)) from error | ||
| if event_data.get("error"): | ||
| logger.warning( | ||
| "error_tracking_issue_context_query_failed", | ||
|
|
@@ -230,13 +239,20 @@ def issue_events(self, request: ValidatedRequest, **kwargs: object) -> Response: | |
| if not facade_api.issue_exists_by_id(self.team.id, issue_id): | ||
| return Response(status=status.HTTP_404_NOT_FOUND) | ||
| date_range = build_date_range(params.get("dateRange")) | ||
| requested_includes = params.get("include") | ||
| includes = ( | ||
| cast(list[str], requested_includes) | ||
| if isinstance(requested_includes, list) | ||
| else DEFAULT_EVENT_CONTEXT_INCLUDES | ||
| ) | ||
| event_selects = build_event_selects(includes) | ||
|
hpouillot marked this conversation as resolved.
hpouillot marked this conversation as resolved.
|
||
| fingerprints = facade_api.resolve_fingerprints(self.team.pk, [issue_id]) | ||
| if not fingerprints: | ||
| return Response({"results": [], "hasMore": False, "limit": limit, "offset": offset}) | ||
| query = EventsQuery( | ||
| kind="EventsQuery", | ||
| event="$exception", | ||
| select=EVENT_SELECTS, | ||
| select=event_selects, | ||
| where=build_fingerprint_event_where(fingerprints, cast(str | None, params.get("searchQuery"))), | ||
| properties=cast(list[dict[str, object]], params.get("filterGroup", [])), | ||
| filterTestAccounts=cast(bool, params.get("filterTestAccounts", True)), | ||
|
|
@@ -248,14 +264,25 @@ def issue_events(self, request: ValidatedRequest, **kwargs: object) -> Response: | |
| tags={"productKey": "error_tracking"}, | ||
| ) | ||
| with tags_context(product=Product.ERROR_TRACKING, feature=Feature.QUERY): | ||
| data = EventsQueryRunner(team=self.team, query=query).calculate().model_dump(mode="json") | ||
| try: | ||
| data = ( | ||
| EventsQueryRunner(team=self.team, query=query, user=request.user) | ||
| .calculate() | ||
| .model_dump(mode="json") | ||
| ) | ||
| except ResolutionError as error: | ||
| raise ValidationError(str(error)) from error | ||
| raw_columns = data.get("columns") | ||
| columns = [str(column) for column in raw_columns] if isinstance(raw_columns, list) else EVENT_SELECTS | ||
| columns = [str(column) for column in raw_columns] if isinstance(raw_columns, list) else event_selects | ||
| raw_results_value = data.get("results") | ||
| raw_results: list[object] = raw_results_value if isinstance(raw_results_value, list) else [] | ||
| verbosity = cast(str, params.get("verbosity", "summary")) | ||
|
Contributor
Author
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. Dropping the verbosity param in favor of include. It won't be backward compatible but this endpoint is only used by agents from what I saw |
||
| include_stacktrace = "stacktrace" in includes or "code_variables" in includes | ||
| only_app_frames = cast(bool, params.get("onlyAppFrames", True)) | ||
| results = [map_event_row(row, columns, verbosity, only_app_frames) for row in raw_results[:limit]] | ||
| include_code_variables = "code_variables" in includes | ||
| results = [ | ||
| map_event_row(row, columns, include_stacktrace, only_app_frames, include_code_variables) | ||
| for row in raw_results[:limit] | ||
| ] | ||
| has_more, next_offset = get_page_info(data, limit, offset) | ||
| payload: dict[str, object] = {"results": results, "hasMore": has_more, "limit": limit, "offset": offset} | ||
| if next_offset is not None: | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
adding few ids to link with other resources
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.
💙