Skip to content

Commit

Permalink
Add process runtime information (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Jan 22, 2025
1 parent a9ffc35 commit 85e9742
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ def _initialize(self) -> None:
otel_resource_attributes: dict[str, Any] = {
ResourceAttributes.SERVICE_NAME: self.service_name,
ResourceAttributes.PROCESS_PID: os.getpid(),
# https://opentelemetry.io/docs/specs/semconv/resource/process/#python-runtimes
ResourceAttributes.PROCESS_RUNTIME_NAME: sys.implementation.name,
ResourceAttributes.PROCESS_RUNTIME_VERSION: get_runtime_version(),
ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION: sys.version,
# Having this giant blob of data associated with every span/metric causes various problems so it's
# disabled for now, but we may want to re-enable something like it in the future
# RESOURCE_ATTRIBUTES_PACKAGE_VERSIONS: json.dumps(collect_package_info(), separators=(',', ':')),
Expand Down Expand Up @@ -1560,5 +1564,12 @@ def default_project_name():
return sanitize_project_name(os.path.basename(os.getcwd()))


def get_runtime_version() -> str:
version_info = sys.implementation.version
if version_info.releaselevel == 'final' and not version_info.serial:
return '.'.join(map(str, version_info[:3]))
return '.'.join(map(str, version_info)) # pragma: no cover


class LogfireNotConfiguredWarning(UserWarning):
pass
22 changes: 22 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pytest
import requests.exceptions
import requests_mock
from dirty_equals import IsStr
from inline_snapshot import snapshot
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
Expand Down Expand Up @@ -62,6 +63,9 @@
from logfire.propagate import NoExtractTraceContextPropagator, WarnOnExtractTraceContextPropagator
from logfire.testing import TestExporter

PROCESS_RUNTIME_VERSION_REGEX = r'(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'
PROCESS_RUNTIME_DESCRIPTION_REGEX = r'(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+) \((?P<branch>\w+), (?P<month>[A-Za-z]{3})\s+(?P<day>\d{1,2}) (?P<year>\d{4}), (?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\)\s*\[\w+ (?P<clang_version>[\d.]+)\s*\]'


def test_propagate_config_to_tags(exporter: TestExporter) -> None:
tags1 = logfire.with_tags('tag1', 'tag2')
Expand Down Expand Up @@ -673,6 +677,9 @@ def test_otel_service_name_env_var(config_kwargs: dict[str, Any], exporter: Test
'service.name': 'potato',
'service.version': '1.2.3',
'service.instance.id': '00000000000000000000000000000000',
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
'process.pid': 1234,
}
},
Expand Down Expand Up @@ -716,6 +723,9 @@ def test_otel_otel_resource_attributes_env_var(config_kwargs: dict[str, Any], ex
'service.version': '1.2.3',
'service.instance.id': 'instance_id',
'process.pid': 1234,
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
}
},
}
Expand Down Expand Up @@ -760,6 +770,9 @@ def test_otel_service_name_has_priority_on_otel_resource_attributes_service_name
'service.version': '1.2.3',
'service.instance.id': '00000000000000000000000000000000',
'process.pid': 1234,
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
}
},
}
Expand Down Expand Up @@ -1658,6 +1671,9 @@ def test_environment(config_kwargs: dict[str, Any], exporter: TestExporter):
'telemetry.sdk.version': '0.0.0',
'service.name': 'unknown_service',
'process.pid': 1234,
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
'service.version': '1.2.3',
'deployment.environment.name': 'production',
}
Expand Down Expand Up @@ -1705,6 +1721,9 @@ def test_code_source(config_kwargs: dict[str, Any], exporter: TestExporter):
'telemetry.sdk.version': '0.0.0',
'service.name': 'unknown_service',
'process.pid': 1234,
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
'logfire.code.root_path': 'logfire',
'logfire.code.work_dir': os.getcwd(),
'vcs.repository.url.full': 'https://github.com/pydantic/logfire',
Expand Down Expand Up @@ -1754,6 +1773,9 @@ def test_code_source_without_root_path(config_kwargs: dict[str, Any], exporter:
'telemetry.sdk.version': '0.0.0',
'service.name': 'unknown_service',
'process.pid': 1234,
'process.runtime.name': 'cpython',
'process.runtime.version': IsStr(regex=PROCESS_RUNTIME_VERSION_REGEX),
'process.runtime.description': IsStr(regex=PROCESS_RUNTIME_DESCRIPTION_REGEX),
'logfire.code.work_dir': os.getcwd(),
'vcs.repository.url.full': 'https://github.com/pydantic/logfire',
'vcs.repository.ref.revision': 'main',
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85e9742

Please sign in to comment.