Skip to content

Commit c8b0197

Browse files
devin-ai-integration[bot]bot_apk
andcommitted
refactor: remove tracemalloc/python_heap_bytes per review feedback
tracemalloc.start() has ~10-30% runtime overhead and was being activated silently for all production connectors. The cgroup-based metrics already provide container memory pressure visibility. tracemalloc-based heap metrics will be added separately behind an explicit opt-in env var flag. Co-Authored-By: bot_apk <apk@cognition.ai>
1 parent 58978c7 commit c8b0197

4 files changed

Lines changed: 4 additions & 50 deletions

File tree

airbyte_cdk/metrics/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
import time
1515
from typing import Any, Optional
1616

17-
from airbyte_cdk.metrics.memory import MemoryInfo, get_memory_info, get_python_heap_bytes
17+
from airbyte_cdk.metrics.memory import MemoryInfo, get_memory_info
1818

1919
logger = logging.getLogger(__name__)
2020

2121
# Metric names
2222
METRIC_MEMORY_USAGE_BYTES = "cdk.memory.usage_bytes"
2323
METRIC_MEMORY_LIMIT_BYTES = "cdk.memory.limit_bytes"
2424
METRIC_MEMORY_USAGE_PERCENT = "cdk.memory.usage_percent"
25-
METRIC_MEMORY_PYTHON_HEAP_BYTES = "cdk.memory.python_heap_bytes"
2625

2726
# Default emission interval in seconds
2827
DEFAULT_EMISSION_INTERVAL_SECONDS = 30.0
@@ -140,7 +139,6 @@ def emit_memory_metrics(self) -> None:
140139
- cdk.memory.usage_bytes: Current container memory usage
141140
- cdk.memory.limit_bytes: Container memory limit (if known)
142141
- cdk.memory.usage_percent: Usage/limit ratio (if limit is known)
143-
- cdk.memory.python_heap_bytes: Python-specific heap via tracemalloc
144142
"""
145143
if not self.enabled:
146144
return
@@ -156,10 +154,6 @@ def emit_memory_metrics(self) -> None:
156154
if info.usage_percent is not None:
157155
self.gauge(METRIC_MEMORY_USAGE_PERCENT, info.usage_percent)
158156

159-
python_heap = get_python_heap_bytes()
160-
if python_heap is not None:
161-
self.gauge(METRIC_MEMORY_PYTHON_HEAP_BYTES, float(python_heap))
162-
163157
except Exception:
164158
# Never let metric collection failures affect the sync
165159
logger.debug("Failed to collect memory metrics", exc_info=True)

airbyte_cdk/metrics/memory.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,3 @@ def get_memory_info() -> MemoryInfo:
149149

150150
# Fallback to rusage
151151
return _read_rusage_memory()
152-
153-
154-
def get_python_heap_bytes() -> Optional[int]:
155-
"""
156-
Get Python-specific heap memory usage via tracemalloc.
157-
158-
Returns the current traced memory size in bytes, or None if tracemalloc
159-
is not active. Tracemalloc is started lazily on first call.
160-
"""
161-
import tracemalloc
162-
163-
if not tracemalloc.is_tracing():
164-
try:
165-
tracemalloc.start()
166-
except RuntimeError:
167-
# tracemalloc may fail to start in some environments
168-
return None
169-
170-
current, _ = tracemalloc.get_traced_memory()
171-
return current

unit_tests/metrics/test_memory.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
_read_cgroup_v2_memory,
1616
_read_rusage_memory,
1717
get_memory_info,
18-
get_python_heap_bytes,
1918
)
2019

2120

@@ -177,11 +176,3 @@ def test_falls_back_to_rusage(self) -> None:
177176

178177
assert info.usage_bytes > 0
179178
assert info.limit_bytes is None
180-
181-
182-
class TestGetPythonHeapBytes:
183-
def test_returns_integer(self) -> None:
184-
result = get_python_heap_bytes()
185-
assert result is not None
186-
assert isinstance(result, int)
187-
assert result >= 0

unit_tests/metrics/test_metrics_client.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,25 @@ def test_emits_all_metrics_when_enabled(self) -> None:
130130
client, mock_instance = _make_enabled_client()
131131

132132
mock_info = MemoryInfo(usage_bytes=100_000_000, limit_bytes=200_000_000)
133-
with (
134-
patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info),
135-
patch("airbyte_cdk.metrics.get_python_heap_bytes", return_value=50_000_000),
136-
):
133+
with patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info):
137134
client.emit_memory_metrics()
138135

139136
gauge_calls = {call[0][0]: call[0][1] for call in mock_instance.gauge.call_args_list}
140137
assert gauge_calls["cdk.memory.usage_bytes"] == 100_000_000.0
141138
assert gauge_calls["cdk.memory.limit_bytes"] == 200_000_000.0
142139
assert gauge_calls["cdk.memory.usage_percent"] == pytest.approx(0.5)
143-
assert gauge_calls["cdk.memory.python_heap_bytes"] == 50_000_000.0
144140

145141
def test_skips_limit_when_unknown(self) -> None:
146142
client, mock_instance = _make_enabled_client()
147143

148144
mock_info = MemoryInfo(usage_bytes=100_000_000, limit_bytes=None)
149-
with (
150-
patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info),
151-
patch("airbyte_cdk.metrics.get_python_heap_bytes", return_value=None),
152-
):
145+
with patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info):
153146
client.emit_memory_metrics()
154147

155148
metric_names = [call[0][0] for call in mock_instance.gauge.call_args_list]
156149
assert "cdk.memory.usage_bytes" in metric_names
157150
assert "cdk.memory.limit_bytes" not in metric_names
158151
assert "cdk.memory.usage_percent" not in metric_names
159-
assert "cdk.memory.python_heap_bytes" not in metric_names
160152

161153
def test_noop_when_disabled(self) -> None:
162154
client = MetricsClient()
@@ -186,10 +178,7 @@ def test_emits_on_interval(self) -> None:
186178
client, mock_instance = _make_enabled_client()
187179

188180
mock_info = MemoryInfo(usage_bytes=100, limit_bytes=200)
189-
with (
190-
patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info),
191-
patch("airbyte_cdk.metrics.get_python_heap_bytes", return_value=50),
192-
):
181+
with patch("airbyte_cdk.metrics.get_memory_info", return_value=mock_info):
193182
client.maybe_emit_memory_metrics(interval_seconds=0.0)
194183
first_call_count = mock_instance.gauge.call_count
195184

0 commit comments

Comments
 (0)