Exemplo n.º 1
0
    def test_unary_exception(self):
        with grpc.secure_channel(
                "localhost:%d" % (_GRPC_PORT),
                credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                stub.SayHello(HelloRequest(name="exception"))

        spans = self.get_spans_with_sync_and_assert(size=2)
        client_span, server_span = spans

        assert client_span.resource == "/helloworld.Hello/SayHello"
        assert client_span.error == 1
        assert client_span.get_tag(ERROR_MSG) == "exception"
        assert client_span.get_tag(ERROR_TYPE) == "StatusCode.INVALID_ARGUMENT"
        assert client_span.get_tag(
            "grpc.status.code") == "StatusCode.INVALID_ARGUMENT"

        assert server_span.resource == "/helloworld.Hello/SayHello"
        assert server_span.error == 1
        assert server_span.get_tag(ERROR_MSG) == "exception"
        assert server_span.get_tag(ERROR_TYPE) == "StatusCode.INVALID_ARGUMENT"
        assert "Traceback" in server_span.get_tag(ERROR_STACK)
        assert "grpc.StatusCode.INVALID_ARGUMENT" in server_span.get_tag(
            ERROR_STACK)
Exemplo n.º 2
0
    def test_server_stream_exception(self):
        with grpc.secure_channel(
                'localhost:%d' % (_GRPC_PORT),
                credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                list(stub.SayHelloTwice(HelloRequest(name='exception')))

        spans = self.get_spans_with_sync_and_assert(size=2)
        server_span, client_span = spans

        assert client_span.resource == '/helloworld.Hello/SayHelloTwice'
        assert client_span.get_tag(errors.ERROR_MSG) == 'exception'
        assert client_span.get_tag(
            errors.ERROR_TYPE) == 'StatusCode.RESOURCE_EXHAUSTED'
        assert client_span.get_tag(
            'grpc.status.code') == 'StatusCode.RESOURCE_EXHAUSTED'

        assert server_span.resource == '/helloworld.Hello/SayHelloTwice'
        assert server_span.get_tag(errors.ERROR_MSG) == 'exception'
        assert server_span.get_tag(
            errors.ERROR_TYPE) == 'StatusCode.RESOURCE_EXHAUSTED'
        assert 'Traceback' in server_span.get_tag(errors.ERROR_STACK)
        assert 'grpc.StatusCode.RESOURCE_EXHAUSTED' in server_span.get_tag(
            errors.ERROR_STACK)
Exemplo n.º 3
0
 def test_unknown_servicer(self):
     with grpc.secure_channel("localhost:%d" % (_GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
         stub = HelloStub(channel)
         with self.assertRaises(grpc.RpcError) as exception_context:
             stub.SayHelloUnknown(HelloRequest(name="unknown"))
         rpc_error = exception_context.exception
         assert grpc.StatusCode.UNIMPLEMENTED == rpc_error.code()
Exemplo n.º 4
0
    def test_unary_exception(self):
        with grpc.secure_channel(
                'localhost:%d' % (_GRPC_PORT),
                credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                stub.SayHello(HelloRequest(name='exception'))

        spans = self.get_spans()
        assert len(spans) == 2
        server_span, client_span = spans

        assert client_span.resource == '/helloworld.Hello/SayHello'
        assert client_span.get_tag(errors.ERROR_MSG) == 'exception'
        assert client_span.get_tag(
            errors.ERROR_TYPE) == 'StatusCode.INVALID_ARGUMENT'
        assert client_span.get_tag(
            'grpc.status.code') == 'StatusCode.INVALID_ARGUMENT'

        assert server_span.resource == '/helloworld.Hello/SayHello'
        assert server_span.get_tag(errors.ERROR_MSG) == 'exception'
        assert server_span.get_tag(
            errors.ERROR_TYPE) == 'StatusCode.INVALID_ARGUMENT'
        assert 'Traceback' in server_span.get_tag(errors.ERROR_STACK)
        assert 'grpc.StatusCode.INVALID_ARGUMENT' in server_span.get_tag(
            errors.ERROR_STACK)
Exemplo n.º 5
0
    def test_server_stream_exception(self):
        # use an event to signal when the callbacks have been called from the response
        callback_called = threading.Event()

        def callback(response):
            callback_called.set()

        with grpc.secure_channel("localhost:%d" % (_GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                responses_iterator = stub.SayHelloTwice(HelloRequest(name="exception"))
                responses_iterator.add_done_callback(callback)
                list(responses_iterator)
            callback_called.wait(timeout=1)

        spans = self.get_spans_with_sync_and_assert(size=2)
        client_span, server_span = spans

        assert client_span.resource == "/helloworld.Hello/SayHelloTwice"
        assert client_span.error == 1
        assert client_span.get_tag(errors.ERROR_MSG) == "exception"
        assert client_span.get_tag(errors.ERROR_TYPE) == "StatusCode.RESOURCE_EXHAUSTED"
        assert client_span.get_tag("grpc.status.code") == "StatusCode.RESOURCE_EXHAUSTED"

        assert server_span.resource == "/helloworld.Hello/SayHelloTwice"
        assert server_span.error == 1
        assert server_span.get_tag(errors.ERROR_MSG) == "exception"
        assert server_span.get_tag(errors.ERROR_TYPE) == "StatusCode.RESOURCE_EXHAUSTED"
        assert "Traceback" in server_span.get_tag(errors.ERROR_STACK)
        assert "grpc.StatusCode.RESOURCE_EXHAUSTED" in server_span.get_tag(errors.ERROR_STACK)
Exemplo n.º 6
0
    def test_analytics_default(self):
        with grpc.secure_channel("localhost:%d" % (_GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            stub.SayHello(HelloRequest(name="test"))

        spans = self.get_spans_with_sync_and_assert(size=2)
        assert spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY) is None
        assert spans[1].get_metric(ANALYTICS_SAMPLE_RATE_KEY) is None
Exemplo n.º 7
0
    def test_analytics_default(self):
        with grpc.secure_channel('localhost:%d' % (GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        self.assertIsNone(spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY))
Exemplo n.º 8
0
def insecure_channel_credentials():
    """Creates a ChannelCredentials for use with an insecure channel.

    THIS IS AN EXPERIMENTAL API.

    This is not for use with secure_channel function. Intead, this should be
    used with grpc.unary_unary, grpc.unary_stream, grpc.stream_unary, or
    grpc.stream_stream.
    """
    return grpc.ChannelCredentials(_insecure_channel_credentials)
Exemplo n.º 9
0
    def _test_secure_channel(self, secure_channel_function):
        target = "localhost:%d" % (_GRPC_PORT)
        with secure_channel_function(target, credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            stub.SayHello(HelloRequest(name="test"))

        spans = self.get_spans_with_sync_and_assert(size=2)
        client_span, server_span = spans

        self._check_client_span(client_span, "grpc-client", "SayHello", "unary")
        self._check_server_span(server_span, "grpc-server", "SayHello", "unary")
Exemplo n.º 10
0
    def _test_secure_channel(self, secure_channel_function):
        target = 'localhost:%d' % (_GRPC_PORT)
        with secure_channel_function(target, credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans_with_sync_and_assert(size=2)
        server_span, client_span = spans

        self._check_client_span(client_span, 'grpc-client', 'SayHello', 'unary')
        self._check_server_span(server_span, 'grpc-server', 'SayHello', 'unary')
Exemplo n.º 11
0
    def test_analytics_without_rate(self):
        with self.override_config("grpc_server", dict(analytics_enabled=True)):
            with self.override_config("grpc", dict(analytics_enabled=True)):
                with grpc.secure_channel(
                        "localhost:%d" % (_GRPC_PORT),
                        credentials=grpc.ChannelCredentials(None)) as channel:
                    stub = HelloStub(channel)
                    stub.SayHello(HelloRequest(name="test"))

        spans = self.get_spans_with_sync_and_assert(size=2)
        assert spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 1.0
        assert spans[1].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 1.0
Exemplo n.º 12
0
    def test_span_in_error(self):
        # Create a channel and send one request to the server
        with grpc.secure_channel('localhost:%d' % (GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(Exception):
                stub.SayError(HelloRequest(name='test'))

        spans = self.get_spans()
        self.assertEqual(len(spans), 1)

        span = spans[0]
        self.assertEqual(span.error, 1)
        self.assertIsNotNone(span.meta['error.stack'])
Exemplo n.º 13
0
    def test_unary_abort(self):
        with grpc.secure_channel('localhost:%d' % (_GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                stub.SayHello(HelloRequest(name='abort'))

        spans = self.get_spans()
        assert len(spans) == 2
        server_span, client_span = spans

        assert client_span.resource == '/helloworld.Hello/SayHello'
        assert client_span.get_tag(errors.ERROR_MSG) == 'aborted'
        assert client_span.get_tag(errors.ERROR_TYPE) == 'StatusCode.ABORTED'
Exemplo n.º 14
0
    def test_analytics_without_rate(self):
        with self.override_config('grpc_server', dict(analytics_enabled=True)):
            with self.override_config('grpc', dict(analytics_enabled=True)):
                with grpc.secure_channel(
                        'localhost:%d' % (_GRPC_PORT),
                        credentials=grpc.ChannelCredentials(None)) as channel:
                    stub = HelloStub(channel)
                    stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans()
        assert len(spans) == 2
        assert spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 1.0
        assert spans[1].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 1.0
Exemplo n.º 15
0
    def test_secure_channel(self):
        # Create a channel and send one request to the server
        with grpc.secure_channel('localhost:%d' % (GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            response = stub.SayHello(HelloRequest(name='test'))

        writer = self._tracer.writer
        spans = writer.pop()
        eq_(len(spans), 1)

        span = spans[0]
        eq_(response.message, 'x-datadog-trace-id=%d;x-datadog-parent-id=%d' % (span.trace_id, span.span_id))
        _check_span(span)
Exemplo n.º 16
0
    def test_unary_abort(self):
        with grpc.secure_channel("localhost:%d" % (_GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            with self.assertRaises(grpc.RpcError):
                stub.SayHello(HelloRequest(name="abort"))

        spans = self.get_spans_with_sync_and_assert(size=2)
        client_span, server_span = spans

        assert client_span.resource == "/helloworld.Hello/SayHello"
        assert client_span.error == 1
        assert client_span.get_tag(errors.ERROR_MSG) == "aborted"
        assert client_span.get_tag(errors.ERROR_TYPE) == "StatusCode.ABORTED"
        assert client_span.get_tag("grpc.status.code") == "StatusCode.ABORTED"
Exemplo n.º 17
0
    def test_analytics_with_rate(self):
        with self.override_config(
            'grpc',
            dict(analytics_enabled=True, analytics_sample_rate=0.5)
        ):
            with grpc.secure_channel(
                    'localhost:%d' % (GRPC_PORT),
                    credentials=grpc.ChannelCredentials(None)
            ) as channel:
                stub = HelloStub(channel)
                stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans()
        self.assertEqual(len(spans), 1)
        self.assertEqual(spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY), 0.5)
Exemplo n.º 18
0
    def test_analytics_with_rate(self):
        with self.override_config(
                'grpc_server',
                dict(analytics_enabled=True, analytics_sample_rate=0.75)):
            with self.override_config(
                    'grpc',
                    dict(analytics_enabled=True, analytics_sample_rate=0.5)):
                with grpc.secure_channel(
                        'localhost:%d' % (_GRPC_PORT),
                        credentials=grpc.ChannelCredentials(None)) as channel:
                    stub = HelloStub(channel)
                    stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans_with_sync_and_assert(size=2)
        assert spans[0].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 0.75
        assert spans[1].get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 0.5
Exemplo n.º 19
0
    def test_span_in_error(self):
        # Create a channel and send one request to the server
        with grpc.secure_channel(
                'localhost:%d' % (GRPC_PORT),
                credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            try:
                stub.SayError(HelloRequest(name='test'))
            except:
                pass  # excepted to throw

        writer = self._tracer.writer
        spans = writer.pop()
        eq_(len(spans), 1)

        span = spans[0]
        eq_(span.error, 1)
        self.assertIsNotNone(span.meta['error.stack'])
Exemplo n.º 20
0
    def test_secure_channel(self):
        # Create a channel and send one request to the server
        with grpc.secure_channel('localhost:%d' % (GRPC_PORT), credentials=grpc.ChannelCredentials(None)) as channel:
            stub = HelloStub(channel)
            response = stub.SayHello(HelloRequest(name='test'))

        spans = self.get_spans()
        self.assertEqual(len(spans), 1)

        span = spans[0]
        self.assertEqual(
            response.message,
            (
                # DEV: Priority sampling is enabled by default
                'x-datadog-trace-id=%d;x-datadog-parent-id=%d;x-datadog-sampling-priority=1' %
                (span.trace_id, span.span_id)
            ),
        )
        self._check_span(span)
Exemplo n.º 21
0
    """Indicates a channel option unique to gRPC Python.

     This enumeration is part of an EXPERIMENTAL API.

     Attributes:
       SingleThreadedUnaryStream: Perform unary-stream RPCs on a single thread.
    """
    SingleThreadedUnaryStream = "SingleThreadedUnaryStream"


class UsageError(Exception):
    """Raised by the gRPC library to indicate usage not allowed by the API."""


_insecure_channel_credentials_sentinel = object()
_insecure_channel_credentials = grpc.ChannelCredentials(
    _insecure_channel_credentials_sentinel)


def insecure_channel_credentials():
    """Creates a ChannelCredentials for use with an insecure channel.

    THIS IS AN EXPERIMENTAL API.

    This is not for use with secure_channel function. Intead, this should be
    used with grpc.unary_unary, grpc.unary_stream, grpc.stream_unary, or
    grpc.stream_stream.
    """
    return _insecure_channel_credentials


class ExperimentalApiWarning(Warning):
Exemplo n.º 22
0
     This enumeration is part of an EXPERIMENTAL API.

     Attributes:
       SingleThreadedUnaryStream: Perform unary-stream RPCs on a single thread.
    """
    SingleThreadedUnaryStream = "SingleThreadedUnaryStream"


class UsageError(Exception):
    """Raised by the gRPC library to indicate usage not allowed by the API."""


# It's important that there be a single insecure credentials object so that its
# hash is deterministic and can be used for indexing in the simple stubs cache.
_insecure_channel_credentials = grpc.ChannelCredentials(
    _cygrpc.channel_credentials_insecure())


def insecure_channel_credentials():
    """Creates a ChannelCredentials for use with an insecure channel.

    THIS IS AN EXPERIMENTAL API.
    """
    return _insecure_channel_credentials


class ExperimentalApiWarning(Warning):
    """A warning that an API is experimental."""


def _warn_experimental(api_name, stack_offset):