Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BlitzkriegSoftware
Created September 10, 2019 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlitzkriegSoftware/02de8f16f02adfa6e8e97ad70cba28c8 to your computer and use it in GitHub Desktop.
Save BlitzkriegSoftware/02de8f16f02adfa6e8e97ad70cba28c8 to your computer and use it in GitHub Desktop.
Elastic APM Wrapper for ASP.NET Framework
using System;
using Elastic.Apm.Api;
// NuGet: Elastic.Apm
namespace Blitzkrieg.ElasticApmHelper {
/// <summary>
/// Elastic Wrapper
/// </summary>
public class ElasticWrapper
{
private ITransaction transaction = null;
/// <summary>
/// Wrap Transaction
/// </summary>
/// <param name="title">Title</param>
/// <param name="apiKind">Type of call</param>
/// <param name="callback">Action</param>
/// <returns>Elastic Transaction</returns>
public void ElasticTransactionHelper(string title, string apiKind, Action callback)
{
transaction = Elastic.Apm.Agent.Tracer.StartTransaction(title, apiKind);
try
{
callback();
}
catch (Exception e)
{
transaction.CaptureException(e);
throw;
}
finally
{
transaction.End();
}
}
/// <summary>
/// Wrap Span inside of an existing transaction
/// </summary>
/// <param name="title">Title</param>
/// <param name="apiKind">Type of call</param>
/// <param name="callback">Action</param>
/// <returns>Elastic Transaction</returns>
public void ElasticSpanHelper(string title, string apiKind, Action callback)
{
var span = transaction.StartSpan(title, apiKind);
try
{
callback();
}
catch (Exception ex)
{
span.CaptureException(ex);
throw;
}
finally
{
span.End();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment