File: serializer.py

package info (click to toggle)
python-jaeger-client 4.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: python: 5,578; makefile: 93; sh: 26; awk: 16
file content (128 lines) | stat: -rw-r--r-- 4,122 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging

from crossdock.thrift_gen.tracetest.ttypes import JoinTraceRequest, StartTraceRequest, \
    Downstream, Transport, TraceResponse, ObservedSpan


#
# Serializers for the downstream calls
#
def set_downstream_object_values(downstream_object, json_obj):
    return set_traced_service_object_values(downstream_object, json_obj, downstream_from_struct)


def join_trace_request_from_json(json_request):
    if isinstance(json_request, (bytes, bytearray)):
        json_request = json_request.decode('utf-8')
    request = JoinTraceRequest()
    set_downstream_object_values(request, json.loads(json_request))
    return request


def start_trace_request_from_json(json_request):
    if isinstance(json_request, (bytes, bytearray)):
        json_request = json_request.decode('utf-8')
    request = StartTraceRequest()
    set_downstream_object_values(request, json.loads(json_request))
    return request


def downstream_from_struct(json_obj):
    downstream = Downstream()
    set_downstream_object_values(downstream, json_obj)
    return downstream


def join_trace_request_to_json(downstream, server_role):
    req = {}
    if downstream is not None:
        req['downstream'] = traced_service_object_to_json(downstream)
    if server_role is not None:
        req['serverRole'] = str(server_role)
    return json.dumps(req)

#
# Serializers for the upstream responses
#


def set_upstream_object_values(obj, json_obj):
    return set_traced_service_object_values(obj, json_obj, traceresponse_from_struct)


def observed_span_from_struct(json_obj):
    os = ObservedSpan()
    set_upstream_object_values(os, json_obj)
    return os


def traceresponse_from_struct(json_obj):
    tr = TraceResponse()
    set_upstream_object_values(tr, json_obj)
    return tr


def traceresponse_from_json(json_str):
    try:
        if isinstance(json_str, (bytes, bytearray)):
            json_str = json_str.decode('utf-8')
        return traceresponse_from_struct(json.loads(json_str))
    except:  # noqa: E722
        logging.exception('Failed to parse JSON')
        raise

# Generic


def class_keys(obj):
    return [a for a in dir(obj) if not a.startswith('__') and not
            callable(getattr(obj, a)) and not
            a == 'type_spec']


def traced_service_object_to_json(obj):
    json_response = {}
    for k in class_keys(obj):
        if k == 'downstream':
            if obj.downstream is not None:
                json_response['downstream'] = traced_service_object_to_json(obj.downstream)
        elif k == 'transport':
            if obj.transport is not None:
                json_response['transport'] = Transport._VALUES_TO_NAMES[obj.transport]
        elif k == 'span':
            if obj.span is not None:
                json_response['span'] = traced_service_object_to_json(obj.span)
        else:
            json_response[k] = getattr(obj, k)
    return json_response


def set_traced_service_object_values(obj, values, downstream_func):
    for k in values.keys():
        if hasattr(obj, k):
            if k == 'downstream':
                if values[k] is not None:
                    obj.downstream = downstream_func(values[k])
            elif k == 'transport':
                if values[k] is not None:
                    obj.transport = Transport._NAMES_TO_VALUES[values[k]]
            elif k == 'span':
                if values[k] is not None:
                    obj.span = observed_span_from_struct(values[k])
            else:
                setattr(obj, k, values[k])