コード例 #1
0
        private HttpClient CreateClient(ElasticSearchSettings settings, HttpPolicy httpPolicy)
        {
            var httpClient = new HttpClient
            {
                BaseAddress = settings.Address,
                Timeout     = httpPolicy.Timeout
            };

            switch (settings.AuthorizationSchema)
            {
            case ElasticSearchAuthorizationSchemes.Anonymous:
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", settings.BearerToken);
                break;

            case ElasticSearchAuthorizationSchemes.Basic:
                var byteArray = Encoding.ASCII.GetBytes($"{settings.UserName}:{settings.Password}");
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                break;

            case ElasticSearchAuthorizationSchemes.BearerToken:
                break;

            default:
                throw new NotImplementedException($"The specified schema {settings.AuthorizationSchema} is not implemented");
            }

            return(httpClient);
        }
コード例 #2
0
        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();
        }
コード例 #3
0
        internal static InfluxDb2ProtocolClient CreateClient(
            InfluxDb2Options influxDb2Options,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var httpClient = httpMessageHandler == null
                ? new HttpClient()
                : new HttpClient(httpMessageHandler);

            httpClient.BaseAddress = influxDb2Options.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;

            if (string.IsNullOrWhiteSpace(influxDb2Options.Token))
            {
                return(new InfluxDb2ProtocolClient(
                           influxDb2Options,
                           httpPolicy,
                           httpClient));
            }

            httpClient.BaseAddress = influxDb2Options.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;
            if (!string.IsNullOrWhiteSpace(influxDb2Options.Token))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", influxDb2Options.Token);
            }

            return(new InfluxDb2ProtocolClient(
                       influxDb2Options,
                       httpPolicy,
                       httpClient));
        }
コード例 #4
0
        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>());
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        public static async Task UdpWriteAsync(
            this List <GraphitePayload> batches,
            GraphiteSettings graphiteSettings,
            HttpPolicy httpPolicy,
            ILogger <GraphiteClient> logger,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            await CreateClient(graphiteSettings, httpPolicy);

            var currentBatch = 1;

            foreach (var batch in batches)
            {
                var text = batch.Format(graphiteSettings.MetricNameFormatter);

                logger.LogDebug(text);

                var datagram = Encoding.UTF8.GetBytes(text);

                await _client.Client.SendAsync(new ArraySegment <byte>(datagram), SocketFlags.None);

                logger.LogTrace($"Successful batch {currentBatch} / {batches.Count} write to Graphite (UDP)");

                currentBatch++;
            }
        }
コード例 #7
0
        private static HttpClient CreateHttpClient(
            InfluxDBSettings influxDbSettings,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var client = httpMessageHandler == null
                ? new HttpClient()
                : new HttpClient(httpMessageHandler);

            client.BaseAddress = influxDbSettings.BaseAddress;
            client.Timeout     = httpPolicy.Timeout;

            if (influxDbSettings.UserName.IsMissing() || influxDbSettings.Password.IsMissing())
            {
                return(client);
            }

            var byteArray = Encoding.ASCII.GetBytes($"{influxDbSettings.UserName}:{influxDbSettings.Password}");

            client.BaseAddress = influxDbSettings.BaseAddress;
            client.Timeout     = httpPolicy.Timeout;
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

            return(client);
        }
コード例 #8
0
        public async Task should_back_off_when_reached_max_failures()
        {
            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
            };

            var client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                       new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await client.WriteAsync(_payload, CancellationToken.None);

                // ReSharper disable ConvertIfStatementToConditionalTernaryExpression
                if (attempt <= policy.FailuresBeforeBackoff)
                // ReSharper restore ConvertIfStatementToConditionalTernaryExpression
                {
                    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>());
                }
            }
        }
コード例 #9
0
        internal static ILineProtocolClient CreateClient(
            AliTSDBOptions influxDbOptions,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var httpClient = httpMessageHandler == null ? new HttpClient() : new HttpClient(httpMessageHandler);

            httpClient.BaseAddress = new Uri(influxDbOptions.Endpoint);


            if (string.IsNullOrWhiteSpace(influxDbOptions.UserName) || string.IsNullOrWhiteSpace(influxDbOptions.Password))
            {
                return(new DefaultLineProtocolClient(
                           influxDbOptions,
                           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,
                       httpClient));
        }
コード例 #10
0
        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 MetricsReportingHostedMetricsOptions
            {
                HostedMetrics = new HostedMetricsOptions
                {
                    BaseUri = new Uri("http://localhost"),
                    ApiKey  = "123"
                },
                HttpPolicy = new HttpPolicy()
            };
            var hostedMetricsClient = HostedMetricsReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await hostedMetricsClient.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)));

            hostedMetricsClient = HostedMetricsReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            var response = await hostedMetricsClient.WriteAsync(Payload, CancellationToken.None);

            response.Success.Should().BeTrue();
        }
コード例 #11
0
        public static IServiceCollection ConfigureHttpClient(this IServiceCollection services)
        {
            services.AddHttpClient <IHttpClient, StandardHttpClient>()
            .AddPolicyHandler(HttpPolicy.GetRetryPolicy())
            .AddPolicyHandler(HttpPolicy.GetCircuitBreakerPolicy());

            return(services);
        }
コード例 #12
0
 public MetricsReportingAliTSDBOptions()
 {
     FlushInterval = TimeSpan.FromSeconds(10);
     HttpPolicy    = new HttpPolicy
     {
         Timeout = TimeSpan.FromSeconds(30)
     };
     AliTSDB = new AliTSDBOptions();
 }
コード例 #13
0
        public static IHttpClientBuilder AddPolicies(
            this IHttpClientBuilder clientBuilder,
            IConfiguration configuration
            )
        {
            var policyOptions = configuration.Get <PolicyOptions>();

            return(clientBuilder.AddPolicyHandler(HttpPolicy.GetTimeout(policyOptions.Timeout))
                   .AddPolicyHandler(HttpPolicy.GetRetryPolicy(policyOptions.HttpRetry))
                   .AddPolicyHandler(HttpPolicy.GetCircuitBreakerPolicy(policyOptions.HttpCircuitBreaker)));
        }
コード例 #14
0
 public MetricsReportingInfluxDb2Options()
 {
     FlushInterval = TimeSpan.FromSeconds(10);
     HttpPolicy    = new HttpPolicy
     {
         FailuresBeforeBackoff = Constants.DefaultFailuresBeforeBackoff,
         BackoffPeriod         = Constants.DefaultBackoffPeriod,
         Timeout = Constants.DefaultTimeout
     };
     InfluxDb2 = new InfluxDb2Options();
 }
        private static async Task <TcpClient> CreateClient(
            GraphiteSettings graphiteSettings,
            HttpPolicy httpPolicy)
        {
            var client = new TcpClient {
                SendTimeout = httpPolicy.Timeout.Milliseconds
            };
            await client.ConnectAsync(graphiteSettings.BaseAddress.Host, graphiteSettings.BaseAddress.Port);

            return(client);
        }
コード例 #16
0
 public InfluxDb2ProtocolClient(
     InfluxDb2Options 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;
 }
コード例 #17
0
#pragma warning disable SA1118

        public GraphiteClient(
            ILoggerFactory loggerFactory,
            GraphiteSettings graphiteSettings,
            HttpPolicy httpPolicy)
        {
            _graphiteSettings      = graphiteSettings ?? throw new ArgumentNullException(nameof(graphiteSettings));
            _httpPolicy            = httpPolicy ?? throw new ArgumentNullException(nameof(httpPolicy));
            _backOffPeriod         = httpPolicy.BackoffPeriod;
            _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff;
            _failureAttempts       = 0;
            _logger = loggerFactory.CreateLogger <GraphiteClient>();
        }
コード例 #18
0
        private static async Task CreateClient(
            GraphiteSettings graphiteSettings,
            HttpPolicy httpPolicy)
        {
            if (_client == null)
            {
                _client = new UdpClient {
                    Client = { SendTimeout = httpPolicy.Timeout.Milliseconds }
                };
            }

            await _client.Client.ConnectAsync(graphiteSettings.BaseAddress.Host, graphiteSettings.BaseAddress.Port);
        }
コード例 #19
0
        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 MetricsReportingHostedMetricsOptions
            {
                HostedMetrics = new HostedMetricsOptions
                {
                    BaseUri = new Uri("http://localhost"),
                    ApiKey  = "123"
                },
                HttpPolicy = policy
            };
            var hostedMetricsClient = HostedMetricsReporterBuilder.CreateClient(settings, policy, httpMessageHandlerMock.Object);

            // Act
            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await hostedMetricsClient.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>());
                }
            }
        }
コード例 #20
0
        public static IDatadogClient CreateClient(
            MetricsReportingDatadogOptions options,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var httpClient = httpMessageHandler == null
                ? new HttpClient()
                : new HttpClient(httpMessageHandler);

            httpClient.BaseAddress = options.Datadog.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;

            return(new DefaultDatadogHttpClient(
                       httpClient,
                       options.Datadog,
                       httpPolicy));
        }
コード例 #21
0
        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 client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                       new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            foreach (var attempt in Enumerable.Range(0, 10))
            {
                await client.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)));
            client = new DefaultLineProtocolClient(new LoggerFactory(),
                                                   new InfluxDBSettings("influx", new Uri("http://localhost")), policy, httpMessageHandlerMock.Object);

            var response = await client.WriteAsync(_payload, CancellationToken.None);

            response.Success.Should().BeTrue();
        }
