public async Task Can_create_database_with_retention_policy() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx", CreateDataBaseIfNotExists = true, CreateDatabaseRetentionPolicy = new RetentionPolicyOptions { Duration = TimeSpan.FromMinutes(70) } }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act await influxClient.WriteAsync(Payload, CancellationToken.None); httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(message => message.RequestUri.ToString().EndsWith("CREATE DATABASE \"influx\" WITH DURATION 70m")), ItExpr.IsAny <CancellationToken>()); }
internal static ILineProtocolClient CreateClient( InfluxDbOptions influxDbOptions, HttpPolicy httpPolicy, HttpMessageHandler httpMessageHandler = null) { var httpClient = httpMessageHandler == null ? new HttpClient() : new HttpClient(httpMessageHandler); httpClient.BaseAddress = influxDbOptions.BaseUri; httpClient.Timeout = httpPolicy.Timeout; if (string.IsNullOrWhiteSpace(influxDbOptions.UserName) || string.IsNullOrWhiteSpace(influxDbOptions.Password)) { return(new DefaultLineProtocolClient( influxDbOptions, httpPolicy, httpClient)); } var byteArray = Encoding.ASCII.GetBytes($"{influxDbOptions.UserName}:{influxDbOptions.Password}"); httpClient.BaseAddress = influxDbOptions.BaseUri; httpClient.Timeout = httpPolicy.Timeout; httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); return(new DefaultLineProtocolClient( influxDbOptions, httpPolicy, httpClient)); }
public async Task Can_write_payload_successfully_with_creds() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx", UserName = "******", Password = "******" }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act var response = await influxClient.WriteAsync(Payload, CancellationToken.None); // Assert response.Success.Should().BeTrue(); }
public async Task Can_write_payload_successfully() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var policy = new HttpPolicy(); var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act LineProtocolWriteResult response; using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload))) { response = await influxClient.WriteAsync(payload, CancellationToken.None); } // Assert response.Success.Should().BeTrue(); }
public MeasurementRepository( InfluxDBClient influxDbClient, IOptions <InfluxDbOptions> influxDbOptions, IOptions <BierCoolOptions> bierCoolOptions ) { _influxDbClient = influxDbClient; _influxDbOptions = influxDbOptions.Value; _bierCoolOptions = bierCoolOptions.Value; }
public MeasurementFixture() { Factory = new WebApplicationFactory <Startup>(); _influxDbClient = Factory.Services.GetService <InfluxDBClient>(); _influxDbOptions = Factory.Services.GetService <IOptions <InfluxDbOptions> >().Value; _bierCoolOptions = Factory.Services.GetService <IOptions <BierCoolOptions> >().Value; SeedInfluxDb(); }
public async Task Should_back_off_when_reached_max_failures_then_retry_after_backoff_period() { var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()). Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest))); var policy = new HttpPolicy { FailuresBeforeBackoff = 3, BackoffPeriod = TimeSpan.FromSeconds(1) }; var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); foreach (var attempt in Enumerable.Range(0, 10)) { await influxClient.WriteAsync(Payload, CancellationToken.None); if (attempt <= policy.FailuresBeforeBackoff) { httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.AtLeastOnce(), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } else { httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.AtMost(3), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } } await Task.Delay(policy.BackoffPeriod); httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); var response = await influxClient.WriteAsync(Payload, CancellationToken.None); response.Success.Should().BeTrue(); }
public DefaultLineProtocolClient( InfluxDbOptions influxDbOptions, HttpPolicy httpPolicy, HttpClient httpClient) { _influxDbOptions = influxDbOptions ?? throw new ArgumentNullException(nameof(influxDbOptions)); _httpClient = httpClient; _backOffPeriod = httpPolicy?.BackoffPeriod ?? throw new ArgumentNullException(nameof(httpPolicy)); _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff; _failureAttempts = 0; }
public MetricsReportingInfluxDbOptions() { FlushInterval = TimeSpan.FromSeconds(10); HttpPolicy = new HttpPolicy { FailuresBeforeBackoff = Constants.DefaultFailuresBeforeBackoff, BackoffPeriod = Constants.DefaultBackoffPeriod, Timeout = Constants.DefaultTimeout }; InfluxDb = new InfluxDbOptions(); }
public void Can_generate_influx_write_endpoint() { var settings = new InfluxDbOptions { Database = "testdb", BaseUri = new Uri("http://localhost"), RetensionPolicy = "defaultrp", Consistenency = "consistency" }; settings.Endpoint.Should().Be("write?db=testdb&rp=defaultrp&consistency=consistency"); }
public async Task Should_back_off_when_reached_max_failures() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()). Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest))); var policy = new HttpPolicy { FailuresBeforeBackoff = 3, BackoffPeriod = TimeSpan.FromMinutes(1) }; var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act foreach (var attempt in Enumerable.Range(0, 10)) { using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload))) { await influxClient.WriteAsync(payload, CancellationToken.None); } // ReSharper disable ConvertIfStatementToConditionalTernaryExpression if (attempt <= policy.FailuresBeforeBackoff) { // ReSharper restore ConvertIfStatementToConditionalTernaryExpression // Assert httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.AtLeastOnce(), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } else { // Assert httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.AtMost(6), // TODO: Starting failing when running all tests with 2.0.0 upgrade, should be 3 ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } } }
public static IHealthChecksBuilder AddInfluxDbPublisher(this IHealthChecksBuilder builder, Action <InfluxDbOptions> setup, string name = default) { builder.Services.AddHttpClient(); var options = new InfluxDbOptions(); setup?.Invoke(options); var registrationName = name ?? NAME; builder.Services.AddSingleton <IHealthCheckPublisher>(sp => { return(new InfluxDBPublisher(() => sp.GetRequiredService <IHttpClientFactory>().CreateClient(registrationName), options)); }); return(builder); }
public void Http_policy_is_required() { // Arrange Action action = () => { var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; // Act var unused = new DefaultLineProtocolClient(settings, null, new HttpClient()); }; // Assert action.Should().Throw <ArgumentNullException>(); }
public async Task Should_reset_failure_attempts_in_case_of_success_request() { // Arrange var httpMessageHandlerMock = new Mock <HttpMessageHandler>(); int callCount = 0; httpMessageHandlerMock.Protected().Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()). ReturnsAsync( () => ++ callCount % 2 == 0 ? new HttpResponseMessage(HttpStatusCode.BadRequest) : new HttpResponseMessage(HttpStatusCode.OK)); var policy = new HttpPolicy { FailuresBeforeBackoff = 3, BackoffPeriod = TimeSpan.FromMinutes(1) }; var settings = new InfluxDbOptions { BaseUri = new Uri("http://localhost"), Database = "influx" }; var influxClient = MetricsInfluxDbReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object); // Act foreach (var attempt in Enumerable.Range(0, 10)) { using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload))) { await influxClient.WriteAsync(payload, CancellationToken.None); } httpMessageHandlerMock.Protected().Verify <Task <HttpResponseMessage> >( "SendAsync", Times.Exactly(attempt + 1), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()); } }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddMvc() .AddControllersAsServices() .AddMetrics(); services.AddAutofac(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1.0", new Info { Title = "Gateway API", Version = "v1.0" }); c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml")); c.OperationFilter <AddRequiredHeaderParameter>(); }); var influxOptions = new InfluxDbOptions(); Configuration.GetSection("InfluxDb").Bind(influxOptions); var metrics = AppMetrics.CreateDefaultBuilder() .Report.ToInfluxDb( options => { options.InfluxDb = influxOptions; options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30); options.HttpPolicy.FailuresBeforeBackoff = 5; options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10); options.FlushInterval = TimeSpan.FromSeconds(20); options.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter(); }) .Build(); metrics.ReportRunner.RunAllAsync(); services.AddMetrics(metrics); services.AddMetricsReportScheduler(); services.AddMetricsTrackingMiddleware(); services.AddMetricsEndpoints(); services.AddJwtAuthentication(Configuration); services.Configure <JwtSettings>(Configuration.GetSection("Jwt")); services.Configure <RabbitMqConfiguration>(Configuration.GetSection("RabbitMq")); services.AddHttpClient <IUserServiceClient, UserServiceClient>(); services.AddHttpClient <IRemarkServiceClient, RemarkServiceClient>(); services.AddHttpClient <IStorageServiceClient, StorageServiceClient>(); var builder = new ContainerBuilder(); builder.Populate(services); builder.RegisterConsumers(Assembly.GetExecutingAssembly()); builder.Register(ctx => Configuration).As <IConfiguration>(); builder.RegisterType <MetricsOptionsFactory>().AsImplementedInterfaces(); builder.Register(context => { var config = context.Resolve <IOptions <RabbitMqConfiguration> >().Value; var busControl = Bus.Factory.CreateUsingRabbitMq(cfg => { var host = cfg.Host(new Uri(config.Host), h => { h.Username(config.User); h.Password(config.Password); h.Heartbeat(5); }); cfg.ReceiveEndpoint(host, $"GatewayApi:{nameof(RemarkCreated)}", c => c.Consumer <RemarkCreatedConsumer>(context)); }); return(busControl); }) .SingleInstance() .As <IBusControl>() .As <IBus>(); builder.RegisterType <JwtTokenService>().AsImplementedInterfaces().SingleInstance(); ApplicationContainer = builder.Build(); return(new AutofacServiceProvider(ApplicationContainer)); }