mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 19:50:49 +08:00 
			
		
		
		
	Monitoring (#219)
* feat: use Https://github.com/ButterflyAPM to monitor each API request monitoring metrics * feat: using DiagnosticSource and Butterfly.OpenTracing * refactor:refactor Ocelot tracing, merge code into OcelotDiagnosticListener * refactor: move OcelotHttpTracingHandler to Requester * fix: Requester\HttpClientBuilder.cs(10,14): error CS0234: The type or namespace name 'Tracing' does not exist in the namespace * feat: add test should_set_up_tracing * feat : Remove extraneous code * feat: remove unused DiagnosticSource diagnostic * fix : test UseTracing * add test should_call_scoped_data_repository_QosProviderError * add test should_return_any_errors * add test HttpClientHttpRequesterTest * it should keep it can not be deleted
This commit is contained in:
		@@ -0,0 +1,76 @@
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Requester;
 | 
			
		||||
using Ocelot.Requester.QoS;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Requester
 | 
			
		||||
{
 | 
			
		||||
    public class HttpClientHttpRequesterTest
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IHttpClientCache> _cacheHandlers;
 | 
			
		||||
        private Mock<IServiceProvider> _serviceProvider;
 | 
			
		||||
        private Response<HttpResponseMessage> _response;
 | 
			
		||||
        private readonly HttpClientHttpRequester _httpClientRequester;
 | 
			
		||||
        private Ocelot.Request.Request _request;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _loggerFactory;
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
 | 
			
		||||
        public HttpClientHttpRequesterTest()
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProvider = new Mock<IServiceProvider>();
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _loggerFactory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _loggerFactory
 | 
			
		||||
                .Setup(x => x.CreateLogger<HttpClientHttpRequester>())
 | 
			
		||||
                .Returns(_logger.Object);
 | 
			
		||||
            _cacheHandlers = new Mock<IHttpClientCache>();
 | 
			
		||||
            _httpClientRequester = new HttpClientHttpRequester(_loggerFactory.Object, _cacheHandlers.Object, _serviceProvider.Object);            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_request_correctly()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x=>x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage() {  RequestUri = new Uri("http://www.bbc.co.uk") }, false, new NoQoSProvider(), false, false, false)))
 | 
			
		||||
                .When(x=>x.WhenIGetResponse())
 | 
			
		||||
                .Then(x => x.ThenTheResponseIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_request_UnableToCompleteRequest()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage() { RequestUri = new Uri("http://localhost:60080") }, false, new NoQoSProvider(), false, false, false)))
 | 
			
		||||
                .When(x => x.WhenIGetResponse())
 | 
			
		||||
                .Then(x => x.ThenTheResponseIsCalledError())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheRequestIs(Ocelot.Request.Request request)
 | 
			
		||||
        {
 | 
			
		||||
            _request = request;            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIGetResponse()
 | 
			
		||||
        {
 | 
			
		||||
            _response = _httpClientRequester.GetResponse(_request).Result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResponseIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            Assert.True(_response.IsError == false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResponseIsCalledError()
 | 
			
		||||
        {
 | 
			
		||||
            Assert.True(_response.IsError == true);
 | 
			
		||||
        }
 | 
			
		||||
    }  
 | 
			
		||||
}
 | 
			
		||||
@@ -1,81 +1,81 @@
 | 
			
		||||
namespace Ocelot.UnitTests.Requester
 | 
			
		||||
{
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using Microsoft.AspNetCore.Builder;
 | 
			
		||||
    using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Ocelot.Requester;
 | 
			
		||||
    using Ocelot.Requester.Middleware;
 | 
			
		||||
    using Ocelot.Requester.QoS;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
    public class HttpRequesterMiddlewareTests : ServerHostedMiddlewareTest
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IHttpRequester> _requester;
 | 
			
		||||
        private OkResponse<HttpResponseMessage> _response;
 | 
			
		||||
        private OkResponse<Ocelot.Request.Request> _request;
 | 
			
		||||
 | 
			
		||||
        public HttpRequesterMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _requester = new Mock<IHttpRequester>();
 | 
			
		||||
 | 
			
		||||
            GivenTheTestServerIsConfigured();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_scoped_data_repository_correctly()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider(), false, false)))
 | 
			
		||||
                .And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
 | 
			
		||||
                .And(x => x.GivenTheScopedRepoReturns())
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheScopedRepoIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
 | 
			
		||||
        {
 | 
			
		||||
            services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
            services.AddSingleton(_requester.Object);
 | 
			
		||||
            services.AddSingleton(ScopedRepository.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
 | 
			
		||||
        {
 | 
			
		||||
            app.UseHttpRequesterMiddleware();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheRequestIs(Ocelot.Request.Request request)
 | 
			
		||||
        {
 | 
			
		||||
            _request = new OkResponse<Ocelot.Request.Request>(request);
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
 | 
			
		||||
                .Returns(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheRequesterReturns(HttpResponseMessage response)
 | 
			
		||||
        {
 | 
			
		||||
            _response = new OkResponse<HttpResponseMessage>(response);
 | 
			
		||||
            _requester
 | 
			
		||||
                .Setup(x => x.GetResponse(It.IsAny<Ocelot.Request.Request>()))
 | 
			
		||||
                .ReturnsAsync(_response);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheScopedRepoReturns()
 | 
			
		||||
        {
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Setup(x => x.Add(It.IsAny<string>(), _response.Data))
 | 
			
		||||
                .Returns(new OkResponse());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheScopedRepoIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
namespace Ocelot.UnitTests.Requester
 | 
			
		||||
{
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using Microsoft.AspNetCore.Builder;
 | 
			
		||||
    using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Ocelot.Requester;
 | 
			
		||||
    using Ocelot.Requester.Middleware;
 | 
			
		||||
    using Ocelot.Requester.QoS;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
    public class HttpRequesterMiddlewareTests : ServerHostedMiddlewareTest
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IHttpRequester> _requester;
 | 
			
		||||
        private OkResponse<HttpResponseMessage> _response;
 | 
			
		||||
        private OkResponse<Ocelot.Request.Request> _request;
 | 
			
		||||
 | 
			
		||||
        public HttpRequesterMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _requester = new Mock<IHttpRequester>();
 | 
			
		||||
 | 
			
		||||
            GivenTheTestServerIsConfigured();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_call_scoped_data_repository_correctly()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => x.GivenTheRequestIs(new Ocelot.Request.Request(new HttpRequestMessage(),true, new NoQoSProvider(), false, false,false)))
 | 
			
		||||
                .And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
 | 
			
		||||
                .And(x => x.GivenTheScopedRepoReturns())
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheScopedRepoIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
 | 
			
		||||
        {
 | 
			
		||||
            services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
            services.AddSingleton(_requester.Object);
 | 
			
		||||
            services.AddSingleton(ScopedRepository.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
 | 
			
		||||
        {
 | 
			
		||||
            app.UseHttpRequesterMiddleware();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheRequestIs(Ocelot.Request.Request request)
 | 
			
		||||
        {
 | 
			
		||||
            _request = new OkResponse<Ocelot.Request.Request>(request);
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Setup(x => x.Get<Ocelot.Request.Request>(It.IsAny<string>()))
 | 
			
		||||
                .Returns(_request);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheRequesterReturns(HttpResponseMessage response)
 | 
			
		||||
        {
 | 
			
		||||
            _response = new OkResponse<HttpResponseMessage>(response);
 | 
			
		||||
            _requester
 | 
			
		||||
                .Setup(x => x.GetResponse(It.IsAny<Ocelot.Request.Request>()))
 | 
			
		||||
                .ReturnsAsync(_response);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheScopedRepoReturns()
 | 
			
		||||
        {
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Setup(x => x.Add(It.IsAny<string>(), _response.Data))
 | 
			
		||||
                .Returns(new OkResponse());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheScopedRepoIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            ScopedRepository
 | 
			
		||||
                .Verify(x => x.Add("HttpResponseMessage", _response.Data), Times.Once());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user