Task OnTick() { try { Queue <PointData> batch; lock (_queueLock) { if (_queue.Count == 0) { return(Task.Delay(0)); } batch = _queue; _queue = new Queue <PointData>(); } _parent.Emit(batch.ToArray()); } catch (Exception ex) { CollectorLog.ReportError("Failed to emit metrics batch", ex); } finally { lock (_stateLock) { if (!_unloading) { _timer.Start(_interval); } } } return(Task.Delay(0)); }
public void ListenerCanUnregister() { var invocationCount = 0; using (CollectorLog.RegisterErrorHandler((message, exception) => invocationCount++)) { CollectorLog.ReportError("", null); } CollectorLog.ReportError("", null); Assert.Equal(1, invocationCount); }
public void Write(string measurement, IReadOnlyDictionary <string, object> fields, IReadOnlyDictionary <string, string> tags = null, DateTime?timestamp = null) { try { var point = new PointData(measurement, fields, tags, timestamp ?? DateTime.UtcNow); Emit(new[] { point }); } catch (Exception ex) { CollectorLog.ReportError("Failed to write point", ex); } }
public void SingleListenerGetsInvoked() { var invocationCount = 0; Exception dummyException = new Exception("Bang!"); const string errorMessage = "Bad things"; CollectorLog.RegisterErrorHandler((message, exception) => { Assert.Equal(errorMessage, message); Assert.Equal(dummyException, exception); invocationCount++; }); CollectorLog.ReportError(errorMessage, dummyException); Assert.Equal(1, invocationCount); }
public void Emit(PointData[] points) { var payload = new LineProtocolPayload(); foreach (var point in points) { payload.Add(new LineProtocolPoint(point.Measurement, point.Fields, point.Tags, point.UtcTimestamp)); } var influxResult = _client.WriteAsync(payload).Result; if (!influxResult.Success) { CollectorLog.ReportError(influxResult.ErrorMessage, null); } }
async void OnTick() { try { lock (_stateLock) { if (_disposed) { return; } // There's a little bit of raciness here, but it's needed to support the // current API, which allows the tick handler to reenter and set the next interval. if (_running) { Monitor.Wait(_stateLock); if (_disposed) { return; } } _running = true; } if (!_cancel.Token.IsCancellationRequested) { await _onTick(_cancel.Token); } } catch (OperationCanceledException tcx) { CollectorLog.ReportError("The timer was canceled during invocation", tcx); } finally { lock (_stateLock) { _running = false; Monitor.PulseAll(_stateLock); } } }
Task OnTick() { try { Queue <PointData> batch; lock (_queueLock) { if (_queue.Count == 0) { return(Task.Delay(0)); } batch = _queue; _queue = new Queue <PointData>(); } if (_maxBatchSize == null || batch.Count <= _maxBatchSize.Value) { _parent.Emit(batch.ToArray()); } else { foreach (var chunk in batch.Batch(_maxBatchSize.Value)) { _parent.Emit(chunk.ToArray()); } } } catch (Exception ex) { CollectorLog.ReportError("Failed to emit metrics batch", ex); } finally { lock (_stateLock) { if (!_unloading) { _timer.Start(_interval); } } } return(Task.Delay(0)); }
public void EnsureListenerExceptionPropagates() { CollectorLog.RegisterErrorHandler((message, exception) => { throw new Exception(""); }); var threw = false; try { CollectorLog.ReportError("", null); } catch { threw = true; } Assert.True(threw, "Excpected an exception thrown by the error handler to propagate to the caller"); }
public void CanReportWithoutListeners() { CollectorLog.ReportError("", null); }