Skip to content

mbtariq82/TemporalCostInsights

Repository files navigation

TemporalCostInsights

A lightweight interceptor plugin for the Temporal .NET SDK that provides visibility into:

  • Workflow execution cost drivers
  • Activity-level execution patterns
  • Retry amplification
  • Signal and query traffic
  • Workflow load hotspots

Features

  • Client-side tracking for workflow starts, signals, and queries
  • Replay-safe workflow execution tracking
  • Activity attempt, retry, failure, and duration tracking
  • Scheduled activity and local activity counts from workflow outbound interceptors
  • Child workflow scheduling counts
  • Configurable estimated cost units
  • Snapshot and hotspot helpers on the metrics store
  • Lightweight in-process metrics store

Architecture

Built using Temporal .NET interceptors:

  • IClientInterceptor records workflow starts, queries, and signals from clients
  • IWorkerInterceptor records workflow execution and activity execution from workers
  • Workflow outbound interception records scheduled activities and child workflows
  • Workflow-side metrics skip replay paths with Workflow.Unsafe.IsReplaying

Example

using Temporalio.Client;
using Temporalio.Worker;
using TemporalCostInsights.Core;

var plugin = new CostInsightsPlugin();

var client = await TemporalClient.ConnectAsync(new("localhost:7233")
{
    Interceptors = new[] { plugin },
});

using var worker = new TemporalWorker(
    client,
    new TemporalWorkerOptions("cost-insights-taskqueue")
        .AddActivity(new SampleActivities().ProcessData)
        .AddWorkflow<SampleWorkflow>());

await worker.ExecuteAsync(cancellationToken);

TemporalWorker automatically receives client interceptors that also implement IWorkerInterceptor, so registering the plugin on the client is enough for the sample path.

Reading Metrics

var topWorkflows = plugin.MetricsStore.GetTopWorkflowsByEstimatedCost(count: 5);

foreach (var workflow in topWorkflows)
{
    Console.WriteLine(workflow);
}

For stable reads, use SnapshotWorkflows() or GetMetrics(workflowId). Both return detached copies.

Cost Weights

The built-in score is a heuristic, not a billing calculator. Tune it per deployment:

var plugin = new CostInsightsPlugin(new CostInsightsOptions
{
    ActivityExecutionUnits = 3.0,
    RetryUnits = 2.0,
    FailureUnits = 5.0,
});

Sample

Run the sample against a local Temporal server:

dotnet run --project TemporalCostInsights.Sample

The sample registers the plugin, runs a worker, and prints a compact top-cost workflow snapshot every 10 seconds when metrics are present.

About

A plugin for Temporal .NET SDK that provides visibility into workflow execution costs.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages