def test_urllib2(scheme, root_span, install_hooks): request = urllib2.Request('%s://localhost:9777/proxy' % scheme, headers={'Remote-LOC': 'New New York', 'Remote-Op': 'antiquing'}) class Response(object): def __init__(self): self.code = 200 self.msg = '' def info(self): return None if root_span: root_span = mock.MagicMock() root_span.context = mock.MagicMock() root_span.finish = mock.MagicMock() root_span.__exit__ = mock.MagicMock() else: root_span = None span = mock.MagicMock() span.set_tag = mock.MagicMock() span.finish = mock.MagicMock() def inject(span_context, format, carrier): carrier['TRACE-ID'] = '123' p_do_open = mock.patch('urllib2.AbstractHTTPHandler.do_open', return_value=Response()) p_start_span = mock.patch.object(opentracing.tracer, 'start_span', return_value=span) p_inject = mock.patch.object(opentracing.tracer, 'inject', side_effect=inject) p_current_span = span_in_context(span=root_span) with p_do_open, p_start_span as start_call, p_inject, p_current_span: resp = urllib2.urlopen(request) expected_references = root_span.context if root_span else None start_call.assert_called_once_with( operation_name='GET:antiquing', child_of=expected_references, tags=None, ) assert resp is not None span.set_tag.assert_any_call('span.kind', 'client') assert span.__enter__.call_count == 1 assert span.__exit__.call_count == 1, 'ensure finish() was called' if root_span: assert root_span.__exit__.call_count == 0, 'do not finish root span' # verify trace-id was correctly injected into headers norm_headers = HTTPHeaders(request.headers) assert norm_headers.get('trace-id') == '123'
def patched_run(*args, **kwargs): span = start_child_span( operation_name=operation, parent=parent_ctx, ) with span, span_in_context(span): try: return run(*args, **kwargs) except Exception as error: span.set_tag(tags.ERROR, True) span.log_kv({ 'event': tags.ERROR, 'error.object': error, }) raise
def wrapper(*args, **kwargs): request = stack.top.request try: # Create a new span context, reading in values (traceid, # spanid, etc) from the incoming x-b3-*** headers. span_ctx = tracer.extract(Format.HTTP_HEADERS, dict(request.headers)) # Note: this tag means that the span will *not* be # a child span. It will use the incoming traceid and # spanid. We do this to propagate the headers verbatim. rpc_tag = {tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER} span = tracer.start_span(operation_name='op', child_of=span_ctx, tags=rpc_tag) except Exception as e: # We failed to create a context, possibly due to no # incoming x-b3-*** headers. Start a fresh span. # Note: This is a fallback only, and will create fresh headers, # not propagate headers. span = tracer.start_span('op') with span_in_context(span): r = f(*args, **kwargs) return r
def consume_wrapper(consumer, message): """Wraps KafkaWorker._consume""" try: carrier = {} for (key, value) in message.headers(): carrier[key] = value log.debug(carrier) parent_ctx = opentracing.tracer.extract( format=Format.TEXT_MAP, carrier=carrier ) except: parent_ctx = None if parent_ctx or getattr(consumer, 'start_new_traces', False): tags_dict = {tags.MESSAGE_BUS_DESTINATION: consumer.get_topic_name()} if message.key(): tags_dict['key'] = str(message.key()) span = opentracing.tracer.start_span( operation_name='kafka:consume', child_of=parent_ctx, tags=tags_dict, ) with span, span_in_context(span): try: _KafkaWorker_consume(consumer, message) except Exception as error: span.set_tag(tags.ERROR, True) span.log_kv({ 'event': tags.ERROR, 'error.object': error, }) raise else: _KafkaWorker_consume(consumer, message)
def fib(index): active_span = tracer.get_span() with open_tracer.start_span('fib', child_of=active_span) as fib: with span_in_context(fib): return fib_iter(index)
def insert_bulk(list_data, collection): with tracer.start_span('insert_bulk', child_of=get_current_span()) as span: span.set_tag('mongodb', 'operation:insert_many') with span_in_context(span): res = collection.insert_many(list_data) return len(res.inserted_ids)
def get_tracebacks_for_day( ES, tracer, date_to_analyze: datetime.date, filter_text: str, hidden_traceback_ids: set, ) -> typing.List[TracebackPlusMetadata]: """ Retrieves the Tracebacks for the given date_to_analyze date. If provided, only returns Tracebacks which match filter_text. Only returns Tracebacks whose ids aren't in hidden_traceback_ids. """ tracer = tracer or opentracing.tracer root_span = get_current_span() # get all tracebacks with tracer.start_span('get all tracebacks', child_of=root_span) as span: with span_in_context(span): tracebacks = traceback_db.get_tracebacks(ES, tracer, date_to_analyze, date_to_analyze) logger.debug('found %s tracebacks', len(tracebacks)) # filter out tracebacks the user has hidden. we use a namedlist to store each traceback + some # metadata we'll use when rendering the html page tb_meta = [ TracebackPlusMetadata(traceback=t) for t in tracebacks if t.origin_papertrail_id not in hidden_traceback_ids ] # get a list of matching jira issues with tracer.start_span('for each traceback, get matching jira issues', child_of=root_span) as span: with span_in_context(span): for tb in tb_meta: tb.jira_issues = jira_issue_db.get_matching_jira_issues( ES, tracer, tb.traceback.traceback_text, es_util.EXACT_MATCH) matching_jira_keys = set(jira_issue.key for jira_issue in tb.jira_issues) similar_jira_issues = jira_issue_db.get_matching_jira_issues( ES, tracer, tb.traceback.traceback_text, es_util.SIMILAR_MATCH) tb.similar_jira_issues = [ similar_jira_issue for similar_jira_issue in similar_jira_issues if similar_jira_issue.key not in matching_jira_keys ] # apply user's filters if filter_text == 'Has Ticket': tb_meta = [tb for tb in tb_meta if tb.jira_issues] elif filter_text == 'No Ticket': tb_meta = [tb for tb in tb_meta if not tb.jira_issues] elif filter_text == 'No Recent Ticket': tb_meta_without_recent_ticket = [] for tb in tb_meta: has_recent_issues = False for issue in tb.jira_issues: if issue.updated > TWO_WEEKS_AGO: has_recent_issues = True break if not has_recent_issues: tb_meta_without_recent_ticket.append(tb) tb_meta = tb_meta_without_recent_ticket elif filter_text == 'Has Open Ticket': tb_meta = [ tb for tb in tb_meta if [issue for issue in tb.jira_issues if issue.status != 'Closed'] ] else: tb_meta = tb_meta # we take at most 100 tracebacks, due to performance issues of having more tb_meta = tb_meta[:100] # for each traceback, get all similar tracebacks with tracer.start_span('for each traceback, get similar tracebacks', child_of=root_span) as span: with span_in_context(span): for tb in tb_meta: tb.similar_tracebacks = [] tb.similar_tracebacks = traceback_db.get_matching_tracebacks( ES, tracer, tb.traceback.traceback_text, es_util.EXACT_MATCH, 100) return tb_meta
def execute_with_tracing(self, tracer): """Will retrain with fresh data and perform predictions in batches.""" with tracer.start_span('aiops_inference_execute', child_of=get_current_span()) as span: with span_in_context(span): return self.execute()
def execute_with_tracing(self, tracer): """Will wrap execution of inference with tracer to measure latency.""" with tracer.start_span('aiops_inference_execute', child_of=get_current_span()) as span: with span_in_context(span): return self.execute()
async def coro(): span = tracer.start_span(operation_name='foobar') with span_in_context(span): span.finish() return span
def execute_with_tracing(self, tracer): """Train models for anomaly detection with tracing enabled.""" with tracer.start_span('aiops_train_execute', child_of=get_current_span()) as span: with span_in_context(span): return self.execute()
def say_hello(hello_to): with tracer.start_span('say-hello') as span: span.set_tag('hello-to', hello_to) with span_in_context(span): hello_str = format_string(hello_to) print_hello(hello_str)
def formatter(tracer, greeting, name): active_span = get_current_span() with tracer.start_span('formatter', child_of=active_span) as format_span: with span_in_context(format_span): return '{} {}!!'.format(greeting.title(), name.title())
def print_hello(hello_str): with tracer.start_span('printHello', child_of=get_current_span()) as span: with span_in_context(span): http_get(8082, 'publish', 'helloStr', hello_str) span.log_kv({'event': 'println'})
def test_insert_one(collection, topic): with tracer.start_span('test_line_insertion') as span: span.set_tag('mongodb', 'insertion_test') with span_in_context(span): data = consumer_single_topic(topic) insert_one(data, collection)
def insert_one(list_data, collection): with tracer.start_span('insert_one', child_of=get_current_span()) as span: span.set_tag('mongodb', 'operation:insert_one') with span_in_context(span): for data in list_data: collection.insert_one(data)
def format_string(hello_to): with tracer.start_span('format', child_of=get_current_span()) as span: with span_in_context(span): hello_str = http_get(8081, 'format', 'helloTo', hello_to) span.log_kv({'event': 'string-format', 'value': hello_str}) return hello_str
async def coro(name, finish): span = tracer.start_span(operation_name=name) with span_in_context(span): if finish: span.finish() return span