void Snapshot(object isCalledFromTimer) { if ((bool)isCalledFromTimer && ShutdownCalled) // don't perform timer actions if we're shutting down { return; } try { if (BeforeSerialization != null && BeforeSerialization.GetInvocationList().Length != 0) { BeforeSerialization(); } var sw = new Stopwatch(); sw.Start(); SerializeMetrics(out var metricsCount, out var bytesWritten); sw.Stop(); var info = new AfterSerializationInfo { Count = metricsCount, BytesWritten = bytesWritten, Duration = sw.Elapsed, }; AfterSerialization?.Invoke(info); } catch (Exception ex) { SendExceptionToHandler(ex); } }
private Task SnapshotAsync() { try { var beforeSerialization = BeforeSerialization; if (beforeSerialization?.GetInvocationList().Length > 0) { beforeSerialization(); } var timestamp = DateTime.UtcNow; var sw = Stopwatch.StartNew(); var batch = new CompositeBatch(_batches); for (var i = 0; i < _endpoints.Length; i++) { _batches[i] = _endpoints[i].Handler.BeginBatch(); } foreach (var source in _sources) { source.Write(batch, timestamp); } if (_hasNewMetadata || DateTime.UtcNow - _lastMetadataFlushTime >= TimeSpan.FromDays(1)) { using (var metadata = _sources.GetMetadata()) { if (metadata.Count > 0) { for (var i = 0; i < _endpoints.Length; i++) { SerializeMetadata(_endpoints[i], metadata); } } } _hasNewMetadata = false; } sw.Stop(); var stats = batch.GetStatistics(); AfterSerialization?.Invoke( new AfterSerializationInfo { BytesWritten = stats.BytesWritten, Count = stats.MetricsWritten, Duration = sw.Elapsed, StartTime = timestamp }); } catch (Exception ex) { SendExceptionToHandler(ex); } return(Task.CompletedTask); }
Task SnapshotAsync(bool isCalledFromTimer) { if (isCalledFromTimer && ShutdownCalled) // don't perform timer actions if we're shutting down { return(Task.CompletedTask); } try { if (BeforeSerialization != null && BeforeSerialization.GetInvocationList().Length != 0) { BeforeSerialization(); } IReadOnlyList <MetaData> metadata = Array.Empty <MetaData>(); if (_hasNewMetadata || DateTime.UtcNow - _lastMetadataFlushTime >= TimeSpan.FromDays(1)) { metadata = GatherMetaData(); } // prep all metrics for serialization var timestamp = DateTime.UtcNow; if (_metricsNeedingPreSerialize.Count > 0) { Parallel.ForEach(_metricsNeedingPreSerialize, m => m.PreSerializeInternal()); } var sw = new Stopwatch(); foreach (var endpoint in _endpoints) { sw.Restart(); SerializeMetrics(endpoint, timestamp, out var metricsCount, out var bytesWritten); // We don't want to send metadata more frequently than the snapshot interval, so serialize it out if we need to if (metadata.Count > 0) { SerializeMetadata(endpoint, metadata); } sw.Stop(); AfterSerialization?.Invoke( new AfterSerializationInfo { Endpoint = endpoint.Name, Count = metricsCount, BytesWritten = bytesWritten, Duration = sw.Elapsed, }); } } catch (Exception ex) { SendExceptionToHandler(ex); } return(Task.CompletedTask); }
private void Snapshot(object isCalledFromTimer) { if ((bool)isCalledFromTimer && ShutdownCalled) // don't perform timer actions if we're shutting down { return; } if (GetBosunUrl != null) { BosunUrl = GetBosunUrl(); } try { if (BeforeSerialization != null && BeforeSerialization.GetInvocationList().Length != 0) { BeforeSerialization(); } var sw = new StopwatchStruct(); sw.Start(); int metricsCount, bytesWritten; SerializeMetrics(out metricsCount, out bytesWritten); sw.Stop(); var info = new AfterSerializationInfo { Count = metricsCount, BytesWritten = bytesWritten, MillisecondsDuration = sw.GetElapsedMilliseconds(), }; LastSerializationInfo = info; AfterSerialization?.Invoke(info); } catch (Exception ex) { if (ShouldThrowException(ex)) { if (HasExceptionHandler) { OnBackgroundException(ex); } else { throw; } } } }
private void Snapshot(object isCalledFromTimer) { if ((bool)isCalledFromTimer && ShutdownCalled) // don't perform timer actions if we're shutting down { return; } if (GetBosunUrl != null) { BosunUrl = GetBosunUrl(); } try { if (BeforeSerialization != null && BeforeSerialization.GetInvocationList().Length != 0) { BeforeSerialization(); } var info = new AfterSerializationInfo(); var sw = new StopwatchStruct(); sw.Start(); var list = GetSerializedMetrics(); sw.Stop(); EnqueueMetrics(list); info.Count = list.Count; info.MillisecondsDuration = sw.GetElapsedMilliseconds(); LastSerializationInfo = info; AfterSerialization?.Invoke(info); } catch (Exception e) { if (HasExceptionHandler) { OnBackgroundException(e); return; } throw; } }