How to use the opencensus.trace.execution_context function in opencensus

To help you get started, we’ve selected a few opencensus 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 census-ecosystem / opencensus-experiments / interoptest / src / pythonservice / grpcserver.py View on Github external
def test(self, request, context):
        """Handle a test request by calling other test services"""
        logger.debug("grpc service received: %s", request)
        if not request.service_hops:
            response = pb2.TestResponse(
                id=request.id,
                status=[pb2.CommonResponseStatus(
                    status=pb2.SUCCESS,
                )],
            )
        else:
            status = ([pb2.CommonResponseStatus(status=pb2.SUCCESS)] +
                      list(service.call_next(request).status))
            response = pb2.TestResponse(id=request.id, status=status)

        tracer = execution_context.get_opencensus_tracer()
        tracer.add_attribute_to_current_span("reqId", request.id)
        return response
github census-ecosystem / opencensus-experiments / interoptest / src / pythonservice / flaskserver.py View on Github external
request = pb2.TestRequest.FromString(flask.request.get_data())
    logger.debug("Flask service received: %s", request)

    if not request.service_hops:
        response = pb2.TestResponse(
            id=request.id,
            status=[pb2.CommonResponseStatus(
                status=pb2.SUCCESS,
            )],
        )
    else:
        status = ([pb2.CommonResponseStatus(status=pb2.SUCCESS)] +
                  list(service.call_next(request).status))
        response = pb2.TestResponse(id=request.id, status=status)

    tracer = execution_context.get_opencensus_tracer()
    tracer.add_attribute_to_current_span("reqId", request.id)
    return response.SerializeToString()
github census-instrumentation / opencensus-python / contrib / opencensus-ext-threading / opencensus / ext / threading / trace.py View on Github external
def call(self, func, args=(), kwds={}, **kwargs):
        wrapped_func = wrap_task_func(func)
        _tracer = execution_context.get_opencensus_tracer()
        propagator = binary_format.BinaryFormatPropagator()

        wrapped_kwargs = {}
        wrapped_kwargs["span_context_binary"] = propagator.to_header(
            _tracer.span_context
        )
        wrapped_kwargs["kwds"] = kwds
        wrapped_kwargs["sampler"] = _tracer.sampler
        wrapped_kwargs["exporter"] = _tracer.exporter
        wrapped_kwargs["propagator"] = _tracer.propagator

        return apply_async_func(
            self, wrapped_func, args=args, kwds=wrapped_kwargs, **kwargs
        )
github census-instrumentation / opencensus-python / contrib / opencensus-ext-dbapi / opencensus / ext / dbapi / trace.py View on Github external
def call(query, *args, **kwargs):
        _tracer = execution_context.get_opencensus_tracer()
        _span = _tracer.start_span()
        _span.name = 'mysql.query'
        _span.span_kind = span_module.SpanKind.CLIENT
        _tracer.add_attribute_to_current_span('mysql.query', query)
        _tracer.add_attribute_to_current_span(
            'mysql.cursor.method.name',
            query_func.__name__)

        result = query_func(query, *args, **kwargs)

        _tracer.end_span()
        return result
    return call
github census-instrumentation / opencensus-python / contrib / opencensus-ext-flask / opencensus / ext / flask / flask_middleware.py View on Github external
span.name = '[{}]{}'.format(
                flask.request.method,
                flask.request.url)
            tracer.add_attribute_to_current_span(
                HTTP_HOST, flask.request.host
            )
            tracer.add_attribute_to_current_span(
                HTTP_METHOD, flask.request.method
            )
            tracer.add_attribute_to_current_span(
                HTTP_PATH, flask.request.path
            )
            tracer.add_attribute_to_current_span(
                HTTP_URL, str(flask.request.url)
            )
            execution_context.set_opencensus_attr(
                'blacklist_hostnames',
                self.blacklist_hostnames
            )
        except Exception:  # pragma: NO COVER
            log.error('Failed to trace request', exc_info=True)
github census-instrumentation / opencensus-python / contrib / opencensus-ext-requests / opencensus / ext / requests / trace.py View on Github external
def call(url, *args, **kwargs):
        # Check if request was sent from an exporter. If so, do not wrap.
        if execution_context.is_exporter():
            return requests_func(url, *args, **kwargs)
        blacklist_hostnames = execution_context.get_opencensus_attr(
            'blacklist_hostnames')
        parsed_url = urlparse(url)
        if parsed_url.port is None:
            dest_url = parsed_url.hostname
        else:
            dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port)
        if utils.disable_tracing_hostname(dest_url, blacklist_hostnames):
            return requests_func(url, *args, **kwargs)

        path = parsed_url.path if parsed_url.path else '/'

        _tracer = execution_context.get_opencensus_tracer()
        _span = _tracer.start_span()
        _span.name = '{}'.format(path)
github Azure / azure-sdk-for-python / sdk / core / azure-core / azure / core / tracing / ext / opencensus_span.py View on Github external
def get_current_span(cls):
        # type: () -> Span
        """
        Get the current span from the execution context. Return None otherwise.
        """
        return execution_context.get_current_span()
github census-instrumentation / opencensus-python / contrib / opencensus-ext-sqlalchemy / opencensus / ext / sqlalchemy / trace.py View on Github external
def _after_cursor_execute(conn, cursor, statement, parameters,
                          context, executemany):
    """Intercept low-level cursor execute() events after execution.
    If executemany is True, this is an executemany call, else an execute call.

    See: http://docs.sqlalchemy.org/en/latest/core/events.html#sqlalchemy.
         events.ConnectionEvents.after_cursor_execute
    """
    _tracer = execution_context.get_opencensus_tracer()
    _tracer.end_span()