| 1 |
# Copyright (c) 2016 Uber Technologies, Inc. |
| 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 json |
| 16 |
import logging |
| 17 |
|
| 18 |
from crossdock.thrift_gen.tracetest.ttypes import JoinTraceRequest, StartTraceRequest, \ |
| 19 |
Downstream, Transport, TraceResponse, ObservedSpan |
| 20 |
|
| 21 |
|
| 22 |
# |
| 23 |
# Serializers for the downstream calls |
| 24 |
# |
| 25 |
def set_downstream_object_values(downstream_object, json_obj): |
| 26 |
return set_traced_service_object_values(downstream_object, json_obj, downstream_from_struct) |
| 27 |
|
| 28 |
|
| 29 |
def join_trace_request_from_json(json_request): |
| 30 |
if isinstance(json_request, (bytes, bytearray)): |
| 31 |
json_request = json_request.decode('utf-8') |
| 32 |
request = JoinTraceRequest() |
| 33 |
set_downstream_object_values(request, json.loads(json_request)) |
| 34 |
return request |
| 35 |
|
| 36 |
|
| 37 |
def start_trace_request_from_json(json_request): |
| 38 |
if isinstance(json_request, (bytes, bytearray)): |
| 39 |
json_request = json_request.decode('utf-8') |
| 40 |
request = StartTraceRequest() |
| 41 |
set_downstream_object_values(request, json.loads(json_request)) |
| 42 |
return request |
| 43 |
|
| 44 |
|
| 45 |
def downstream_from_struct(json_obj): |
| 46 |
downstream = Downstream() |
| 47 |
set_downstream_object_values(downstream, json_obj) |
| 48 |
return downstream |
| 49 |
|
| 50 |
|
| 51 |
def join_trace_request_to_json(downstream, server_role): |
| 52 |
req = {} |
| 53 |
if downstream is not None: |
| 54 |
req['downstream'] = traced_service_object_to_json(downstream) |
| 55 |
if server_role is not None: |
| 56 |
req['serverRole'] = str(server_role) |
| 57 |
return json.dumps(req) |
| 58 |
|
| 59 |
# |
| 60 |
# Serializers for the upstream responses |
| 61 |
# |
| 62 |
|
| 63 |
|
| 64 |
def set_upstream_object_values(obj, json_obj): |
| 65 |
return set_traced_service_object_values(obj, json_obj, traceresponse_from_struct) |
| 66 |
|
| 67 |
|
| 68 |
def observed_span_from_struct(json_obj): |
| 69 |
os = ObservedSpan() |
| 70 |
set_upstream_object_values(os, json_obj) |
| 71 |
return os |
| 72 |
|
| 73 |
|
| 74 |
def traceresponse_from_struct(json_obj): |
| 75 |
tr = TraceResponse() |
| 76 |
set_upstream_object_values(tr, json_obj) |
| 77 |
return tr |
| 78 |
|
| 79 |
|
| 80 |
def traceresponse_from_json(json_str): |
| 81 |
try: |
| 82 |
if isinstance(json_str, (bytes, bytearray)): |
| 83 |
json_str = json_str.decode('utf-8') |
| 84 |
return traceresponse_from_struct(json.loads(json_str)) |
| 85 |
except: # noqa: E722 |
| 86 |
logging.exception('Failed to parse JSON') |
| 87 |
raise |
| 88 |
|
| 89 |
# Generic |
| 90 |
|
| 91 |
|
| 92 |
def class_keys(obj): |
| 93 |
return [a for a in dir(obj) if not a.startswith('__') and not |
| 94 |
callable(getattr(obj, a)) and not |
| 95 |
a == 'type_spec'] |
| 96 |
|
| 97 |
|
| 98 |
def traced_service_object_to_json(obj): |
| 99 |
json_response = {} |
| 100 |
for k in class_keys(obj): |
| 101 |
if k == 'downstream': |
| 102 |
if obj.downstream is not None: |
| 103 |
json_response['downstream'] = traced_service_object_to_json(obj.downstream) |
| 104 |
elif k == 'transport': |
| 105 |
if obj.transport is not None: |
| 106 |
json_response['transport'] = Transport._VALUES_TO_NAMES[obj.transport] |
| 107 |
elif k == 'span': |
| 108 |
if obj.span is not None: |
| 109 |
json_response['span'] = traced_service_object_to_json(obj.span) |
| 110 |
else: |
| 111 |
json_response[k] = getattr(obj, k) |
| 112 |
return json_response |
| 113 |
|
| 114 |
|
| 115 |
def set_traced_service_object_values(obj, values, downstream_func): |
| 116 |
for k in values.keys(): |
| 117 |
if hasattr(obj, k): |
| 118 |
if k == 'downstream': |
| 119 |
if values[k] is not None: |
| 120 |
obj.downstream = downstream_func(values[k]) |
| 121 |
elif k == 'transport': |
| 122 |
if values[k] is not None: |
| 123 |
obj.transport = Transport._NAMES_TO_VALUES[values[k]] |
| 124 |
elif k == 'span': |
| 125 |
if values[k] is not None: |
| 126 |
obj.span = observed_span_from_struct(values[k]) |
| 127 |
else: |
| 128 |
setattr(obj, k, values[k]) |