Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/opencensus/trace/trace_options.py: 68%
25 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 06:04 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-06 06:04 +0000
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.
15import logging
17# Enabled field is the least significant bit of trace options.
18_ENABLED_BITMASK = 1 << 0
20# Default trace options
21DEFAULT = '1'
24class TraceOptions(object):
25 """A class that represents global trace options.
27 :type trace_options_byte: str
28 :param trace_options_byte: 1 byte bitmap for trace options.
29 """
31 def __init__(self, trace_options_byte=None):
32 if trace_options_byte is None:
33 trace_options_byte = DEFAULT
35 self.trace_options_byte = self.check_trace_options(trace_options_byte)
36 self.enabled = self.get_enabled()
38 def check_trace_options(self, trace_options_byte):
39 trace_options_int = int(trace_options_byte)
41 if trace_options_int < 0 or trace_options_int > 255:
42 logging.warning("Trace options invalid, should be 1 byte.")
43 trace_options_byte = DEFAULT
45 return trace_options_byte
47 def __repr__(self):
48 fmt = '{}(enabled={})'
49 return fmt.format(
50 type(self).__name__,
51 self.get_enabled(),
52 )
54 def get_enabled(self):
55 """Get the last bit from the trace options which is the enabled field.
57 :type trace_options: byte
58 :param trace_options: 1 byte field which indicates 8 trace options,
59 currently only have the enabled option. 1 means
60 enabled, 0 means not enabled.
62 :rtype: bool
63 :returns: Enabled tracing or not.
64 """
65 enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK)
67 return enabled
69 def set_enabled(self, enabled):
70 """Update the last bit of the trace options byte str.
72 :type enabled: bool
73 :param enabled: Whether enable tracing in this span context or not.
74 """
75 enabled_bit = '1' if enabled else '0'
76 self.trace_options_byte = str(
77 self.trace_options_byte)[:-1] + enabled_bit
78 self.enabled = self.get_enabled()