コード例 #22
0
        public DefaultLineProtocolClient(ILoggerFactory loggerFactory, InfluxDBSettings influxDbSettings,
                                         HttpPolicy httpPolicy, HttpMessageHandler httpMessageHandler = null)
        {
            if (influxDbSettings == null)
            {
                throw new ArgumentNullException(nameof(influxDbSettings));
            }

            if (httpPolicy == null)
            {
                throw new ArgumentNullException(nameof(httpPolicy));
            }

            _httpClient       = CreateHttpClient(influxDbSettings, httpPolicy, httpMessageHandler);
            _influxDbSettings = influxDbSettings;
            _policy           = httpPolicy.AsPolicy();
            _logger           = loggerFactory.CreateLogger <DefaultLineProtocolClient>();
        }
コード例 #23
0
        internal static IHostedMetricsClient CreateClient(
            MetricsReportingHostedMetricsOptions options,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            var httpClient = httpMessageHandler == null
                ? new HttpClient()
                : new HttpClient(httpMessageHandler);

            httpClient.BaseAddress = options.HostedMetrics.BaseUri;
            httpClient.Timeout     = httpPolicy.Timeout;

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", options.HostedMetrics.ApiKey);

            return(new DefaultHostedMetricsHttpClient(
                       httpClient,
                       options.HostedMetrics,
                       httpPolicy));
        }
コード例 #24
0
        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 InfluxDb2Options
            {
                BaseUri      = new Uri("http://localhost"),
                Organization = "influx",
                Bucket       = "bucket",
            };
            var influxClient = MetricsInfluxDb2ReporterBuilder.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>());
            }
        }
コード例 #25
0
        public ElasticSearchBulkClient(
            ILoggerFactory loggerFactory,
            ElasticSearchSettings elasticSearchSettings,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            if (elasticSearchSettings == null)
            {
                throw new ArgumentNullException(nameof(elasticSearchSettings));
            }

            if (httpPolicy == null)
            {
                throw new ArgumentNullException(nameof(httpPolicy));
            }

            _httpClient            = CreateClient(elasticSearchSettings, httpPolicy);
            _backOffPeriod         = httpPolicy.BackoffPeriod;
            _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff;
            _failureAttempts       = 0;
            _logger = loggerFactory.CreateLogger <ElasticSearchBulkClient>();
        }
        public static async Task TcpWriteAsync(
            this List <GraphitePayload> batches,
            GraphiteSettings graphiteSettings,
            HttpPolicy httpPolicy,
            ILogger <GraphiteClient> logger,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            using (var client = await CreateClient(graphiteSettings, httpPolicy))
            {
                using (var stream = client.GetStream())
                {
                    using (var writer = new StreamWriter(stream)
                    {
                        NewLine = "\n"
                    })
                    {
                        var currentBatch = 1;

                        foreach (var batch in batches)
                        {
                            var text = batch.Format(graphiteSettings.MetricNameFormatter);

                            logger.LogDebug(text);

                            await writer.WriteLineAsync(text);

                            logger.LogTrace($"Successful batch {currentBatch} / {batches.Count} write to Graphite (TCP)");

                            currentBatch++;
                        }

                        await writer.FlushAsync();
                    }

                    await stream.FlushAsync(cancellationToken);
                }
            }
        }
コード例 #27
0
#pragma warning disable SA1118

        public DefaultLineProtocolClient(
            ILoggerFactory loggerFactory,
            InfluxDBSettings influxDbSettings,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            if (influxDbSettings == null)
            {
                throw new ArgumentNullException(nameof(influxDbSettings));
            }

            if (httpPolicy == null)
            {
                throw new ArgumentNullException(nameof(httpPolicy));
            }

            _httpClient            = CreateHttpClient(influxDbSettings, httpPolicy, httpMessageHandler);
            _influxDbSettings      = influxDbSettings;
            _backOffPeriod         = httpPolicy.BackoffPeriod;
            _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff;
            _failureAttempts       = 0;
            _logger = loggerFactory.CreateLogger <DefaultLineProtocolClient>();
        }
コード例 #28
0
        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))
            {
                using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
                {
                    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);

            LineProtocolWriteResult response;

            using (var payload = new MemoryStream(Encoding.UTF8.GetBytes(Payload)))
            {
                response = await influxClient.WriteAsync(payload, CancellationToken.None);
            }

            response.Success.Should().BeTrue();
        }
コード例 #29
0
 public MetricsReportingHttpOptions()
 {
     HttpSettings = new HttpSettings();
     HttpPolicy   = new HttpPolicy();
 }