Skip to content

Instantly share code, notes, and snippets.

@yohanb
Created November 18, 2020 19:01
Show Gist options
  • Save yohanb/85955f89df6aefd9a3778a389d4ebb56 to your computer and use it in GitHub Desktop.
Save yohanb/85955f89df6aefd9a3778a389d4ebb56 to your computer and use it in GitHub Desktop.
Instrumented stash receive actor
/// <summary>
/// ReceiveActor with unbounded stash implementation that sends stash depth metrics with AppMetrics.
/// Use when you wish to monitor the stash depth with reporting tools such as DataDog.
/// </summary>
public abstract class InstrumentedStashReceiveActor : ReceiveActor, IWithUnboundedStash
{
private int _stashSize = 0;
private static readonly GaugeOptions GaugeOptions = new GaugeOptions
{
Name = "akka.actor.stash.depth",
};
private readonly MetricTags _metricTags;
protected InstrumentedStashReceiveActor()
{
this._metricTags = new MetricTags(
new[] { AppMetricsTagKeys.ActorType, AppMetricsTagKeys.AkkaAddress },
new[] { this.GetType().FullName, Cluster.Get(Context.System).SelfAddress.ToString() });
}
IStash IActorStash.Stash { get; set; }
protected void AddToStash()
{
((IWithUnboundedStash)this).Stash.Stash();
this._stashSize++;
this.SetGaugeValue();
}
protected void UnstashAll()
{
((IWithUnboundedStash)this).Stash.UnstashAll();
this._stashSize = 0;
this.SetGaugeValue();
}
private void SetGaugeValue()
{
Context.GetInstrumentation().Monitor.Gauge.SetValue(GaugeOptions, this._metricTags, this._stashSize);
}
private static class AppMetricsTagKeys
{
public const string ActorType = "actortype";
public const string AkkaAddress = "akka_address";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment