Skip to content

Commit

Permalink
Default Observed Timestamp (#3377)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
  • Loading branch information
jeremydvoss and ocelotl committed Jul 27, 2023
1 parent 8378db9 commit b9f31e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#3362](https://github.com/open-telemetry/opentelemetry-python/pull/3362))
- Fixed bug where logging export is tracked as trace
[#3375](https://github.com/open-telemetry/opentelemetry-python/pull/3375))
- Default LogRecord observed_timestamp to current timestamp
[#3377](https://github.com/open-telemetry/opentelemetry-python/pull/3377))


## Version 1.18.0/0.39b0 (2023-05-19)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from abc import ABC, abstractmethod
from logging import getLogger
from os import environ
from time import time_ns
from typing import Any, Optional, cast

from opentelemetry._logs.severity import SeverityNumber
Expand Down Expand Up @@ -70,6 +71,8 @@ def __init__(
attributes: Optional["Attributes"] = None,
):
self.timestamp = timestamp
if observed_timestamp is None:
observed_timestamp = time_ns()
self.observed_timestamp = observed_timestamp
self.trace_id = trace_id
self.span_id = span_id
Expand Down
27 changes: 27 additions & 0 deletions opentelemetry-api/tests/logs/test_log_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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 unittest
from unittest.mock import patch

from opentelemetry._logs import LogRecord

OBSERVED_TIMESTAMP = "OBSERVED_TIMESTAMP"


class TestLogRecord(unittest.TestCase):
@patch("opentelemetry._logs._internal.time_ns")
def test_log_record_observed_timestamp_default(self, time_ns_mock): # type: ignore
time_ns_mock.return_value = OBSERVED_TIMESTAMP
self.assertEqual(LogRecord().observed_timestamp, OBSERVED_TIMESTAMP)

0 comments on commit b9f31e9

Please sign in to comment.