Skip to content

Commit 1621855

Browse files
committed
responding to code review
1 parent 95c09a6 commit 1621855

6 files changed

Lines changed: 82 additions & 50 deletions

File tree

custom_metric/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
This sample deminstrates two things: (1) how to make a custom metric, and (2) how to use an interceptor.
44
The custom metric in this sample is an activity schedule-to-start-latency with a workflow type tag.
55

6-
Please see the top-level README for prerequisites such as Python, uv, starting the local temporal development server, etc.
6+
Please see the top-level [README](../README.md) for prerequisites such as Python, uv, starting the local temporal development server, etc.
77

88
1. Run the worker with `uv run custom_metric/worker.py`
9-
2. Request execution of the workflow with `temporal workflow start --type ExecuteActivityWorkflow --task-queue custom-metric-task-queue`
9+
2. Request execution of the workflow with `uv run custom_metric/starter.py`
1010
3. Go to `http://127.0.0.1:9090/metrics` in your browser
1111

1212
You'll get something like the following:
1313

1414
```txt
15-
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
16-
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
17-
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
18-
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
19-
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
20-
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
21-
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
22-
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
23-
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
24-
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
15+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="100"} 1
16+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="500"} 1
17+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="1000"} 1
18+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="5000"} 2
19+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="10000"} 2
20+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="100000"} 2
21+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="1000000"} 2
22+
custom_activity_schedule_to_start_latency_bucket{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow",le="+Inf"} 2
23+
custom_activity_schedule_to_start_latency_sum{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow"} 1010
24+
custom_activity_schedule_to_start_latency_count{activity_type="print_and_sleep",namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",workflow_type="StartTwoActivitiesWorkflow"} 2
2525
...
2626
temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="100"} 1
2727
temporal_activity_schedule_to_start_latency_bucket{namespace="default",service_name="temporal-core-sdk",task_queue="custom-metric-task-queue",le="500"} 1

custom_metric/activity.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
3+
from temporalio import activity
4+
5+
6+
@activity.defn
7+
def print_and_sleep():
8+
print("In the activity.")
9+
time.sleep(1)

custom_metric/starter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
import uuid
3+
4+
from temporalio.client import Client
5+
6+
from custom_metric.workflow import StartTwoActivitiesWorkflow
7+
8+
9+
async def main():
10+
11+
client = await Client.connect(
12+
"localhost:7233",
13+
)
14+
15+
await client.start_workflow(
16+
StartTwoActivitiesWorkflow.run,
17+
id="execute-activity-workflow-" + str(uuid.uuid4()),
18+
task_queue="custom-metric-task-queue",
19+
)
20+
21+
22+
if __name__ == "__main__":
23+
asyncio.run(main())

custom_metric/worker.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import asyncio
2-
import time
32
from concurrent.futures import ThreadPoolExecutor
4-
from datetime import timedelta
53

6-
from temporalio import activity, workflow
4+
from temporalio import activity
75
from temporalio.client import Client
86
from temporalio.runtime import PrometheusConfig, Runtime, TelemetryConfig
97
from temporalio.worker import (
@@ -13,30 +11,8 @@
1311
Worker,
1412
)
1513

16-
17-
@activity.defn
18-
def print_message():
19-
print("In the activity.")
20-
time.sleep(1)
21-
22-
23-
@workflow.defn
24-
class ExecuteActivityWorkflow:
25-
26-
@workflow.run
27-
async def run(self):
28-
# Request two concurrent activities with only one task slot so
29-
# we can see nontrivial schedule to start times.
30-
activity1 = workflow.execute_activity(
31-
print_message,
32-
start_to_close_timeout=timedelta(seconds=5),
33-
)
34-
activity2 = workflow.execute_activity(
35-
print_message,
36-
start_to_close_timeout=timedelta(seconds=5),
37-
)
38-
await asyncio.gather(activity1, activity2)
39-
return None
14+
from custom_metric.activity import print_and_sleep
15+
from custom_metric.workflow import StartTwoActivitiesWorkflow
4016

4117

4218
class SimpleWorkerInterceptor(Interceptor):
@@ -82,8 +58,8 @@ async def main():
8258
client,
8359
task_queue="custom-metric-task-queue",
8460
interceptors=[SimpleWorkerInterceptor()],
85-
workflows=[ExecuteActivityWorkflow],
86-
activities=[print_message],
61+
workflows=[StartTwoActivitiesWorkflow],
62+
activities=[print_and_sleep],
8763
# only one activity executor with two concurrently scheduled activities
8864
# to force a nontrivial schedule to start times
8965
activity_executor=ThreadPoolExecutor(1),

custom_metric/workflow.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import asyncio
2+
from datetime import timedelta
3+
4+
from temporalio import workflow
5+
6+
with workflow.unsafe.imports_passed_through():
7+
from custom_metric.activity import print_and_sleep
8+
9+
10+
@workflow.defn
11+
class StartTwoActivitiesWorkflow:
12+
13+
@workflow.run
14+
async def run(self):
15+
# Request two concurrent activities with only one task slot so
16+
# we can see nontrivial schedule to start times.
17+
activity1 = workflow.execute_activity(
18+
print_and_sleep,
19+
start_to_close_timeout=timedelta(seconds=5),
20+
)
21+
activity2 = workflow.execute_activity(
22+
print_and_sleep,
23+
start_to_close_timeout=timedelta(seconds=5),
24+
)
25+
await asyncio.gather(activity1, activity2)
26+
return None

tests/custom_metric/workflow_test.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import uuid
22

33
from temporalio import activity
4-
from temporalio.testing import WorkflowEnvironment
4+
from temporalio.client import Client
55
from temporalio.worker import Worker
66

7-
from custom_metric.worker import ExecuteActivityWorkflow
7+
from custom_metric.worker import StartTwoActivitiesWorkflow
88

99
_TASK_QUEUE = "custom-metric-task-queue"
1010

1111
activity_counter = 0
1212

1313

14-
async def test_sleep_for_days_workflow():
14+
async def test_custom_metric_workflow(client: Client):
1515

1616
@activity.defn(name="print_message")
1717
async def print_message_mock():
1818
global activity_counter
1919
activity_counter += 1
2020

21-
async with await WorkflowEnvironment.start_time_skipping() as env:
22-
2321
async with Worker(
24-
env.client,
22+
client,
2523
task_queue=_TASK_QUEUE,
26-
workflows=[ExecuteActivityWorkflow],
24+
workflows=[StartTwoActivitiesWorkflow],
2725
activities=[print_message_mock],
2826
):
29-
result = await env.client.execute_workflow(
30-
ExecuteActivityWorkflow.run,
27+
result = await client.execute_workflow(
28+
StartTwoActivitiesWorkflow.run,
3129
id=str(uuid.uuid4()),
3230
task_queue=_TASK_QUEUE,
3331
)

0 commit comments

Comments
 (0)