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.mehod), transport_handler=handler, sample_rate=self._sample_rate, zipkin_attrs=zipkin_attrs ) g._zipkin_span = span g._zipkin_span.start()
def _read_zipkin_attrs(worker_ctx): if TRACE_ID_HEADER not in worker_ctx.data: return None return zipkin.ZipkinAttrs( trace_id=worker_ctx.data[TRACE_ID_HEADER], span_id=generate_random_64bit_string(), parent_span_id=worker_ctx.data[PARENT_SPAN_ID_HEADER], flags=worker_ctx.data[FLAGS_HEADER], is_sampled=worker_ctx.data[SAMPLED_HEADER] == '1')
def _read_zipkin_attrs(worker_ctx): if TRACE_ID_HEADER not in worker_ctx.data: trace_id = generate_random_64bit_string() logger.info('No {} header in context, created trace ID: {}'.format(TRACE_ID_HEADER, trace_id)) else: trace_id = worker_ctx.data[TRACE_ID_HEADER] return zipkin.ZipkinAttrs(trace_id=trace_id, span_id=generate_random_64bit_string(), parent_span_id=worker_ctx.data.get(PARENT_SPAN_ID_HEADER), flags=worker_ctx.data.get(FLAGS_HEADER), is_sampled=worker_ctx.data.get(SAMPLED_HEADER) == '1')
def _start_call_span(self, app, sender, task_id): # type: (celery.app.Celery, str, str) -> py_zipkin.zipkin.server_span zipkin_attrs = zipkin.get_zipkin_attrs( ) # type: py_zipkin.zipkin.ZipkinAttrs """:type: py_zipkin.zipkin.ZipkinAttrs""" if zipkin_attrs: zipkin_attrs = zipkin.ZipkinAttrs( trace_id=zipkin_attrs.trace_id, span_id=zipkin.generate_random_64bit_string(), parent_span_id=zipkin_attrs.span_id, flags=zipkin_attrs.flags, is_sampled=zipkin_attrs.is_sampled, ) else: zipkin_attrs = zipkin.create_attrs_for_span(self.sample_rate) # Get task definition to see if task ignores result task = get_task_from_app(app, sender) # Is Call Complete at this point. if task.ignore_result or not self._has_result_event: call_type = 'async' backend = None else: call_type = 'call' backend = get_task_backend_name(task, is_eager) span = zipkin.zipkin_server_span( service_name='celery.{}'.format(sender.split('.', 1)[0]), span_name='task.{}:{}'.format(call_type, sender), transport_handler=self.transport_handler, sample_rate=self.sample_rate, zipkin_attrs=zipkin_attrs, ) print span.service_name print span.span_name print span.zipkin_attrs span.start() span.update_binary_annotations({ 'celery.task.name': sender, 'celery.task.type': call_type, 'celery.task.backend': backend, 'celery.machine': self._hostname, 'celery.platform': self._platform, }) return span
def generate_zipkin_attrs(self, trace_id=None, span_id=None, parent_span_id=None, flags=None, is_sampled=None): zipkin_attrs = zipkin.ZipkinAttrs( trace_id=trace_id, span_id=span_id, parent_span_id=parent_span_id, flags=flags, is_sampled=is_sampled, ) console.info("zipkin_attrs...begin") console.info(zipkin_attrs) console.info("zipkin_attrs...end") return zipkin_attrs
def _start_rpc_span(self, sender, zipkin_attrs): # type: (str, py_zipkin.zipkin.ZipkinAttrs) -> py_zipkin.zipkin.client_span zipkin_attrs = zipkin.ZipkinAttrs( trace_id=zipkin_attrs.trace_id, span_id=zipkin.generate_random_64bit_string(), parent_span_id=zipkin_attrs.span_id, flags=zipkin_attrs.flags, is_sampled=zipkin_attrs.is_sampled, ) span = zipkin.zipkin_client_span( service_name='celery.{}'.format(sender.split('.', 1)[0]), span_name='task.rpc.{}'.format(sender), transport_handler=self.transport_handler, sample_rate=self.sample_rate, zipkin_attrs=zipkin_attrs, ) span.start() return span
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 _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 _start_taskrun_span(self, sender, task, span_id=None, trace_id=None, parent_id=None, flags=None, is_sampled=None): # type: (str, str, str, str, str, str) -> py_zipkin.zipkin.server_span zipkin_attrs = zipkin.get_zipkin_attrs( ) # type: py_zipkin.zipkin.ZipkinAttrs """:type: py_zipkin.zipkin.ZipkinAttrs""" if not zipkin_attrs: zipkin_attrs = zipkin.create_attrs_for_span(self.sample_rate) zipkin_attrs = zipkin.ZipkinAttrs( trace_id=trace_id or zipkin_attrs.trace_id, span_id=span_id or zipkin.generate_random_64bit_string(), parent_span_id=parent_id or zipkin_attrs.span_id, flags=flags or zipkin_attrs.flags, is_sampled=zipkin_attrs.is_sampled if is_sampled is None else is_sampled, ) span = zipkin.zipkin_server_span( service_name='celery.{}'.format(sender.split('.', 1)[0]), span_name='task.run:{}'.format(sender), transport_handler=self.transport_handler, sample_rate=self.sample_rate, zipkin_attrs=zipkin_attrs, ) backend = None context = task.request is_eager = context.get('is_eager', False) if task.ignore_result or not self._has_result_event: call_type = 'async' else: call_type = 'call' if is_eager: backend = 'is_eager' else: backend = get_task_backend_name(task, is_eager) span.start() span.update_binary_annotations({ 'celery.task.name': sender, 'celery.task.type': call_type, 'celery.task.backend': backend, 'celery.machine': context.get('hostname', self._hostname), 'celery.is_eager': context.get('is_eager', False), 'celery.platform': self._platform, }) return span