Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.