diff --git a/docs/integrations/system-metrics.md b/docs/integrations/system-metrics.md index 4366e3f09..218e4d043 100644 --- a/docs/integrations/system-metrics.md +++ b/docs/integrations/system-metrics.md @@ -28,15 +28,15 @@ By default, `instrument_system_metrics` collects only the metrics it needs to di ```py logfire.instrument_system_metrics({ - 'process.runtime.cpu.utilization': None, # (1)! + 'process.cpu.utilization': None, # (1)! 'system.cpu.simple_utilization': None, # (2)! 'system.memory.utilization': ['available'], # (3)! 'system.swap.utilization': ['used'], # (4)! }) ``` -1. `process.runtime.cpu.utilization` will lead to exporting a metric that is actually named `process.runtime.cpython.cpu.utilization` or a similar name depending on the Python implementation used. The `None` value means that there are no fields to configure for this metric. The value of this metric is [`psutil.Process().cpu_percent()`](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent)`/ 100`, i.e. the fraction of CPU time used by this process, where 1 means using 100% of a single CPU core. The value can be greater than 1 if the process uses multiple cores. In the next major release, the default will instead emit `process.cpu.core_utilization`, which is the same metric but with a simpler name. -2. The `None` value means that there are no fields to configure for this metric. The value of this metric is [`psutil.cpu_percent()`](https://psutil.readthedocs.io/en/latest/#psutil.cpu_percent)`/ 100`, i.e. the fraction of CPU time used by the whole system, where 1 means using 100% of all CPU cores. +1. The `None` value means that there are no fields to configure for this metric. The value of this metric is [`psutil.Process().cpu_percent()`](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent)`/100`, i.e. the fraction of CPU time used by this process, where 1 means using 100% of a single CPU core. The value can be greater than 1 if the process uses multiple cores. +2. The `None` value means that there are no fields to configure for this metric. The value of this metric is [`psutil.cpu_percent()`](https://psutil.readthedocs.io/en/latest/#psutil.cpu_percent)`/100`, i.e. the fraction of CPU time used by the whole system, where 1 means using 100% of all CPU cores. 3. The value here is a list of 'modes' of memory. The full list can be seen in the [`psutil` documentation](https://psutil.readthedocs.io/en/latest/#psutil.virtual_memory). `available` is "the memory that can be given instantly to processes without the system going into swap. This is calculated by summing different memory metrics that vary depending on the platform. It is supposed to be used to monitor actual memory usage in a cross platform fashion." The value of the metric is a number between 0 and 1, and subtracting the value from 1 gives the fraction of memory used. 4. This is the fraction of available swap used. The value is a number between 0 and 1. @@ -78,13 +78,6 @@ logfire.instrument_system_metrics({ 'process.memory.usage': None, 'process.memory.virtual': None, 'process.thread.count': None, - # These are deprecated and equivalent to some of the above. - # base='full' will stop including them in the next major release. - 'process.runtime.memory': ['rss', 'vms'], - 'process.runtime.cpu.time': ['user', 'system'], - 'process.runtime.thread_count': None, - 'process.runtime.cpu.utilization': None, - 'process.runtime.context_switches': ['involuntary', 'voluntary'], }) ``` diff --git a/logfire/_internal/integrations/system_metrics.py b/logfire/_internal/integrations/system_metrics.py index 8f8007e2c..259a5f6b8 100644 --- a/logfire/_internal/integrations/system_metrics.py +++ b/logfire/_internal/integrations/system_metrics.py @@ -53,12 +53,6 @@ 'process.memory.virtual', 'process.thread.count', 'process.runtime.gc_count', - # ##### These are deprecated: - 'process.runtime.memory', - 'process.runtime.cpu.time', - 'process.runtime.thread_count', - 'process.runtime.cpu.utilization', - 'process.runtime.context_switches', ] ] = Literal[ # type: ignore # but pyright doesn't like it 'system.cpu.simple_utilization', @@ -86,12 +80,6 @@ 'process.memory.virtual', 'process.thread.count', 'process.runtime.gc_count', - # ##### These are deprecated: - 'process.runtime.memory', - 'process.runtime.cpu.time', - 'process.runtime.thread_count', - 'process.runtime.cpu.utilization', - 'process.runtime.context_switches', ] Config = dict[MetricName, Optional[Iterable[str]]] @@ -109,6 +97,7 @@ FULL_CONFIG: Config = { **cast(Config, _DEFAULT_CONFIG), 'system.cpu.simple_utilization': None, + 'process.cpu.utilization': None, 'process.cpu.core_utilization': None, 'system.cpu.time': CPU_FIELDS, 'system.cpu.utilization': CPU_FIELDS, @@ -125,8 +114,17 @@ # upstream pr: https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2008 FULL_CONFIG.pop('system.network.connections', None) +for _deprecated in [ + 'process.runtime.memory', + 'process.runtime.cpu.time', + 'process.runtime.thread_count', + 'process.runtime.cpu.utilization', + 'process.runtime.context_switches', +]: + FULL_CONFIG.pop(_deprecated, None) # type: ignore + BASIC_CONFIG: Config = { - 'process.runtime.cpu.utilization': None, + 'process.cpu.utilization': None, 'system.cpu.simple_utilization': None, # The actually used memory ratio can be calculated as `1 - available`. 'system.memory.utilization': ['available'], @@ -156,10 +154,11 @@ def instrument_system_metrics(logfire_instance: Logfire, config: Config | None = if 'process.cpu.core_utilization' in config: measure_process_cpu_core_utilization(logfire_instance) - if 'process.runtime.cpu.utilization' in config: + if 'process.runtime.cpu.utilization' in config: # type: ignore # Override OTEL here, see comment in measure_process_runtime_cpu_utilization..callback. + # (The name is also deprecated by OTEL, but that's not really important) measure_process_runtime_cpu_utilization(logfire_instance) - del config['process.runtime.cpu.utilization'] + del config['process.runtime.cpu.utilization'] # type: ignore if 'process.cpu.utilization' in config: # Override OTEL here to avoid emitting 0 in the first measurement. diff --git a/tests/otel_integrations/test_system_metrics.py b/tests/otel_integrations/test_system_metrics.py index 91f6306be..ad6cd9aa4 100644 --- a/tests/otel_integrations/test_system_metrics.py +++ b/tests/otel_integrations/test_system_metrics.py @@ -27,7 +27,7 @@ def test_default_system_metrics_collection(metrics_reader: InMemoryMetricReader) logfire.instrument_system_metrics() assert get_collected_metric_names(metrics_reader) == snapshot( [ - 'process.runtime.cpython.cpu.utilization', + 'process.cpu.utilization', 'system.cpu.simple_utilization', 'system.memory.utilization', 'system.swap.utilization', @@ -46,12 +46,7 @@ def test_all_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> 'process.memory.usage', 'process.memory.virtual', 'process.open_file_descriptor.count', - 'process.runtime.cpython.context_switches', - 'process.runtime.cpython.cpu.utilization', - 'process.runtime.cpython.cpu_time', 'process.runtime.cpython.gc_count', - 'process.runtime.cpython.memory', - 'process.runtime.cpython.thread_count', 'process.thread.count', 'system.cpu.simple_utilization', 'system.cpu.time', @@ -72,6 +67,12 @@ def test_all_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> ) +def test_measure_process_runtime_cpu_utilization(metrics_reader: InMemoryMetricReader) -> None: + # This metric is now deprecated by OTEL, but there isn't a strong reason to stop allowing it when requested + logfire.instrument_system_metrics({'process.runtime.cpu.utilization': None}, base=None) # type: ignore + assert get_collected_metric_names(metrics_reader) == ['process.runtime.cpython.cpu.utilization'] + + def test_custom_system_metrics_collection(metrics_reader: InMemoryMetricReader) -> None: logfire.instrument_system_metrics( { @@ -92,7 +93,7 @@ def test_custom_system_metrics_collection(metrics_reader: InMemoryMetricReader) def test_basic_base(): assert get_base_config('basic') == { - 'process.runtime.cpu.utilization': None, + 'process.cpu.utilization': None, 'system.cpu.simple_utilization': None, 'system.memory.utilization': ['available'], 'system.swap.utilization': ['used'], @@ -158,16 +159,10 @@ def test_full_base(): 'process.memory.virtual': None, 'process.cpu.time': ['user', 'system'], # There's no reason for OTel to give a value here, so the docs say `None` - 'process.cpu.utilization': ['user', 'system'], + 'process.cpu.utilization': None, 'process.cpu.core_utilization': None, 'process.thread.count': None, 'process.context_switches': ['involuntary', 'voluntary'], - # These are deprecated: - 'process.runtime.memory': ['rss', 'vms'], - 'process.runtime.cpu.time': ['user', 'system'], - 'process.runtime.cpu.utilization': None, - 'process.runtime.thread_count': None, - 'process.runtime.context_switches': ['involuntary', 'voluntary'], }, 'Docs and the MetricName type need to be updated if this test fails'