Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions docs/integrations/system-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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'],
})
```

Expand Down
29 changes: 14 additions & 15 deletions logfire/_internal/integrations/system_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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]]]
Expand All @@ -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,
Expand All @@ -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'],
Expand Down Expand Up @@ -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.<locals>.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.
Expand Down
23 changes: 9 additions & 14 deletions tests/otel_integrations/test_system_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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(
{
Expand All @@ -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'],
Expand Down Expand Up @@ -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'


Expand Down
Loading