-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ProxyLoggerProvider, ProxyLogger and associated tests (#3575)
* 3060 adds ProxyLoggerProvider, ProxyLogger and associated tests * Updates changelog for 3060 (add proxy classes for logging) * Removes proxy classes from module level log import following review * Moves changelog entry to unreleased * Fixes build failures * Fix mypy and lint ---------
- Loading branch information
1 parent
39f0f82
commit 3500f5c
Showing
5 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 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. | ||
|
||
# pylint: disable=W0212,W0222,W0221 | ||
import typing | ||
import unittest | ||
|
||
import opentelemetry._logs._internal as _logs_internal | ||
from opentelemetry import _logs | ||
from opentelemetry.sdk._logs import LogRecord # type: ignore | ||
from opentelemetry.test.globals_test import LoggingGlobalsTest | ||
|
||
|
||
class TestProvider(_logs.NoOpLoggerProvider): | ||
def get_logger( | ||
self, | ||
name: str, | ||
version: typing.Optional[str] = None, | ||
schema_url: typing.Optional[str] = None, | ||
) -> _logs.Logger: | ||
return TestLogger(name) | ||
|
||
|
||
class TestLogger(_logs.NoOpLogger): | ||
def emit(self, *args, **kwargs): | ||
return LogRecord(timestamp=0) | ||
|
||
|
||
class TestProxy(LoggingGlobalsTest, unittest.TestCase): | ||
def test_proxy_logger(self): | ||
provider = _logs.get_logger_provider() | ||
# proxy provider | ||
self.assertIsInstance(provider, _logs_internal.ProxyLoggerProvider) | ||
|
||
# provider returns proxy logger | ||
logger = provider.get_logger("proxy-test") | ||
self.assertIsInstance(logger, _logs_internal.ProxyLogger) | ||
|
||
# set a real provider | ||
_logs.set_logger_provider(TestProvider()) | ||
|
||
# get_logger_provider() now returns the real provider | ||
self.assertIsInstance(_logs.get_logger_provider(), TestProvider) | ||
|
||
# logger provider now returns real instance | ||
self.assertIsInstance( | ||
_logs.get_logger_provider().get_logger("fresh"), TestLogger | ||
) | ||
|
||
# references to the old provider still work but return real logger now | ||
real_logger = provider.get_logger("proxy-test") | ||
self.assertIsInstance(real_logger, TestLogger) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters