|
| 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 | +# Enabled field is the least significant bit of trace options. |
| 18 | +_ENABLED_BITMASK = 1 << 0 |
| 19 | + |
| 20 | +# Default trace options |
| 21 | +DEFAULT = '1' |
| 22 | + |
| 23 | + |
| 24 | +class TraceOptions(object): |
| 25 | + """A class that represents global trace options. |
| 26 | +
|
| 27 | + :type trace_options_byte: str |
| 28 | + :param trace_options_byte: 1 byte bitmap for trace options. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, trace_options_byte=None): |
| 32 | + if trace_options_byte is None: |
| 33 | + trace_options_byte = DEFAULT |
| 34 | + |
| 35 | + self.trace_options_byte = self.check_trace_options(trace_options_byte) |
| 36 | + self.enabled = self.get_enabled |
| 37 | + |
| 38 | + def check_trace_options(self, trace_options_byte): |
| 39 | + trace_options_int = int(trace_options_byte) |
| 40 | + |
| 41 | + if trace_options_int < 0 or trace_options_int > 255: |
| 42 | + logging.warn("Trace options invalid, should be 1 byte.") |
| 43 | + trace_options_byte = DEFAULT |
| 44 | + |
| 45 | + return trace_options_byte |
| 46 | + |
| 47 | + @property |
| 48 | + def get_enabled(self): |
| 49 | + """Get the last bit from the trace options which is the enabled field. |
| 50 | +
|
| 51 | + :type trace_options: byte |
| 52 | + :param trace_options: 1 byte field which indicates 8 trace options, |
| 53 | + currently only have the enabled option. 1 means |
| 54 | + enabled, 0 means not enabled. |
| 55 | +
|
| 56 | + :rtype: bool |
| 57 | + :returns: Enabled tracing or not. |
| 58 | + """ |
| 59 | + enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK) |
| 60 | + |
| 61 | + return enabled |
0 commit comments