private void PostToLibratoInternal(LibratoMetric[] lines) { var pendingLines = 0; foreach (var epochGroup in lines.GroupBy(p => p.Epoch)) { var payload = GetPayload(epochGroup); pendingLines = payload.gauges.Length + payload.counters.Length; _systemMetrics.LogGauge("backends.librato.lines", pendingLines); Interlocked.Add(ref _pendingOutputCount, pendingLines); var request = new RestRequest("/v1/metrics", Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader("User-Agent", "statsd.net-librato-backend/" + _serviceVersion); request.AddBody(payload); _retryPolicy.ExecuteAction(() => { bool succeeded = false; try { _systemMetrics.LogCount("backends.librato.post.attempt"); var result = _client.Execute(request); if (result.StatusCode == HttpStatusCode.Unauthorized) { _systemMetrics.LogCount("backends.librato.error.unauthorised"); throw new UnauthorizedAccessException("Librato.com reports that your access is not authorised. Is your API key and email address correct?"); } else if (result.StatusCode != HttpStatusCode.OK) { _systemMetrics.LogCount("backends.librato.error." + result.StatusCode.ToString()); throw new Exception(String.Format("Request could not be processed. Server said {0}", result.StatusCode.ToString())); } else { succeeded = true; _log.Info(String.Format("Wrote {0} lines to Librato.", pendingLines)); } } finally { Interlocked.Add(ref _pendingOutputCount, -pendingLines); _systemMetrics.LogCount("backends.librato.post." + (succeeded ? "success" : "failure")); } }); } }
private void PostToLibrato(LibratoMetric[] lines) { try { PostToLibratoInternal(lines); } catch (Exception ex) { _log.Error("Failed to post metrics to Librato.com", ex); _systemMetrics.LogCount("backends.librato.post.error." + ex.GetType().Name); } }