Skip to content

Commit

Permalink
Add logging to IHttpModule.EndRequest if transaction is null (#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Apr 26, 2023
1 parent e5f767d commit de265dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
40 changes: 28 additions & 12 deletions src/Elastic.Apm.AspNetFullFramework/ElasticApmModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public class ElasticApmModule : IHttpModule
private readonly string _dbgInstanceName;
private HttpApplication _application;
private IApmLogger _logger;
private readonly Lazy<Type> _httpRouteDataInterfaceType = new Lazy<Type>(() => Type.GetType("System.Web.Http.Routing.IHttpRouteData,System.Web.Http"));

private readonly Lazy<Type> _httpRouteDataInterfaceType =
new Lazy<Type>(() => Type.GetType("System.Web.Http.Routing.IHttpRouteData,System.Web.Http"));

private Func<object, string> _routeDataTemplateGetter;
private Func<object, decimal> _routePrecedenceGetter;

Expand Down Expand Up @@ -256,7 +259,7 @@ private static Dictionary<string, string> ConvertHeaders(NameValueCollection hea
{
var value = headers.Get(key);
if (value != null)
convertedHeaders.Add(key,value);
convertedHeaders.Add(key, value);
}
return convertedHeaders;
}
Expand All @@ -279,17 +282,30 @@ private void ProcessError(object sender)

private void ProcessEndRequest(object sender)
{
var transaction = Agent.Instance.Tracer.CurrentTransaction;
if (transaction is null) return;

var application = (HttpApplication)sender;
var context = application.Context;
var request = context.Request;
var transaction = Agent.Instance.Tracer.CurrentTransaction;

if (transaction is null)
{
// We expect transaction to be null if `TransactionIgnoreUrls` matches
if (WildcardMatcher.IsAnyMatch(Agent.Instance.ConfigurationReader.TransactionIgnoreUrls, request.Unvalidated.Path))
return;

var hasHttpContext = HttpContext.Current?.Items[HttpContextCurrentExecutionSegmentsContainer.CurrentTransactionKey] is not null;
_logger.Warning()
?.Log(
$"{nameof(ITracer.CurrentTransaction)} is null in {nameof(ProcessEndRequest)}. HttpContext for transaction: {hasHttpContext}"
);
return;
}

var response = context.Response;

// update the transaction name based on route values, if applicable
if (transaction is Transaction t && !t.HasCustomName)
{
var request = application.Request;
var values = request.RequestContext?.RouteData?.Values;
if (values?.Count > 0)
{
Expand Down Expand Up @@ -335,7 +351,9 @@ private void ProcessEndRequest(object sender)
var precedence = _routePrecedenceGetter(subRoute);
if (precedence < minPrecedence)
{
_logger?.Trace()?.Log($"Calculating transaction name from web api attribute routing (route precedence: {precedence})");
_logger?.Trace()
?.Log(
$"Calculating transaction name from web api attribute routing (route precedence: {precedence})");
minPrecedence = precedence;
name = _routeDataTemplateGetter(subRoute);
}
Expand Down Expand Up @@ -367,8 +385,8 @@ private void ProcessEndRequest(object sender)

var realTransaction = transaction as Transaction;
realTransaction?.SetOutcome(response.StatusCode >= 500
? Outcome.Failure
: Outcome.Success);
? Outcome.Failure
: Outcome.Success);

// Try and update transaction name with SOAP action if applicable.
if (realTransaction == null || !realTransaction.HasCustomName)
Expand All @@ -391,9 +409,7 @@ private void ProcessEndRequest(object sender)
private static void FillSampledTransactionContextResponse(HttpResponse response, ITransaction transaction) =>
transaction.Context.Response = new Response
{
Finished = true,
StatusCode = response.StatusCode,
Headers = _isCaptureHeadersEnabled ? ConvertHeaders(response.Headers) : null
Finished = true, StatusCode = response.StatusCode, Headers = _isCaptureHeadersEnabled ? ConvertHeaders(response.Headers) : null
};

private void FillSampledTransactionContextUser(HttpContext context, ITransaction transaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class HttpContextCurrentExecutionSegmentsContainer : ICurrentExe
private readonly AsyncLocal<ITransaction> _currentTransaction = new AsyncLocal<ITransaction>();

private const string CurrentSpanKey = "Elastic.Apm.Agent.CurrentSpan";
private const string CurrentTransactionKey = "Elastic.Apm.Agent.CurrentTransaction";
internal const string CurrentTransactionKey = "Elastic.Apm.Agent.CurrentTransaction";

public ISpan CurrentSpan
{
Expand Down

0 comments on commit de265dc

Please sign in to comment.