1# Copyright The OpenTracing Authors
2# Copyright Uber Technologies, Inc
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from __future__ import absolute_import
17from .span import Span  # noqa
18from .span import SpanContext  # noqa
19from .scope import Scope  # noqa
20from .scope_manager import ScopeManager  # noqa
21from .tracer import child_of  # noqa
22from .tracer import follows_from  # noqa
23from .tracer import Reference  # noqa
24from .tracer import ReferenceType  # noqa
25from .tracer import Tracer  # noqa
26from .tracer import start_child_span  # noqa
27from .propagation import Format  # noqa
28from .propagation import InvalidCarrierException  # noqa
29from .propagation import SpanContextCorruptedException  # noqa
30from .propagation import UnsupportedFormatException  # noqa
31
32# Global variable that should be initialized to an instance of real tracer.
33# Note: it should be accessed via 'opentracing.tracer', not via
34# 'from opentracing import tracer', the latter seems to take a copy.
35# DEPRECATED, use global_tracer() and set_global_tracer() instead.
36tracer = Tracer()
37is_tracer_registered = False
38
39
40def global_tracer():
41    """Returns the global tracer.
42    The default value is an instance of :class:`opentracing.Tracer`
43
44    :rtype: :class:`Tracer`
45    :return: The global tracer instance.
46    """
47    return tracer
48
49
50def set_global_tracer(value):
51    """Sets the global tracer.
52    It is an error to pass ``None``.
53
54    :param value: the :class:`Tracer` used as global instance.
55    :type value: :class:`Tracer`
56    """
57    if value is None:
58        raise ValueError('The global Tracer tracer cannot be None')
59
60    global tracer, is_tracer_registered
61    tracer = value
62    is_tracer_registered = True
63
64
65def is_global_tracer_registered():
66    """Indicates if a global tracer has been registered.
67
68    :rtype: :value:bool
69    :return: True if a global tracer has been registered, otherwise False.
70    """
71    return is_tracer_registered
72
73
74def _reset_global_tracer():
75    """Reset any previously registered tracer. Intended for internal usage."""
76
77    global tracer, is_tracer_registered
78    tracer = Tracer()
79    is_tracer_registered = False
80