def before_cursor_execute(self, conn, cursor, statement, parameters, context, executemany): if not current_app: return params = [] # the type of parameters passed in varies depending on DB - handle list, dict, and tuple if type(parameters) == tuple or type(parameters) == list: for param in parameters: if type(param) == datetime.datetime: param = param.isoformat() params.append(param) elif type(parameters) == dict: for k, v in parameters.items(): param = "%s=" % k if type(v) == datetime.datetime: v = v.isoformat() param += "%s" % v params.append(param) self.state.span = beeline.start_span( context={ "name": "flask_db_query", "type": "db", "db.query": statement, "db.query_args": params, }) self.query_start_time = datetime.datetime.now()
def _render_template(fn, instance, args, kwargs): span = beeline.start_span(context={ "name": "jinja2_render_template", "template.name": instance.name or "[string]", }) try: return fn(*args, **kwargs) finally: beeline.finish_span(span)
def test_start_trace_returns_value(self): ''' ensure the top-level start_span and start_trace APIs return the value form their calls to the tracer ''' self.m_gbl.tracer_impl.start_span.return_value = 'wooimaspan' val = beeline.start_span() self.assertEqual(val, 'wooimaspan') self.m_gbl.tracer_impl.start_trace.return_value = 'wooimatrace' val = beeline.start_trace() self.assertEqual(val, 'wooimatrace')
def request(_request, instance, args, kwargs): span = beeline.start_span(context={"meta.type": "http_client"}) b = beeline.get_beeline() if b and b.http_trace_propagation_hook is not None: new_headers = beeline.http_trace_propagation_hook() if new_headers: b.log( "requests lib - adding trace context to outbound request: %s", new_headers) instance.headers.update(new_headers) else: b.log("requests lib - no trace context found") try: resp = None # Required as Python treats the `or` keyword differently in string # interpolation vs. when assigning a variable. method = kwargs.get('method') or args[0] beeline.add_context({ "name": "requests_%s" % method, "request.method": method, "request.url": kwargs.get('url') or args[1], }) resp = _request(*args, **kwargs) return resp except Exception as e: beeline.add_context({ "request.error_type": str(type(e)), "request.error": beeline.internal.stringify_exception(e), }) raise finally: if resp is not None: content_type = resp.headers.get('content-type') if content_type: beeline.add_context_field("response.content_type", content_type) content_length = resp.headers.get('content-length') if content_length: beeline.add_context_field("response.content_length", content_length) if hasattr(resp, 'status_code'): beeline.add_context_field("response.status_code", resp.status_code) beeline.finish_span(span)
def request(_request, instance, args, kwargs): span = beeline.start_span(context={"meta.type": "http_client"}) b = beeline.get_beeline() if b: context = b.tracer_impl.marshal_trace_context() if context: b.log( "requests lib - adding trace context to outbound request: %s", context) instance.headers['X-Honeycomb-Trace'] = context else: b.log("requests lib - no trace context found") try: resp = None beeline.add_context({ "name": "requests_%s" % kwargs.get('method') or args[0], "request.method": kwargs.get('method') or args[0], "request.url": kwargs.get('url') or args[1], }) resp = _request(*args, **kwargs) return resp except Exception as e: beeline.add_context({ "request.error_type": str(type(e)), "request.error": beeline.internal.stringify_exception(e), }) raise finally: if resp: content_type = resp.headers.get('content-type') if content_type: beeline.add_context_field("response.content_type", content_type) content_length = resp.headers.get('content-length') if content_length: beeline.add_context_field("response.content_length", content_length) if hasattr(resp, 'status_code'): beeline.add_context_field("response.status_code", resp.status_code) beeline.finish_span(span)
def _urllibopen(_urlopen, instance, args, kwargs): # urlopen accepts either a string URL or a Request object as its first arg # It's easier to process the info contained in the request and modify it # by converting the URL string into a Request if type(args[0]) != urllib.request.Request: args = (urllib.request.Request(args[0]), ) + tuple(args[1:]) span = beeline.start_span(context={"meta.type": "http_client"}) b = beeline.get_beeline() if b and b.http_trace_propagation_hook is not None: new_headers = beeline.http_trace_propagation_hook() if new_headers: # Merge the new headers into the existing headers for the outbound request b.log("urllib lib - adding trace context to outbound request: %s", new_headers) args[0].headers.update(new_headers) try: resp = None beeline.add_context({ "name": "urllib_%s" % args[0].get_method(), "request.method": args[0].get_method(), "request.uri": args[0].full_url }) resp = _urlopen(*args, **kwargs) return resp except Exception as e: beeline.add_context({ "request.error_type": str(type(e)), "request.error": beeline.internal.stringify_exception(e), }) raise finally: if resp: beeline.add_context_field("response.status_code", resp.status) content_type = resp.getheader('content-type') if content_type: beeline.add_context_field("response.content_type", content_type) content_length = resp.getheader('content-length') if content_length: beeline.add_context_field("response.content_length", content_length) beeline.finish_span(span)
def _urllibopen(_urlopen, instance, args, kwargs): if type(args[0]) != urllib.request.Request: args[0] = urllib.request.Request(args[0]) span = beeline.start_span(context={"meta.type": "http_client"}) b = beeline.get_beeline() if b: context = b.tracer_impl.marshal_trace_context() if context: b.log("urllib lib - adding trace context to outbound request: %s", context) args[0].headers['X-Honeycomb-Trace'] = context else: b.log("urllib lib - no trace context found") try: resp = None beeline.add_context({ "name": "urllib_%s" % args[0].get_method(), "request.method": args[0].get_method(), "request.uri": args[0].full_url }) resp = _urlopen(*args, **kwargs) return resp except Exception as e: beeline.add_context({ "request.error_type": str(type(e)), "request.error": beeline.internal.stringify_exception(e), }) raise finally: if resp: beeline.add_context_field("response.status_code", resp.status) content_type = resp.getheader('content-type') if content_type: beeline.add_context_field("response.content_type", content_type) content_length = resp.getheader('content-length') if content_length: beeline.add_context_field("response.content_length", content_length) beeline.finish_span(span)
def before_cursor_execute(self, conn, cursor, statement, parameters, context, executemany): if not current_app: return params = [] for param in parameters: if type(param) == datetime.datetime: param = param.isoformat() params.append(param) self.state.span = beeline.start_span( context={ "name": "flask_db_query", "type": "db", "db.query": statement, "db.query_args": params, }) self.query_start_time = datetime.datetime.now()
def before_cursor_execute( self, conn, cursor, statement, parameters, context, executemany ): span = getattr(self.state, "span", None) query_start_time = getattr(self.state, "query_start_time", None) if span or query_start_time: warnings.warn( "The before_cursor_execute event fired multiple times inside the same " "thread, without a corresponding after_cursor_execute or handle_error " "event." ) return params = [] # the type of parameters passed in varies depending on DB. # handle list, dict, and tuple if type(parameters) == tuple or type(parameters) == list: for param in parameters: if type(param) == datetime.datetime: param = param.isoformat() params.append(param) elif type(parameters) == dict: for k, v in parameters.items(): param = "%s=" % k if type(v) == datetime.datetime: v = v.isoformat() param += str(v) params.append(param) self.state.span = beeline.start_span( context={ "name": "sqlalchemy_query", "type": "db", "db.query": statement, "db.query_args": params, } ) self.state.query_start_time = datetime.datetime.now()
def random_sleep(): logging.info("Generating a span") span = beeline.start_span(context={"name": "do_assorted_data_processing"}) time.sleep(random.randint(1, 10)) beeline.finish_span(span)
def hello_world(): span = beeline.start_span(context={"name": "Preparing to greet the world"}) message = "Hello World" beeline.add_trace_field('message', message) beeline.finish_span(span) return message