protected override void OnEventWritten(EventWrittenEventArgs eventData) { if (eventData.EventId == EventGcSuspendBegin) { _gcStart = eventData.TimeStamp; } else if (eventData.EventId == EventGcRestartEnd) { var start = _gcStart; if (start != null) { GcPauseTime?.Invoke(eventData.TimeStamp - start.Value); } } else if (eventData.EventId == EventGcHeapStats) { GcHeapStats?.Invoke(HeapStats.FromPayload(eventData.Payload)); } else if (eventData.EventId == EventContentionStop) { Contention?.Invoke((double)eventData.Payload[2]); } else if (eventData.EventId == EventGcGlobalHeapHistory) { GcHeapHistory?.Invoke(HeapHistory.FromPayload(eventData.Payload)); } }
protected override void OnEventWritten(EventWrittenEventArgs eventData) { if (_statsd == null) { // I know it sounds crazy at first, but because OnEventSourceCreated is called from the base constructor, // and EnableEvents is called from OnEventSourceCreated, it's entirely possible that OnEventWritten // gets called before the child constructor is called. // In that case, just bail out. return; } try { if (eventData.EventName == "EventCounters") { ExtractCounters(eventData.Payload); } else if (eventData.EventId == EventGcSuspendBegin) { _gcStart = eventData.TimeStamp; } else if (eventData.EventId == EventGcRestartEnd) { var start = _gcStart; if (start != null) { _statsd.Timer(MetricsNames.GcPauseTime, (eventData.TimeStamp - start.Value).TotalMilliseconds); Log.Debug("Sent the following metrics: {metrics}", MetricsNames.GcPauseTime); } } else { if (eventData.EventId == EventGcHeapStats) { var stats = HeapStats.FromPayload(eventData.Payload); _statsd.Gauge(MetricsNames.Gen0HeapSize, stats.Gen0Size); _statsd.Gauge(MetricsNames.Gen1HeapSize, stats.Gen1Size); _statsd.Gauge(MetricsNames.Gen2HeapSize, stats.Gen2Size); _statsd.Gauge(MetricsNames.LohSize, stats.LohSize); Log.Debug("Sent the following metrics: {metrics}", GcHeapStatsMetrics); } else if (eventData.EventId == EventContentionStop) { var durationInNanoseconds = (double)eventData.Payload[2]; _contentionTime.Time(durationInNanoseconds / 1_000_000); Interlocked.Increment(ref _contentionCount); } else if (eventData.EventId == EventGcGlobalHeapHistory) { var heapHistory = HeapHistory.FromPayload(eventData.Payload); if (heapHistory.MemoryLoad != null) { _statsd.Gauge(MetricsNames.GcMemoryLoad, heapHistory.MemoryLoad.Value); } _statsd.Increment(GcCountMetricNames[heapHistory.Generation], 1, tags: heapHistory.Compacting ? CompactingGcTags : NotCompactingGcTags); Log.Debug("Sent the following metrics: {metrics}", GcGlobalHeapMetrics); } } } catch (Exception ex) { Log.Warning <int, string>(ex, "Error while processing event {EventId} {EventName}", eventData.EventId, eventData.EventName); } }