Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Correlation Id Provider with Task #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions samples/3.1/MvcSample/DoNothingCorrelationIdProvider.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Threading.Tasks;
using CorrelationId.Abstractions;
using Microsoft.AspNetCore.Http;

namespace MvcSample
{
public class DoNothingCorrelationIdProvider : ICorrelationIdProvider
{
public string GenerateCorrelationId(HttpContext context)
{
return null;
}
public Task<string> GenerateCorrelationId(HttpContext _) => null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this implementation be Task.FromResult<string>(null); instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

}
}
5 changes: 3 additions & 2 deletions src/CorrelationId/Abstractions/ICorrelationIdProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace CorrelationId.Abstractions
{
Expand All @@ -12,6 +13,6 @@ public interface ICorrelationIdProvider
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> of the current request.</param>
/// <returns>A string representing the correlation ID.</returns>
string GenerateCorrelationId(HttpContext context);
Task<string> GenerateCorrelationId(HttpContext context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the method is now async, it should probably be named GenerateCorrelationIdAsync

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on accepting a CancellationToken as well?

}
}
6 changes: 3 additions & 3 deletions src/CorrelationId/CorrelationIdMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla

if (_options.IgnoreRequestHeader || RequiresGenerationOfCorrelationId(hasCorrelationIdHeader, cid))
{
correlationId = GenerateCorrelationId(context);
correlationId = await GenerateCorrelationId(context);
}

if (!string.IsNullOrEmpty(correlationId) && _options.UpdateTraceIdentifier)
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task Invoke(HttpContext context, ICorrelationContextFactory correla
private static bool RequiresGenerationOfCorrelationId(bool idInHeader, StringValues idFromHeader) =>
!idInHeader || StringValues.IsNullOrEmpty(idFromHeader);

private string GenerateCorrelationId(HttpContext ctx)
private async Task<string> GenerateCorrelationId(HttpContext ctx)
{
string correlationId;

Expand All @@ -143,7 +143,7 @@ private string GenerateCorrelationId(HttpContext ctx)
return correlationId;
}

correlationId = _correlationIdProvider.GenerateCorrelationId(ctx);
correlationId = await _correlationIdProvider.GenerateCorrelationId(ctx);
Log.GeneratedHeaderUsingProvider(_logger, correlationId, _correlationIdProvider.GetType());
return correlationId;
}
Expand Down
3 changes: 2 additions & 1 deletion src/CorrelationId/Providers/GuidCorrelationIdProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using CorrelationId.Abstractions;
using Microsoft.AspNetCore.Http;

Expand All @@ -10,6 +11,6 @@ namespace CorrelationId.Providers
public class GuidCorrelationIdProvider : ICorrelationIdProvider
{
/// <inheritdoc />
public string GenerateCorrelationId(HttpContext ctx) => Guid.NewGuid().ToString();
public Task<string> GenerateCorrelationId(HttpContext _) => Task.FromResult(Guid.NewGuid().ToString());
}
}
5 changes: 3 additions & 2 deletions src/CorrelationId/Providers/TraceIdCorrelationIdProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CorrelationId.Abstractions;
using System.Threading.Tasks;
using CorrelationId.Abstractions;
using Microsoft.AspNetCore.Http;

namespace CorrelationId.Providers
Expand All @@ -9,6 +10,6 @@ namespace CorrelationId.Providers
public class TraceIdCorrelationIdProvider : ICorrelationIdProvider
{
/// <inheritdoc />
public string GenerateCorrelationId(HttpContext ctx) => ctx.TraceIdentifier;
public Task<string> GenerateCorrelationId(HttpContext ctx) => Task.FromResult(ctx.TraceIdentifier);
}
}
2 changes: 1 addition & 1 deletion test/CorrelationId.Tests/CorrelationIdMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ private class TestCorrelationIdProvider : ICorrelationIdProvider
{
public const string FixedCorrelationId = "TestCorrelationId";

public string GenerateCorrelationId(HttpContext context) => FixedCorrelationId;
public Task<string> GenerateCorrelationId(HttpContext context) => Task.FromResult(FixedCorrelationId);
}

}
Expand Down