def start_workunit(self, workunit): """Implementation of Reporter callback.""" if workunit.has_label(WorkUnitLabel.GOAL): service_name = "pants goal" elif workunit.has_label(WorkUnitLabel.TASK): service_name = "pants task" else: service_name = "pants workunit" # Check if it is the first workunit first_span = not self._workunits_to_spans if first_span: # If trace_id and parent_id are given as flags create zipkin_attrs if self.trace_id is not None and self.parent_id is not None: zipkin_attrs = ZipkinAttrs( # trace_id and parent_id are passed to Pants by another process that collects # Zipkin trace trace_id=self.trace_id, span_id=generate_random_64bit_string(), parent_span_id=self.parent_id, flags='0', # flags: stores flags header. Currently unused is_sampled=True, ) else: zipkin_attrs = create_attrs_for_span( # trace_id is the same as run_uuid that is created in run_tracker and is the part of # pants_run id trace_id=self.trace_id, sample_rate=self.sample_rate, # Value between 0.0 and 100.0 ) self.trace_id = zipkin_attrs.trace_id # TODO delete this line when parent_id will be passed in v2 engine: # - with ExecutionRequest when Nodes from v2 engine are called by a workunit; # - when a v2 engine Node is called by another v2 engine Node. self.parent_id = zipkin_attrs.span_id span = zipkin_span( service_name=service_name, span_name=workunit.name, transport_handler=self.handler, encoding=Encoding.V1_THRIFT, zipkin_attrs=zipkin_attrs, span_storage=self.span_storage, ) else: span = zipkin_span( service_name=service_name, span_name=workunit.name, span_storage=self.span_storage, ) self._workunits_to_spans[workunit] = span span.start() # Goals and tasks save their start time at the beginning of their run. # This start time is passed to workunit, because the workunit may be created much later. span.start_timestamp = workunit.start_time if first_span and span.zipkin_attrs.is_sampled: span.logging_context.start_timestamp = workunit.start_time
def tween(request): # Creates zipkin_attrs and attaches a zipkin_trace_id attr to the request zipkin_attrs = create_zipkin_attr(request) if 'zipkin.transport_handler' in request.registry.settings: transport_handler = request.registry.settings[ 'zipkin.transport_handler'] else: raise ZipkinError( "`zipkin.transport_handler` is a required config property, which" " is missing. It is a callback method which takes a message as a" " param and logs it via scribe/kafka." ) service_name = request.registry.settings.get('service_name', 'unknown') span_name = '{0} {1}'.format(request.method, request.path) with zipkin_span( service_name=service_name, span_name=span_name, zipkin_attrs=zipkin_attrs, transport_handler=transport_handler, port=request.server_port, ) as zipkin_context: response = handler(request) zipkin_context.update_binary_annotations_for_root_span( get_binary_annotations(request, response), ) return response
def tween(request): zipkin_settings = _get_settings_from_request(request) tween_kwargs = dict( service_name=zipkin_settings.service_name, span_name=zipkin_settings.span_name, zipkin_attrs=zipkin_settings.zipkin_attrs, transport_handler=zipkin_settings.transport_handler, host=zipkin_settings.host, port=zipkin_settings.port, add_logging_annotation=zipkin_settings.add_logging_annotation, report_root_timestamp=zipkin_settings.report_root_timestamp, context_stack=zipkin_settings.context_stack, max_span_batch_size=zipkin_settings.max_span_batch_size, ) if zipkin_settings.firehose_handler is not None: tween_kwargs['firehose_handler'] = zipkin_settings.firehose_handler with zipkin_span(**tween_kwargs) as zipkin_context: response = handler(request) zipkin_context.update_binary_annotations( get_binary_annotations(request, response), ) return response
def tween(request): zipkin_settings = _get_settings_from_request(request) # If this request isn't sampled, don't go through the work # of initializing the rest of the zipkin attributes if not zipkin_settings.zipkin_attrs.is_sampled: return handler(request) with zipkin_span( service_name=zipkin_settings.service_name, span_name=zipkin_settings.span_name, zipkin_attrs=zipkin_settings.zipkin_attrs, transport_handler=zipkin_settings.transport_handler, host=zipkin_settings.host, port=zipkin_settings.port, add_logging_annotation=zipkin_settings.add_logging_annotation, report_root_timestamp=zipkin_settings.report_root_timestamp, ) as zipkin_context: response = handler(request) zipkin_context.update_binary_annotations( get_binary_annotations(request, response), ) return response
def save_data(stock_data, session): zipkin_attrs = construct_zipkin_attrs(stock_data) with zipkin_span(service_name = 'data-storage', span_name = 'save_data', transport_handler = http_transport_handler, zipkin_attrs= zipkin_attrs): # with zipkin_span(service_name = 'data-storage', span_name = 'save_data', transport_handler = http_transport_handler, sample_rate = 100.0): parsed = json.loads(stock_data) try: logger.debug('start to save data %s', stock_data) parsed = json.loads(stock_data) # parsed = json.loads(stock_data) #?? symbol = parsed.get('symbol') price = float(parsed.get('price')) timestamp = parsed.get('last_trade_time') statement = "INSERT INTO %s (symbol, trade_time, price) VALUES ('%s','%s',%f)" %(table, symbol,timestamp,price) session.execute(statement) logger.info('saved data into cassandra111 %s', stock_data) except Exception as e: logger.error('cannot save data %s', stock_data)
def test_starting_zipkin_trace_with_128bit_trace_id(): mock_transport_handler, mock_logs = mock_logger() mock_firehose_handler, mock_firehose_logs = mock_logger() with zipkin.zipkin_span( service_name="test_service_name", span_name="test_span_name", transport_handler=mock_transport_handler, sample_rate=100.0, binary_annotations={"some_key": "some_value"}, add_logging_annotation=True, use_128bit_trace_id=True, firehose_handler=mock_firehose_handler, encoding=Encoding.V2_JSON, ): pass def check_span(span): assert "traceId" in span assert len(span["traceId"]) == 32 check_span(json.loads(mock_logs[0])[0]) check_span(json.loads(mock_firehose_logs[0])[0])
def start_span(self, operation_name=None, child_of=None, references=None, tags=None, start_time=None, ignore_active_span=False): logger.debug("Starting Zipkin span [%s] for service [%s]", operation_name, self.service_name) parent: Span = self.active_span if self.active_span and not ignore_active_span else child_of if parent is None: zipkin_attrs = zipkin.create_attrs_for_span(self._sample_rate) else: zipkin_attrs = zipkin.ZipkinAttrs( trace_id=parent.trace_id or zipkin.generate_random_64bit_string(), span_id=zipkin.generate_random_64bit_string(), parent_span_id=parent.span_id, flags=parent.flags, is_sampled=int(parent.flags) & SAMPLED_FLAG == SAMPLED_FLAG, ) logger.debug('ZipkinAttrs: %s', repr(zipkin_attrs)) zipkin_span = zipkin.zipkin_span( service_name=self.service_name, zipkin_attrs=zipkin_attrs, span_name=operation_name, transport_handler=self._transport_handler, port=self._port, sample_rate=self._sample_rate, binary_annotations=dict({ 'span.tag.environment': SPAN_TAG_ENVIRONMENT, 'span.tag.service': self.service_name, }, **(tags or {})) ) context = SpanContext(*zipkin_attrs) return Span(self, context, operation_name, zipkin_span)
def test_zipkin_span_trace_with_0_sample_rate( logging_context_cls_mock, create_endpoint_mock, create_attrs_for_span_mock, mock_context_stack, firehose_enabled, ): create_attrs_for_span_mock.return_value = ZipkinAttrs( trace_id=generate_random_64bit_string(), span_id=generate_random_64bit_string(), parent_span_id=None, flags='0', is_sampled=False, ) transport_handler = MockTransportHandler() span_storage = SpanStorage() with zipkin.zipkin_span(service_name='some_service_name', span_name='span_name', transport_handler=transport_handler, sample_rate=0.0, context_stack=mock_context_stack, span_storage=span_storage, firehose_handler=mock.Mock() if firehose_enabled else None) as zipkin_context: assert zipkin_context.port == 0 create_attrs_for_span_mock.assert_called_once_with( sample_rate=0.0, use_128bit_trace_id=False, ) mock_context_stack.push.assert_called_once_with( create_attrs_for_span_mock.return_value) # When firehose mode is on, we log regardless of sample rate assert create_endpoint_mock.call_count == (1 if firehose_enabled else 0) assert logging_context_cls_mock.call_count == (1 if firehose_enabled else 0) mock_context_stack.pop.assert_called_once_with()
def say_hello(data: io.BytesIO = None): logging.getLogger().info("Inside app: " + app_name + " | function: " + func_name + " | method: say_hello") with zipkin_span( service_name=apm_service_name, span_name="say_hello method (custom child span)", binary_annotations=apm_binary_annotations) as span_context: name = "OCI Functions-APM integration!" try: body = json.loads(data.getvalue()) name = body.get("name") except (Exception, ValueError) as ex: logging.getLogger().info( "Inside app: " + app_name + " | function: " + func_name + " | method: say_hello | error parsing json payload: " + str(ex)) errors = {"Error": True, "ErrorMessage": str(ex)} span_context.update_binary_annotations(errors) message = "Hello {0}".format(name) logging.getLogger().info("message: " + message) return message
def _before_request(self): if self._disable: return _app_ctx_stack.top._view_func = \ current_app.view_functions.get(request.endpoint) if not self._should_use_token(_app_ctx_stack.top._view_func): return headers = request.headers trace_id = headers.get('X-B3-TraceId') or self._gen_random_id() span_id = headers.get('X-B3-SpanId') or self._gen_random_id() parent_span_id = headers.get('X-B3-ParentSpanId') is_sampled = str(headers.get('X-B3-Sampled') or '1') == '1' flags = headers.get('X-B3-Flags') zipkin_attrs = zipkin.ZipkinAttrs( trace_id=trace_id, span_id=span_id, parent_span_id=parent_span_id, flags=flags, is_sampled=is_sampled, ) handler = self._transport_handler or self.default_handler span = zipkin.zipkin_span( service_name=self.app.config.get('ZIPKIN_SERVICE_NAME', self.app.name), span_name='{0}.{1}'.format(request.endpoint, request.method), transport_handler=handler, sample_rate=self._sample_rate, zipkin_attrs=zipkin_attrs, encoding=self._encoding) g._zipkin_span = span g._zipkin_span.start() default_tags = self.app.config.get('ZIPKIN_TAGS', {'hostname': socket.gethostname()}) self.update_tags(default_tags)
def index(): now = datetime.datetime.today() # --------------------------------------------------------------------- # Create the span "webapp" and its callback handler - "http_transport" # Notice that both of our functions are called within the body of the # zipkin_span. # with zp.zipkin_span( service_name ="webapp", span_name='index', transport_handler = http_transport, port=5000, sample_rate= 100): external_service1() time.sleep(random.randint(1,3)) external_service2() later = datetime.datetime.today() return template('<b>Started {{today}} but finished {{later}}</b>!', today=now.ctime(), later=later.ctime())
def tween(request): settings = request.registry.settings # Creates zipkin_attrs and attaches a zipkin_trace_id attr to the request if 'zipkin.create_zipkin_attr' in settings: zipkin_attrs = settings['zipkin.create_zipkin_attr'](request) else: zipkin_attrs = create_zipkin_attr(request) if 'zipkin.transport_handler' in settings: transport_handler = settings['zipkin.transport_handler'] stream_name = settings.get('zipkin.stream_name', 'zipkin') transport_handler = functools.partial(transport_handler, stream_name) else: raise ZipkinError( "`zipkin.transport_handler` is a required config property, which" " is missing. It is a callback method which takes a log stream" " and a message as params and logs the message via scribe/kafka." ) service_name = request.registry.settings.get('service_name', 'unknown') span_name = '{0} {1}'.format(request.method, request.path) with zipkin_span( service_name=service_name, span_name=span_name, zipkin_attrs=zipkin_attrs, transport_handler=transport_handler, port=request.server_port, add_logging_annotation=settings.get( 'zipkin.add_logging_annotation', False, ), ) as zipkin_context: response = handler(request) zipkin_context.update_binary_annotations_for_root_span( get_binary_annotations(request, response), ) return response
def _before_request(self): if self._disable: return _app_ctx_stack.top._view_func = \ current_app.view_functions.get(request.endpoint) if not self._should_use_token(_app_ctx_stack.top._view_func): return headers = request.headers trace_id = headers.get('X-B3-TraceId') or self._gen_random_id() parent_span_id = headers.get('X-B3-ParentSpanId') is_sampled = str(headers.get('X-B3-Sampled') or '0') == '1' flags = headers.get('X-B3-Flags') zipkin_attrs = zipkin.ZipkinAttrs( trace_id=trace_id, span_id=self._gen_random_id(), parent_span_id=parent_span_id, flags=flags, is_sampled=is_sampled, ) handler = self._transport_handler or self.default_handler span = zipkin.zipkin_span( service_name=self.app.name, span_name='{0}.{1}'.format(request.endpoint, request.method), transport_handler=handler, sample_rate=self._sample_rate, zipkin_attrs=zipkin_attrs, host=self.app.config.get('APP_HOST', '127.0.0.1'), port=self.app.config.get('APP_PORT', 0), ) g._zipkin_span = span g._zipkin_span.start()
def test_zipkin_span_for_new_trace( logging_context_cls_mock, logger_handler_cls_mock, create_endpoint_mock, create_attrs_for_span_mock, push_zipkin_attrs_mock, pop_zipkin_attrs_mock, ): transport_handler = mock.Mock() with zipkin.zipkin_span( service_name='some_service_name', span_name='span_name', transport_handler=transport_handler, port=5, sample_rate=100.0, ) as zipkin_context: assert zipkin_context.port == 5 pass create_attrs_for_span_mock.assert_called_once_with( sample_rate=100.0, use_128bit_trace_id=False, ) push_zipkin_attrs_mock.assert_called_once_with( create_attrs_for_span_mock.return_value) create_endpoint_mock.assert_called_once_with(5, 'some_service_name', None) logger_handler_cls_mock.assert_called_once_with( create_attrs_for_span_mock.return_value) logging_context_cls_mock.assert_called_once_with( create_attrs_for_span_mock.return_value, create_endpoint_mock.return_value, logger_handler_cls_mock.return_value, 'span_name', transport_handler, report_root_timestamp=True, binary_annotations={}, add_logging_annotation=False, ) pop_zipkin_attrs_mock.assert_called_once_with()
def test_log_debug_for_existing_span(mock_logger, default_annotations): mock_transport_handler, mock_logs = mock_logger with zipkin.zipkin_span( service_name='test_service_name', span_name='test_span_name', transport_handler=mock_transport_handler, sample_rate=100.0, binary_annotations={'some_key': 'some_value'}, add_logging_annotation=True, ): zipkin_logger.debug({ 'annotations': { 'test_annotation': 42, }, 'binary_annotations': { 'extra_binary_annotation': 'extra_value', } }) pass assert len(mock_logs) == 1 span = _decode_binary_thrift_obj(mock_logs[0]) assert span.name == 'test_span_name' assert span.annotations[0].host.service_name == 'test_service_name' assert span.parent_id is None assert len(span.annotations) == 4 annotations = sorted(span.annotations, key=lambda ann: ann.value) assert annotations[3].value == 'test_annotation' assert annotations[3].timestamp == 42000000 default_annotations.add('test_annotation') assert set([ann.value for ann in annotations]) == default_annotations assert len(span.binary_annotations) == 2 binary_annotations = sorted( span.binary_annotations, key=lambda bin_ann: bin_ann.key) assert binary_annotations[0].key == 'extra_binary_annotation' assert binary_annotations[0].value == 'extra_value' assert binary_annotations[1].key == 'some_key' assert binary_annotations[1].value == 'some_value'
def test_service_span_that_is_independently_sampled(default_annotations, ): mock_transport_handler, mock_logs = mock_logger() mock_firehose_handler, mock_firehose_logs = mock_logger() zipkin_attrs = ZipkinAttrs( trace_id='0', span_id='1', parent_span_id='2', flags='0', is_sampled=False, ) with zipkin.zipkin_span( service_name='test_service_name', span_name='service_span', zipkin_attrs=zipkin_attrs, transport_handler=mock_transport_handler, port=45, sample_rate=100.0, binary_annotations={'some_key': 'some_value'}, add_logging_annotation=True, firehose_handler=mock_firehose_handler, ): pass def check_span(span): assert span.name == 'service_span' assert span.annotations[0].host.service_name == 'test_service_name' assert span.parent_id is None assert span.binary_annotations[0].key == 'some_key' assert span.binary_annotations[0].value == 'some_value' # Spans that are part of an unsampled trace which start their own sampling # should report timestamp/duration, as they're acting as root spans. assert span.timestamp is not None assert span.duration is not None assert set([ann.value for ann in span.annotations]) == default_annotations check_span(_decode_binary_thrift_obj(mock_logs[0])) check_span(_decode_binary_thrift_obj(mock_firehose_logs[0]))
def urlopen(self, method, url, **kw): if parsed_host and self.host == parsed_host.hostname: # Don't trace zipkin calls return func(self, method, url, **kw) attrs = { zipkin_core.HTTP_HOST: self.host, zipkin_core.HTTP_METHOD: method, zipkin_core.HTTP_PATH: url, } with zipkin_span(service_name=self.host, span_name=self._absolute_url(url), binary_annotations=attrs) as span: headers = kw['headers'] or {} headers.update(create_http_headers_for_new_span()) if 'headers' in kw: del kw['headers'] try: out = func(self, method, url, headers=headers, **kw) if hasattr(out.connection, 'sock') and out.connection.sock: peer = out.connection.sock.getpeername() span.add_sa_binary_annotation(peer[1], self.host, peer[0]) except: # always add sa_binary even in case of error # but if we do it before firing urlopen, then we ended up with two annotations span.add_sa_binary_annotation(self.port, self.host) raise span.update_binary_annotations({ zipkin_core.HTTP_STATUS_CODE: out.status, 'http.retries': out.retries, }) return out
def test_starting_zipkin_trace_with_128bit_trace_id( default_annotations, ): mock_transport_handler, mock_logs = mock_logger() mock_firehose_handler, mock_firehose_logs = mock_logger() with zipkin.zipkin_span( service_name='test_service_name', span_name='test_span_name', transport_handler=mock_transport_handler, sample_rate=100.0, binary_annotations={'some_key': 'some_value'}, add_logging_annotation=True, use_128bit_trace_id=True, firehose_handler=mock_firehose_handler ): pass def check_span(span): assert span.trace_id is not None assert span.trace_id_high is not None check_span(_decode_binary_thrift_obj(mock_logs[0])) check_span(_decode_binary_thrift_obj(mock_firehose_logs[0]))
def test_starting_zipkin_trace_with_sampling_rate( mock_logger, default_annotations, ): mock_transport_handler, mock_logs = mock_logger with zipkin.zipkin_span( service_name='test_service_name', span_name='test_span_name', transport_handler=mock_transport_handler, sample_rate=100.0, binary_annotations={'some_key': 'some_value'}, add_logging_annotation=True, ): pass span = _decode_binary_thrift_obj(mock_logs[0]) assert span.name == 'test_span_name' assert span.annotations[0].host.service_name == 'test_service_name' assert span.parent_id is None assert span.binary_annotations[0].key == 'some_key' assert span.binary_annotations[0].value == 'some_value' assert set([ann.value for ann in span.annotations]) == default_annotations
def square_number(): with zipkin_span( service_name='square_api', zipkin_attrs=ZipkinAttrs( trace_id=request.headers['X-B3-TraceID'], span_id=request.headers['X-B3-SpanID'], parent_span_id=request.headers['X-B3-ParentSpanID'], flags=request.headers['X-B3-Flags'], is_sampled=request.headers['X-B3-Sampled'], ), span_name='receive_inp_and_run_square_method', transport_handler=http_transport, sample_rate=100, port=7000, ): input_json = request.get_json() try: inp_num = int(input_json["num"]) res = square_nums(inp_num) except: res = {"success": False, "message": "Unknown error"} return json.dumps(res)
def topamenitiesbyhotel(): with zipkin_span(service_name='hotels.com:amenitiesquery', zipkin_attrs=ZipkinAttrs(trace_id=request.headers['X-B3-TraceID'], span_id=request.headers['X-B3-SpanID'], parent_span_id=request.headers['X-B3-ParentSpanID'], flags='1', is_sampled= request.headers['X-B3-Sampled']),span_name='amenitiesquery:amenitiesbyhotel', transport_handler=http_transport, port=PORT, sample_rate=ZIPKINSAMPLERATE): sessionid = request.args.get('sessionid') debugmessage('BEGIN AMENITIES SEARCH: SESSIONID', sessionid) status = setupsearchindex(sessionid) if (status == False): return jsonify({}) amenities = getamenities(sessionid, 6) amenitiesdict = defaultdict(defaultdict) for amenity in amenities: amenitiesdict[amenity['hotelid']] = [] for amenity in amenities: amenitiesdict[amenity['hotelid']].append(amenity) debugmessage('END AMENITIES SEARCH: SESSIONID', sessionid) return jsonify(amenitiesdict)
def test_adding_sa_binary_annotation_without_sampling(): """Even if we're not sampling, we still want to add binary annotations since they're useful for firehose traces. """ context = zipkin.zipkin_span( service_name='my_service', span_name='span_name', transport_handler=MockTransportHandler(), sample_rate=0.0, ) with context: context.add_sa_binary_annotation( port=123, service_name='test_service', host='1.2.3.4', ) expected_sa_endpoint = _encoding_helpers.create_endpoint( port=123, service_name='test_service', host='1.2.3.4', ) assert context.sa_endpoint == expected_sa_endpoint
def span_context(dummy_request): # These annotations should go to the server span zipkin_logger.debug({ 'annotations': { 'server_annotation': 1 }, 'binary_annotations': { 'server': 'true' }, }) # Creates a new span, a child of the server span with zipkin_span( service_name='child', span_name='get', binary_annotations={'foo': 'bar'}, ): # These annotations go to the child span zipkin_logger.debug({ 'annotations': { 'child_annotation': 1 }, 'binary_annotations': { 'child': 'true' }, }) # This should log a new span with `child` as its parent zipkin_logger.debug({ 'annotations': { 'grandchild_annotation': 1 }, 'binary_annotations': { 'grandchild': 'true' }, 'service_name': 'grandchild', 'name': 'put', }) return {}
def create_token(body): logging.debug("{authentication_controller} BEGIN function create_token()") if body['email'] is '' or body['password'] is '': return RESP.response_400(message='A given parameter is empty!') payload = {'email': body['email'], 'password': body['password']} logging.debug("{authentication_controller} %s", USERS_MS) with zipkin_span(service_name='authentication_ms', span_name='create_token') as zipkin_context: headers = {} headers.update(create_http_headers_for_new_span()) r = requests.post(USERS_MS + '/login', json=payload, headers=headers) zipkin_context.update_binary_annotations({ 'http.method': 'POST', 'http.url': USERS_MS + '/login', 'http.status_code': r.status_code }) if r.status_code == requests.codes.ok: token_info = { 'id': json.loads(r.content).get('id'), 'name': json.loads(r.content).get('name'), 'email': json.loads(r.content).get('email') } token = jwt.encode(token_info, TOKEN_SECRET, algorithm=ALGORITHM) return RESP.response_200(message={'token': token.decode('utf-8')}) if r.status_code == 500: return RESP.response_500(message='Users_MS is down!') return RESP.response_400(message='Credentials are incorrect!')
def _before_request(self): if self._disable: return _app_ctx_stack.top._view_func = \ current_app.view_functions.get(request.endpoint) if not self._should_use_token(_app_ctx_stack.top._view_func): return safe_headers = self._safe_headers(request.headers) parent_span_id = safe_headers.get('x-b3-parentspanid') trace_id = safe_headers.get('x-b3-traceid') or self._gen_random_id() is_sampled = str(safe_headers.get('x-b3-sampled') or '0') == '1' flags = safe_headers.get('x-b3-flags') zipkin_attrs = zipkin.ZipkinAttrs( trace_id=trace_id, span_id=self._gen_random_id(), parent_span_id=parent_span_id, flags=flags, is_sampled=is_sampled, ) handler = self._transport_handler or self.default_handler span = zipkin.zipkin_span(service_name=self.app.name, span_name='{0}.{1}'.format( request.endpoint, request.method), transport_handler=handler, sample_rate=self._sample_rate, zipkin_attrs=zipkin_attrs) g._zipkin_span = span g._zipkin_span.start()
def updatelocation(locationkey): with zipkin_span(service_name='hotels.com:locationquery', span_name='locationquery:updatelocation', transport_handler=http_transport, port=PORT, sample_rate=ZIPKINSAMPLERATE): rdb = redis.StrictRedis(connection_pool=redispool) if not request.json: abort(400) locationkey = 'L-' + str(locationkey) location = { 'id': request.json['id'], 'displayname': request.json['displayname'], 'acname': request.json['acname'], 'icon': request.json.get('icon', ''), 'latitude': request.json.get('latitude', 0), 'longitude': request.json.get('longitude', 0) } rdb.lset(locationkey, 0, location['id']) rdb.lset(locationkey, 1, location['displayname']) rdb.lset(locationkey, 2, location['acname']) rdb.lset(locationkey, 3, location['icon']) rdb.lset(locationkey, 4, location['latitude']) rdb.lset(locationkey, 5, location['longitude']) return jsonify({ 'locations': makepubliclocation(location) })
def bulk_record_workunits(self, engine_workunits): """A collection of workunits from v2 engine part""" for workunit in engine_workunits: duration = workunit['end_timestamp'] - workunit['start_timestamp'] span = zipkin_span( service_name="pants", span_name=workunit['name'], duration=duration, span_storage=self.span_storage, ) span.zipkin_attrs = ZipkinAttrs( trace_id=self.trace_id, span_id=workunit['span_id'], # TODO change it when we properly pass parent_id to the v2 engine Nodes # TODO Pass parent_id with ExecutionRequest when v2 engine is called by a workunit # TODO pass parent_id when v2 engine Node is called by another v2 engine Node parent_span_id=workunit.get("parent_id", self.parent_id), flags='0', # flags: stores flags header. Currently unused is_sampled=True, ) span.start() span.start_timestamp = workunit['start_timestamp'] span.stop()
def test_service_span_report_timestamp_override(default_annotations, ): mock_transport_handler, mock_logs = mock_logger() zipkin_attrs = ZipkinAttrs( trace_id='0', span_id='1', parent_span_id='2', flags='0', is_sampled=True, ) with zipkin.zipkin_span( service_name='test_service_name', span_name='service_span', zipkin_attrs=zipkin_attrs, transport_handler=mock_transport_handler, binary_annotations={'some_key': 'some_value'}, add_logging_annotation=True, report_root_timestamp=True, ): pass span = _decode_binary_thrift_obj(mock_logs[0]) _verify_service_span(span, default_annotations) assert span.timestamp is not None assert span.duration is not None
def span_context(dummy_request): # These annotations should go to the server span zipkin_logger.debug({ 'annotations': {'server_annotation': 1}, 'binary_annotations': {'server': 'true'}, }) # Creates a new span, a child of the server span with zipkin_span( service_name='child', span_name='get', binary_annotations={'foo': 'bar'}, ): # These annotations go to the child span zipkin_logger.debug({ 'annotations': {'child_annotation': 1}, 'binary_annotations': {'child': 'true'}, }) # This should log a new span with `child` as its parent zipkin_logger.debug({ 'annotations': {'grandchild_annotation': 1}, 'binary_annotations': {'grandchild': 'true'}, 'service_name': 'grandchild', 'name': 'put', }) return {}
def wrapper(*args, **kwargs): z_attrs = ThreadLocalStack().get() if z_attrs is None: headers = request.headers if headers.__contains__('X-B3-Traceid'): trace_id = request.headers['X-B3-Traceid'] parent_id = request.headers['X-B3-Parentspanid'] attrs = ZipkinAttrs( trace_id=trace_id, parent_span_id=parent_id, span_id=generate_random_64bit_string(), flags=0, is_sampled=True, ) with zipkin_span( service_name=service_name, span_name=span_name, transport_handler=transport_handler, port=port, binary_annotations=binary_annotations, zipkin_attrs=attrs, max_span_batch_size=max_span_batch_size, annotations=annotations, add_logging_annotation=add_logging_annotation, report_root_timestamp=report_root_timestamp, use_128bit_trace_id=use_128bit_trace_id, host=host, context_stack=context_stack, firehose_handler=firehose_handler): result = function(*args, **kwargs) return result else: with zipkin_span( service_name=service_name, span_name=span_name, transport_handler=transport_handler, port=port, sample_rate=sample_rate, binary_annotations=binary_annotations, zipkin_attrs=zipkin_attrs, max_span_batch_size=max_span_batch_size, annotations=annotations, add_logging_annotation=add_logging_annotation, report_root_timestamp=report_root_timestamp, use_128bit_trace_id=use_128bit_trace_id, host=host, context_stack=context_stack, firehose_handler=firehose_handler): result = function(*args, **kwargs) return result else: with zipkin_span(service_name=service_name, span_name=span_name, transport_handler=transport_handler, port=port, binary_annotations=binary_annotations, zipkin_attrs=zipkin_attrs, max_span_batch_size=max_span_batch_size, annotations=annotations, add_logging_annotation=add_logging_annotation, report_root_timestamp=report_root_timestamp, use_128bit_trace_id=use_128bit_trace_id, host=host, context_stack=context_stack, firehose_handler=firehose_handler): result = function(*args, **kwargs) return result
def do_stuff(): time.sleep(2) with zipkin_span(service_name='service2', span_name='service2_db_search'): db_search() return 'OK'
def test_exit(self): context = zipkin.zipkin_span("test_service", "test_span") with mock.patch.object(context, "stop", autospec=True) as mock_stop: context.__exit__(ValueError, "error", None) assert mock_stop.call_args == mock.call(ValueError, "error", None)
def test_enter(self): # Test that __enter__ calls self.start context = zipkin.zipkin_span('test_service', 'test_span') with mock.patch.object(context, 'start', autospec=True) as mock_start: context.__enter__() assert mock_start.call_count == 1
def test_exit(self): context = zipkin.zipkin_span('test_service', 'test_span') with mock.patch.object(context, 'stop', autospec=True) as mock_stop: context.__exit__(ValueError, 'error', None) assert mock_stop.call_args == mock.call(ValueError, 'error', None)
stock_table = settings.get('assignment6', 'CONDIG_CASSANDRA_STOCK_TABLE') averate_price_table = settings.get( 'assignment6', 'CONDIG_CASSANDRA_STOCK_AVERAGE_PRICE_TABLE') stock_trend_table = settings.get('assignment6', 'CONFIG_CASSANDRA_STOCK_TREND') stock_tweet_table = settings.get('assignment6', 'CONFIG_CASSANDRA_STOCK_TWEET') cassandra_host = settings.get('assignment6', 'CONFIG_CASSANDRA_CONTACT_POINT') kafka_brokers = brokers + ":" + kafka_broker_port with zipkin_span( service_name='assignment6', span_name= 'Spark streaming to process stock price and calculate average price', transport_handler=http_transport_handler, sample_rate=100.0, # Value between 0.0 and 100.0 ): # - create Cassandra session, keyspace and tables session = startCassandraSession(cassandra_host) cassandraCreateTable(session, keyspace, stock_table) cassandraCreateTable(session, keyspace, averate_price_table) cassandraCreateTable(session, keyspace, stock_trend_table) cassandraCreateTable(session, keyspace, stock_tweet_table) # - instantiate a simple kafka producer kafka_producer = KafkaProducer(bootstrap_servers=kafka_brokers) # - instantiate a kafka stock stream and a tweet stream for processing stockDirectKafkaStream = KafkaUtils.createDirectStream(