def open(self, fullurl, data=None): parsed_url = urlparse.urlparse(fullurl) host = parsed_url.hostname or None port = parsed_url.port or None if get_current_span() is None: span = opentracing.tracer.begin_trace( operation_name='urllib' ) # TODO pass client=True else: span = get_current_span().start_child(operation_name='http') # use span as context manager so that its finish() method is called with span: span.set_tag('http.url', fullurl) if host: span.set_tag(tags.PEER_HOST_IPV4, host) if port: span.set_tag(tags.PEER_PORT, port) # TODO add callee service name # TODO add headers to propagate trace # cannot use super here, this is an old style class fileobj = urllib.FancyURLopener.open(self, fullurl, data) if fileobj.getcode() is not None: span.set_tag('status_code', fileobj.getcode()) return fileobj
async def handle(self, req: BaseRequest, **kwargs) -> object: current_span = get_current_span() if get_current_span() is None: current_span = request_span.get() span_tags = { tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER, "request_type": req.get_class_name(), "request_value": req } with tracer.start_active_span(req.get_class_name(), child_of=current_span, tags=span_tags, finish_on_close=True): response = await self.next().handle(req=req, **kwargs) return response
def tracing_execute_command(*args, **kwargs): if is_klass: # Unbound method, we will get 'self' in args. self = args[0] reported_args = args[1:] else: self = None reported_args = args command = reported_args[0] span = opentracing_instrumentation.utils.start_child_span( operation_name=_get_operation_name(command), parent=opentracing_instrumentation.get_current_span()) _set_base_span_tags(self, span, _normalize_stmt(reported_args)) try: rv = execute_command_method(*args, **kwargs) except Exception as exc: span.set_tag('error', 'true') span.set_tag('error.object', exc) raise finally: span.finish() return rv
def join_trace(request): join_trace_request = request.body.request or None logging.info('TChannel join_trace request: %s', join_trace_request) response = yield self.prepare_response(get_current_span(), join_trace_request.downstream) logging.info('TChannel join_trace response: %s', response) raise tornado.gen.Return(trace_response_to_thriftrw(service, response))
def yeet_generator(self): with tracer.start_span("yeet_gen_1", child_of=get_current_span()) as span: with span_in_context(span): y = [pyfiglet.figlet_format("Yeet!!") for _ in range(1000)] x = self.yeet_gen_2() x.append(y) return y
def open(self, fullurl, data=None): parsed_url = urlparse.urlparse(fullurl) host = parsed_url.hostname or None port = parsed_url.port or None span = utils.start_child_span( operation_name='urllib', parent=get_current_span()) span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) # use span as context manager so that its finish() method is called with span: span.set_tag(ext_tags.HTTP_URL, fullurl) if host: span.set_tag(ext_tags.PEER_HOST_IPV4, host) if port: span.set_tag(ext_tags.PEER_PORT, port) # TODO add callee service name # TODO add headers to propagate trace # cannot use super here, this is an old style class fileobj = urllib.FancyURLopener.open(self, fullurl, data) if fileobj.getcode() is not None: span.set_tag('http.status_code', fileobj.getcode()) return fileobj
def produce_wrapper(producer, value, key=None, **kwargs): """Wraps Producer._produce""" parent_ctx = get_current_span() if parent_ctx: span = start_child_span( operation_name='kafka:produce', parent=parent_ctx, ) span.set_tag(tags.MESSAGE_BUS_DESTINATION, producer.get_topic_name()) if key is not None: span.set_tag('key', str(key)) headers = kwargs.pop('headers', {}) try: opentracing.tracer.inject(span_context=span.context, format=Format.TEXT_MAP, carrier=headers) except opentracing.UnsupportedFormatException: pass with span: try: _Producer_produce(producer, value, key=key, headers=headers, **kwargs) except Exception as error: span.set_tag(tags.ERROR, True) span.log_kv({ 'event': tags.ERROR, 'error.object': error, }) raise else: _Producer_produce(producer, value, key=key, **kwargs)
def wrapped_call(*args, **kwargs): parent_span = get_current_span() if parent_span is None: return original_func(*args, **kwargs) # The reason this works is because TChannel is only looking at # trace_id and span_id fields in the object it retrieves from context with request_context_func(parent_span): return original_func(*args, **kwargs)
def observe_span(): span = get_current_span() logging.info('Observed span: %s', span) if span is None: return api.ObservedSpan(traceId='missing', sampled=False, baggage='') return api.ObservedSpan( traceId="%x" % span.trace_id, sampled=span.is_sampled(), baggage=span.get_baggage_item(api.BAGGAGE_KEY), )
def _wrappper(*args, **kwargs): if len(args) == 0: http_url = kwargs.get("url") http_method = kwargs.get("method") elif len(args) == 1: http_url = kwargs.get("url") http_method = args[0] elif len(args) == 2: http_url = args[1] http_method = args[0] else: http_url = "" http_method = "POST" current_tracer = global_tracer() current_span = get_current_span() def start_child_span(parent_span): with current_tracer.start_active_span( http_url.split("/")[-1], child_of=parent_span) as scope: trace_headers = {} current_tracer.inject(scope.span.context, Format.HTTP_HEADERS, trace_headers) headers = kwargs.get("headers") if not headers: headers = trace_headers else: for k, v in trace_headers.items(): headers[k] = v kwargs["headers"] = headers scope.span.set_tag("http.url", http_url) scope.span.set_tag("http.method", http_method) result = func(*args, **kwargs) if not result: scope.span.set_tag("http.status_code", 499) else: scope.span.set_tag("http.status_code", result.status_code) return result parent_greenlet_span = kwargs.pop("parent_greenlet_span", None) if parent_greenlet_span: with current_tracer.scope_manager.activate(parent_greenlet_span, finish_on_close=False): request_result = start_child_span( parent_span=parent_greenlet_span) else: request_result = start_child_span(parent_span=current_span) logging.debug( "Tracing request: tracer:{}, span:{}, http_url:{}, http_method:{}". format(current_tracer, parent_greenlet_span if not current_span else current_span, http_url, http_method)) return request_result
def inject_span_in_headers(headers): # FLASK https://github.com/opentracing-contrib/python-flask tracer = current_app.tracer # Add traces span = None if tracer: span = tracer.get_span(request=request) if not span: # pragma: no cover span = get_current_span() if not span: span = tracer.tracer.start_span() context = span.context if span else None tracer.tracer.inject(context, opentracing.Format.HTTP_HEADERS, headers) return headers
def tracing_immediate_execute_command(*args, **options): command = args[0] span = opentracing_instrumentation.utils.start_child_span( operation_name=_get_operation_name(command), parent=opentracing_instrumentation.get_current_span()) _set_base_span_tags(pipe, span, _normalize_stmt(args)) try: res = immediate_execute_method(*args, **options) except Exception as exc: span.set_tag('error', 'true') span.set_tag('error.object', exc) finally: span.finish()
def before_request(cmd, name): """ A common method to record the memcache calls :param cmd: either 'get' or 'set' :param name: name of the key gettting or setting (Added as a tag) :return: the created span """ operation_name = 'memcache:%s' % (cmd) span = utils.start_child_span(operation_name=operation_name, parent=get_current_span()) span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) # TODO: What is the PEER_SERVICE ? span.set_tag(ext_tags.PEER_SERVICE, 'memcache') span.set_tag('memcache_key', name) return span
def tracing_parse_response(block=True, timeout=0): span = opentracing_instrumentation.utils.start_child_span( operation_name=_get_operation_name('SUB'), parent=opentracing_instrumentation.get_current_span()) _set_base_span_tags(pubsub, span, '') try: rv = parse_response_method(block=block, timeout=timeout) except Exception as exc: span.set_tag('error', 'true') span.set_tag('error.object', exc) raise finally: span.finish() return rv
def execute_command(self, cmd, *args, **kwargs): operation_name = 'redis:%s' % (cmd,) span = utils.start_child_span( operation_name=operation_name, parent=get_current_span()) span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) span.set_tag(ext_tags.PEER_SERVICE, 'redis') # set the peer information (remote host/port) for tag_key, tag_val in self.peer_tags(): span.set_tag(tag_key, tag_val) # for certain commands we'll add extra attributes such as the redis key for tag_key, tag_val in getattr(self, '_extra_tags', []): span.set_tag(tag_key, tag_val) self._extra_tags = [] with span: return ORIG_METHODS['execute_command'](self, cmd, *args, **kwargs)
def execute_command(self, cmd, *args, **kwargs): operation_name = 'redis:%s' % (cmd, ) span = utils.start_child_span(operation_name=operation_name, parent=get_current_span()) span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) span.set_tag(ext_tags.PEER_SERVICE, 'redis') # set the peer information (remote host/port) for tag_key, tag_val in self.peer_tags(): span.set_tag(tag_key, tag_val) # for certain commands we'll add extra attributes such as the redis key for tag_key, tag_val in getattr(self, '_extra_tags', []): span.set_tag(tag_key, tag_val) self._extra_tags = [] with span: return ORIG_METHODS['execute_command'](self, cmd, *args, **kwargs)
def _wrappper(*args, **kwargs): current_tracer = global_tracer() current_span = get_current_span() parent_greenlet_span = kwargs.pop("parent_greenlet_span", None) if parent_greenlet_span: with global_tracer().scope_manager.activate(parent_greenlet_span, finish_on_close=False): with current_tracer.start_active_span( func.__name__, child_of=parent_greenlet_span): result = func(*args, **kwargs) else: with current_tracer.start_active_span(func.__name__, child_of=current_span): result = func(*args, **kwargs) logging.debug("Tracing function: tracer:{}, span:{}, name:{}".format( current_tracer, parent_greenlet_span if not current_span else current_span, func.__name__)) return result
def decorator(*args, **kwargs): parent_span = get_current_span() if parent_span is None and require_active_trace: return func(*args, **kwargs) span = utils.start_child_span(operation_name=operation_name, parent=parent_span) if callable(on_start): on_start(span, *args, **kwargs) with span, span_in_context(span): try: return func(*args, **kwargs) except Exception as error: span.set_tag(tags.ERROR, True) span.log_kv({ 'event': tags.ERROR, 'error.object': error, }) raise
def decorator(*args, **kwargs): parent_span = get_current_span() if parent_span is None and require_active_trace: return func(*args, **kwargs) span = utils.start_child_span( operation_name=operation_name, parent=parent_span) if callable(on_start): on_start(span, *args, **kwargs) with span, span_in_context(span): try: return func(*args, **kwargs) except Exception as error: span.set_tag(tags.ERROR, True) span.log_kv({ 'event': tags.ERROR, 'error.object': error, }) raise
def tracing_execute(raise_on_error=True): if not pipe.command_stack: # Nothing to process/handle. return execute_method(raise_on_error=raise_on_error) span = opentracing_instrumentation.utils.start_child_span( operation_name=_get_operation_name('MULTI'), parent=opentracing_instrumentation.get_current_span()) _set_base_span_tags(pipe, span, _normalize_stmts(pipe.command_stack)) try: res = execute_method(raise_on_error=raise_on_error) except Exception as exc: span.set_tag('error', 'true') span.set_tag('error.object', exc) raise finally: span.finish() return res
def db_span(sql_statement, module_name, sql_parameters=None, connect_params=None, cursor_params=None): span = get_current_span() @contextlib2.contextmanager def empty_ctx_mgr(): yield None if span is None: return empty_ctx_mgr() statement = sql_statement.strip() add_sql_tag = True if sql_statement in _TRANS_TAGS: operation = sql_statement add_sql_tag = False else: space_idx = statement.find(' ') if space_idx == -1: operation = '' # unrecognized format of the query else: operation = statement[0:space_idx] tags = dict() if add_sql_tag: tags['sql'] = statement if sql_parameters: tags['sql.params'] = sql_parameters if connect_params: tags['sql.conn'] = connect_params if cursor_params: tags['sql.cursor'] = cursor_params return span.start_child( operation_name='%s:%s' % (module_name, operation), tags=tags )
def func_span(func, tags=None, require_active_trace=True): """ Creates a new local span for execution of the given `func`. The returned span is best used as a context manager, e.g. .. code-block:: python with func_span('my_function'): return my_function(...) At this time the func should be a string name. In the future this code can be enhanced to accept a real function and derive its qualified name. :param func: name of the function or method :param tags: optional tags to add to the child span :param require_active_trace: controls what to do when there is no active trace. If require_active_trace=True, then no span is created. If require_active_trace=False, a new trace is started. :return: new child span, or a dummy context manager if there is no active/current parent span """ current_span = get_current_span() if current_span is None and require_active_trace: @contextlib.contextmanager def empty_ctx_mgr(): yield None return empty_ctx_mgr() # TODO convert func to a proper name: module:class.func operation_name = str(func) span = utils.start_child_span(operation_name=operation_name, parent=current_span, tags=tags) with span_in_context(span): return span
def tracing_wrapper(*args, **kwargs): op_name = func.__name__ if span_name is None else span_name args_dict = dict(zip(func.__code__.co_varnames, args)) args_dict.update(kwargs) parent = args_dict.get(parent_span_arg, None) try: parent = deserialize_span_context(parent) except: pass if parent is None: parent = get_current_span() with opentracing.tracer.start_span(op_name, child_of=parent) as span: #add tags try: for arg in args_as_tags: span.set_tag(arg, args_dict.get(arg, None)) except TypeError: pass with span_in_context(span): value = func(*args, **kwargs) return value
def func_span(func, tags=None, require_active_trace=True): """ Creates a new local span for execution of the given `func`. The returned span is best used as a context manager, e.g. .. code-block:: python with func_span('my_function'): return my_function(...) At this time the func should be a string name. In the future this code can be enhanced to accept a real function and derive its qualified name. :param func: name of the function or method :param tags: optional tags to add to the child span :param require_active_trace: controls what to do when there is no active trace. If require_active_trace=True, then no span is created. If require_active_trace=False, a new trace is started. :return: new child span, or a dummy context manager if there is no active/current parent span """ current_span = get_current_span() if current_span is None and require_active_trace: @contextlib.contextmanager def empty_ctx_mgr(): yield None return empty_ctx_mgr() # TODO convert func to a proper name: module:class.func operation_name = str(func) span = utils.start_child_span( operation_name=operation_name, parent=current_span, tags=tags) with span_in_context(span): return span
def get_global_span(): return get_current_span()
def serialize_current_span(): """ get the current span and serialize it.""" return serialize_span(get_current_span())
def get_current_span(self): """ :return: The current :py:class:`Span` for this thread/request. """ return opentracing_instrumentation.get_current_span()
def yeet_gen_2(): with tracer.start_span("yeet_gen_2", child_of=get_current_span()) as span: with span_in_context(span): y = pyfiglet.figlet_format("Yeet!!") return [y for _ in range(10000)]