Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44e9b55

Browse files
Alex Botenocelotlsrikanthccv
authored
[logs] prevent None from causing problems (open-telemetry#2410)
* [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 <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]> * add unit test Co-authored-by: Diego Hurtado <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent a659966 commit 44e9b55

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
([#2383](https://github.com/open-telemetry/opentelemetry-python/pull/2383))
2626
- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add Gauge to OTLPMetricExporter
2727
([#2408](https://github.com/open-telemetry/opentelemetry-python/pull/2408))
28+
- [logs] prevent None from causing problems
29+
([#2410](https://github.com/open-telemetry/opentelemetry-python/pull/2410))
2830

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

opentelemetry-sdk/src/opentelemetry/sdk/_logs/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ def to_json(self) -> str:
8787
"severity_text": self.severity_text,
8888
"attributes": self.attributes,
8989
"timestamp": ns_to_iso_str(self.timestamp),
90-
"trace_id": f"0x{format_trace_id(self.trace_id)}",
91-
"span_id": f"0x{format_span_id(self.span_id)}",
90+
"trace_id": f"0x{format_trace_id(self.trace_id)}"
91+
if self.trace_id is not None
92+
else "",
93+
"span_id": f"0x{format_span_id(self.span_id)}"
94+
if self.span_id is not None
95+
else "",
9296
"trace_flags": self.trace_flags,
9397
"resource": repr(self.resource.attributes)
9498
if self.resource
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import unittest
17+
18+
from opentelemetry.sdk._logs import LogRecord
19+
20+
21+
class TestLogRecord(unittest.TestCase):
22+
def test_log_record_to_json(self):
23+
expected = json.dumps(
24+
{
25+
"body": "a log line",
26+
"name": None,
27+
"severity_number": "None",
28+
"severity_text": None,
29+
"attributes": None,
30+
"timestamp": "1970-01-01T00:00:00.000000Z",
31+
"trace_id": "",
32+
"span_id": "",
33+
"trace_flags": None,
34+
"resource": "",
35+
},
36+
indent=4,
37+
)
38+
actual = LogRecord(
39+
timestamp=0,
40+
body="a log line",
41+
).to_json()
42+
self.assertEqual(expected, actual)

0 commit comments

Comments
 (0)