Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve resource field structure for LogRecords #3972

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Log a warning when a `LogRecord` in `sdk/log` has dropped attributes
due to reaching limits
([#3946](https://github.com/open-telemetry/opentelemetry-python/pull/3946))

- Fix RandomIdGenerator can generate invalid Span/Trace Ids
([#3949](https://github.com/open-telemetry/opentelemetry-python/pull/3949))
- Add Python 3.12 to tox
([#3616](https://github.com/open-telemetry/opentelemetry-python/pull/3616))
- Improve resource field structure for LogRecords
([#3972](https://github.com/open-telemetry/opentelemetry-python/pull/3972))
- Update Semantic Conventions code generation scripts:
- fix namespace exclusion that resulted in dropping `os` and `net` namespaces.
- add `Final` decorator to constants to prevent collisions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ def __init__(
),
}
)
self.resource = resource
self.resource = (
resource if isinstance(resource, Resource) else Resource.create({})
)
if self.dropped_attributes > 0:
warnings.warn(
"Log record attributes were dropped due to limits",
Expand Down Expand Up @@ -238,9 +240,7 @@ def to_json(self, indent=4) -> str:
else ""
),
"trace_flags": self.trace_flags,
"resource": (
repr(self.resource.attributes) if self.resource else ""
),
"resource": json.loads(self.resource.to_json()),
},
indent=indent,
)
Expand Down
16 changes: 13 additions & 3 deletions opentelemetry-sdk/tests/logs/test_log_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
LogLimits,
LogRecord,
)
from opentelemetry.sdk.resources import Resource


class TestLogRecord(unittest.TestCase):
Expand All @@ -38,16 +39,25 @@ def test_log_record_to_json(self):
"trace_id": "",
"span_id": "",
"trace_flags": None,
"resource": "",
"resource": {
"attributes": {"service.name": "foo"},
"schema_url": "",
},
},
indent=4,
)
actual = LogRecord(
timestamp=0,
observed_timestamp=0,
body="a log line",
).to_json()
self.assertEqual(expected, actual)
resource=Resource({"service.name": "foo"}),
)

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": ""}}',
)

def test_log_record_bounded_attributes(self):
attr = {"key": "value"}
Expand Down