Skip to content

Commit

Permalink
[logs] prevent None from causing problems (#2410)
Browse files Browse the repository at this point in the history
* [logs] prevent None from causing problems

If the trace or span ID is None for a log entry, the result would raise an error, this change prevents that.

* update changelog

* Apply suggestions from code review

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>

* add unit test

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
  • Loading branch information
3 people committed Jan 26, 2022
1 parent a659966 commit 44e9b55
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2383](https://github.com/open-telemetry/opentelemetry-python/pull/2383))
- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add Gauge to OTLPMetricExporter
([#2408](https://github.com/open-telemetry/opentelemetry-python/pull/2408))
- [logs] prevent None from causing problems
([#2410](https://github.com/open-telemetry/opentelemetry-python/pull/2410))

## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17

Expand Down
8 changes: 6 additions & 2 deletions opentelemetry-sdk/src/opentelemetry/sdk/_logs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ def to_json(self) -> str:
"severity_text": self.severity_text,
"attributes": self.attributes,
"timestamp": ns_to_iso_str(self.timestamp),
"trace_id": f"0x{format_trace_id(self.trace_id)}",
"span_id": f"0x{format_span_id(self.span_id)}",
"trace_id": f"0x{format_trace_id(self.trace_id)}"
if self.trace_id is not None
else "",
"span_id": f"0x{format_span_id(self.span_id)}"
if self.span_id is not None
else "",
"trace_flags": self.trace_flags,
"resource": repr(self.resource.attributes)
if self.resource
Expand Down
42 changes: 42 additions & 0 deletions opentelemetry-sdk/tests/logs/test_log_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import unittest

from opentelemetry.sdk._logs import LogRecord


class TestLogRecord(unittest.TestCase):
def test_log_record_to_json(self):
expected = json.dumps(
{
"body": "a log line",
"name": None,
"severity_number": "None",
"severity_text": None,
"attributes": None,
"timestamp": "1970-01-01T00:00:00.000000Z",
"trace_id": "",
"span_id": "",
"trace_flags": None,
"resource": "",
},
indent=4,
)
actual = LogRecord(
timestamp=0,
body="a log line",
).to_json()
self.assertEqual(expected, actual)

0 comments on commit 44e9b55

Please sign in to comment.