public void UpdateMetrics() { if (_gcInfo.Enabled) { GcCpuRatio?.Set(_gcCpuRatio.CalculateConsumedRatio(GcCollectionSeconds)); GcPauseRatio?.Set(_gcPauseRatio.CalculateConsumedRatio(GcPauseSeconds)); } #if !NETSTANDARD2_1 AvailableMemory?.Set(GC.GetGCMemoryInfo().TotalAvailableMemoryBytes); #endif }
public void UpdateMetrics() { GcCpuRatio.Set(_gcCpuRatio.CalculateConsumedRatio(GcCollectionSeconds)); GcPauseRatio.Set(_gcPauseRatio.CalculateConsumedRatio(GcPauseSeconds)); }
public void RegisterMetrics(MetricFactory metrics) { #if !NETSTANDARD2_1 AvailableMemory = metrics.CreateGauge("dotnet_gc_memory_total_available_bytes", "The upper limit on the amount of physical memory .NET can allocate to"); #endif // No registered sources available- cannot produce metrics if (!_gcInfo.Enabled && !_gcVerbose.Enabled && !_runtimeCounters.Enabled) { return; } if (_runtimeCounters.Enabled && !_gcInfo.Enabled) { GcPauseRatio = metrics.CreateGauge("dotnet_gc_pause_ratio", "The percentage of time the process spent paused for garbage collection"); _runtimeCounters.Events.TimeInGc += e => GcPauseRatio.Set(e.Mean / 100.0); GcHeapSizeBytes = metrics.CreateGauge( "dotnet_gc_heap_size_bytes", "The current size of all heaps (only updated after a garbage collection)", LabelGeneration); _runtimeCounters.Events.Gen0Size += e => GcHeapSizeBytes.Labels("0").Set(e.Mean); _runtimeCounters.Events.Gen1Size += e => GcHeapSizeBytes.Labels("1").Set(e.Mean); _runtimeCounters.Events.Gen2Size += e => GcHeapSizeBytes.Labels("2").Set(e.Mean); _runtimeCounters.Events.LohSize += e => GcHeapSizeBytes.Labels("loh").Set(e.Mean); GcCollections = metrics.CreateCounter( "dotnet_gc_collection_count_total", "Counts the number of garbage collections that have occurred, broken down by generation number.", LabelGeneration); _runtimeCounters.Events.Gen0GcCount += e => GcCollections.Labels("0").Inc(e.IncrementedBy); _runtimeCounters.Events.Gen1GcCount += e => GcCollections.Labels("1").Inc(e.IncrementedBy); _runtimeCounters.Events.Gen2GcCount += e => GcCollections.Labels("2").Inc(e.IncrementedBy); } if (_gcInfo.Enabled) { GcCollectionSeconds = metrics.CreateHistogram( "dotnet_gc_collection_seconds", "The amount of time spent running garbage collections", new HistogramConfiguration() { Buckets = _options.HistogramBuckets, LabelNames = new[] { LabelGeneration, LabelType } } ); _gcInfo.Events.CollectionComplete += (e) => GcCollectionSeconds.Labels(GetGenerationToString(e.Generation), GcTypeToLabels[e.Type]).Observe(e.Duration.TotalSeconds); GcPauseSeconds = metrics.CreateHistogram( "dotnet_gc_pause_seconds", "The amount of time execution was paused for garbage collection", new HistogramConfiguration() { Buckets = _options.HistogramBuckets } ); _gcInfo.Events.PauseComplete += (e) => GcPauseSeconds.Observe(e.PauseDuration.TotalSeconds); GcCollections = metrics.CreateCounter( "dotnet_gc_collection_count_total", "Counts the number of garbage collections that have occurred, broken down by generation number and the reason for the collection.", LabelGeneration, LabelReason); _gcInfo.Events.CollectionStart += (e) => GcCollections.Labels(GetGenerationToString(e.Generation), GcReasonToLabels[e.Reason]).Inc(); GcCpuRatio = metrics.CreateGauge("dotnet_gc_cpu_ratio", "The percentage of process CPU time spent running garbage collections"); GcPauseRatio = metrics.CreateGauge("dotnet_gc_pause_ratio", "The percentage of time the process spent paused for garbage collection"); GcHeapSizeBytes = metrics.CreateGauge( "dotnet_gc_heap_size_bytes", "The current size of all heaps (only updated after a garbage collection)", LabelGeneration); GcNumPinnedObjects = metrics.CreateGauge("dotnet_gc_pinned_objects", "The number of pinned objects"); GcFinalizationQueueLength = metrics.CreateGauge("dotnet_gc_finalization_queue_length", "The number of objects waiting to be finalized"); _gcInfo.Events.HeapStats += e => { GcHeapSizeBytes.Labels("0").Set(e.Gen0SizeBytes); GcHeapSizeBytes.Labels("1").Set(e.Gen1SizeBytes); GcHeapSizeBytes.Labels("2").Set(e.Gen2SizeBytes); GcHeapSizeBytes.Labels("loh").Set(e.LohSizeBytes); GcFinalizationQueueLength.Set(e.FinalizationQueueLength); GcNumPinnedObjects.Set(e.NumPinnedObjects); }; } if (_gcVerbose.Enabled || _runtimeCounters.Enabled) { AllocatedBytes = metrics.CreateCounter( "dotnet_gc_allocated_bytes_total", "The total number of bytes allocated on the managed heap", labelNames: _gcVerbose.Enabled ? new [] { LabelHeap } : new string[0]); if (_gcVerbose.Enabled) { _gcVerbose.Events.AllocationTick += e => AllocatedBytes.Labels(e.IsLargeObjectHeap ? "loh" : "soh").Inc(e.AllocatedBytes); } else { _runtimeCounters.Events.AllocRate += r => AllocatedBytes.Inc(r.IncrementedBy); } } }