Exemple #1
0
 def handle(self, node):
     try:
         if self.framework == 'flask' or self.framework == 'bottle':
             console.info("handle flask")
             self.handle_flask(node)
         elif self.framework == 'oslo_messaging':
             console.info("handle oslo_messaging")
             self.handle_rabbitmq(node)
         elif self.framework == 'pika':
             console.info("handle pika")
             self.handle_pika(node)
         else:
             pass
     except Exception as e:
         console.exception("handle error")
Exemple #2
0
    def wrapper(wrapped, instance, args, kwargs):
        console.info("args===>")
        console.info(args)
        console.info("args<===")
        tracker = current_tracker()
        console.info("wsgi_wrapper_inline>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        console.info("current_tracker:1: %s" % tracker)
        if tracker:
            console.info("current_tracker:2: None")
            console.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            return wrapped(*args, **kwargs)

        environ, start_response = parse_wsgi_protocol(target, *args, **kwargs)
        disable_agent = environ.get(AGENT_REQUEST_SWITCH, None)
        if disable_agent:
            console.debug("Current trace is disabled with http request environment. %s", environ)
            return wrapped(*args, **kwargs)

        tracker = Tracer(proxy_instance(), environ, framework)
        tracker.generate_trace_id()
        tracker.start_work()

        def _start_response(status, response_headers, *args):
            # deal the response header/data
            process_header(tracker, response_headers)
            tracker.deal_response(status, response_headers, *args)
            _write = start_response(status, response_headers, *args)

            return _write

        result = []
        try:
            tracker.set_tracker_name(callable_name(wrapped), priority=1)
            application = function_trace_wrapper(wrapped)
            console.info("current_tracker:2: %s" % tracker)
            with FunctionTracker(tracker, name='Application', group='Python.WSGI'):
                result = TingYunWSGIBrowserRumMiddleware(tracker, application, _start_response, environ)()  # sunyan: 1
                # result = application(environ, start_response)
            console.info("wsgi_wrapper_inline<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
        except:
            console.exception("wsgi_entrance")
            tracker.finish_work(*sys.exc_info())
            raise

        return WSGIApplicationResponse(tracker, result)
    def wrapper(wrapped, instance, args, kwargs):

        incoming_message = args[0][0]
        msg = incoming_message.message
        console.info("msg:")
        console.info(msg)
        ctxt = incoming_message.ctxt
        console.info("ctxt:")
        console.info(ctxt)

        oslo_messaging_trace_id = ctxt['oslo_messaging_trace_id'] if 'oslo_messaging_trace_id' in ctxt else None

        tracker = current_tracker()
        console.info("oslo_messaging_wrapper_inline>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        console.info("current_tracker:1: %s" % tracker)
        if tracker:
            console.info("current_tracker:2: None")
            console.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            return wrapped(*args, **kwargs)

        environ = {
            'k1': 'v1'
        }

        tracker = Tracer(proxy_instance(), environ, framework)
        if oslo_messaging_trace_id is not None:
            tracker._trace_id = oslo_messaging_trace_id
        tracker.generate_trace_id()
        tracker.start_work()

        result = None
        try:
            tracker.set_tracker_name(callable_name(wrapped), priority=1)
            with FunctionTracker(tracker, name='Application', group='Python.oslo_messaging', params=msg['args']):
                result = wrapped(*args, **kwargs)
            console.info("oslo_messaging_wrapper_inline<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
        except:
            console.exception("oslo_messaging_entrance")
            tracker.finish_work(*sys.exc_info())
            raise
        else:
            tracker.finish_work(None, None, None)

        return result
Exemple #4
0
    def default_handler(self, encoded_span):
        if self.app_name is None:
            console.info("APP_NAME is none, error!")
            return

        try:
            body = str.encode('\x0c\x00\x00\x00\x01') + encoded_span
            console.info("url:{0}".format(self.zipkin_dsn))
            rec = requests.post(
                self.zipkin_dsn,
                data=body,
                headers={'Content-Type': 'application/x-thrift'},
                timeout=1,
            )
            console.info("rec:{0}".format(rec))
            return rec
        except Exception as e:
            console.exception("flask_zipkin")
            if self._transport_exception_handler:
                self._transport_exception_handler(e)
            else:
                self.default_exception_handler(e)
Exemple #5
0
    def handle_rabbitmq(self, node):
        try:
            console.info("environ>>>>>>")
            print(self.environ)
            console.info("environ<<<<<<")

            if node.type == 'WebAction':
                for child in node.children:
                    if 'oslo_messaging' in child.group:
                        transport_handler = self._transport_handler or self.default_handler

                        trace_id = node.trace_id
                        span_name = json.dumps(child.params)
                        start_time = child.start_time
                        end_time = child.end_time
                        parent_span_id = None
                        is_sampled = True
                        flags = '0'

                        zipkin_attrs = self.generate_zipkin_attrs(
                            trace_id=trace_id,
                            span_id=self._gen_random_id(),
                            parent_span_id=parent_span_id,
                            flags=flags,
                            is_sampled=is_sampled)
                        span2 = self.generate_span_2(
                            service_name=self.app_name,
                            span_name=span_name,
                            zipkin_attrs=zipkin_attrs,
                            transport_handler=transport_handler)
                        span2.start(start_time)
                        span2.stop(start_timestamp=start_time,
                                   end_timestamp=end_time)

        except Exception as e:
            console.exception("handle_rabbitmq error")
    def wrapper(wrapped, instance, args, kwargs):

        console.info('args:')
        console.info(args)
        console.info('kwargs:')
        console.info(kwargs)

        method = args[0]
        header = args[1]
        body = None
        pika_trace_id = None
        try:
            wrapped_message = json.loads(args[2])
            if 'pika_trace_id' in wrapped_message:
                pika_trace_id = wrapped_message['pika_trace_id']
                body = wrapped_message['body']
            else:
                body = args[2]
        except Exception:
            body = args[2]
        args = (method, header, body)
        params = {
            #'method': str(method),
            #'header': str(header),
            'body': body
        }

        tracker = current_tracker()
        console.info(
            "pika_wrapper_inline>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        console.info("current_tracker:1: %s" % tracker)
        if tracker:
            console.info("current_tracker:2: None")
            console.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            return wrapped(*args, **kwargs)

        environ = {'k1': 'v1'}

        tracker = Tracer(proxy_instance(), environ, framework)

        if pika_trace_id is not None:
            tracker._trace_id = pika_trace_id
        tracker.generate_trace_id()
        tracker.start_work()

        result = None
        try:
            tracker.set_tracker_name(callable_name(wrapped), priority=1)
            with FunctionTracker(tracker,
                                 name='Application',
                                 group='Python.pika',
                                 params=params):
                result = wrapped(*args, **kwargs)
            console.info(
                "pika_wrapper_inline<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
        except:
            console.exception("pika_wrapper_inline")
            tracker.finish_work(*sys.exc_info())
            raise
        else:
            tracker.finish_work(None, None, None)

        return result
Exemple #7
0
    def handle_flask(self, node):
        try:
            console.info("environ>>>>>>")
            print(self.environ)
            console.info("environ<<<<<<")
            """
            headers = self.environ['werkzeug.request'].headers
            url = self.environ['werkzeug.request'].url
            trace_id = headers.get('X-B3-TraceId') or self._gen_random_id()
            parent_span_id = headers.get('X-B3-Parentspanid')
            is_sampled = str(headers.get('X-B3-Sampled') or '0') == '1'
            flags = headers.get('X-B3-Flags')
            """

            trace_id = node.trace_id
            #trace_id = self.environ.get('HTTP_X_B3_TRACEID') or self._gen_random_id()
            start_time = node.start_time
            end_time = node.end_time
            url = "http://{0}{1}".format(self.environ.get('HTTP_HOST'),
                                         self.environ.get('PATH_INFO'))
            parent_span_id = self.environ.get('HTTP_X_B3_PARENTSPANID')
            #is_sampled = str(self.environ.get('HTTP_X_B3_SAMPLED') or '0') == '1'
            is_sampled = True
            #flags = self.environ.get('HTTP_X_B3_FLAGS')
            flags = '0'

            # parent span
            transport_handler = self._transport_handler or self.default_handler
            zipkin_attrs = self.generate_zipkin_attrs(
                trace_id=trace_id,
                span_id=self._gen_random_id(),
                parent_span_id=parent_span_id,
                flags=flags,
                is_sampled=is_sampled)
            span2 = self.generate_span_2(service_name=self.app_name,
                                         span_name=url,
                                         zipkin_attrs=zipkin_attrs,
                                         transport_handler=transport_handler)
            span2.start(start_time)

            if node.type == 'WebAction':
                for child in node.children:
                    # flask
                    if child.name == 'Response':
                        for child2 in child.children:
                            # flask or bottle
                            if 'flask' in child2.name or 'bottle' in child2.name:
                                for child3 in child2.children:
                                    if child3.group == 'Function':
                                        for child4 in child3.children:
                                            # mysql
                                            if type(
                                                    child4
                                            ) == DatabaseNode and child4.sql != 'COMMIT':
                                                sql = child4.sql
                                                span_name = ''
                                                execute_params = child4.execute_params
                                                if sql.strip().upper(
                                                ).startswith("SELECT"):
                                                    sql = sql % execute_params[
                                                        0]
                                                    span_name = "SELECT"
                                                elif sql.strip().upper(
                                                ).startswith("INSERT"):
                                                    sql = sql % execute_params[
                                                        0]
                                                    span_name = "INSERT"
                                                elif sql.strip().upper(
                                                ).startswith("DELETE"):
                                                    sql = sql % execute_params[
                                                        0]
                                                    span_name = "DELETE"
                                                elif sql.strip().upper(
                                                ).startswith("UPDATE"):
                                                    sql = sql % execute_params[
                                                        0][0]
                                                    span_name = "UPDATE"
                                                else:
                                                    pass

                                                # child span
                                                span3 = self.generate_span_3(
                                                    service_name=self.app_name,
                                                    span_name=span_name,
                                                    binary_annotations={
                                                        'jdbc.query': sql
                                                    })
                                                span3.start(child4.start_time)
                                                span3.stop(
                                                    start_timestamp=child4.
                                                    start_time,
                                                    end_timestamp=child4.
                                                    end_time)
                                            # requests
                                            elif type(child4) == ExternalNode:
                                                # TODO ?
                                                pass
                                            # rabbitmq, FIXME sunyan: right now, we don't need this branch, but may be useful in the future!
                                            #elif type(child4) == FunctionNode and 'oslo_messaging' in child4.name:
                                            #    span3 = self.generate_span_3(service_name=self.app_name,
                                            #                                 span_name=json.dumps(child4.params),
                                            #                                 binary_annotations=child4.params)
                                            #    span3.start(child4.start_time)
                                            #    span3.stop(start_timestamp=child4.start_time,
                                            #               end_timestamp=child4.end_time)
                                            else:
                                                pass
                                    else:
                                        pass
                            # not flask and bottle
                            else:
                                pass
                    # others
                    else:
                        pass

            span2.stop(start_timestamp=start_time, end_timestamp=end_time)

        except Exception as e:
            console.exception("handle_flask error")