How to use the instana.singletons.tracer.active_span function in instana

To help you get started, we’ve selected a few instana examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github instana / python-sensor / tests / clients / test_sqlalchemy.py View on Github external
def test_session_add(self):
        with tracer.start_active_span('test'):
            self.session.add(stan_user)
            self.session.commit()

        spans = self.recorder.queued_spans()
        self.assertEqual(2, len(spans))

        sql_span = spans[0]
        test_span = spans[1]

        self.assertIsNone(tracer.active_span)

        # Same traceId
        self.assertEqual(test_span.t, sql_span.t)

        # Parent relationships
        self.assertEqual(sql_span.p, test_span.s)

        # Error logging
        self.assertIsNone(test_span.ec)
        self.assertIsNone(sql_span.ec)

        # SQLAlchemy span
        self.assertEqual('sqlalchemy', sql_span.n)
        self.assertFalse('custom' in sql_span.data)
        self.assertTrue('sqlalchemy' in sql_span.data)
github instana / python-sensor / tests / clients / test_pymongo.py View on Github external
def test_successful_mutiple_queries(self):
        with tracer.start_active_span("test"):
            self.conn.test.records.bulk_write([pymongo.InsertOne({"type": "string"}),
                                               pymongo.UpdateOne({"type": "string"}, {"$set": {"type": "int"}}),
                                               pymongo.DeleteOne({"type": "string"})])

        assert_is_none(tracer.active_span)

        spans = self.recorder.queued_spans()
        self.assertEqual(len(spans), 4)

        test_span = spans.pop()

        seen_span_ids = set()
        commands = []
        for span in spans:
            self.assertEqual(test_span.t, span.t)
            self.assertEqual(span.p, test_span.s)

            # check if all spans got a unique id
            assert_false(span.s in seen_span_ids)

            seen_span_ids.add(span.s)
github instana / python-sensor / tests / frameworks / test_pyramid.py View on Github external
assert('X-Instana-T' in response.headers)
        assert(int(response.headers['X-Instana-T'], 16))
        self.assertEqual(response.headers['X-Instana-T'], pyramid_span.t)

        assert('X-Instana-S' in response.headers)
        assert(int(response.headers['X-Instana-S'], 16))
        self.assertEqual(response.headers['X-Instana-S'], pyramid_span.s)

        assert('X-Instana-L' in response.headers)
        self.assertEqual(response.headers['X-Instana-L'], '1')

        assert('Server-Timing' in response.headers)
        server_timing_value = "intid;desc=%s" % pyramid_span.t
        self.assertEqual(response.headers['Server-Timing'], server_timing_value)

        self.assertIsNone(tracer.active_span)

        # Same traceId
        self.assertEqual(test_span.t, urllib3_span.t)
        self.assertEqual(test_span.t, pyramid_span.t)

        # Parent relationships
        self.assertEqual(urllib3_span.p, test_span.s)
        self.assertEqual(pyramid_span.p, urllib3_span.s)

        # Error logging
        self.assertIsNone(test_span.ec)
        self.assertEqual(1, urllib3_span.ec)
        self.assertEqual(1, pyramid_span.ec)

        # wsgi
        self.assertEqual("sdk", pyramid_span.n)
github instana / python-sensor / tests / clients / test_redis.py View on Github external
with tracer.start_active_span('test'):
            self.client.set('foox', 'barX')
            self.client.set('fooy', 'barY')
            result = self.client.get('foox')

        spans = self.recorder.queued_spans()
        self.assertEqual(4, len(spans))

        self.assertEqual(b'barX', result)

        rs1_span = spans[0]
        rs2_span = spans[1]
        rs3_span = spans[2]
        test_span = spans[3]

        self.assertIsNone(tracer.active_span)

        # Same traceId
        self.assertEqual(test_span.t, rs1_span.t)
        self.assertEqual(test_span.t, rs2_span.t)
        self.assertEqual(test_span.t, rs3_span.t)

        # Parent relationships
        self.assertEqual(rs1_span.p, test_span.s)
        self.assertEqual(rs2_span.p, test_span.s)
        self.assertEqual(rs3_span.p, test_span.s)

        # Error logging
        self.assertIsNone(test_span.ec)
        self.assertIsNone(rs1_span.ec)
        self.assertIsNone(rs2_span.ec)
        self.assertIsNone(rs3_span.ec)
