-
Notifications
You must be signed in to change notification settings - Fork 21
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
Added Counter Interceptor Sample #77
Changes from 14 commits
d678366
f428229
26fdbee
c68e75d
ec9da55
31c0743
ca90698
7ac67a9
34c83af
7682937
37692a7
d548089
d113bd3
c0131fd
4557ffe
c7b6e31
f3a82cc
078bbe6
fc4fada
c6f5e55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||||||||||||||||||||
public record Counts | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
public Counts() | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
clientExecutions = 0; | ||||||||||||||||||||||||
clientQueries = 0; | ||||||||||||||||||||||||
clientSignals = 0; | ||||||||||||||||||||||||
workflowExecutions = 0; | ||||||||||||||||||||||||
workflowSignals = 0; | ||||||||||||||||||||||||
workflowQueries = 0; | ||||||||||||||||||||||||
workflowChildExecutions = 0; | ||||||||||||||||||||||||
workflowActivityExecutions = 0; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not needed, this is the default value already of every field here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||||||||||||||||||||
private uint clientExecutions; | ||||||||||||||||||||||||
private uint clientQueries; | ||||||||||||||||||||||||
private uint clientSignals; | ||||||||||||||||||||||||
private uint workflowExecutions; | ||||||||||||||||||||||||
private uint workflowSignals; | ||||||||||||||||||||||||
private uint workflowQueries; | ||||||||||||||||||||||||
private uint workflowChildExecutions; | ||||||||||||||||||||||||
private uint workflowActivityExecutions; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint ClientExecutions => ref clientExecutions; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint ClientSignals => ref clientSignals; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint ClientQueries => ref clientQueries; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public string ClientInfo() => | ||||||||||||||||||||||||
$"\n\tTotal Number of Workflow Exec: {ClientExecutions}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Signals: {ClientSignals}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Queries: {ClientQueries}"; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint WorkflowExecutions => ref workflowExecutions; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint WorkflowSignals => ref workflowSignals; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint WorkflowQueries => ref workflowQueries; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint WorkflowChildExecutions => ref workflowChildExecutions; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public ref uint WorkflowActivityExecutions => ref workflowActivityExecutions; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public string WorkflowInfo() => | ||||||||||||||||||||||||
$"\n\tTotal Number of Workflow Exec: {WorkflowExecutions}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Child Workflow Exec: {WorkflowChildExecutions}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Activity Exec: {WorkflowActivityExecutions}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Signals: {WorkflowSignals}\n\t" + | ||||||||||||||||||||||||
$"Total Number of Queries: {WorkflowQueries}"; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public override string ToString() => | ||||||||||||||||||||||||
ClientInfo() + WorkflowInfo(); | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||
namespace TemporalioSamples.CounterInterceptor; | ||||||
|
||||||
using System.Diagnostics; | ||||||
using Temporalio.Activities; | ||||||
|
||||||
public class MyActivities | ||||||
{ | ||||||
[Activity] | ||||||
public string SayHello(string name, string title) => | ||||||
"Hello " + title + " " + name; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think interpolation is a bit clearer here and in next activity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||
|
||||||
[Activity] | ||||||
public string SayGoodBye(string name, string title) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are inconsistent with your single-line methods, this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||
{ | ||||||
return "Goodbye " + title + " " + name; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace TemporalioSamples.CounterInterceptor; | ||
|
||
using Temporalio.Workflows; | ||
|
||
[Workflow] | ||
public class MyChildWorkflow | ||
{ | ||
private readonly ActivityOptions activityOptions = new() | ||
{ | ||
StartToCloseTimeout = TimeSpan.FromSeconds(10), | ||
}; | ||
|
||
[WorkflowRun] | ||
public async Task<string> RunAsync(string name, string title) => | ||
await Workflow.ExecuteActivityAsync((MyActivities act) => act.SayHello(name, title), activityOptions) + | ||
await Workflow.ExecuteActivityAsync((MyActivities act) => act.SayGoodBye(name, title), activityOptions); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,137 @@ | ||||||||||
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 | ||||||||||
{ | ||||||||||
private ConcurrentDictionary<string, Counts> counts = new(); | ||||||||||
|
||||||||||
public ConcurrentDictionary<string, Counts> Counts => counts; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use auto properties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||||||
|
||||||||||
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 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"; | ||||||||||
// Need to add an empty record if none exists | ||||||||||
// we don't care if it doesn't add it as we will | ||||||||||
// still increment the current value. | ||||||||||
root.counts.TryAdd(id, new Counts()); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should happen on every call, not just start/execute workflow. The private void Increment(string id, Action<Counts> increment) =>
increment(counts.GetOrAdd(id, _ => new())); Or something like that (untested). Then you can just change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I debated on whether it needed to be done on every call or not. I was able to use your example as you had it. Worked the first time. Thanks! |
||||||||||
Interlocked.Increment(ref root.counts[id].ClientExecutions); | ||||||||||
return base.StartWorkflowAsync<TWorkflow, TResult>(input); | ||||||||||
} | ||||||||||
|
||||||||||
public override Task SignalWorkflowAsync(SignalWorkflowInput input) | ||||||||||
{ | ||||||||||
var id = input.Id ?? "None"; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||||||
Interlocked.Increment(ref root.counts[id].ClientSignals); | ||||||||||
return base.SignalWorkflowAsync(input); | ||||||||||
} | ||||||||||
|
||||||||||
public override Task<TResult> QueryWorkflowAsync<TResult>(QueryWorkflowInput input) | ||||||||||
{ | ||||||||||
var id = input.Id ?? "None"; | ||||||||||
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) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pedantic, but can also be single-method syntax using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||
{ | ||||||||||
base.Init(new WorkflowOutbound(root, outbound)); | ||||||||||
} | ||||||||||
|
||||||||||
public override Task<object?> ExecuteWorkflowAsync(ExecuteWorkflowInput input) | ||||||||||
{ | ||||||||||
var id = Workflow.Info.WorkflowId; | ||||||||||
// Need to add an empty record if none exists | ||||||||||
// we don't care if it doesn't add it as we will | ||||||||||
// still increment the current value. | ||||||||||
root.counts.TryAdd(id, new Counts()); | ||||||||||
Interlocked.Increment(ref root.counts[id].WorkflowExecutions); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, this is not workflow executions, but workflow replays. You could get 1000 of these for the same workflow. Same for other counts in the workflow inbound/outbound. These will be double counted on every replay. You can consider not doing this on replay, or you can clarify in docs of the fields that they include replay attempts (you probably want the former). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow. That's important to call out. I see that Workflow.Info has an Attempt field, but not sure it makes sense to use that to determine replay, as I think that is used for the number of retry attempts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||||||||||
return base.ExecuteWorkflowAsync(input); | ||||||||||
} | ||||||||||
|
||||||||||
public override Task HandleSignalAsync(HandleSignalInput input) | ||||||||||
{ | ||||||||||
var id = Workflow.Info.WorkflowId; | ||||||||||
Interlocked.Increment(ref root.counts[id].WorkflowSignals); | ||||||||||
return base.HandleSignalAsync(input); | ||||||||||
} | ||||||||||
|
||||||||||
public override object? HandleQuery(HandleQueryInput input) | ||||||||||
{ | ||||||||||
var id = Workflow.Info.WorkflowId; | ||||||||||
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) | ||||||||||
{ | ||||||||||
var id = Workflow.Info.WorkflowId; | ||||||||||
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 id = ActivityExecutionContext.Current.Info.WorkflowId; | ||||||||||
Interlocked.Increment(ref root.counts[id].WorkflowActivityExecutions); | ||||||||||
return base.ExecuteActivityAsync(input); | ||||||||||
} | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace TemporalioSamples.CounterInterceptor; | ||
|
||
using Temporalio.Workflows; | ||
|
||
[Workflow] | ||
public class MyWorkflow | ||
{ | ||
private bool exit; // Automatically defaults to false | ||
|
||
[WorkflowRun] | ||
public async Task<string> RunAsync() | ||
{ | ||
// Wait for greeting info | ||
await Workflow.WaitConditionAsync(() => Name != null && Title != null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These properties can never be null, so this statement has no value, it will always pass immediately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump, this was never addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump again, this is still not addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
// Execute Child Workflow | ||
var result = await Workflow.ExecuteChildWorkflowAsync( | ||
(MyChildWorkflow wf) => wf.RunAsync(Name, Title), | ||
new() { Id = "counter-interceptor-child" }); | ||
|
||
// Wait for exit signal | ||
await Workflow.WaitConditionAsync(() => exit); | ||
|
||
return result; | ||
} | ||
|
||
[WorkflowSignal] | ||
public async Task SignalNameAndTitleAsync(string name, string title) | ||
{ | ||
Name = name; | ||
Title = title; | ||
} | ||
|
||
[WorkflowQuery] | ||
public string Name { get; private set; } = string.Empty; | ||
|
||
[WorkflowQuery] | ||
public string Title { get; private set; } = string.Empty; | ||
|
||
[WorkflowSignal] | ||
public async Task ExitAsync() => exit = true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace TemporalioSamples.CounterInterceptor; | ||
|
||
using Temporalio.Client; | ||
using Temporalio.Worker; | ||
|
||
internal class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
var counterInterceptor = new MyCounterInterceptor(); | ||
var client = await TemporalClient.ConnectAsync( | ||
options: new("localhost:7233") | ||
{ | ||
Interceptors = new[] | ||
{ | ||
counterInterceptor, | ||
}, | ||
}); | ||
|
||
using var tokenSource = new CancellationTokenSource(); | ||
Console.CancelKeyPress += (_, eventArgs) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This program is a run then complete, you don't need ctrl+c support as if it was a long running worker. I would recommend just removing cancel token and shutting down the worker manually if you must have the entire program self-contained (i.e. combining client and worker). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump, this was never addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump again, this is still not addressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
{ | ||
tokenSource.Cancel(); | ||
eventArgs.Cancel = true; | ||
}; | ||
|
||
var activities = new MyActivities(); | ||
|
||
var taskQueue = "CounterInterceptorTaskQueue"; | ||
|
||
var workerOptions = new TemporalWorkerOptions(taskQueue). | ||
AddAllActivities(activities). | ||
AddWorkflow<MyWorkflow>(). | ||
AddWorkflow<MyChildWorkflow>(); | ||
|
||
// workerOptions.Interceptors = new[] { counterInterceptor }; | ||
using var worker = new TemporalWorker( | ||
client, | ||
workerOptions); | ||
|
||
// Run worker until cancelled | ||
Console.WriteLine("Running worker..."); | ||
try | ||
{ | ||
// Start the workers | ||
var workerResult = worker.ExecuteAsync(tokenSource.Token); | ||
|
||
// Start the workflow | ||
var handle = await client.StartWorkflowAsync( | ||
(MyWorkflow wf) => wf.RunAsync(), | ||
new(id: Guid.NewGuid().ToString(), taskQueue: taskQueue)); | ||
|
||
Console.WriteLine("Sending name and title to workflow"); | ||
await handle.SignalAsync(wf => wf.SignalNameAndTitleAsync("John", "Customer")); | ||
|
||
var name = await handle.QueryAsync(wf => wf.Name); | ||
var title = await handle.QueryAsync(wf => wf.Title); | ||
|
||
// Send exit signal to workflow | ||
await handle.SignalAsync(wf => wf.ExitAsync()); | ||
|
||
var result = await handle.GetResultAsync(); | ||
|
||
Console.WriteLine($"Workflow result is {result}"); | ||
|
||
Console.WriteLine("Query results: "); | ||
Console.WriteLine($"\tName: {name}"); | ||
Console.WriteLine($"\tTitle: {title}"); | ||
|
||
// Print worker counter info | ||
Console.WriteLine("\nCollected Worker Counter Info:\n"); | ||
Console.WriteLine(counterInterceptor.WorkerInfo()); | ||
Console.WriteLine($"Number of workers: {counterInterceptor.Counts.Count}"); | ||
|
||
// Print client counter info | ||
Console.WriteLine(); | ||
Console.WriteLine("Collected Client Counter Info:\n"); | ||
Console.WriteLine(counterInterceptor.ClientInfo()); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
Console.WriteLine("Worker cancelled"); | ||
} | ||
} | ||
} |
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.
No benefit to making a record, might as well make a class. Also, make sure you put the namespace on this file.
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.
fixed