|
| 1 | +# Copyright 2018, 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 | +# -*- coding: utf-8 -*- |
| 16 | + |
| 17 | +import logging |
| 18 | +import six |
| 19 | + |
| 20 | +from google.protobuf.internal.encoder import _VarintBytes |
| 21 | + |
| 22 | +from opencensus.tags import tag_map as tag_map_module |
| 23 | + |
| 24 | +# Used for decoding hex bytes to hex string. |
| 25 | +UTF8 = 'utf-8' |
| 26 | + |
| 27 | +VERSION_ID = 0 |
| 28 | +TAG_FIELD_ID = 0 |
| 29 | +TAG_MAP_SERIALIZED_SIZE_LIMIT = 8192 |
| 30 | + |
| 31 | + |
| 32 | +class BinarySerializer(object): |
| 33 | + def from_byte_array(self, binary): |
| 34 | + if len(binary) <= 0: |
| 35 | + logging.warning("Input byte[] cannot be empty/") |
| 36 | + return tag_map_module.TagMap() |
| 37 | + else: |
| 38 | + buffer = memoryview(binary) |
| 39 | + version_id = buffer[0] |
| 40 | + if six.PY2: |
| 41 | + version_id = ord(version_id) |
| 42 | + if version_id != VERSION_ID: |
| 43 | + raise ValueError("Invalid version id.") |
| 44 | + return self._parse_tags(buffer) |
| 45 | + |
| 46 | + def to_byte_array(self, tag_context): |
| 47 | + encoded_bytes = b'' |
| 48 | + encoded_bytes += _VarintBytes(VERSION_ID) |
| 49 | + total_chars = 0 |
| 50 | + for tag in tag_context.tags: |
| 51 | + for tag_key, tag_value in tag.items(): |
| 52 | + total_chars += len(tag_key) |
| 53 | + total_chars += len(tag_value) |
| 54 | + encoded_bytes = self._encode_tag( |
| 55 | + tag_key, tag_value, encoded_bytes) |
| 56 | + if total_chars <= TAG_MAP_SERIALIZED_SIZE_LIMIT: |
| 57 | + return encoded_bytes |
| 58 | + else: # pragma: NO COVER |
| 59 | + logging.warning("Size of the tag context exceeds the maximum size") |
| 60 | + |
| 61 | + def _parse_tags(self, buffer): |
| 62 | + tag_context = tag_map_module.TagMap() |
| 63 | + limit = len(buffer) |
| 64 | + total_chars = 0 |
| 65 | + i = 1 |
| 66 | + while i < limit: |
| 67 | + field_id = buffer[i] if six.PY3 else ord(buffer[i]) |
| 68 | + if field_id == TAG_FIELD_ID: |
| 69 | + i += 1 |
| 70 | + key = self._decode_string(buffer, i) |
| 71 | + i += len(key) |
| 72 | + total_chars += len(key) |
| 73 | + i += 1 |
| 74 | + val = self._decode_string(buffer, i) |
| 75 | + i += len(val) |
| 76 | + total_chars += len(val) |
| 77 | + i += 1 |
| 78 | + if total_chars > \ |
| 79 | + TAG_MAP_SERIALIZED_SIZE_LIMIT: # pragma: NO COVER |
| 80 | + logging.warning("Size of the tag context exceeds maximum") |
| 81 | + break |
| 82 | + else: |
| 83 | + tag_context.insert(str(key), str(val)) |
| 84 | + else: |
| 85 | + break |
| 86 | + return tag_context |
| 87 | + |
| 88 | + def _encode_tag(self, tag_key, tag_value, encoded_bytes): |
| 89 | + encoded_bytes += _VarintBytes(TAG_FIELD_ID) |
| 90 | + encoded_bytes = self._encode_string(tag_key, encoded_bytes) |
| 91 | + encoded_bytes = self._encode_string(tag_value, encoded_bytes) |
| 92 | + return encoded_bytes |
| 93 | + |
| 94 | + def _encode_string(self, input_str, encoded_bytes): |
| 95 | + encoded_bytes += _VarintBytes(len(input_str)) |
| 96 | + encoded_bytes += input_str.encode(UTF8) |
| 97 | + return encoded_bytes |
| 98 | + |
| 99 | + def _decode_string(self, buffer, pos): |
| 100 | + length = buffer[pos] if six.PY3 else ord(buffer[pos]) |
| 101 | + builder = "" |
| 102 | + i = 1 |
| 103 | + while i <= length: |
| 104 | + bytes_to_decode = buffer[pos + i] if six.PY3 \ |
| 105 | + else ord(buffer[pos + i]) |
| 106 | + builder += _VarintBytes(bytes_to_decode).decode() |
| 107 | + i += 1 |
| 108 | + return builder |
0 commit comments