Skip to content

Commit

Permalink
Introduce fault header keys (#6876)
Browse files Browse the repository at this point in the history
* Cleanup fault extractor

* Move to faults folder

* Extract fault header keys

* Cosmetics

* Approve API

* Dots can make real differences
  • Loading branch information
danielmarbach committed Oct 5, 2023
1 parent aeef18c commit 8adaa8b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,14 @@ namespace NServiceBus.Faults
}
public static class FaultsHeaderKeys
{
public const string ExceptionType = "NServiceBus.ExceptionInfo.ExceptionType";
public const string FailedQ = "NServiceBus.FailedQ";
public const string HelpLink = "NServiceBus.ExceptionInfo.HelpLink";
public const string InnerExceptionType = "NServiceBus.ExceptionInfo.InnerExceptionType";
public const string Message = "NServiceBus.ExceptionInfo.Message";
public const string Source = "NServiceBus.ExceptionInfo.Source";
public const string StackTrace = "NServiceBus.ExceptionInfo.StackTrace";
public const string TimeOfFailure = "NServiceBus.TimeOfFailure";
}
public class ImmediateRetryMessage
{
Expand Down
71 changes: 0 additions & 71 deletions src/NServiceBus.Core/Recoverability/FaultMetadataExtractor.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace NServiceBus
{
using System;
using System.Collections;
using System.Collections.Generic;
using Faults;
using Transport;

class FaultMetadataExtractor(Dictionary<string, string> staticFaultMetadata, Action<Dictionary<string, string>> headerCustomizations)
{
public Dictionary<string, string> Extract(ErrorContext errorContext)
{
var metadata = new Dictionary<string, string>(staticFaultMetadata)
{
[FaultsHeaderKeys.FailedQ] = errorContext.ReceiveAddress
};

SetExceptionMetadata(metadata, errorContext.Exception);

headerCustomizations(metadata);

return metadata;
}

static void SetExceptionMetadata(Dictionary<string, string> headers, Exception e)
{
headers[FaultsHeaderKeys.ExceptionType] = e.GetType().FullName;

if (e.InnerException != null)
{
headers[FaultsHeaderKeys.InnerExceptionType] = e.InnerException.GetType().FullName;
}

headers[FaultsHeaderKeys.HelpLink] = e.HelpLink;
headers[FaultsHeaderKeys.Message] = Truncate(e.GetMessage(), 16384);
headers[FaultsHeaderKeys.Source] = e.Source;
headers[FaultsHeaderKeys.StackTrace] = e.ToString();
headers[FaultsHeaderKeys.TimeOfFailure] = DateTimeOffsetHelper.ToWireFormattedString(DateTimeOffset.UtcNow);

foreach (DictionaryEntry entry in e.Data)
{
if (entry.Value == null)
{
continue;
}
headers[$"{FaultsHeaderKeys.ExceptionInfoDataPrefix}{entry.Key}"] = entry.Value.ToString();
}
}

static string Truncate(string value, int maxLength) =>
string.IsNullOrEmpty(value)
? value
: value.Length <= maxLength
? value
: value[..maxLength];
}
}
40 changes: 40 additions & 0 deletions src/NServiceBus.Core/Recoverability/Faults/FaultsHeaderKeys.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace NServiceBus.Faults
{
using System;

/// <summary>
/// Class holding keys to message headers for faults.
/// </summary>
Expand All @@ -9,5 +11,43 @@ public static class FaultsHeaderKeys
/// Header key for setting/getting the queue at which the message processing failed.
/// </summary>
public const string FailedQ = "NServiceBus.FailedQ";

/// <summary>
/// Header key for setting/getting the exception type that caused the message processing to fail.
/// </summary>
public const string ExceptionType = $"{ExceptionInfoPrefix}ExceptionType";

/// <summary>
/// Header key for setting/getting the inner exception type that caused the message processing to fail.
/// </summary>
public const string InnerExceptionType = $"{ExceptionInfoPrefix}InnerExceptionType";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.HelpLink"/> of the exception that caused the message processing to fail.
/// </summary>
public const string HelpLink = $"{ExceptionInfoPrefix}HelpLink";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.Message"/> of the exception that caused the message processing to fail.
/// </summary>
public const string Message = $"{ExceptionInfoPrefix}Message";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.Source"/> of the exception that caused the message processing to fail.
/// </summary>
public const string Source = $"{ExceptionInfoPrefix}Source";

/// <summary>
/// Header key for setting/getting the <see cref="Exception.StackTrace"/> of the exception that caused the message processing to fail.
/// </summary>
public const string StackTrace = $"{ExceptionInfoPrefix}StackTrace";

/// <summary>
/// Header key for setting/getting the normalized time of the failure.
/// </summary>
public const string TimeOfFailure = "NServiceBus.TimeOfFailure";

const string ExceptionInfoPrefix = "NServiceBus.ExceptionInfo.";
internal const string ExceptionInfoDataPrefix = $"{ExceptionInfoPrefix}Data.";
}
}

0 comments on commit 8adaa8b

Please sign in to comment.