-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathMyCounterInterceptor.cs
More file actions
145 lines (120 loc) · 5.67 KB
/
MyCounterInterceptor.cs
File metadata and controls
145 lines (120 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
namespace TemporalioSamples.CounterInterceptor;
using System.Collections.Concurrent;
using Temporalio.Activities;
using Temporalio.Client;
using Temporalio.Client.Interceptors;
using Temporalio.Worker.Interceptors;
using Temporalio.Workflows;
public class MyCounterInterceptor : IClientInterceptor, IWorkerInterceptor
{
public ConcurrentDictionary<string, Counts> Counts { get; } = new();
public string WorkerInfo() =>
string.Join(
"\n",
Counts.Select(kvp => $"** Workflow ID: {kvp.Key} {kvp.Value.WorkflowInfo()}"));
public string ClientInfo() =>
string.Join(
"\n",
Counts.Select(kvp => $"** Workflow ID: {kvp.Key} {kvp.Value.ClientInfo()}"));
public ClientOutboundInterceptor InterceptClient(ClientOutboundInterceptor nextInterceptor) =>
new ClientOutbound(this, nextInterceptor);
public WorkflowInboundInterceptor InterceptWorkflow(WorkflowInboundInterceptor nextInterceptor) =>
new WorkflowInbound(this, nextInterceptor);
public ActivityInboundInterceptor InterceptActivity(ActivityInboundInterceptor nextInterceptor) =>
new ActivityInbound(this, nextInterceptor);
private void Increment(string id, Action<Counts> increment) =>
increment(Counts.GetOrAdd(id, _ => new()));
private sealed class ClientOutbound : ClientOutboundInterceptor
{
private MyCounterInterceptor root;
public ClientOutbound(MyCounterInterceptor root, ClientOutboundInterceptor next)
: base(next) => this.root = root;
public override Task<WorkflowHandle<TWorkflow, TResult>> StartWorkflowAsync<TWorkflow, TResult>(
StartWorkflowInput input)
{
var id = input.Options.Id ?? "None";
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].ClientExecutions));
return base.StartWorkflowAsync<TWorkflow, TResult>(input);
}
public override Task SignalWorkflowAsync(SignalWorkflowInput input)
{
var id = input.Id;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].ClientSignals));
return base.SignalWorkflowAsync(input);
}
public override Task<TResult> QueryWorkflowAsync<TResult>(QueryWorkflowInput input)
{
var id = input.Id;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].ClientQueries));
return base.QueryWorkflowAsync<TResult>(input);
}
}
private sealed class WorkflowInbound : WorkflowInboundInterceptor
{
private readonly MyCounterInterceptor root;
internal WorkflowInbound(MyCounterInterceptor root, WorkflowInboundInterceptor next)
: base(next) => this.root = root;
public override void Init(WorkflowOutboundInterceptor outbound) =>
base.Init(new WorkflowOutbound(root, outbound));
public override Task<object?> ExecuteWorkflowAsync(ExecuteWorkflowInput input)
{
// Count only if we're not replaying
if (!Workflow.Unsafe.IsReplaying)
{
var id = Workflow.Info.WorkflowId;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].WorkflowReplays));
}
return base.ExecuteWorkflowAsync(input);
}
public override Task HandleSignalAsync(HandleSignalInput input)
{
// Count only if we're not replaying
if (!Workflow.Unsafe.IsReplaying)
{
var id = Workflow.Info.WorkflowId;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].WorkflowSignals));
}
return base.HandleSignalAsync(input);
}
public override object? HandleQuery(HandleQueryInput input)
{
// Count only if we're not replaying
if (!Workflow.Unsafe.IsReplaying)
{
var id = Workflow.Info.WorkflowId;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].WorkflowQueries));
}
return base.HandleQuery(input);
}
}
private sealed class WorkflowOutbound : WorkflowOutboundInterceptor
{
private readonly MyCounterInterceptor root;
internal WorkflowOutbound(MyCounterInterceptor root, WorkflowOutboundInterceptor next)
: base(next) => this.root = root;
public override Task<ChildWorkflowHandle<TWorkflow, TResult>> StartChildWorkflowAsync<TWorkflow, TResult>(
StartChildWorkflowInput input)
{
// Count only if we're not replaying
if (!Workflow.Unsafe.IsReplaying)
{
var id = Workflow.Info.WorkflowId;
root.Increment(id, c => Interlocked.Increment(ref root.Counts[id].WorkflowChildExecutions));
}
return base.StartChildWorkflowAsync<TWorkflow, TResult>(input);
}
}
private sealed class ActivityInbound : ActivityInboundInterceptor
{
private readonly MyCounterInterceptor root;
internal ActivityInbound(MyCounterInterceptor root, ActivityInboundInterceptor next)
: base(next) => this.root = root;
public override Task<object?> ExecuteActivityAsync(ExecuteActivityInput input)
{
var info = ActivityExecutionContext.Current.Info;
var workflowId = info.IsWorkflowActivity ? info.WorkflowId! : throw new InvalidOperationException("Activity must be invoked from a workflow");
root.Increment(workflowId, c => Interlocked.Increment(ref root.Counts[workflowId].WorkflowActivityExecutions));
return base.ExecuteActivityAsync(input);
}
}
}