|
8 | 8 | import com.microsoft.azure.functions.annotation.FunctionName; |
9 | 9 | import com.microsoft.azure.functions.annotation.HttpTrigger; |
10 | 10 | import com.microsoft.durabletask.DurableTaskClient; |
| 11 | +import com.microsoft.durabletask.Task; |
11 | 12 | import com.microsoft.durabletask.TaskOrchestrationContext; |
12 | 13 | import com.microsoft.durabletask.azurefunctions.DurableActivityTrigger; |
13 | 14 | import com.microsoft.durabletask.azurefunctions.DurableClientContext; |
14 | 15 | import com.microsoft.durabletask.azurefunctions.DurableClientInput; |
15 | 16 | import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger; |
16 | 17 |
|
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
17 | 21 | import java.util.Optional; |
| 22 | +import java.util.stream.Collectors; |
18 | 23 |
|
19 | 24 | /** |
20 | 25 | * Sample demonstrating distributed tracing with Durable Functions. |
21 | 26 | * <p> |
| 27 | + * Uses a Fan-Out/Fan-In pattern with a timer, matching the DTS sample (TracingPattern.java). |
22 | 28 | * Trace context is automatically propagated from the HTTP trigger through the |
23 | | - * orchestration to each activity and sub-orchestration. When Application Insights |
24 | | - * or an OpenTelemetry exporter is configured, you will see correlated traces |
25 | | - * across the entire workflow. |
| 29 | + * orchestration to each activity. When Application Insights is configured, |
| 30 | + * you will see correlated traces across the entire workflow. |
26 | 31 | */ |
27 | 32 | public class TracingChain { |
28 | 33 |
|
29 | | - @FunctionName("StartTracingChain") |
30 | | - public HttpResponseMessage startTracingChain( |
| 34 | + @FunctionName("StartFanOutFanIn") |
| 35 | + public HttpResponseMessage startFanOutFanIn( |
31 | 36 | @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, |
32 | 37 | authLevel = AuthorizationLevel.ANONYMOUS) |
33 | 38 | HttpRequestMessage<Optional<String>> request, |
34 | 39 | @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
35 | 40 | final ExecutionContext context) { |
36 | | - context.getLogger().info("Starting TracingChain orchestration"); |
| 41 | + context.getLogger().info("Starting FanOutFanIn orchestration"); |
37 | 42 |
|
38 | 43 | DurableTaskClient client = durableContext.getClient(); |
39 | | - String instanceId = client.scheduleNewOrchestrationInstance("TracingChain"); |
| 44 | + String instanceId = client.scheduleNewOrchestrationInstance("FanOutFanIn"); |
40 | 45 | context.getLogger().info("Created orchestration with instance ID = " + instanceId); |
41 | 46 | return durableContext.createCheckStatusResponse(request, instanceId); |
42 | 47 | } |
43 | 48 |
|
44 | 49 | /** |
45 | | - * Orchestration that chains activities and a sub-orchestration. |
46 | | - * Trace context flows from the client through each step. |
| 50 | + * Fan-Out/Fan-In orchestration: waits 1s, then runs 5 GetWeather activities |
| 51 | + * in parallel, and finally calls CreateSummary to aggregate the results. |
47 | 52 | */ |
48 | | - @FunctionName("TracingChain") |
49 | | - public String tracingChain( |
| 53 | + @FunctionName("FanOutFanIn") |
| 54 | + public String fanOutFanIn( |
50 | 55 | @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
51 | | - String input = ctx.getInput(String.class); |
52 | | - if (input == null) { |
53 | | - input = "Hello"; |
54 | | - } |
55 | 56 |
|
56 | | - // Each activity execution creates a child span under the orchestration span |
57 | | - String step1 = ctx.callActivity("TracingReverse", input, String.class).await(); |
58 | | - String step2 = ctx.callActivity("TracingCapitalize", step1, String.class).await(); |
| 57 | + // Wait 1 second (creates a timer span) |
| 58 | + ctx.createTimer(Duration.ofSeconds(1)).await(); |
59 | 59 |
|
60 | | - // Sub-orchestration also propagates trace context |
61 | | - String result = ctx.callSubOrchestrator("TracingChildOrch", step2, String.class).await(); |
| 60 | + // Fan-out: schedule 5 GetWeather activities in parallel |
| 61 | + List<String> cities = Arrays.asList("Seattle", "Tokyo", "London", "Paris", "Sydney"); |
| 62 | + List<Task<String>> tasks = cities.stream() |
| 63 | + .map(city -> ctx.callActivity("GetWeather", city, String.class)) |
| 64 | + .collect(Collectors.toList()); |
| 65 | + List<String> results = ctx.allOf(tasks).await(); |
62 | 66 |
|
63 | | - return result; |
| 67 | + // Fan-in: aggregate results |
| 68 | + String combined = String.join(", ", results); |
| 69 | + return ctx.callActivity("CreateSummary", combined, String.class).await(); |
64 | 70 | } |
65 | 71 |
|
66 | | - /** |
67 | | - * Sub-orchestration that receives propagated trace context. |
68 | | - */ |
69 | | - @FunctionName("TracingChildOrch") |
70 | | - public String tracingChildOrch( |
71 | | - @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
72 | | - String input = ctx.getInput(String.class); |
73 | | - return ctx.callActivity("TracingAddSuffix", input, String.class).await(); |
74 | | - } |
75 | | - |
76 | | - @FunctionName("TracingReverse") |
77 | | - public String tracingReverse( |
78 | | - @DurableActivityTrigger(name = "input") String input, |
79 | | - final ExecutionContext context) { |
80 | | - context.getLogger().info("TracingReverse: " + input); |
81 | | - return new StringBuilder(input).reverse().toString(); |
82 | | - } |
83 | | - |
84 | | - @FunctionName("TracingCapitalize") |
85 | | - public String tracingCapitalize( |
86 | | - @DurableActivityTrigger(name = "input") String input, |
| 72 | + @FunctionName("GetWeather") |
| 73 | + public String getWeather( |
| 74 | + @DurableActivityTrigger(name = "city") String city, |
87 | 75 | final ExecutionContext context) { |
88 | | - context.getLogger().info("TracingCapitalize: " + input); |
89 | | - return input.toUpperCase(); |
| 76 | + context.getLogger().info("[GetWeather] Getting weather for: " + city); |
| 77 | + return city + "=72F"; |
90 | 78 | } |
91 | 79 |
|
92 | | - @FunctionName("TracingAddSuffix") |
93 | | - public String tracingAddSuffix( |
| 80 | + @FunctionName("CreateSummary") |
| 81 | + public String createSummary( |
94 | 82 | @DurableActivityTrigger(name = "input") String input, |
95 | 83 | final ExecutionContext context) { |
96 | | - context.getLogger().info("TracingAddSuffix: " + input); |
97 | | - return input + "-traced"; |
| 84 | + context.getLogger().info("[CreateSummary] Creating summary for: " + input); |
| 85 | + return "Weather Report: " + input; |
98 | 86 | } |
99 | 87 | } |
0 commit comments