You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Interceptors run as a chain. Each interceptor wraps the entire inner call: your code runs before the call, invokes `next` to execute the rest of the chain, and then runs after the call completes. This means you can inspect or modify both the `input` and the result, handle errors, and perform side effects at either stage.
92
+
93
+
94
+
### Implementing Client call Interceptors
87
95
88
96
To modify outbound Client calls, define a class inheriting from
89
97
[`client.Interceptor`](https://python.temporal.io/temporalio.client.Interceptor.html), and implement the method
@@ -151,17 +159,19 @@ look to the first Interceptor instance, get hold of the appropriate intercepted
151
159
method will perform its function then call the same method on the next Interceptor in the chain. At the end of the chain
152
160
the SDK will call the "real" SDK method.
153
161
154
-
## Worker call Interceptors
162
+
### Implementing Worker call Interceptors
155
163
156
164
To modify inbound and outbound Workflow and Activity calls, define a class inheriting from `worker.Interceptor`. This is
157
165
an interface with two methods named `intercept_activity` and `workflow_interceptor_class`, which you can use to
158
166
configure interceptions of Activity and Workflow calls, respectively. `intercept_activity` returns an
159
167
`ActivityInboundInterceptor`.
160
168
161
-
This example demonstrates using an interceptor to measure
This example demonstrates using an interceptor to measure[Schedule-To-Start](/encyclopedia/detecting-activity-failures#schedule-to-start-timeout) and Schedule-To-Close latency.
170
+
Notice how the interceptor wraps the call: it records Schedule-To-Start before `execute_activity`, then records Schedule-To-Close after it completes:
163
171
164
172
```python
173
+
from datetime import datetime, timezone
174
+
from temporalio import activity
165
175
from temporalio.worker import (
166
176
ActivityInboundInterceptor,
167
177
ExecuteActivityInput,
@@ -173,27 +183,35 @@ class SimpleWorkerInterceptor(Interceptor):
Copy file name to clipboardExpand all lines: docs/develop/typescript/interceptors.mdx
+11-4Lines changed: 11 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,11 +15,20 @@ description:
15
15
---
16
16
17
17
Interceptors are SDK hooks that let you intercept inbound and outbound Temporal calls. You use them to apply shared
18
-
behavior across many calls, such as tracing and authorization, before calls reach the SDK's underlying implementation.
18
+
behavior across many calls, such as tracing and authorization, before calls reach the application code and after they return.
19
19
This is similar to middleware in other frameworks.
20
20
21
+
There are two main types of interceptors--inbound and outbound.
22
+
23
+
* Outbound interceptors wrap network calls, running before they reach the network and after they return.
24
+
* Inbound interceptors run after the network hop, wrapping application code and running before it starts and after it returns.
25
+
26
+
Those further break down into concrete Interceptor types--see below.
27
+
21
28
## How to implement interceptors in TypeScript {#interceptors}
22
29
30
+
Interceptors run as a chain. Each interceptor wraps the entire inner call: your code runs before the call, invokes `next` to execute the rest of the chain, and then runs after the call completes. This means you can inspect or modify both the `input` and the result, handle errors, and perform side effects at either stage.
31
+
23
32
The TypeScript SDK comes with an optional interceptor package that adds tracing with
24
33
[OpenTelemetry](https://www.npmjs.com/package/@temporalio/interceptors-opentelemetry). See how to use it in the
0 commit comments