-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(temporal): Added posthog client to temporal to enable exceptions…
… capture (#26583)
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
from typing import Any, Optional | ||
from posthoganalytics.client import Client | ||
from temporalio.worker import ( | ||
ActivityInboundInterceptor, | ||
ExecuteActivityInput, | ||
ExecuteWorkflowInput, | ||
Interceptor, | ||
WorkflowInboundInterceptor, | ||
WorkflowInterceptorClassInput, | ||
) | ||
|
||
|
||
class _PostHogClientActivityInboundInterceptor(ActivityInboundInterceptor): | ||
async def execute_activity(self, input: ExecuteActivityInput) -> Any: | ||
ph_client = Client(api_key="sTMFPsFhdP1Ssg", enable_exception_autocapture=True) | ||
|
||
try: | ||
activity_result = await super().execute_activity(input) | ||
except: | ||
raise | ||
finally: | ||
await asyncio.to_thread(ph_client.flush) | ||
|
||
return activity_result | ||
|
||
|
||
class _PostHogClientWorkflowInterceptor(WorkflowInboundInterceptor): | ||
async def execute_workflow(self, input: ExecuteWorkflowInput) -> Any: | ||
ph_client = Client(api_key="sTMFPsFhdP1Ssg", enable_exception_autocapture=True) | ||
|
||
try: | ||
workflow_result = await super().execute_workflow(input) | ||
except: | ||
raise | ||
finally: | ||
await asyncio.to_thread(ph_client.flush) | ||
|
||
return workflow_result | ||
|
||
|
||
class PostHogClientInterceptor(Interceptor): | ||
"""PostHog Interceptor class which will report workflow & activity exceptions to PostHog""" | ||
|
||
def intercept_activity(self, next: ActivityInboundInterceptor) -> ActivityInboundInterceptor: | ||
"""Implementation of | ||
:py:meth:`temporalio.worker.Interceptor.intercept_activity`. | ||
""" | ||
return _PostHogClientActivityInboundInterceptor(super().intercept_activity(next)) | ||
|
||
def workflow_interceptor_class( | ||
self, input: WorkflowInterceptorClassInput | ||
) -> Optional[type[WorkflowInboundInterceptor]]: | ||
return _PostHogClientWorkflowInterceptor |
This file contains 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