Debian Codegraph @

crossdock/server/endtoend.py

raw
1 # Copyright (c) 2016-2018 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 tornado.web
16 import logging
17 import json
18 import os
19
20 from jaeger_client.local_agent_net import LocalAgentSender
21 from jaeger_client.config import (
22 Config,
23 DEFAULT_SAMPLING_PORT,
24 DEFAULT_REPORTING_PORT,
25 )
26 from jaeger_client.constants import (
27 SAMPLER_TYPE_CONST,
28 SAMPLER_TYPE_REMOTE,
29 )
30 from jaeger_client.sampler import RemoteControlledSampler, ConstSampler
31 from jaeger_client.reporter import Reporter
32 from jaeger_client.throttler import RemoteThrottler
33 from jaeger_client.tracer import Tracer
34
35 config = {
36 'service_name': 'crossdock-python',
37 'enabled': True,
38 'sampler': {
39 'type': 'probabilistic',
40 'param': 1,
41 },
42 'reporter_flush_interval': 1,
43 'sampling_refresh_interval': 5,
44 }
45
46
47 class EndToEndHandler(object):
48 """
49 Handler that creates traces from a http request.
50
51 json: {
52 "type": "remote"
53 "operation": "operationName",
54 "count": 2,
55 "tags": {
56 "key": "value"
57 }
58 }
59
60 Given the above json payload, the handler will use a tracer with the RemoteControlledSampler
61 to create 2 traces for the "operationName" operation with the tags: {"key":"value"}. These
62 traces are reported to the agent with the hostname "test_driver".
63 """
64
65 def __init__(self):
66 cfg = Config(config)
67 init_sampler = cfg.sampler
68 channel = self.local_agent_sender
69
70 reporter = Reporter(channel=channel,
71 flush_interval=cfg.reporter_flush_interval)
72
73 remote_sampler = RemoteControlledSampler(
74 channel=channel,
75 service_name=cfg.service_name,
76 sampling_refresh_interval=cfg.sampling_refresh_interval,
77 init_sampler=init_sampler)
78
79 throttler = RemoteThrottler(channel, cfg.service_name)
80
81 remote_tracer = Tracer(
82 service_name=cfg.service_name,
83 reporter=reporter,
84 sampler=remote_sampler,
85 throttler=throttler)
86
87 const_tracer = Tracer(
88 service_name=cfg.service_name,
89 reporter=reporter,
90 sampler=ConstSampler(decision=True),
91 throttler=throttler
92 )
93
94 self._tracers = {
95 SAMPLER_TYPE_CONST: const_tracer,
96 SAMPLER_TYPE_REMOTE: remote_tracer
97 }
98
99 @property
100 def tracers(self):
101 return self._tracers
102
103 @tracers.setter
104 def tracers(self, tracers):
105 self._tracers = tracers
106
107 @property
108 def local_agent_sender(self):
109 host, port = _determine_host_port()
110 return LocalAgentSender(
111 host=host,
112 sampling_port=DEFAULT_SAMPLING_PORT,
113 reporting_port=port,
114 throttling_port=DEFAULT_SAMPLING_PORT,
115 )
116
117 @tornado.gen.coroutine
118 def generate_traces(self, request, response_writer):
119 if isinstance(request.body, (bytes, bytearray)):
120 request.body = request.body.decode('utf-8')
121 req = json.loads(request.body)
122 sampler_type = req.get('type', 'remote')
123 tracer = self.tracers[sampler_type]
124 for _ in range(req.get('count', 0)):
125 span = tracer.start_span(req['operation'])
126 for k, v in req.get('tags', {}).items():
127 span.set_tag(k, v)
128 span.finish()
129 response_writer.finish()
130
131
132 def _determine_host_port():
133 host_port = os.environ.get('AGENT_HOST_PORT', None)
134 if host_port:
135 host, port = _parse_host_port(host_port,
136 'jaeger-agent',
137 DEFAULT_REPORTING_PORT)
138 else:
139 host, port = 'jaeger-agent', DEFAULT_REPORTING_PORT
140 return host, port
141
142
143 def _parse_host_port(host_port, default_host, default_port):
144 try:
145 host, port_str = host_port.split(':')
146 port = int(port_str)
147 return host, port
148 except ValueError:
149 logging.getLogger().error(
150 'Invalid host port (%s), using default host port (%s:%d)',
151 host_port, default_host, default_port)
152 return default_host, default_port