|
| 1 | +"""Memory usage metrics for Prometheus monitoring.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from prometheus_client import Gauge, Histogram |
| 8 | + |
| 9 | +from gitingest.utils.memory_utils import get_memory_usage |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + import types |
| 13 | + from typing import Self |
| 14 | + |
| 15 | +# Memory usage gauges |
| 16 | +memory_usage_rss_mb = Gauge( |
| 17 | + "gitingest_memory_usage_rss_mb", |
| 18 | + "Resident Set Size memory usage in MB", |
| 19 | + ["repo_url"], |
| 20 | +) |
| 21 | + |
| 22 | +memory_usage_vms_mb = Gauge( |
| 23 | + "gitingest_memory_usage_vms_mb", |
| 24 | + "Virtual Memory Size usage in MB", |
| 25 | + ["repo_url"], |
| 26 | +) |
| 27 | + |
| 28 | +memory_usage_percent = Gauge( |
| 29 | + "gitingest_memory_usage_percent", |
| 30 | + "Memory usage percentage", |
| 31 | + ["repo_url"], |
| 32 | +) |
| 33 | + |
| 34 | +# Memory usage histogram to track distribution of memory consumption per repository |
| 35 | +memory_consumption_histogram = Histogram( |
| 36 | + "gitingest_memory_consumption_mb", |
| 37 | + "Memory consumption distribution per repository in MB", |
| 38 | + ["repo_url"], |
| 39 | + buckets=(50, 100, 250, 500, 1000, 2000, 3000, 5000, 10000, float("inf")), |
| 40 | +) |
| 41 | + |
| 42 | +# Peak memory usage gauge |
| 43 | +peak_memory_usage_mb = Gauge( |
| 44 | + "gitingest_peak_memory_usage_mb", |
| 45 | + "Peak memory usage during ingestion in MB", |
| 46 | + ["repo_url"], |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +def record_memory_usage(repo_url: str) -> dict[str, float]: |
| 51 | + """Record current memory usage metrics for a repository. |
| 52 | +
|
| 53 | + Parameters |
| 54 | + ---------- |
| 55 | + repo_url : str |
| 56 | + The repository URL to label the metrics with |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + dict[str, float] |
| 61 | + Current memory usage statistics |
| 62 | +
|
| 63 | + """ |
| 64 | + # Truncate URL for label to avoid excessive cardinality |
| 65 | + repo_label = repo_url[:255] |
| 66 | + |
| 67 | + # Get current memory stats |
| 68 | + memory_stats = get_memory_usage() |
| 69 | + |
| 70 | + # Record current memory usage |
| 71 | + memory_usage_rss_mb.labels(repo_url=repo_label).set(memory_stats["rss_mb"]) |
| 72 | + memory_usage_vms_mb.labels(repo_url=repo_label).set(memory_stats["vms_mb"]) |
| 73 | + memory_usage_percent.labels(repo_url=repo_label).set(memory_stats["percent"]) |
| 74 | + |
| 75 | + # Record in histogram for distribution analysis |
| 76 | + memory_consumption_histogram.labels(repo_url=repo_label).observe(memory_stats["rss_mb"]) |
| 77 | + |
| 78 | + return memory_stats |
| 79 | + |
| 80 | + |
| 81 | +def record_peak_memory_usage(repo_url: str, peak_mb: float) -> None: |
| 82 | + """Record peak memory usage for a repository ingestion. |
| 83 | +
|
| 84 | + Parameters |
| 85 | + ---------- |
| 86 | + repo_url : str |
| 87 | + The repository URL to label the metrics with |
| 88 | + peak_mb : float |
| 89 | + Peak memory usage in MB |
| 90 | +
|
| 91 | + """ |
| 92 | + repo_label = repo_url[:255] |
| 93 | + peak_memory_usage_mb.labels(repo_url=repo_label).set(peak_mb) |
| 94 | + |
| 95 | + |
| 96 | +class MemoryTracker: |
| 97 | + """Context manager to track memory usage during repository ingestion. |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + repo_url : str |
| 102 | + Repository URL for labeling metrics |
| 103 | +
|
| 104 | + """ |
| 105 | + |
| 106 | + def __init__(self, repo_url: str) -> None: |
| 107 | + self.repo_url = repo_url |
| 108 | + self.initial_memory = 0.0 |
| 109 | + self.peak_memory = 0.0 |
| 110 | + |
| 111 | + def __enter__(self) -> Self: |
| 112 | + """Start memory tracking.""" |
| 113 | + initial_stats = get_memory_usage() |
| 114 | + self.initial_memory = initial_stats["rss_mb"] |
| 115 | + self.peak_memory = self.initial_memory |
| 116 | + |
| 117 | + # Record initial memory usage |
| 118 | + record_memory_usage(self.repo_url) |
| 119 | + |
| 120 | + return self |
| 121 | + |
| 122 | + def __exit__( |
| 123 | + self, |
| 124 | + exc_type: type[BaseException] | None, |
| 125 | + exc_val: BaseException | None, |
| 126 | + exc_tb: types.TracebackType | None, |
| 127 | + ) -> None: |
| 128 | + """End memory tracking and record peak usage.""" |
| 129 | + # Record final memory usage |
| 130 | + final_stats = record_memory_usage(self.repo_url) |
| 131 | + |
| 132 | + # Update peak if current is higher |
| 133 | + self.peak_memory = max(self.peak_memory, final_stats["rss_mb"]) |
| 134 | + |
| 135 | + # Record peak memory usage |
| 136 | + record_peak_memory_usage(self.repo_url, self.peak_memory) |
| 137 | + |
| 138 | + def update_peak(self) -> None: |
| 139 | + """Update peak memory if current usage is higher.""" |
| 140 | + current_stats = get_memory_usage() |
| 141 | + self.peak_memory = max(self.peak_memory, current_stats["rss_mb"]) |
| 142 | + |
| 143 | + # Also record current usage |
| 144 | + record_memory_usage(self.repo_url) |
0 commit comments