Created
September 10, 2019 17:13
-
-
Save BlitzkriegSoftware/02de8f16f02adfa6e8e97ad70cba28c8 to your computer and use it in GitHub Desktop.
Elastic APM Wrapper for ASP.NET Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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