def track_start_request(self, request): headers = request.headers trace_name = request.path_qs if request.matched_route: # we only get a matched route if we've gone through the router. trace_name = request.matched_route.pattern trace = Trace( request.method + " " + trace_name, int_or_none(headers.get("X-B3-TraceId", None)), int_or_none(headers.get("X-B3-SpanId", None)), int_or_none(headers.get("X-B3-ParentSpanId", None)), endpoint=self.endpoint, ) if "X-B3-TraceId" not in headers: log.info("no trace info from request: %s", request.path_qs) if request.matchdict: # matchdict maybe none if no route is registered for k, v in request.matchdict.items(): trace.record(Annotation.string("route.param.%s" % k, v)) trace.record(Annotation.string("http.path", request.path_qs)) log.info("new trace %r", trace.trace_id) stack_trace(trace) trace.record(Annotation.server_recv()) self.trace = trace
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany): try: endpoint = endpoints.get(conn.engine) parent_trace = get_current_trace() if not parent_trace: log.error('No parent found while tracing SQL') return try: context.trace = parent_trace.child('SQL', endpoint=endpoint) abstract = context except AttributeError: cursor.trace = parent_trace.child('SQL', endpoint=endpoint) abstract = cursor abstract.trace.record(Annotation.string('db.statement', statement)) if parameters: if isinstance(parameters, dict): parameters = dict([(key, getattr(param, 'logged_value', param)) for key, param in parameters.items()]) else: parameters = [ getattr(param, 'logged_value', param) for param in parameters ] abstract.trace.record( Annotation.string('db.parameters', repr(parameters))) abstract.trace.record(Annotation.string('span.kind', 'client')) abstract.trace.record(Annotation.server_recv()) except Exception: log.exception('Unexpected exception while tracing SQL')
def request(self, host, handler, request_body, verbose=0): try: https = isinstance(self, xmlrpclib.SafeTransport) protocol = "https://" if https else "http://" target = "%s%s%s" % (protocol, host, handler) match = _parse_method_name.search(request_body) method = match.group(1) if match else None parent_trace = local().current self._trace = parent_trace.child("xmlrpclib") self._trace.record(Annotation.string("uri", target)) if method: self._trace.record(Annotation.string("method", method)) self._trace.record(Annotation.server_recv()) except Exception as exc: log.error(repr(exc)) try: return self.__origin.request(self, host, handler, request_body, verbose) finally: try: self._trace.record(Annotation.server_send()) except: pass
def wrapped(cursor, statement, vars=None): trace = None parent_trace = get_current_trace() if parent_trace: trace = parent_trace.child(trace_name) trace.record(Annotation.string("db.statement", statement)) if vars: if isinstance(vars, dict): params = dict([(key, getattr(param, "logged_value", param)) for key, param in vars.items()]) else: params = [ getattr(param, "logged_value", param) for param in vars ] trace.record( Annotation.string("db.parameters", repr(params))) trace.record(Annotation.string("span.kind", "client")) trace.record(Annotation.server_send()) try: fn(cursor, statement, vars) finally: if trace: status = "OK" if sys.exc_info()[0] is None else "KO" trace.record(Annotation.string("status", status)) trace.record(Annotation.server_recv())
def init_trace(request): headers = request.headers trace_name = request.method + " " + request.path_info trace = Trace( trace_name, int_or_none(headers.get("X-B3-TraceId", None)), int_or_none(headers.get("X-B3-SpanId", None)), int_or_none(headers.get("X-B3-ParentSpanId", None)), endpoint=Endpoint(ZipkinConfig.service_name), ) trace.record(Annotation.string("http.path", request.path_info)) trace.record(Annotation.string("span.kind", "client")) trace.record(Annotation.server_recv()) stack_trace(trace) return trace
def pre_response(app, response, **extra): request.trace.record( Annotation.string('http.responsecode', '{0}'.format(response.status_code))) request.trace.record(Annotation.server_send()) log(request.trace) local().pop()
def log_response(trace, response): trace.record( Annotation.string("http.responsecode", "{0}".format(response.status_code))) trace.record(Annotation.server_send()) try: zipkin_log(trace) except Exception as err: log.error("Error while sending trace: %s", trace)
def dbapi_error(conn, cursor, statement, parameters, context, exception): if not hasattr(context, 'trace') and not hasattr(cursor, 'trace'): return abstract = context if hasattr(context, 'trace') else cursor try: abstract.trace.record(Annotation.string('status', 'KO')) abstract.trace.record(Annotation.server_send()) except Exception: log.exception('Unexpected exception while tracing SQL')
def dbapi_error(conn, cursor, statement, parameters, context, exception): if not hasattr(context, "trace") and not hasattr(cursor, "trace"): return abstract = context if hasattr(context, "trace") else cursor try: abstract.trace.record(Annotation.string("status", "KO")) abstract.trace.record(Annotation.server_send()) except Exception: log.exception("Unexpected exception while tracing SQL")
def pre_request(request): parent_trace = local().current if not parent_trace: return request request.trace = parent_trace.child("requests:%s %s" % (request.method, request.url)) forwarded_trace = request.trace.child_noref("subservice") request.headers['X-B3-TraceId'] = hex_str(forwarded_trace.trace_id) request.headers['X-B3-SpanId'] = hex_str(forwarded_trace.span_id) if forwarded_trace.parent_span_id is not None: request.headers['X-B3-ParentSpanId'] = \ hex_str(forwarded_trace.parent_span_id) request.trace.record(Annotation.string('http.method', request.method)) request.trace.record(Annotation.string('http.url', request.url)) request.trace.record(Annotation.string('span.kind', 'client')) request.trace.record(Annotation.server_recv()) return request
def pre_request(app, **extra): headers = request.headers trace = Trace(request.method + ' ' + request.url, int_or_none(headers.get('X-B3-TraceId', None)), int_or_none(headers.get('X-B3-SpanId', None)), int_or_none(headers.get('X-B3-ParentSpanId', None)), endpoint=endpoint) setattr(request, 'trace', trace) local().append(trace) trace.record(Annotation.string('http.uri', request.url)) trace.record(Annotation.server_recv())
def pre_response(resp, req=None): if not req: req = resp.request if not hasattr(req, 'trace'): return resp req.trace.record( Annotation.string('http.status_code', '{0}'.format(getattr(resp, 'status', None)))) req.trace.record(Annotation.server_send()) return resp
def pre_response(resp, req=None): if not req: req = resp.request if not hasattr(req, "trace"): return resp req.trace.record( Annotation.string("http.status_code", "{0}".format(getattr(resp, "status", None)))) req.trace.record(Annotation.server_send()) return resp
def pre_request(request): parent_trace = local().current if not parent_trace: return request url = filter_url_path(request.url) request.trace = parent_trace.child("requests:%s %s" % (request.method, url)) forwarded_trace = request.trace.child_noref("subservice") request.headers["X-B3-TraceId"] = hex_str(forwarded_trace.trace_id) request.headers["X-B3-SpanId"] = hex_str(forwarded_trace.span_id) if forwarded_trace.parent_span_id is not None: request.headers["X-B3-ParentSpanId"] = hex_str( forwarded_trace.parent_span_id) request.trace.record(Annotation.string("http.method", request.method)) request.trace.record(Annotation.string("http.url", request.url)) request.trace.record(Annotation.string("span.kind", "client")) request.trace.record(Annotation.server_recv()) return request
def pre_request(app, **extra): headers = request.headers trace = Trace( request.method + " " + request.url, int_or_none(headers.get("X-B3-TraceId", None)), int_or_none(headers.get("X-B3-SpanId", None)), int_or_none(headers.get("X-B3-ParentSpanId", None)), endpoint=endpoint, ) setattr(request, "trace", trace) local().append(trace) trace.record(Annotation.string("http.uri", request.url)) trace.record(Annotation.server_recv())