def test_baggage_items(self): """Test Baggage Items.""" # Create parentCtx with baggage items tracer = WavefrontTracer(ConsoleReporter(), self.application_tags) baggage_item = {'foo': 'bar', 'user': '******'} parent_ctx = WavefrontSpanContext(trace_id=uuid.uuid1(), span_id=uuid.uuid1(), baggage=baggage_item, decision=True) span = tracer.start_span('test_op', child_of=parent_ctx) self.assertEqual('bar', span.get_baggage_item('foo')) self.assertEqual('name', span.get_baggage_item('user')) # Parent and Follows baggage_item = {'tracer': 'id', 'db.name': 'name'} follows_ctx = WavefrontSpanContext(trace_id=uuid.uuid1(), span_id=uuid.uuid1(), baggage=baggage_item, decision=True) span = tracer.start_span('test_op', references=[ opentracing.child_of(parent_ctx), opentracing.child_of(follows_ctx) ]) self.assertEqual('bar', span.get_baggage_item('foo')) self.assertEqual('name', span.get_baggage_item('user')) self.assertEqual('id', span.get_baggage_item('tracer')) self.assertEqual('name', span.get_baggage_item('db.name')) # Validate root span span = tracer.start_span('test_op') self.assertIsNotNone(span.context.baggage) self.assertTrue(not bool(span.context.baggage))
def test_child_span(tracer): span = tracer.start_span("test") child = tracer.start_span("child", references=child_of(span.context)) child.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) child.set_tag('bender', 'is great') child.log_event('kiss-my-shiny-metal-...') child.finish() span.finish() tracer.reporter.report_span.assert_called_once() assert len(span.logs) == 0, 'Parent span is Local, must not have events' assert len(child.logs) == 1, 'Child must have one events' add_zipkin_annotations(span=span, endpoint=None) add_zipkin_annotations(span=child, endpoint=None) assert len([t for t in span.tags if t.key == g.LOCAL_COMPONENT]) == 1 assert len(child.logs) == 3, 'Child must have three events' assert log_exists(child, g.CLIENT_SEND), 'Must have cs event' assert log_exists(child, g.CLIENT_RECV), 'Must have cr event' tracer.sampler = ConstSampler(False) span = tracer.start_span("test") child = tracer.start_span("child", references=child_of(span.context)) child.set_tag('bender', 'is great') child.log_event('kiss-my-shiny-metal-...') child.finish() span.finish() assert len(child.logs) == 0, "Must have no events, not sampled" assert len(child.tags) == 0, "Must have no attributes, not sampled" tracer.close()
def test_span(): tracer = Tracer() parent = tracer.start_span('parent') child = tracer.start_span('test', references=child_of(parent.context)) assert parent == child child.log_kv({'event': 'cache_hit', 'size.bytes': 42}) child.log_kv({'event': 'cache_miss'}, time.time()) child.log_event('cache_hit', ['arg1', 'arg2']) with mock.patch.object(parent, 'finish') as finish: with mock.patch.object(parent, 'log_event') as log_event: with mock.patch.object(parent, 'log_kv') as log_kv: try: with parent: raise ValueError() except ValueError: pass assert finish.call_count == 1 assert log_event.call_count == 0 assert log_kv.call_count == 1 with mock.patch.object(parent, 'finish') as finish: with mock.patch.object(parent, 'log_event') as log_kv: with parent: pass assert finish.call_count == 1 assert log_kv.call_count == 0 parent.set_tag('x', 'y').set_tag('z', 1) # test chaining parent.set_tag(tags.PEER_SERVICE, 'test-service') parent.set_tag(tags.PEER_HOST_IPV4, 127 << 24 + 1) parent.set_tag(tags.PEER_HOST_IPV6, '::') parent.set_tag(tags.PEER_HOSTNAME, 'uber.com') parent.set_tag(tags.PEER_PORT, 123) parent.finish()
def get_new_span(f, func_args, func_kwargs, operation_name=None, inspect_stack=True, ignore_parent_span=False, span_extractor=None, use_follows_from=False): parent_span = None span_arg_name = DEFAULT_SPAN_ARG_NAME if not ignore_parent_span: if callable(span_extractor): try: parent_span = span_extractor(*func_args, **func_kwargs) except Exception: logger.exception('Failed to extract span from: {}'.format( span_extractor.__name__)) if not parent_span: span_arg_name, parent_span = get_parent_span( inspect_stack=inspect_stack, **func_kwargs) op_name = f.__name__ if not operation_name else operation_name references = None if parent_span: references = [ follows_from(parent_span.context) ] if use_follows_from else [child_of(parent_span.context)] return span_arg_name, opentracing.tracer.start_span(operation_name=op_name, references=references)
async def procedure_callback_fn(req): try: span_context = self.tracer.extract( format=Format.HTTP_HEADERS, carrier=req.headers, ) with self.tracer.start_span( operation_name=f"{sfs_signature}_handler_procedure", references=child_of(span_context)): body_obj = json_loads_attrs(await req.text()) result = await handler( IntentfulCtx(self.provider, prefix, version, req.headers), Entity( metadata=body_obj.metadata, spec=body_obj.get("spec", {}), status=body_obj.get("status", {}), ), body_obj.input, ) return web.json_response(result) except InvocationError as e: return web.json_response(e.to_response(), status=e.status_code) except Exception as e: e = InvocationError.from_error(e) return web.json_response(e.to_response(), status=e.status_code)
def test_join_trace(): tracer = Tracer() span_ctx = tracer.extract(format=Format.TEXT_MAP, carrier={}) span = tracer.start_span(operation_name='test', references=opentracing.child_of(span_ctx)) span.set_tag('x', 'y') span.set_baggage_item('a', 'b') span.log_event('z') child = tracer.start_span(operation_name='child', references=opentracing.child_of(span.context)) child.log_event('w') child.finish() span.finish()
def test_span(): tracer = Tracer() parent = tracer.start_span('parent') child = tracer.start_span('test', references=child_of(parent.context)) assert parent == child child.log_event('cache_hit', ['arg1', 'arg2']) with mock.patch.object(parent, 'finish') as finish: with mock.patch.object(parent, 'log_event') as log_event: try: with parent: raise ValueError() except ValueError: pass assert finish.call_count == 1 assert log_event.call_count == 1 with mock.patch.object(parent, 'finish') as finish: with mock.patch.object(parent, 'log_event') as log_event: with parent: pass assert finish.call_count == 1 assert log_event.call_count == 0 parent.set_tag('x', 'y').set_tag('z', 1) # test chaining parent.set_tag(tags.PEER_SERVICE, 'test-service') parent.set_tag(tags.PEER_HOST_IPV4, 127 << 24 + 1) parent.set_tag(tags.PEER_HOST_IPV6, '::') parent.set_tag(tags.PEER_HOSTNAME, 'uber.com') parent.set_tag(tags.PEER_PORT, 123) parent.finish()
def compiler_target_function(comm, msg): """ A function that takes a vega spec and converts its vega-transforms to ibis transforms, which will later be able to lazily evaluate upon user interactions. """ data = msg["content"]["data"] spec = data["spec"] injected_span = data["span"] root_span = data["rootSpan"] root_ref = opentracing.child_of( (tracer.extract(opentracing.Format.TEXT_MAP, injected_span)) ) with tracer.start_span("compile-vega", references=root_ref) as span: span.log_kv({"vega-spec:initial": spec}) debug("vega-spec:initial", spec) try: with tracer.start_span("transform-vega", child_of=span) as transform_span: updated_spec = _transform(spec, root_span) transform_span.log_kv({"vega-spec:transformed": updated_spec}) span.log_kv({"vega-spec:transformed": updated_spec}) comm.send(updated_spec) except ValueError as e: # If there was an error transforming the spec, which can happen # if we don't support all the required transforms, or if # the spec references an old, unavailable ibis expression, # then send an empty vega spec. comm.send(EMPTY_VEGA) raise e
def start_bundle(self): self.do_fn.start_bundle() self.tracer = TracerClass(self.config) span_ctx_obj = self.tracer.extract(Format.TEXT_MAP, self.span_ctx) self.span = self.tracer.start_span( operation_name=self.do_fn.__class__.__name__, references=opentracing.child_of(span_ctx_obj))
def serialize(span_id): parent_ctx = SpanContext(trace_id=span_id, span_id=span_id, parent_id=0, flags=1) parent = Span(context=parent_ctx, operation_name='x', tracer=tracer) span = tracer.start_span(operation_name='x', references=child_of(parent.context)) span.finish() _marshall_span(span)
def extract_tracing_span(carrier, use_follows_from=False): try: span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, carrier) references = [follows_from(span_context)] if use_follows_from else [child_of(span_context)] return opentracing.tracer.start_span(references=references) except Exception: return opentracing.tracer.start_span()
def test_start_span_references(self, ot_tracer, writer): """Start a span using references.""" with ot_tracer.start_span('one', references=[child_of()]): pass spans = writer.pop() assert spans[0].parent_id is None root = ot_tracer.start_active_span('root') # create a child using a parent reference that is not the context parent with ot_tracer.start_active_span('one'): with ot_tracer.start_active_span('two', references=[child_of(root.span)]): pass root.close() spans = writer.pop() assert spans[2].parent_id is spans[0].span_id
def test_start_span_references(self, ot_tracer, test_spans): """Start a span using references.""" with ot_tracer.start_span("one", references=[child_of()]): pass spans = test_spans.pop() assert spans[0].parent_id is None root = ot_tracer.start_active_span("root") # create a child using a parent reference that is not the context parent with ot_tracer.start_active_span("one"): with ot_tracer.start_active_span("two", references=[child_of(root.span)]): pass root.close() spans = test_spans.pop() assert spans[2].parent_id is spans[0].span_id
def serialize(trace_id, span_id): """Checks that there are no exceptions during marshalling.""" parent_ctx = SpanContext(trace_id=trace_id, span_id=span_id, parent_id=0, flags=1) parent = Span(context=parent_ctx, operation_name='x', tracer=tracer) span = tracer.start_span(operation_name='x', references=child_of(parent.context)) span.finish() _marshall_span(span)
def test_parse_span_references(tracer): span = tracer.start_span('test') span2 = tracer.start_span('test2') follow_span = tracer.start_span( 'test-follow', references=[follows_from(span.context), child_of(span2.context)]) span.finish() span2.finish() follow_span.finish() _marshall_span(follow_span)
def extract_tracing_span(carrier, use_follows_from=False): try: span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, carrier) references = [follows_from(span_context) ] if use_follows_from else [child_of(span_context)] return opentracing.tracer.start_span(references=references) except Exception: return opentracing.tracer.start_span()
def test_references(self): """Test span creation using the `references` argument.""" with self.shim.start_span("ParentSpan") as parent: ref = opentracing.child_of(parent.context) with self.shim.start_active_span("ChildSpan", references=[ref]) as child: self.assertEqual( child.span.unwrap().links[0].context, parent.context.unwrap(), )
def create_child_span(): ''' This request will also be automatically traced. This is a more complicated example of accessing the current request from within a handler and creating new spans manually. ''' parent_span = tracer.get_span() child_span = ls_tracer.start_span("inside create_child_span", opentracing.child_of(parent_span.context)) ans = calculate_some_stuff() child_span.finish() return str(ans)
def test_child_span(tracer): span = tracer.start_span("test") child = tracer.start_span("child", references=child_of(span.context)) child.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) child.set_tag('bender', 'is great') child.log_event('kiss-my-shiny-metal-...') child.finish() span.finish() tracer.reporter.report_span.assert_called_once() assert len(span.logs) == 0, 'Parent span is Local, must not have events' assert len(child.logs) == 1, 'Child must have one events' tracer.sampler = ConstSampler(False) span = tracer.start_span("test") child = tracer.start_span("child", references=child_of(span.context)) child.set_tag('bender', 'is great') child.log_event('kiss-my-shiny-metal-...') child.finish() span.finish() assert len(child.logs) == 0, "Must have no events, not sampled" assert len(child.tags) == 0, "Must have no attributes, not sampled" tracer.close()
def test_one_span_per_rpc(tracer, one_span_per_rpc): tracer.one_span_per_rpc = one_span_per_rpc span = tracer.start_span("client") span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT) child = tracer.start_span( "server", references=child_of(span.context), tags={ext_tags.SPAN_KIND: ext_tags.SPAN_KIND_RPC_SERVER}, ) assert span.trace_id == child.trace_id, "Must have the same trace ids" if one_span_per_rpc: assert span.span_id == child.span_id, "Must have the same span ids" else: assert span.span_id != child.span_id, "Must have different span ids"
def start_active_span_from_edu( edu_content, operation_name, references: Optional[list] = None, tags=None, start_time=None, ignore_active_span=False, finish_on_close=True, ): """ Extracts a span context from an edu and uses it to start a new active span Args: edu_content (dict): and edu_content with a `context` field whose value is canonical json for a dict which contains opentracing information. For the other args see opentracing.tracer """ references = references or [] if opentracing is None: return noop_context_manager() # type: ignore[unreachable] carrier = json_decoder.decode(edu_content.get("context", "{}")).get( "opentracing", {} ) context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, carrier) _references = [ opentracing.child_of(span_context_from_string(x)) for x in carrier.get("references", []) ] # For some reason jaeger decided not to support the visualization of multiple parent # spans or explicitly show references. I include the span context as a tag here as # an aid to people debugging but it's really not an ideal solution. references += _references scope = opentracing.tracer.start_active_span( operation_name, child_of=context, references=references, tags=tags, start_time=start_time, ignore_active_span=ignore_active_span, finish_on_close=finish_on_close, ) scope.span.set_tag("references", carrier.get("references", [])) return scope
def test_start_child(tracer, mode): root = tracer.start_span("test") if mode == 'arg': span = tracer.start_span("test", child_of=root.context) elif mode == 'ref': span = tracer.start_span("test", references=child_of(root.context)) else: raise ValueError('bad mode') span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER) assert span.is_sampled(), "Must be sampled" assert span.trace_id == root.trace_id, "Must have the same trace id" assert span.parent_id == root.span_id, "Must inherit parent id" span.finish() assert span.end_time is not None, "Must have end_time set" tracer.reporter.assert_called_once() tracer.close()
def get_new_span( f, func_args, func_kwargs, operation_name=None, inspect_stack=True, ignore_parent_span=False, span_extractor=None, use_follows_from=False): parent_span = None using_scope_manager = False span_arg_name = None if not ignore_parent_span: if callable(span_extractor): try: parent_span = span_extractor(*func_args, **func_kwargs) except Exception: logger.exception('Failed to extract span from: {}'.format(span_extractor.__name__)) if not parent_span: span_arg_name, parent_span = get_span_from_kwargs(**func_kwargs) if not parent_span: try: # We try inspecting ``active_span`` managed by ``tracer.scope_manager``. parent_span = opentracing.tracer.active_span using_scope_manager = True if parent_span else False except AttributeError: # Old opentracing lib! pass # Finally, try to inspect call stack frames. if not parent_span: span_arg_name, parent_span = get_parent_span( inspect_stack=inspect_stack, inspect_kwargs=False, **func_kwargs) op_name = f.__name__ if not operation_name else operation_name references = None if parent_span: references = [follows_from(parent_span.context)] if use_follows_from else [child_of(parent_span.context)] span_arg_name = span_arg_name or DEFAULT_SPAN_ARG_NAME return ( span_arg_name, using_scope_manager, opentracing.tracer.start_span(operation_name=op_name, references=references) )
async def procedure_callback_fn(req): try: body_obj = json_loads_attrs(await req.text()) span_context = self.tracer.extract( format=Format.HTTP_HEADERS, carrier=req.headers, ) with self.tracer.start_span( operation_name=f"{name}_provider_procedure_sdk", references=child_of(span_context)): result = await handler( ProceduralCtx(self, prefix, version, req.headers), body_obj) return web.json_response(result) except InvocationError as e: return web.json_response(e.to_response(), status=e.status_code) except Exception as e: e = InvocationError.from_error(e) return web.json_response(e.to_response(), status=e.status_code)
def test_start_child(tracer, mode): root = tracer.start_span("test") if mode == 'arg': span = tracer.start_span("test", child_of=root.context) elif mode == 'ref': span = tracer.start_span("test", references=child_of(root.context)) else: raise ValueError('bad mode') span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_SERVER) assert span.is_sampled(), "Must be sampled" assert span.trace_id == root.trace_id, "Must have the same trace id" assert span.parent_id == root.span_id, "Must inherit parent id" span.finish() assert span.end_time is not None, "Must have end_time set" add_zipkin_annotations(span, None) assert len(span.logs) == 2, "Must have two events" assert log_exists(span, g.SERVER_SEND), 'Must have ss event' assert log_exists(span, g.SERVER_RECV), 'Must have sr event' tracer.reporter.assert_called_once() tracer.close()
def test_new_trace(): tracer = Tracer() span = tracer.start_span(operation_name='test') span.set_baggage_item('Fry', 'Leela') span.set_tag('x', 'y') span.log_event('z') child = tracer.start_span(operation_name='child', references=opentracing.child_of(span.context)) child.log_event('w') assert child.get_baggage_item('Fry') is None carrier = {} tracer.inject(span_context=child.context, format=Format.TEXT_MAP, carrier=carrier) assert carrier == dict() child.finish() span.finish()
def main(): try: # Input Setup options, args = getopt.getopt( sys.argv[1:], "f:c:u:i:o:r:", [ "funcopt=", "config=", "user_config=","runtime_config=", "input_paths=", "output_path="]) funcopt = config_json = user_json = runtime_json = input_paths = output_path = "" for opt, arg in options: if opt in ('-f', '--funcopt'): funcopt = json.loads(arg) elif opt in ('-c', '--config'): config_json = json.loads(arg) elif opt in ('-u', '--user_config'): user_json = json.loads(arg) elif opt in ('-i', '--input_paths'): input_paths = json.loads(arg) elif opt in ('-r', '--runtime'): print(arg) runtime_json = json.loads(arg) elif opt in ('-o', '--output_path'): output_path = arg if funcopt == "": raise Exception('Missing input option -f or funcopt.') if config_json == "": raise Exception('Missing input option -c or config.') if user_json == "": raise Exception('Missing input option -u or user_config.') if runtime_json == "": raise Exception('Missing input option -r or runtime.') if input_paths == "": raise Exception('Missing input option -i or input_paths.') if output_path == "": raise Exception('Missing input option -o or output_path.') if not os.path.exists(output_path): raise FileNotFoundError(output_path, "does not exist.") logging_obj = CustomLogging(funcopt) logging_obj.create_default_loggers() # info_logger = logging_obj.get_logger("info") status_logger = logging_obj.get_logger("status") error_logger = logging_obj.get_logger("error") config_logger = logging_obj.get_logger("config") logging_obj.create_tracer(funcopt['func_name']) tracer = logging_obj.get_tracer() config_logger.info(config_json) # info_logger.info("Function started") status_logger.info("Function started") parent_span = tracer.extract(format=Format.TEXT_MAP,carrier = funcopt) if parent_span: span = tracer.start_span('main',references=opentracing.child_of(parent_span)) else: span = tracer.start_span('main') with tracer.scope_manager.activate(span, True) as scope: span.set_tag('', funcopt) span.log_kv({'info': 'string-format', 'value': 'Function started'}) if funcopt['task'] == 'train': func_impl.train(logging_obj, config_json, user_json, runtime_json, input_paths, output_path) elif funcopt['task'] == 'train_and_validate': func_impl.train_and_validate(funcopt, logging_obj, config_json, user_json, runtime_json, input_paths, output_path) elif funcopt['task'] == 'validate': func_impl.validate(logging_obj, config_json, user_json, runtime_json, input_paths, output_path) elif funcopt['task'] == 'test': func_impl.test(logging_obj, config_json, user_json, runtime_json, input_paths, output_path) elif funcopt['task'] == 'predict': func_impl.predict(logging_obj, config_json, user_json, runtime_json, input_paths, output_path) else: func_impl.run(logging_obj, config_json, user_json, runtime_json, input_paths, output_path) span.finish() # info_logger.info("Function completed successfully") status_logger.info("Function completed successfully") except: if 'status_logger' in vars() or 'status_logger' in globals(): status_logger.exception("Failure") error_logger.exception("Failure") else: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print("ERROR: Class: ", exc_type, " Message :", exc_obj, " Filename: ", fname, "Line No: ", exc_tb.tb_lineno) sys.exit(100) finally: if 'status_logger' in vars() or 'status_logger' in globals(): # info_logger.info("Cleaning up resources..") status_logger.info("Cleaning up resources..") if 'tracer' in vars() or 'tracer' in globals(): time.sleep(2) tracer.close() if 'status_logger' in vars() or 'status_logger' in globals(): # info_logger.info("Main exited") status_logger.info("Main exited")
# Create Tracer with Composite Reporter. tracer = WavefrontTracer(reporter=composite_reporter) global_tags = [("global_key", "global_val")] # Create span1, return a newly started and activated Scope. scope = tracer.start_active_span(operation_name="span1", tags=global_tags, ignore_active_span=True, finish_on_close=True) span1 = scope.span time.sleep(1) # Create span2, span3 child of span1. span2 = tracer.start_span(operation_name="span2", references=child_of(span1.context), tags=[("span2_key", "span2_val")]) span3 = tracer.start_span(operation_name="span3", child_of=span1, tags=[("span3_key", "span3_val")]) time.sleep(2) span2.finish() time.sleep(1) span3.finish() # Create span4 follows from span3. span4 = tracer.start_span(operation_name="span4", references=follows_from(span3.context), tags=[("span4_key", "span4_val")]) time.sleep(2) span4.finish()
def test_tracer(): tracer = Tracer() span = tracer.start_span(operation_name="root") child = tracer.start_span(operation_name="child", references=child_of(span)) assert span == child
tracer = WavefrontTracer(reporter=composite_reporter, application_tags=application_tag) global_tags = [('global_key', 'global_val')] # Create span1, return a newly started and activated Scope. scope = tracer.start_active_span(operation_name='span1', tags=global_tags, ignore_active_span=True, finish_on_close=True) span1 = scope.span time.sleep(0.1) # Create span2, span3 child of span1. span2 = tracer.start_span(operation_name='span2', references=opentracing.child_of(span1.context), tags=[('span2_key', 'span2_val')]) span2.log_kv({'foo': 'bar'}) span3 = tracer.start_span(operation_name='span3', child_of=span1, tags=[('span3_key', 'span3_val')]) time.sleep(0.2) span2.finish() time.sleep(0.1) span3.finish() # Create span4 follows from span3. span4 = tracer.start_span(operation_name='span4', references=opentracing.follows_from(span3.context), tags=[('span4_key', 'span4_val')]) time.sleep(0.2)
def test_tracer(): tracer = Tracer() span = tracer.start_span(operation_name='root') child = tracer.start_span(operation_name='child', references=child_of(span)) assert span == child
def start_span(self, operation_name, child_of=None, references=None, tags=None, start_time=None): """Starts and returns a new Span representing a unit of work. Starting a root Span (a Span with no causal references):: tracer.start_span('...') Starting a child Span (see also start_child_span()):: tracer.start_span( '...', child_of=parent_span) Starting a child Span in a more verbose way:: tracer.start_span( '...', references=[opentracing.child_of(parent_span)]) :param operation_name: name of the operation represented by the new span from the perspective of the current service. :param child_of: (optional) a Span or SpanContext instance representing the parent in a REFERENCE_CHILD_OF Reference. If specified, the `references` parameter must be omitted. :param references: (optional) a list of Reference objects that identify one or more parent SpanContexts. (See the Reference documentation for detail) :param tags: an optional dictionary of Span Tags. The caller gives up ownership of that dictionary, because the Tracer may use it as-is to avoid extra data copying. :param start_time: an explicit Span start time as a unix timestamp per time.time() :return: Returns an already-started Span instance. """ if isinstance(child_of, SpanContext): ref = opentracing.child_of(child_of) elif isinstance(child_of, MySpan): ref = opentracing.child_of(child_of.context) elif references: ref = references if isinstance( references, opentracing.Reference) else references[0] else: ref = None span = self.spanCls(tracer=self, context=self.spanContextCls(ref)) span.set_operation_name(operation_name) _tags = tags or dict() for k, v in _tags: span.set_tag(k, v) if start_time: span.set_start_time(start_time) return span
def execute_query(parameters: dict): injected_span: object = parameters.pop("span") with tracer.start_active_span( "queryibis", references=[ opentracing.child_of( (tracer.extract(opentracing.Format.TEXT_MAP, injected_span))) ], ) as scope: scope.span.log_kv(parameters) name: str = parameters.pop("name") transforms: typing.Optional[str] = parameters.pop("transform", None) if name not in _expr_map: raise ValueError(f"{name} is not an expression known to us!") expr = _expr_map[name] sql = expr.compile() scope.span.log_kv({"sql:initial": sql}) debug( "query:initial", { "transforms": transforms, "parameters": parameters, "sql": sql }, ) if transforms: # Replace all string instances of data references with value in schema for k, v in parameters.items(): # All data items are added to parameters as `:<data name>`. # They also should be in the `data` paramater, but you have to call # this with a tuple which I am not sure where to get from # https://github.com/vega/vega/blob/65fe7cb2485be90e16298d9dff87bf56045afb8d/packages/vega-transforms/src/Filter.js#L48 if not k.startswith(":"): continue k = k[1:] res = json.dumps(v) for t in transforms: if t["type"] == "filter" or t["type"] == "formula": t["expr"] = _patch_vegaexpr(t["expr"], k, res) try: expr = apply(expr, transforms) except Exception as e: raise ValueError( f"Failed to convert {transforms} with error message message '{e}'" ) with tracer.start_span("ibis:execute") as execute_span: sql = expr.compile() execute_span.log_kv({"sql": sql}) if ENABLE_MULTIPROCESSING: data = execute_new_client(expr) else: data = expr.execute() values = altair.to_values(data)["values"] debug("query:result", { "transforms": transforms, "sql": sql, "values": values }) return values