Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Initial implementation of a global Tracer. #95

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Somewhere in your server's request handler code:
.. code-block:: python

def handle_request(request):
span = before_request(request, opentracing.tracer)
span = before_request(request, opentracing.global_tracer())
# store span in some request-local storage using Tracer.scope_manager,
# using the returned `Scope` as Context Manager to ensure
# `Span` will be cleared and (in this case) `Span.finish()` be called.
Expand Down Expand Up @@ -111,7 +111,7 @@ Somewhere in your service that's about to make an outgoing call:
def before_http_request(request, current_span_extractor):
op = request.operation
parent_span = current_span_extractor()
outbound_span = opentracing.tracer.start_span(
outbound_span = opentracing.global_tracer().start_span(
operation_name=op,
child_of=parent_span
)
Expand All @@ -127,7 +127,7 @@ Somewhere in your service that's about to make an outgoing call:
outbound_span.set_tag(tags.PEER_PORT, port)

http_header_carrier = {}
opentracing.tracer.inject(
opentracing.global_tracer().inject(
span_context=outbound_span,
format=Format.HTTP_HEADERS,
carrier=http_header_carrier)
Expand Down
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Classes

Utility Functions
-----------------
.. autofunction:: opentracing.global_tracer

.. autofunction:: opentracing.set_global_tracer

.. autofunction:: opentracing.start_child_span

.. autofunction:: opentracing.child_of
Expand Down
25 changes: 25 additions & 0 deletions opentracing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,29 @@
# Global variable that should be initialized to an instance of real tracer.
# Note: it should be accessed via 'opentracing.tracer', not via
# 'from opentracing import tracer', the latter seems to take a copy.
# DEPRECATED, use global_tracer() and set_global_tracer() instead.
tracer = Tracer()


def global_tracer():
"""Returns the global tracer.
The default value is an instance of :class:`opentracing.Tracer`

:rtype: :class:`Tracer`
:return: The global tracer instance.
"""
return tracer


def set_global_tracer(value):
"""Sets the global tracer.
It is an error to pass ``None``.

:param value: the :class:`Tracer` used as global instance.
:type value: :class:`Tracer`
"""
if value is None:
raise ValueError('The global Tracer tracer cannot be None')

global tracer
tracer = value
38 changes: 38 additions & 0 deletions tests/test_globaltracer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright The OpenTracing 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.

from __future__ import absolute_import
import pytest
import mock
import opentracing


def test_opentracing_tracer():
assert opentracing.tracer is opentracing.global_tracer()
assert isinstance(opentracing.global_tracer(), opentracing.Tracer)


@mock.patch('opentracing.tracer')
def test_set_global_tracer(mock_obj):
tracer = mock.Mock()
opentracing.set_global_tracer(tracer)
assert opentracing.global_tracer() is tracer

opentracing.set_global_tracer(mock_obj)
assert opentracing.global_tracer() is mock_obj


def test_register_none():
with pytest.raises(ValueError):
opentracing.set_global_tracer(None)