diff --git a/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs b/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs index fbe659cf3..37b0a0979 100644 --- a/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs +++ b/src/Ocelot/Configuration/Creator/HttpHandlerOptionsCreator.cs @@ -1,24 +1,26 @@ -using Butterfly.Client.Tracing; -using Ocelot.Configuration.File; -using Ocelot.Requester; - -namespace Ocelot.Configuration.Creator -{ - public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator - { - private readonly IServiceTracer _tracer; - - public HttpHandlerOptionsCreator(IServiceTracer tracer) - { - _tracer = tracer; - } - - public HttpHandlerOptions Create(FileHttpHandlerOptions options) - { - var useTracing = _tracer.GetType() != typeof(FakeServiceTracer) && options.UseTracing; - - return new HttpHandlerOptions(options.AllowAutoRedirect, - options.UseCookieContainer, useTracing, options.UseProxy); - } - } -} +namespace Ocelot.Configuration.Creator +{ + using System; + using Butterfly.Client.Tracing; + using Microsoft.Extensions.DependencyInjection; + using Ocelot.Configuration.File; + using Ocelot.Requester; + + public class HttpHandlerOptionsCreator : IHttpHandlerOptionsCreator + { + private readonly IServiceTracer _tracer; + + public HttpHandlerOptionsCreator(IServiceProvider services) + { + _tracer = services.GetService(); + } + + public HttpHandlerOptions Create(FileHttpHandlerOptions options) + { + var useTracing = _tracer!= null && options.UseTracing; + + return new HttpHandlerOptions(options.AllowAutoRedirect, + options.UseCookieContainer, useTracing, options.UseProxy); + } + } +} diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 07e623588..c742fd0f3 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -158,8 +158,6 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo _services.TryAddSingleton(); _services.AddSingleton(); - // We add this here so that we can always inject something into the factory for IoC.. - _services.AddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); @@ -237,8 +235,6 @@ public IOcelotBuilder AddDelegatingHandler(bool global = false) public IOcelotBuilder AddOpenTracing(Action settings) { - // Earlier we add FakeServiceTracer and need to remove it here before we add butterfly - _services.RemoveAll(); _services.AddButterfly(settings); return this; } diff --git a/src/Ocelot/Logging/OcelotDiagnosticListener.cs b/src/Ocelot/Logging/OcelotDiagnosticListener.cs index f2c517609..15952ce01 100644 --- a/src/Ocelot/Logging/OcelotDiagnosticListener.cs +++ b/src/Ocelot/Logging/OcelotDiagnosticListener.cs @@ -1,96 +1,95 @@ -using System; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DiagnosticAdapter; -using Butterfly.Client.AspNetCore; -using Butterfly.OpenTracing; -using Ocelot.Middleware; -using Butterfly.Client.Tracing; -using System.Linq; -using System.Collections.Generic; -using Ocelot.Infrastructure.Extensions; -using Ocelot.Requester; - -namespace Ocelot.Logging -{ - public class OcelotDiagnosticListener - { - private readonly IServiceTracer _tracer; - private readonly IOcelotLogger _logger; - - public OcelotDiagnosticListener(IOcelotLoggerFactory factory, IServiceTracer tracer) - { - _tracer = tracer; - _logger = factory.CreateLogger(); - } - - [DiagnosticName("Ocelot.MiddlewareException")] - public virtual void OcelotMiddlewareException(Exception exception, DownstreamContext context, string name) - { - _logger.LogTrace($"Ocelot.MiddlewareException: {name}; {exception.Message};"); - Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); - } - - [DiagnosticName("Ocelot.MiddlewareStarted")] - public virtual void OcelotMiddlewareStarted(DownstreamContext context, string name) - { - _logger.LogTrace($"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); - Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); - } - - [DiagnosticName("Ocelot.MiddlewareFinished")] - public virtual void OcelotMiddlewareFinished(DownstreamContext context, string name) - { - _logger.LogTrace($"Ocelot.MiddlewareFinished: {name}; {context.HttpContext.Request.Path}"); - Event(context.HttpContext, $"OcelotMiddlewareFinished: {name}; {context.HttpContext.Request.Path}"); - } - - [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting")] - public virtual void OnMiddlewareStarting(HttpContext httpContext, string name) - { - _logger.LogTrace($"MiddlewareStarting: {name}; {httpContext.Request.Path}"); - Event(httpContext, $"MiddlewareStarting: {name}; {httpContext.Request.Path}"); - } - - [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")] - public virtual void OnMiddlewareException(Exception exception, string name) - { - _logger.LogTrace($"MiddlewareException: {name}; {exception.Message};"); - } - - [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished")] - public virtual void OnMiddlewareFinished(HttpContext httpContext, string name) - { - _logger.LogTrace($"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}"); - Event(httpContext, $"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}"); - } - - private void Event(HttpContext httpContext, string @event) - { - // todo - if the user isnt using tracing the code gets here and will blow up on - // _tracer.Tracer.TryExtract. We already use the fake tracer for another scenario - // so sticking it here as well..I guess we need a factory for this but cba to do it at - // the moment - if(_tracer.GetType() == typeof(FakeServiceTracer)) - { - return; - } - - var span = httpContext.GetSpan(); - - if(span == null) - { - var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}"); - if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(), - c => c.Select(x => new KeyValuePair(x.Key, x.Value.GetValue())).GetEnumerator())) - { - spanBuilder.AsChildOf(spanContext); - } - - span = _tracer.Start(spanBuilder); - httpContext.SetSpan(span); - } - - span?.Log(LogField.CreateNew().Event(@event)); - } - } -} +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DiagnosticAdapter; +using Microsoft.Extensions.DependencyInjection; +using Butterfly.Client.AspNetCore; +using Butterfly.OpenTracing; +using Ocelot.Middleware; +using Butterfly.Client.Tracing; +using System.Linq; +using System.Collections.Generic; +using Ocelot.Infrastructure.Extensions; +using Ocelot.Requester; + +namespace Ocelot.Logging +{ + public class OcelotDiagnosticListener + { + private readonly IServiceTracer _tracer; + private readonly IOcelotLogger _logger; + + public OcelotDiagnosticListener(IOcelotLoggerFactory factory, IServiceProvider services) + { + _tracer = services.GetService(); + _logger = factory.CreateLogger(); + } + + [DiagnosticName("Ocelot.MiddlewareException")] + public virtual void OcelotMiddlewareException(Exception exception, DownstreamContext context, string name) + { + _logger.LogTrace($"Ocelot.MiddlewareException: {name}; {exception.Message};"); + Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); + } + + [DiagnosticName("Ocelot.MiddlewareStarted")] + public virtual void OcelotMiddlewareStarted(DownstreamContext context, string name) + { + _logger.LogTrace($"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); + Event(context.HttpContext, $"Ocelot.MiddlewareStarted: {name}; {context.HttpContext.Request.Path}"); + } + + [DiagnosticName("Ocelot.MiddlewareFinished")] + public virtual void OcelotMiddlewareFinished(DownstreamContext context, string name) + { + _logger.LogTrace($"Ocelot.MiddlewareFinished: {name}; {context.HttpContext.Request.Path}"); + Event(context.HttpContext, $"OcelotMiddlewareFinished: {name}; {context.HttpContext.Request.Path}"); + } + + [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareStarting")] + public virtual void OnMiddlewareStarting(HttpContext httpContext, string name) + { + _logger.LogTrace($"MiddlewareStarting: {name}; {httpContext.Request.Path}"); + Event(httpContext, $"MiddlewareStarting: {name}; {httpContext.Request.Path}"); + } + + [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareException")] + public virtual void OnMiddlewareException(Exception exception, string name) + { + _logger.LogTrace($"MiddlewareException: {name}; {exception.Message};"); + } + + [DiagnosticName("Microsoft.AspNetCore.MiddlewareAnalysis.MiddlewareFinished")] + public virtual void OnMiddlewareFinished(HttpContext httpContext, string name) + { + _logger.LogTrace($"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}"); + Event(httpContext, $"MiddlewareFinished: {name}; {httpContext.Response.StatusCode}"); + } + + private void Event(HttpContext httpContext, string @event) + { + // todo - if the user isnt using tracing the code gets here and will blow up on + // _tracer.Tracer.TryExtract.. + if(_tracer == null) + { + return; + } + + var span = httpContext.GetSpan(); + + if(span == null) + { + var spanBuilder = new SpanBuilder($"server {httpContext.Request.Method} {httpContext.Request.Path}"); + if (_tracer.Tracer.TryExtract(out var spanContext, httpContext.Request.Headers, (c, k) => c[k].GetValue(), + c => c.Select(x => new KeyValuePair(x.Key, x.Value.GetValue())).GetEnumerator())) + { + spanBuilder.AsChildOf(spanContext); + } + + span = _tracer.Start(spanBuilder); + httpContext.SetSpan(span); + } + + span?.Log(LogField.CreateNew().Event(@event)); + } + } +} diff --git a/src/Ocelot/Requester/FakeServiceTracer.cs b/src/Ocelot/Requester/FakeServiceTracer.cs deleted file mode 100644 index 95347ef6e..000000000 --- a/src/Ocelot/Requester/FakeServiceTracer.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Butterfly.Client.Tracing; -using Butterfly.OpenTracing; - -namespace Ocelot.Requester -{ - public class FakeServiceTracer : IServiceTracer - { - public ITracer Tracer { get; } - public string ServiceName { get; } - public string Environment { get; } - public string Identity { get; } - public ISpan Start(ISpanBuilder spanBuilder) - { - return null; - } - } -} diff --git a/src/Ocelot/Requester/TracingHandlerFactory.cs b/src/Ocelot/Requester/TracingHandlerFactory.cs index f0eb97b1c..943de4601 100644 --- a/src/Ocelot/Requester/TracingHandlerFactory.cs +++ b/src/Ocelot/Requester/TracingHandlerFactory.cs @@ -1,19 +1,21 @@ -using Butterfly.Client.Tracing; -using Ocelot.Infrastructure.RequestData; - namespace Ocelot.Requester { + using System; + using Butterfly.Client.Tracing; + using Ocelot.Infrastructure.RequestData; + using Microsoft.Extensions.DependencyInjection; + public class TracingHandlerFactory : ITracingHandlerFactory { private readonly IServiceTracer _tracer; private readonly IRequestScopedDataRepository _repo; public TracingHandlerFactory( - IServiceTracer tracer, + IServiceProvider services, IRequestScopedDataRepository repo) { _repo = repo; - _tracer = tracer; + _tracer = services.GetService(); } public ITracingHandler Get() diff --git a/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs index 94b5413bf..aea0e6593 100644 --- a/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs @@ -1,175 +1,180 @@ -using System; -using Butterfly.Client.Tracing; -using Butterfly.OpenTracing; -using Ocelot.Configuration; -using Ocelot.Configuration.Creator; -using Ocelot.Configuration.File; -using Ocelot.Requester; -using Shouldly; -using TestStack.BDDfy; -using Xunit; - -namespace Ocelot.UnitTests.Configuration -{ - public class HttpHandlerOptionsCreatorTests - { - private IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; - private FileReRoute _fileReRoute; - private HttpHandlerOptions _httpHandlerOptions; - private IServiceTracer _serviceTracer; - - public HttpHandlerOptionsCreatorTests() - { - _serviceTracer = new FakeServiceTracer(); - _httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceTracer); - } - - [Fact] - public void should_not_use_tracing_if_fake_tracer_registered() - { - var fileReRoute = new FileReRoute - { - HttpHandlerOptions = new FileHttpHandlerOptions - { - UseTracing = true - } - }; - - var expectedOptions = new HttpHandlerOptions(false, false, false, true); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); - } - - [Fact] - public void should_use_tracing_if_real_tracer_registered() - { - var fileReRoute = new FileReRoute - { - HttpHandlerOptions = new FileHttpHandlerOptions - { - UseTracing = true - } - }; - - var expectedOptions = new HttpHandlerOptions(false, false, true, true); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .And(x => GivenARealTracer()) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); - } - - [Fact] - public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default() - { - var fileReRoute = new FileReRoute(); - var expectedOptions = new HttpHandlerOptions(false, false, false, true); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); - } - - [Fact] - public void should_create_options_with_specified_useCookie_and_allowAutoRedirect() - { - var fileReRoute = new FileReRoute - { - HttpHandlerOptions = new FileHttpHandlerOptions - { - AllowAutoRedirect = false, - UseCookieContainer = false, - UseTracing = false - } - }; - - var expectedOptions = new HttpHandlerOptions(false, false, false, true); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); +using System; +using Butterfly.Client.Tracing; +using Butterfly.OpenTracing; +using Microsoft.Extensions.DependencyInjection; +using Ocelot.Configuration; +using Ocelot.Configuration.Creator; +using Ocelot.Configuration.File; +using Ocelot.Requester; +using Shouldly; +using TestStack.BDDfy; +using Xunit; + +namespace Ocelot.UnitTests.Configuration +{ + public class HttpHandlerOptionsCreatorTests + { + private IHttpHandlerOptionsCreator _httpHandlerOptionsCreator; + private FileReRoute _fileReRoute; + private HttpHandlerOptions _httpHandlerOptions; + private IServiceProvider _serviceProvider; + private IServiceCollection _serviceCollection; + + public HttpHandlerOptionsCreatorTests() + { + _serviceCollection = new ServiceCollection(); + _serviceProvider = _serviceCollection.BuildServiceProvider(); + _httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceProvider); + } + + [Fact] + public void should_not_use_tracing_if_fake_tracer_registered() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + UseTracing = true + } + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_use_tracing_if_real_tracer_registered() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + UseTracing = true + } + }; + + var expectedOptions = new HttpHandlerOptions(false, false, true, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .And(x => GivenARealTracer()) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_create_options_with_useCookie_false_and_allowAutoRedirect_true_as_default() + { + var fileReRoute = new FileReRoute(); + var expectedOptions = new HttpHandlerOptions(false, false, false, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_create_options_with_specified_useCookie_and_allowAutoRedirect() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + AllowAutoRedirect = false, + UseCookieContainer = false, + UseTracing = false + } + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_create_options_with_useproxy_true_as_default() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions() + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, true); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void should_create_options_with_specified_useproxy() + { + var fileReRoute = new FileReRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + UseProxy = false + } + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, false); + + this.Given(x => GivenTheFollowing(fileReRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + private void GivenTheFollowing(FileReRoute fileReRoute) + { + _fileReRoute = fileReRoute; } - [Fact] - public void should_create_options_with_useproxy_true_as_default() - { - var fileReRoute = new FileReRoute - { - HttpHandlerOptions = new FileHttpHandlerOptions() - }; - - var expectedOptions = new HttpHandlerOptions(false, false, false, true); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); + private void WhenICreateHttpHandlerOptions() + { + _httpHandlerOptions = _httpHandlerOptionsCreator.Create(_fileReRoute.HttpHandlerOptions); } - [Fact] - public void should_create_options_with_specified_useproxy() - { - var fileReRoute = new FileReRoute - { - HttpHandlerOptions = new FileHttpHandlerOptions - { - UseProxy = false - } - }; - - var expectedOptions = new HttpHandlerOptions(false, false, false, false); - - this.Given(x => GivenTheFollowing(fileReRoute)) - .When(x => WhenICreateHttpHandlerOptions()) - .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) - .BDDfy(); - } - - private void GivenTheFollowing(FileReRoute fileReRoute) - { - _fileReRoute = fileReRoute; - } - - private void WhenICreateHttpHandlerOptions() - { - _httpHandlerOptions = _httpHandlerOptionsCreator.Create(_fileReRoute.HttpHandlerOptions); - } - - private void ThenTheFollowingOptionsReturned(HttpHandlerOptions expected) - { - _httpHandlerOptions.ShouldNotBeNull(); - _httpHandlerOptions.AllowAutoRedirect.ShouldBe(expected.AllowAutoRedirect); - _httpHandlerOptions.UseCookieContainer.ShouldBe(expected.UseCookieContainer); + private void ThenTheFollowingOptionsReturned(HttpHandlerOptions expected) + { + _httpHandlerOptions.ShouldNotBeNull(); + _httpHandlerOptions.AllowAutoRedirect.ShouldBe(expected.AllowAutoRedirect); + _httpHandlerOptions.UseCookieContainer.ShouldBe(expected.UseCookieContainer); _httpHandlerOptions.UseTracing.ShouldBe(expected.UseTracing); - _httpHandlerOptions.UseProxy.ShouldBe(expected.UseProxy); - } - - private void GivenARealTracer() - { - var tracer = new RealTracer(); - _httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(tracer); - } - - class RealTracer : IServiceTracer - { - public ITracer Tracer => throw new NotImplementedException(); - - public string ServiceName => throw new NotImplementedException(); - - public string Environment => throw new NotImplementedException(); - - public string Identity => throw new NotImplementedException(); - - public ISpan Start(ISpanBuilder spanBuilder) - { - throw new NotImplementedException(); - } - } - } -} + _httpHandlerOptions.UseProxy.ShouldBe(expected.UseProxy); + } + + private void GivenARealTracer() + { + var tracer = new RealTracer(); + _serviceCollection.AddSingleton(); + _serviceProvider = _serviceCollection.BuildServiceProvider(); + _httpHandlerOptionsCreator = new HttpHandlerOptionsCreator(_serviceProvider); + } + + class RealTracer : IServiceTracer + { + public ITracer Tracer => throw new NotImplementedException(); + + public string ServiceName => throw new NotImplementedException(); + + public string Environment => throw new NotImplementedException(); + + public string Identity => throw new NotImplementedException(); + + public ISpan Start(ISpanBuilder spanBuilder) + { + throw new NotImplementedException(); + } + } + } +} diff --git a/test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs b/test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs index 3703ddb0b..1a463cfa3 100644 --- a/test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs +++ b/test/Ocelot.UnitTests/Logging/OcelotDiagnosticListenerTests.cs @@ -7,6 +7,7 @@ using Ocelot.Middleware; using Microsoft.AspNetCore.Http; using System; +using Microsoft.Extensions.DependencyInjection; namespace Ocelot.UnitTests.Logging { @@ -15,7 +16,8 @@ public class OcelotDiagnosticListenerTests private readonly OcelotDiagnosticListener _listener; private Mock _factory; private readonly Mock _logger; - private IServiceTracer _tracer; + private IServiceCollection _serviceCollection; + private IServiceProvider _serviceProvider; private DownstreamContext _downstreamContext; private string _name; private Exception _exception; @@ -24,9 +26,10 @@ public OcelotDiagnosticListenerTests() { _factory = new Mock(); _logger = new Mock(); - _tracer = new FakeServiceTracer(); + _serviceCollection = new ServiceCollection(); + _serviceProvider = _serviceCollection.BuildServiceProvider(); _factory.Setup(x => x.CreateLogger()).Returns(_logger.Object); - _listener = new OcelotDiagnosticListener(_factory.Object, _tracer); + _listener = new OcelotDiagnosticListener(_factory.Object, _serviceProvider); } [Fact] diff --git a/test/Ocelot.UnitTests/Requester/TracingHandlerFactoryTests.cs b/test/Ocelot.UnitTests/Requester/TracingHandlerFactoryTests.cs index bd633cb35..9128c710e 100644 --- a/test/Ocelot.UnitTests/Requester/TracingHandlerFactoryTests.cs +++ b/test/Ocelot.UnitTests/Requester/TracingHandlerFactoryTests.cs @@ -1,4 +1,6 @@ +using System; using Butterfly.Client.Tracing; +using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Infrastructure.RequestData; using Ocelot.Requester; @@ -11,13 +13,18 @@ public class TracingHandlerFactoryTests { private TracingHandlerFactory _factory; private Mock _tracer; + private IServiceCollection _serviceCollection; + private IServiceProvider _serviceProvider; private Mock _repo; public TracingHandlerFactoryTests() { _tracer = new Mock(); + _serviceCollection = new ServiceCollection(); + _serviceCollection.AddSingleton(_tracer.Object); + _serviceProvider = _serviceCollection.BuildServiceProvider(); _repo = new Mock(); - _factory = new TracingHandlerFactory(_tracer.Object, _repo.Object); + _factory = new TracingHandlerFactory(_serviceProvider, _repo.Object); } [Fact] @@ -27,4 +34,4 @@ public void should_return() handler.ShouldBeOfType(); } } -} \ No newline at end of file +}