示例#1
0
        public void Configure(string collectorName, XElement configElement, ISystemMetricsService systemMetrics)
        {
            _completionTask = new Task(() => IsActive = false);
            _systemMetrics  = systemMetrics;

            var config = new LibratoBackendConfiguration(
                email: configElement.Attribute("email").Value,
                token: configElement.Attribute("token").Value,
                numRetries: configElement.ToInt("numRetries"),
                retryDelay: Utility.ConvertToTimespan(configElement.Attribute("retryDelay").Value),
                postTimeout: Utility.ConvertToTimespan(configElement.Attribute("postTimeout").Value),
                maxBatchSize: configElement.ToInt("maxBatchSize"),
                countersAsGauges: configElement.ToBoolean("countersAsGauges")
                );

            _config         = config;
            _source         = collectorName;
            _serviceVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

            _preprocessorBlock = new ActionBlock <Bucket>(bucket => ProcessBucket(bucket), Utility.UnboundedExecution());
            _batchBlock        = new BatchBlock <LibratoMetric>(_config.MaxBatchSize);
            _outputBlock       = new ActionBlock <LibratoMetric[]>(lines => PostToLibrato(lines), Utility.OneAtATimeExecution());
            _batchBlock.LinkTo(_outputBlock);

            _client = new HttpClient()
            {
                BaseAddress = new Uri(LIBRATO_API_URL)
            };

            var authByteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", _config.Email, _config.Token));

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authByteArray));
            _client.Timeout = TimeSpan.FromMilliseconds(_config.PostTimeout.TotalMilliseconds);


            _retryPolicy = Policy.Handle <TimeoutException>().WaitAndRetry(_config.NumRetries, retryAttempt => _config.RetryDelay, (exception, timeSpan) =>
            {
                _log.WarnException(String.Format("Retry failed. Trying again. Delay {1}, Error: {2}", timeSpan, exception.Message), exception);
                _systemMetrics.LogCount("backends.librato.retry");
            });

            IsActive = true;
        }
示例#2
0
        public void Configure(string collectorName, XElement configElement, ISystemMetricsService systemMetrics)
        {
            _completionTask = new Task(() => IsActive = false);
            _log            = SuperCheapIOC.Resolve <ILog>();
            _systemMetrics  = systemMetrics;

            var config = new LibratoBackendConfiguration(
                email: configElement.Attribute("email").Value,
                token: configElement.Attribute("token").Value,
                numRetries: configElement.ToInt("numRetries"),
                retryDelay: Utility.ConvertToTimespan(configElement.Attribute("retryDelay").Value),
                postTimeout: Utility.ConvertToTimespan(configElement.Attribute("postTimeout").Value),
                maxBatchSize: configElement.ToInt("maxBatchSize"),
                countersAsGauges: configElement.ToBoolean("countersAsGauges")
                );

            _config         = config;
            _source         = collectorName;
            _serviceVersion = Assembly.GetEntryAssembly().GetName().Version.ToString();

            _preprocessorBlock = new ActionBlock <Bucket>(bucket => ProcessBucket(bucket), Utility.UnboundedExecution());
            _batchBlock        = new BatchBlock <LibratoMetric>(_config.MaxBatchSize);
            _outputBlock       = new ActionBlock <LibratoMetric[]>(lines => PostToLibrato(lines), Utility.OneAtATimeExecution());
            _batchBlock.LinkTo(_outputBlock);

            _client = new RestClient(LIBRATO_API_URL);
            _client.Authenticator = new HttpBasicAuthenticator(_config.Email, _config.Token);
            _client.Timeout       = (int)_config.PostTimeout.TotalMilliseconds;

            _retryPolicy           = new RetryPolicy <LibratoErrorDetectionStrategy>(_config.NumRetries);
            _retryPolicy.Retrying += (sender, args) =>
            {
                _log.Warn(String.Format("Retry {0} failed. Trying again. Delay {1}, Error: {2}", args.CurrentRetryCount, args.Delay, args.LastException.Message), args.LastException);
                _systemMetrics.LogCount("backends.librato.retry");
            };
            _retryStrategy = new Incremental(_config.NumRetries, _config.RetryDelay, TimeSpan.FromSeconds(2));
            IsActive       = true;
        }