Repository: PrefectHQ/prefect

Issue:
Include the `task-run` as a related resources when emitting events.
### First check

- [X] I added a descriptive title to this issue.
- [X] I used the GitHub search to find a similar request and didn't find it.
- [X] I searched the Prefect documentation for this feature.

### Prefect Version

2.x

### Describe the current behavior

Currently task-runs aren't included as related resources when emitting events.

### Describe the proposed behavior

The current task run in the TaskRunContext should be included in any event fired while a task is running.

### Example Use

_No response_

### Additional context

_No response_


Discussion:

Resolving patch:
diff --git a/src/prefect/events/related.py b/src/prefect/events/related.py
--- a/src/prefect/events/related.py
+++ b/src/prefect/events/related.py
@@ -80,15 +80,33 @@ async def related_resources_from_run_context(
         async def dummy_read():
             return {}
 
-        related_objects = [
-            await _get_and_cache_related_object(
-                kind="flow-run",
-                role="flow-run",
-                client_method=client.read_flow_run,
-                obj_id=flow_run_id,
-                cache=RESOURCE_CACHE,
+        if flow_run_context:
+            related_objects.append(
+                {
+                    "kind": "flow-run",
+                    "role": "flow-run",
+                    "object": flow_run_context.flow_run,
+                },
+            )
+        else:
+            related_objects.append(
+                await _get_and_cache_related_object(
+                    kind="flow-run",
+                    role="flow-run",
+                    client_method=client.read_flow_run,
+                    obj_id=flow_run_id,
+                    cache=RESOURCE_CACHE,
+                )
+            )
+
+        if task_run_context:
+            related_objects.append(
+                {
+                    "kind": "task-run",
+                    "role": "task-run",
+                    "object": task_run_context.task_run,
+                },
             )
-        ]
 
         flow_run = related_objects[0]["object"]
 


Test patch:
diff --git a/tests/events/test_events_related_from_context.py b/tests/events/test_events_related_from_context.py
--- a/tests/events/test_events_related_from_context.py
+++ b/tests/events/test_events_related_from_context.py
@@ -135,7 +135,7 @@ async def test_flow():
     assert f"prefect.flow-run.{flow_run.id}" not in related
 
 
-async def test_gets_flow_run_from_task_run_context(prefect_client):
+async def test_gets_related_from_task_run_context(prefect_client):
     @task
     async def test_task():
         # Clear the FlowRunContext to simulated a task run in a remote worker.
@@ -144,14 +144,16 @@ async def test_task():
 
     @flow
     async def test_flow():
-        return await test_task()
+        return await test_task._run()
 
     state = await test_flow._run()
+    task_state = await state.result()
 
     flow_run = await prefect_client.read_flow_run(state.state_details.flow_run_id)
     db_flow = await prefect_client.read_flow(flow_run.flow_id)
+    task_run = await prefect_client.read_task_run(task_state.state_details.task_run_id)
 
-    related = await state.result()
+    related = await task_state.result()
 
     assert related == [
         RelatedResource(
@@ -161,6 +163,13 @@ async def test_flow():
                 "prefect.resource.name": flow_run.name,
             }
         ),
+        RelatedResource(
+            __root__={
+                "prefect.resource.id": f"prefect.task-run.{task_run.id}",
+                "prefect.resource.role": "task-run",
+                "prefect.resource.name": task_run.name,
+            }
+        ),
         RelatedResource(
             __root__={
                 "prefect.resource.id": f"prefect.flow.{db_flow.id}",