Skip to content

Commit

Permalink
Merge pull request #6 from Moesif/feature-transaction-id
Browse files Browse the repository at this point in the history
Add: Transaction Id to Request-Response Headers
  • Loading branch information
dgilling authored Mar 1, 2019
2 parents 41863ec + 11ff219 commit 066407b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Moesif.Middleware/Moesif.Middleware.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Moesif.Middleware</id>
<version>0.1.5</version>
<version>0.1.6</version>
<title>MoesifMiddleware</title>
<authors>Moesif</authors>
<owners>Moesif</owners>
Expand Down
49 changes: 49 additions & 0 deletions Moesif.Middleware/MoesifMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class MoesifMiddleware

public bool debug;

public string transactionId;

public bool Debug()
{
bool localDebug;
Expand Down Expand Up @@ -91,6 +93,8 @@ public MoesifMiddleware(RequestDelegate next, Dictionary<string, object> _middle
client = new MoesifApiClient(moesifOptions["ApplicationId"].ToString());
debug = Debug();
_next = next;
// Initialize Transaction Id
transactionId = null;
// Create a new thread to get the application config
new Thread(async () =>
{
Expand All @@ -104,6 +108,12 @@ public async Task Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext)

var originalBodyStream = httpContext.Response.Body;

// Add Transaction Id to the Response Header
if (!string.IsNullOrEmpty(transactionId))
{
httpContext.Response.Headers.Add("X-Moesif-Transaction-Id", transactionId);
}

using (var responseBody = new MemoryStream())
{
httpContext.Response.Body = responseBody;
Expand Down Expand Up @@ -243,6 +253,39 @@ private async Task<EventRequestModel> FormatRequest(HttpRequest request)
}
}

// Add Transaction Id to the Request Header
var transation_id_out = new Object();
var captureTransactionId = moesifOptions.TryGetValue("DisableTransactionId", out transation_id_out);

bool GetCaptureTransactionId = false;
if (captureTransactionId)
{
GetCaptureTransactionId = (bool)(transation_id_out);
}

if (!GetCaptureTransactionId) {
if (reqHeaders.ContainsKey("X-Moesif-Transaction-Id"))
{
string reqTransId = reqHeaders["X-Moesif-Transaction-Id"];
if (!string.IsNullOrEmpty(reqTransId))
{
transactionId = reqTransId;
if (string.IsNullOrEmpty(transactionId)) {
transactionId = Guid.NewGuid().ToString();
}
}
else {
transactionId = Guid.NewGuid().ToString();
}
}
else
{
transactionId = Guid.NewGuid().ToString();
}
// Add Transaction Id to the Request Header
reqHeaders["X-Moesif-Transaction-Id"] = transactionId;
}

var reqBody = new object();
reqBody = null;
try
Expand Down Expand Up @@ -312,6 +355,12 @@ private async Task<EventResponseModel> FormatResponse(HttpResponse response)
}
}

// Add Transaction Id to Response Header
if (!string.IsNullOrEmpty(transactionId))
{
rspHeaders["X-Moesif-Transaction-Id"] = transactionId;
}

var rspBody = new object();
rspBody = null;
try
Expand Down
4 changes: 2 additions & 2 deletions Moesif.Middleware/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.5")]
[assembly: AssemblyFileVersion("0.1.5")]
[assembly: AssemblyVersion("0.1.6")]
[assembly: AssemblyFileVersion("0.1.6")]

0 comments on commit 066407b

Please sign in to comment.