Esempio n. 1
0
def test_scope_error_report():
    tracer = Tracer()
    scope = tracer.start_active_span('foo')
    error_message = 'unexpected_situation'

    with mock.patch.object(scope.span, 'log_kv') as log_kv:
        with mock.patch.object(scope.span, 'set_tag') as set_tag:
            try:
                with scope:
                    raise ValueError(error_message)
            except ValueError:
                pass

            assert set_tag.call_count == 1
            assert set_tag.call_args[0] == (tags.ERROR, True)

            assert log_kv.call_count == 1
            log_kv_args = log_kv.call_args[0][0]
            assert log_kv_args.get(logs.EVENT, None) is tags.ERROR
            assert log_kv_args.get(logs.MESSAGE, None) is error_message
            assert log_kv_args.get(logs.ERROR_KIND, None) is ValueError
            assert isinstance(log_kv_args.get(logs.ERROR_OBJECT, None),
                              ValueError)
            assert isinstance(log_kv_args.get(logs.STACK, None),
                              types.TracebackType)
Esempio n. 2
0
def test_scope_manager():
    # ensure the activation returns the noop `Scope` that is always active
    scope_manager = ScopeManager()
    span = Span(tracer=Tracer(), context=SpanContext())
    scope = scope_manager.activate(span, False)
    assert scope == scope_manager._noop_scope
    assert scope == scope_manager.active
Esempio n. 3
0
def test_scope_context_manager():
    # ensure `Scope` can be used in a Context Manager that
    # calls the `close()` method
    span = Span(tracer=Tracer(), context=SpanContext())
    scope = Scope(ScopeManager(), span)
    with mock.patch.object(scope, 'close') as close:
        with scope:
            pass
        assert close.call_count == 1
Esempio n. 4
0
def test_scope_wrapper():
    # ensure `Scope` wraps the `Span` argument
    span = Span(tracer=Tracer(), context=SpanContext())
    scope = Scope(ScopeManager, span)
    assert scope.span == span
Esempio n. 5
0
    def __init__(self,
                 cluster_or_uri=None,
                 connect_timeout=30000,
                 socket_timeout=300000,
                 pool_size_per_route=10,
                 pool_size_total=100,
                 retry_count=3,
                 tls_skip_verify=False,
                 tls_ca_certificate_path="",
                 use_manual_address=False,
                 tracer=None):
        """Creates a Client.

        :param object cluster_or_uri: A ``pilosa.Cluster`` or ``pilosa.URI` instance
        :param int connect_timeout: The maximum amount of time in milliseconds to wait for a connection attempt to a server
        to succeed
        :param int socket_timeout: The maximum amount of time in milliseconds to wait between consecutive
        read operations for a response from the server
        :param int pool_size_per_route: Number of connections in the pool per server
        :param int pool_size_total: Total number of connections in the pool
        :param int retry_count: Number of connection trials
        :param bool tls_skip_verify: Do not verify the TLS certificate of the server (Not recommended for production)
        :param str tls_ca_certificate_path: Server's TLS certificate (Useful when using self-signed certificates)
        :param bool use_manual_address: Forces the client to use only the manual server address
        :param opentracing.tracer.Tracer tracer: Set the OpenTracing tracer. See: https://opentracing.io

        * See `Pilosa Python Client/Server Interaction <https://github.com/pilosa/python-pilosa/blob/master/docs/server-interaction.md>`_.
        """
        self.use_manual_address = use_manual_address
        self.connect_timeout = connect_timeout / 1000.0
        self.socket_timeout = socket_timeout / 1000.0
        self.pool_size_per_route = pool_size_per_route
        self.pool_size_total = pool_size_total
        self.retry_count = retry_count
        self.tls_skip_verify = tls_skip_verify
        self.tls_ca_certificate_path = tls_ca_certificate_path
        self.__current_host = None
        self.__client = None
        self.logger = logging.getLogger("pilosa")
        self.__coordinator_lock = threading.RLock()
        self.__coordinator_uri = None
        self.tracer = tracer or Tracer()

        if cluster_or_uri is None:
            self.cluster = Cluster(URI())
        elif isinstance(cluster_or_uri, Cluster):
            self.cluster = cluster_or_uri.copy()
        elif isinstance(cluster_or_uri, URI):
            if use_manual_address:
                self.__coordinator_uri = cluster_or_uri
                self.__current_host = cluster_or_uri
            else:
                self.cluster = Cluster(cluster_or_uri)
        elif isinstance(cluster_or_uri, str):
            uri = URI.address(cluster_or_uri)
            if use_manual_address:
                self.__coordinator_uri = uri
                self.__current_host = uri
            else:
                self.cluster = Cluster(uri)
        else:
            raise PilosaError("Invalid cluster_or_uri: %s" % cluster_or_uri)