Skip to content

Commit

Permalink
chore: Removed deprecated .AddHttpCorrelation (#441)
Browse files Browse the repository at this point in the history
* Removed deprecated .AddHttpCorrelation

* Removed unittest that are not needed anymore.

---------

Co-authored-by: Roeland Lutters <[email protected]>
  • Loading branch information
Roeland Lutters and Roeland Lutters authored Mar 17, 2023
1 parent e5f32f1 commit fc14ee6
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,6 @@ public static IServiceCollection AddHttpCorrelation(this IServiceCollection serv
return AddHttpCorrelation(services, configureOptions: (HttpCorrelationInfoOptions options) => { });
}

/// <summary>
/// Adds operation and transaction correlation to the application.
/// </summary>
/// <param name="services">The services collection containing the dependency injection services.</param>
/// <param name="configureOptions">The function to configure additional options how the correlation works.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="services"/> is <c>null</c>.</exception>
[Obsolete("Use the " + nameof(AddHttpCorrelation) + " method overload with the " + nameof(HttpCorrelationInfoOptions) + " instead")]
public static IServiceCollection AddHttpCorrelation(
this IServiceCollection services,
Action<CorrelationInfoOptions> configureOptions)
{
Guard.NotNull(services, nameof(services), "Requires a services collection to add the HTTP correlation services");

services.AddHttpContextAccessor();
services.AddCorrelation(
serviceProvider => (HttpCorrelationInfoAccessor) serviceProvider.GetRequiredService<IHttpCorrelationInfoAccessor>(),
configureOptions);
services.AddSingleton<IHttpCorrelationInfoAccessor>(serviceProvider =>
{
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
return new HttpCorrelationInfoAccessor(httpContextAccessor);
});
services.AddSingleton(serviceProvider =>
{
var options = serviceProvider.GetRequiredService<IOptions<CorrelationInfoOptions>>();
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
var correlationInfoAccessor = serviceProvider.GetRequiredService<IHttpCorrelationInfoAccessor>();
var logger = serviceProvider.GetService<ILogger<HttpCorrelation>>();

return new HttpCorrelation(options, httpContextAccessor, correlationInfoAccessor, logger);
});

return services;
}

/// <summary>
/// Adds operation and transaction correlation to the application.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,119 +435,6 @@ public async Task SendRequest_WithCorrelateOptionsUpstreamServiceExtractFromRequ
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task SendRequest_WithCorrelateOptionsOperationParentExtractFromRequest_UsesUpstreamOperationId(bool extractFromRequest)
{
// Arrange
var operationParentId = Guid.NewGuid().ToString();
var requestId = $"|{Guid.NewGuid()}.{operationParentId}";
var options = new TestApiServerOptions()
.ConfigureServices(services => services.AddHttpCorrelation(opt =>
{
opt.OperationParent.ExtractFromRequest = extractFromRequest;
}))
.PreConfigure(app => app.UseHttpCorrelation());

await using (var server = await TestApiServer.StartNewAsync(options, _logger))
{
var request = HttpRequestBuilder
.Get(CorrelationController.GetRoute)
.WithHeader(DefaultOperationParentId, requestId);

// Act
using (HttpResponseMessage response = await server.SendAsync(request))
{
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseRequestId = GetResponseHeader(response, DefaultOperationParentId);

CorrelationInfo correlation = await ReadCorrelationInfoFromResponseBodyAsync(response);
Assert.Equal(correlation.OperationId, GetResponseHeader(response, DefaultOperationId));
Assert.Equal(correlation.TransactionId, GetResponseHeader(response, DefaultTransactionId));
Assert.Equal(extractFromRequest, operationParentId == correlation.OperationParentId);
Assert.Equal(extractFromRequest, requestId == responseRequestId);
}
}
}

[Theory]
[InlineData(false, "|abc.def", null)]
[InlineData(true, "|abc.def", "def")]
[InlineData(false, "abc", null)]
[InlineData(true, "abc", "abc")]
public async Task SendRequest_WithCorrelateOptionsOperationParentExtractFromRequest_UsesRequestId(bool extractFromRequest, string requestId, string expectedParentId)
{
// Arrange
var options = new TestApiServerOptions()
.ConfigureServices(services => services.AddHttpCorrelation(opt =>
{
opt.OperationParent.ExtractFromRequest = extractFromRequest;
}))
.PreConfigure(app => app.UseHttpCorrelation());

await using (var server = await TestApiServer.StartNewAsync(options, _logger))
{
var request = HttpRequestBuilder
.Get(CorrelationController.GetRoute)
.WithHeader(DefaultOperationParentId, requestId);

// Act
using (HttpResponseMessage response = await server.SendAsync(request))
{
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseRequestId = GetResponseHeader(response, DefaultOperationParentId);

CorrelationInfo correlation = await ReadCorrelationInfoFromResponseBodyAsync(response);
Assert.Equal(correlation.OperationId, GetResponseHeader(response, DefaultOperationId));
Assert.Equal(correlation.TransactionId, GetResponseHeader(response, DefaultTransactionId));
Assert.Equal(extractFromRequest, expectedParentId == correlation.OperationParentId);
Assert.Equal(extractFromRequest, requestId == responseRequestId);
}
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task SendRequest_WithCorrelateOptionsOperationParentCustomOperationParentIHeaderName_UsesUpstreamOperationId(bool extractFromRequest)
{
// Arrange
var operationParentId = Guid.NewGuid().ToString();
var requestId = $"|{Guid.NewGuid()}.{operationParentId}";
var operationParentIdHeaderName = "My-Request-Id";
var options = new TestApiServerOptions()
.ConfigureServices(services => services.AddHttpCorrelation(opt =>
{
opt.OperationParent.ExtractFromRequest = extractFromRequest;
opt.OperationParent.OperationParentIdHeaderName = operationParentIdHeaderName;
}))
.PreConfigure(app => app.UseHttpCorrelation());

await using (var server = await TestApiServer.StartNewAsync(options, _logger))
{
var request = HttpRequestBuilder
.Get(CorrelationController.GetRoute)
.WithHeader(operationParentIdHeaderName, requestId);

// Act
using (HttpResponseMessage response = await server.SendAsync(request))
{
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseRequestId = GetResponseHeader(response, operationParentIdHeaderName);

CorrelationInfo correlation = await ReadCorrelationInfoFromResponseBodyAsync(response);
Assert.Equal(correlation.OperationId, GetResponseHeader(response, DefaultOperationId));
Assert.Equal(correlation.TransactionId, GetResponseHeader(response, DefaultTransactionId));
Assert.Equal(extractFromRequest, operationParentId == correlation.OperationParentId);
Assert.Equal(extractFromRequest, responseRequestId == requestId);
}
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Arcus.Observability.Correlation;
#if NET6_0
using Arcus.WebApi.Logging.Core.Correlation;
Expand All @@ -15,24 +12,6 @@ namespace Arcus.WebApi.Tests.Unit.Logging
// ReSharper disable once InconsistentNaming
public class IServiceCollectionExtensionsTests
{
[Fact]
public void AddHttpCorrelation_WithOptions_RegistersDedicatedCorrelation()
{
// Arrange
var services = new ServiceCollection();

// Act
#pragma warning disable CS0618 // Type or member is obsolete
services.AddHttpCorrelation((Action<CorrelationInfoOptions>) null);
#pragma warning restore CS0618 // Type or member is obsolete

// Assert
IServiceProvider provider = services.BuildServiceProvider();
Assert.NotNull(provider.GetService<IHttpCorrelationInfoAccessor>());
Assert.NotNull(provider.GetService<ICorrelationInfoAccessor<CorrelationInfo>>());
Assert.NotNull(provider.GetService<ICorrelationInfoAccessor>());
}

[Fact]
public void AddHttpCorrelation_WithHttpOptions_RegistersDedicatedCorrelation()
{
Expand Down

0 comments on commit fc14ee6

Please sign in to comment.