Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 2ab5b1c

Browse files
authored
Move logging integration into ext package (#655)
1 parent 551522d commit 2ab5b1c

File tree

14 files changed

+195
-11
lines changed

14 files changed

+195
-11
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ OpenCensus supports integration with popular web frameworks, client libraries an
189189
- `Google Cloud Client Libraries`_
190190
- `gRPC`_
191191
- `httplib`_
192+
- `logging`_
192193
- `MySQL`_
193194
- `PostgreSQL`_
194195
- `pymongo`_
@@ -222,6 +223,7 @@ Stats Exporter
222223
.. _gRPC: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-grpc
223224
.. _httplib: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-httplib
224225
.. _Jaeger: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-jaeger
226+
.. _logging: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-logging
225227
.. _MySQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-mysql
226228
.. _OCAgent: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-ocagent
227229
.. _PostgreSQL: https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-postgresql
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
## Unreleased
4+
- Initial version
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
OpenCensus logging Integration
2+
============================================================================
3+
4+
|pypi|
5+
6+
.. |pypi| image:: https://badge.fury.io/py/opencensus-ext-logging.svg
7+
:target: https://pypi.org/project/opencensus-ext-logging/
8+
9+
Installation
10+
------------
11+
12+
::
13+
14+
pip install opencensus-ext-logging
15+
16+
Usage
17+
-----
18+
19+
.. code:: python
20+
21+
import logging
22+
23+
from opencensus.trace import config_integration
24+
from opencensus.trace.samplers import AlwaysOffSampler
25+
from opencensus.trace.tracer import Tracer
26+
27+
config_integration.trace_integrations(['logging'])
28+
logging.basicConfig(format='%(asctime)s traceId=%(traceId)s spanId=%(spanId)s %(message)s')
29+
tracer = Tracer(sampler=AlwaysOffSampler())
30+
31+
logger = logging.getLogger(__name__)
32+
logger.warning('Before the span')
33+
with tracer.span(name='hello'):
34+
logger.warning('In the span')
35+
logger.warning('After the span')
36+
37+
38+
References
39+
----------
40+
41+
* `OpenCensus Project <https://opencensus.io/>`_
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2017, OpenCensus 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+
from opencensus.ext.logging import trace
16+
17+
__all__ = ['trace']
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2017, OpenCensus 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 logging
16+
17+
from opencensus.log import TraceLogger
18+
19+
20+
def trace_integration(tracer=None):
21+
"""Replace the global default logging class with `TraceLogger`.
22+
23+
Loggers created after the integration will produce `LogRecord`s
24+
with extra traceId, spanId, and traceSampled attributes from the opencensus
25+
context.
26+
"""
27+
logging.setLoggerClass(TraceLogger)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
universal = 1
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2019, OpenCensus 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+
from setuptools import find_packages
16+
from setuptools import setup
17+
from version import __version__
18+
19+
setup(
20+
name='opencensus-ext-logging',
21+
version=__version__, # noqa
22+
author='OpenCensus Authors',
23+
author_email='[email protected]',
24+
classifiers=[
25+
'Intended Audience :: Developers',
26+
'Development Status :: 3 - Alpha',
27+
'Intended Audience :: Developers',
28+
'License :: OSI Approved :: Apache Software License',
29+
'Programming Language :: Python',
30+
'Programming Language :: Python :: 2',
31+
'Programming Language :: Python :: 2.7',
32+
'Programming Language :: Python :: 3',
33+
'Programming Language :: Python :: 3.4',
34+
'Programming Language :: Python :: 3.5',
35+
'Programming Language :: Python :: 3.6',
36+
'Programming Language :: Python :: 3.7',
37+
],
38+
description='OpenCensus logging Integration',
39+
include_package_data=True,
40+
long_description=open('README.rst').read(),
41+
install_requires=[
42+
'opencensus >= 0.6.dev0, < 1.0.0',
43+
],
44+
extras_require={},
45+
license='Apache-2.0',
46+
packages=find_packages(exclude=('tests',)),
47+
namespace_packages=[],
48+
url='https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-logging', # noqa: E501
49+
zip_safe=False,
50+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2017, OpenCensus 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 logging
16+
import unittest
17+
18+
from opencensus.trace import config_integration
19+
20+
21+
class TestLoggingIntegration(unittest.TestCase):
22+
@classmethod
23+
def setUpClass(cls):
24+
cls._old_logger_class = logging.getLoggerClass()
25+
26+
@classmethod
27+
def tearDownClass(cls):
28+
logging.setLoggerClass(cls._old_logger_class)
29+
30+
def test_integration(self):
31+
self.assertEqual(self._old_logger_class, logging.getLoggerClass())
32+
config_integration.trace_integrations(['logging'])
33+
self.assertNotEqual(self._old_logger_class, logging.getLoggerClass())

0 commit comments

Comments
 (0)