-
Notifications
You must be signed in to change notification settings - Fork 108
added custom metric sample #177
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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
755fe64
added custom metric sample
GSmithApps e606ed5
Responding to code review
GSmithApps 70c1c73
responding to code review
GSmithApps 95c09a6
added tests
GSmithApps 1621855
responding to code review
GSmithApps 2ab0cf1
ran formatter
GSmithApps 3379774
fixing lints
GSmithApps 84fdfee
responding to code review
GSmithApps 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| .venv | ||
| .idea | ||
| __pycache__ | ||
| .vscode | ||
| .DS_Store |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Custom Metric | ||
|
GSmithApps marked this conversation as resolved.
|
||
|
|
||
| This sample deminstrates two things: (1) how to make a custom metric, and (2) how to use an interceptor. | ||
| The custom metric in this sample is an activity schedule-to-start-latency with a workflow type tag. | ||
|
|
||
| Please see the top-level README for prerequisites such as Python, uv, starting the local temporal development server, etc. | ||
|
GSmithApps marked this conversation as resolved.
Outdated
|
||
|
|
||
| 1. Run the worker with `uv run custom_metric/worker.py` | ||
| 2. Request execution of the workflow with `temporal workflow start --type ExecuteActivityWorkflow --task-queue custom-metric-task-queue` | ||
|
GSmithApps marked this conversation as resolved.
Outdated
|
||
| 3. Go to `http://127.0.0.1:9090/metrics` in your browser | ||
|
|
||
| You'll get something like the following: | ||
|
|
||
| ```txt | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="100"} 1 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="500"} 1 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="1000"} 1 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="5000"} 2 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="10000"} 2 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="100000"} 2 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="1000000"} 2 | ||
| custom_activity_schedule_to_start_latency_bucket{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow",le="+Inf"} 2 | ||
| custom_activity_schedule_to_start_latency_sum{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow"} 1010 | ||
| custom_activity_schedule_to_start_latency_count{activity_type="print_message",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="ExecuteActivityWorkflow"} 2 | ||
| ... | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="100"} 1 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="500"} 1 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="1000"} 1 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="5000"} 2 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="10000"} 2 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="100000"} 2 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="1000000"} 2 | ||
| temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="+Inf"} 2 | ||
| temporal_activity_schedule_to_start_latency_sum{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue"} 1010 | ||
| temporal_activity_schedule_to_start_latency_count{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue"} 2 | ||
| ``` | ||
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import asyncio | ||
| import time | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from datetime import timedelta | ||
|
|
||
| from temporalio import activity, workflow | ||
| from temporalio.client import Client | ||
| from temporalio.runtime import PrometheusConfig, Runtime, TelemetryConfig | ||
| from temporalio.worker import ( | ||
| ActivityInboundInterceptor, | ||
| ExecuteActivityInput, | ||
| Interceptor, | ||
| Worker, | ||
| ) | ||
|
|
||
|
|
||
| @activity.defn | ||
| def print_message(): | ||
| print("In the activity.") | ||
| time.sleep(1) | ||
|
|
||
|
|
||
| @workflow.defn | ||
|
GSmithApps marked this conversation as resolved.
Outdated
|
||
| class ExecuteActivityWorkflow: | ||
|
|
||
| @workflow.run | ||
| async def run(self): | ||
| # Request two concurrent activities with only one task slot so | ||
| # we can see nontrivial schedule to start times. | ||
| activity1 = workflow.execute_activity( | ||
| print_message, | ||
| start_to_close_timeout=timedelta(seconds=5), | ||
| ) | ||
| activity2 = workflow.execute_activity( | ||
| print_message, | ||
| start_to_close_timeout=timedelta(seconds=5), | ||
| ) | ||
| await asyncio.gather(activity1, activity2) | ||
| return None | ||
|
|
||
|
|
||
| class SimpleWorkerInterceptor(Interceptor): | ||
|
|
||
| def intercept_activity( | ||
| self, next: ActivityInboundInterceptor | ||
| ) -> ActivityInboundInterceptor: | ||
| return CustomScheduleToStartInterceptor(next) | ||
|
|
||
|
|
||
| class CustomScheduleToStartInterceptor(ActivityInboundInterceptor): | ||
|
|
||
| async def execute_activity(self, input: ExecuteActivityInput): | ||
|
|
||
| schedule_to_start = ( | ||
| activity.info().started_time | ||
| - activity.info().current_attempt_scheduled_time | ||
| ) | ||
| # Could do the original schedule time instead of current attempt | ||
| # schedule_to_start_second_option = activity.info().started_time - activity.info().scheduled_time | ||
|
|
||
| meter = activity.metric_meter() | ||
| histogram = meter.create_histogram_timedelta( | ||
| "custom_activity_schedule_to_start_latency", | ||
| description="Time between activity scheduling and start", | ||
| unit="duration", | ||
| ) | ||
| histogram.record( | ||
| schedule_to_start, {"workflow_type": activity.info().workflow_type} | ||
| ) | ||
| return await self.next.execute_activity(input) | ||
|
|
||
|
|
||
| async def main(): | ||
| runtime = Runtime( | ||
| telemetry=TelemetryConfig(metrics=PrometheusConfig(bind_address="0.0.0.0:9090")) | ||
| ) | ||
| client = await Client.connect( | ||
| "localhost:7233", | ||
| runtime=runtime, | ||
| ) | ||
| worker = Worker( | ||
| client, | ||
| task_queue="custom-metric-task-queue", | ||
| interceptors=[SimpleWorkerInterceptor()], | ||
| workflows=[ExecuteActivityWorkflow], | ||
| activities=[print_message], | ||
| # only one activity executor with two concurrently scheduled activities | ||
| # to force a nontrivial schedule to start times | ||
| activity_executor=ThreadPoolExecutor(1), | ||
| max_concurrent_activities=1, | ||
| ) | ||
|
|
||
| await worker.run() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
Empty file.
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import uuid | ||
|
|
||
| from temporalio import activity | ||
| from temporalio.testing import WorkflowEnvironment | ||
| from temporalio.worker import Worker | ||
|
|
||
| from custom_metric.worker import ExecuteActivityWorkflow | ||
|
|
||
| _TASK_QUEUE = "custom-metric-task-queue" | ||
|
|
||
| activity_counter = 0 | ||
|
|
||
|
|
||
| async def test_sleep_for_days_workflow(): | ||
|
|
||
| @activity.defn(name="print_message") | ||
| async def print_message_mock(): | ||
| global activity_counter | ||
| activity_counter += 1 | ||
|
|
||
| async with await WorkflowEnvironment.start_time_skipping() as env: | ||
|
GSmithApps marked this conversation as resolved.
Outdated
|
||
|
|
||
| async with Worker( | ||
|
GSmithApps marked this conversation as resolved.
Outdated
|
||
| env.client, | ||
| task_queue=_TASK_QUEUE, | ||
| workflows=[ExecuteActivityWorkflow], | ||
| activities=[print_message_mock], | ||
| ): | ||
| result = await env.client.execute_workflow( | ||
| ExecuteActivityWorkflow.run, | ||
| id=str(uuid.uuid4()), | ||
| task_queue=_TASK_QUEUE, | ||
| ) | ||
| assert result is None | ||
| assert activity_counter == 2 | ||
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.
Can you add a test or two? (I know not all samples have tests, something we regret, but adding them helps us catch if they start breaking)
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.
Added!