From 6812da2787ef9265ed617f8bc89a9924981efb50 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 26 Nov 2024 09:32:24 +0100 Subject: [PATCH 1/2] opentelemetry-sdk: serialize logs severity_number as int (#4324) * opentelemetry-sdk: serialize logs severity_number as int Instead of a string. * Update changelog --- CHANGELOG.md | 2 ++ .../sdk/_logs/_internal/__init__.py | 4 +++- opentelemetry-sdk/tests/logs/test_log_record.py | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8fb2bd3f4d..ea5f2483efa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4270](https://github.com/open-telemetry/opentelemetry-python/pull/4270)) - api: fix logging of duplicate EventLogger setup warning ([#4299](https://github.com/open-telemetry/opentelemetry-python/pull/4299)) +- sdk: fix serialization of logs severity_number field to int + ([#4324](https://github.com/open-telemetry/opentelemetry-python/pull/4324)) ## Version 1.28.0/0.49b0 (2024-11-05) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index 110bb89c992..c2db81687a2 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -221,7 +221,9 @@ def to_json(self, indent=4) -> str: return json.dumps( { "body": self.body, - "severity_number": repr(self.severity_number), + "severity_number": self.severity_number.value + if self.severity_number is not None + else None, "severity_text": self.severity_text, "attributes": ( dict(self.attributes) if bool(self.attributes) else None diff --git a/opentelemetry-sdk/tests/logs/test_log_record.py b/opentelemetry-sdk/tests/logs/test_log_record.py index 9c3746989b1..f42d3a26ea4 100644 --- a/opentelemetry-sdk/tests/logs/test_log_record.py +++ b/opentelemetry-sdk/tests/logs/test_log_record.py @@ -16,6 +16,7 @@ import unittest import warnings +from opentelemetry._logs.severity import SeverityNumber from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk._logs import ( LogDroppedAttributesWarning, @@ -30,7 +31,7 @@ def test_log_record_to_json(self): expected = json.dumps( { "body": "a log line", - "severity_number": "None", + "severity_number": None, "severity_text": None, "attributes": None, "dropped_attributes": 0, @@ -56,9 +57,21 @@ def test_log_record_to_json(self): self.assertEqual(expected, actual.to_json(indent=4)) self.assertEqual( actual.to_json(indent=None), - '{"body": "a log line", "severity_number": "None", "severity_text": null, "attributes": null, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}', + '{"body": "a log line", "severity_number": null, "severity_text": null, "attributes": null, "dropped_attributes": 0, "timestamp": "1970-01-01T00:00:00.000000Z", "observed_timestamp": "1970-01-01T00:00:00.000000Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": {"attributes": {"service.name": "foo"}, "schema_url": ""}}', ) + def test_log_record_to_json_serializes_severity_number_as_int(self): + actual = LogRecord( + timestamp=0, + severity_number=SeverityNumber.WARN, + observed_timestamp=0, + body="a log line", + resource=Resource({"service.name": "foo"}), + ) + + decoded = json.loads(actual.to_json()) + self.assertEqual(SeverityNumber.WARN.value, decoded["severity_number"]) + def test_log_record_bounded_attributes(self): attr = {"key": "value"} From 1ee27746acd4fc07226e23407f2276d33e13c6e8 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 26 Nov 2024 22:22:52 +0800 Subject: [PATCH 2/2] Fix typo: `pustil` -> `psutil` (#4311) * Fix typo: `pustil` -> `psutil` * Update CHANGELOG.md * Update test-requirements.txt --------- Co-authored-by: Riccardo Magliocchetti --- CHANGELOG.md | 2 ++ opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py | 2 +- opentelemetry-sdk/test-requirements.txt | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea5f2483efa..b83101842fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4270](https://github.com/open-telemetry/opentelemetry-python/pull/4270)) - api: fix logging of duplicate EventLogger setup warning ([#4299](https://github.com/open-telemetry/opentelemetry-python/pull/4299)) +- sdk: fix setting of process owner in ProcessResourceDetector + ([#4311](https://github.com/open-telemetry/opentelemetry-python/pull/4311)) - sdk: fix serialization of logs severity_number field to int ([#4324](https://github.com/open-telemetry/opentelemetry-python/pull/4324)) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 3235ba10dcd..a52573ac1c9 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -84,7 +84,7 @@ try: import psutil as psutil_module - pustil = psutil_module + psutil = psutil_module except ImportError: pass diff --git a/opentelemetry-sdk/test-requirements.txt b/opentelemetry-sdk/test-requirements.txt index 8a7ccc7bd78..68755072fb1 100644 --- a/opentelemetry-sdk/test-requirements.txt +++ b/opentelemetry-sdk/test-requirements.txt @@ -5,6 +5,7 @@ importlib-metadata==6.11.0 iniconfig==2.0.0 packaging==24.0 pluggy==1.5.0 +psutil==5.9.6; sys_platform != 'win32' py-cpuinfo==9.0.0 pytest==7.4.4 tomli==2.0.1