github instana / python-sensor / instana / instrumentation / sqlalchemy.py View on Github external
def receive_before_cursor_execute(**kw):
        try:
            parent_span = tracer.active_span

            # If we're not tracing, just return
            if parent_span is None:
                return

            scope = tracer.start_active_span("sqlalchemy", child_of=parent_span)
            context = kw['context']
            context._stan_scope = scope

            conn = kw['conn']
            url = str(conn.engine.url)
            scope.span.set_tag('sqlalchemy.sql', kw['statement'])
            scope.span.set_tag('sqlalchemy.eng', conn.engine.name)
            scope.span.set_tag('sqlalchemy.url', url_regexp.sub('//', url))
        except Exception as e:
            logger.debug(e)
github instana / python-sensor / instana / instrumentation / logging.py View on Github external
def log_with_instana(wrapped, instance, argv, kwargs):
    # argv[0] = level
    # argv[1] = message
    # argv[2] = args for message
    try:
        parent_span = tracer.active_span

        # Only needed if we're tracing and serious log
        if parent_span and argv[0] >= logging.WARN:

            msg = str(argv[1])
            args = argv[2]
            if args and len(args) == 1 and isinstance(args[0], collections.Mapping) and args[0]:
                args = args[0]

            # get the formatted log message
            msg = msg % args

            # get additional information if an exception is being handled
            parameters = None
            (t, v, tb) = sys.exc_info()
            if t is not None and v is not None:
github instana / python-sensor / instana / instrumentation / celery / hooks.py View on Github external
def before_task_publish(*args, **kwargs):
        try:
            parent_span = tracer.active_span
            if parent_span is not None:
                body = kwargs['body']
                headers = kwargs['headers']
                task_name = kwargs['sender']
                task = registry.tasks.get(task_name)
                task_id = get_task_id(headers, body)

                scope = tracer.start_active_span("celery-client", child_of=parent_span)
                scope.span.set_tag("task", task_name)
                scope.span.set_tag("task_id", task_id)
                add_broker_tags(scope.span, task.app.conf['broker_url'])

                # Context propagation
                context_headers = {}
                tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, context_headers)
github instana / python-sensor / instana / instrumentation / sudsjurko.py View on Github external
def send_with_instana(wrapped, instance, args, kwargs):
        parent_span = tracer.active_span

        # If we're not tracing, just return
        if parent_span is None:
            return wrapped(*args, **kwargs)

        with tracer.start_active_span("soap", child_of=parent_span) as scope:
            try:
                scope.span.set_tag('soap.action', instance.method.name)
                scope.span.set_tag(ext.HTTP_URL, instance.method.location)
                scope.span.set_tag(ext.HTTP_METHOD, 'POST')

                tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, instance.options.headers)

                rv = wrapped(*args, **kwargs)

            except Exception as e:
github instana / python-sensor / instana / instrumentation / cassandra_inst.py View on Github external
def request_init_with_instana(fn):
        parent_span = tracer.active_span

        if parent_span is not None:
            ctags = dict()
            if isinstance(fn.query, cassandra.query.SimpleStatement):
                ctags["cassandra.query"] = fn.query.query_string
            elif isinstance(fn.query, cassandra.query.BoundStatement):
                ctags["cassandra.query"] = fn.query.prepared_statement.query_string

            ctags["cassandra.keyspace"] = fn.session.keyspace
            ctags["cassandra.cluster"] = fn.session.cluster.metadata.cluster_name

            span = tracer.start_span(
                operation_name="cassandra",
                child_of=parent_span,
                tags=ctags)
github instana / python-sensor / instana / instrumentation / grpcio.py View on Github external
def stream_unary_future_with_instana(wrapped, instance, argv, kwargs):
        parent_span = tracer.active_span

        # If we're not tracing, just return
        if parent_span is None:
            return wrapped(*argv, **kwargs)

        with tracer.start_active_span("rpc-client", child_of=parent_span) as scope:
            try:
                if not "metadata" in kwargs:
                    kwargs["metadata"] = []

                kwargs["metadata"] = tracer.inject(scope.span.context, opentracing.Format.BINARY, kwargs['metadata'])
                collect_tags(scope.span, instance, argv, kwargs)
                scope.span.set_tag('rpc.call_type', 'stream')

                rv = wrapped(*argv, **kwargs)
            except Exception as e: