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 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 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.warning("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 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(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(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 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 track_end_request(self, request, response): if self.trace: self.trace.record(Annotation.server_send()) log.info("reporting trace %s", self.trace.name) response.headers["Trace-Id"] = str(self.trace.trace_id) zipkin_log(self.trace)
def task_prerun_handler(task_id, task, **kwargs): request = task.request trace = Trace('Task execute %r' % task.name, int_or_none(request.headers.get('X-B3-TraceId', None)), int_or_none(request.headers.get('X-B3-SpanId', None)), int_or_none(request.headers.get('X-B3-ParentSpanId', None)), endpoint=endpoint) setattr(request, 'trace', trace) local().append(trace) trace.record(Annotation.server_recv())
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 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 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 wrap(event): request = event.request headers = request.headers trace = Trace(request.method + ' ' + request.path_qs, 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.server_recv()) request.add_finished_callback(log_response(endpoint))
def task_send_handler(body, exchange, routing_key, headers, **kwargs): trace = local().current if not trace: logger.warn("No zipkin parent trace found, ignoring tracing task.") return forwarded_trace = trace.child_noref("subservice") headers["X-B3-TraceId"] = hex_str(forwarded_trace.trace_id) headers["X-B3-SpanId"] = hex_str(forwarded_trace.span_id) forwarded_trace.record(Annotation.client_send()) if forwarded_trace.parent_span_id is not None: headers["X-B3-ParentSpanId"] = hex_str(forwarded_trace.parent_span_id)
def wrap(request): trace = request.trace trace.record(Annotation.server_send()) log(trace) local().pop()