Esempio n. 1
0
async def test_create_span_field_headers_async():
    client = TraceServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(), )

    # Any value that is part of the HTTP/1.1 URI should be sent as
    # a field header. Set these to a non-empty value.
    request = trace.Span()
    request.name = "name/value"

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._client._transport.create_span),
                           "__call__") as call:
        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(trace.Span())

        await client.create_span(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls)
        _, args, _ = call.mock_calls[0]
        assert args[0] == request

    # Establish that the field header was sent.
    _, _, kw = call.mock_calls[0]
    assert (
        "x-goog-request-params",
        "name=name/value",
    ) in kw["metadata"]
Esempio n. 2
0
async def test_batch_write_spans_flattened_async():
    client = TraceServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(), )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._client._transport.batch_write_spans),
                           "__call__") as call:
        # Designate an appropriate return value for the call.
        call.return_value = None

        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None)
        # Call the method with a truthy value for each flattened field,
        # using the keyword arguments to the method.
        response = await client.batch_write_spans(
            name="name_value",
            spans=[trace.Span(name="name_value")],
        )

        # Establish that the underlying call was made with the expected
        # request object values.
        assert len(call.mock_calls)
        _, args, _ = call.mock_calls[0]

        assert args[0].name == "name_value"

        assert args[0].spans == [trace.Span(name="name_value")]
Esempio n. 3
0
async def test_batch_write_spans_flattened_error_async():
    client = TraceServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(), )

    # Attempting to call a method with both a request object and flattened
    # fields is an error.
    with pytest.raises(ValueError):
        await client.batch_write_spans(
            tracing.BatchWriteSpansRequest(),
            name="name_value",
            spans=[trace.Span(name="name_value")],
        )
Esempio n. 4
0
async def test_create_span_async(transport: str = "grpc_asyncio"):
    client = TraceServiceAsyncClient(
        credentials=credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = trace.Span()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._client._transport.create_span),
                           "__call__") as call:
        # Designate an appropriate return value for the call.
        call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(
            trace.Span(
                name="name_value",
                span_id="span_id_value",
                parent_span_id="parent_span_id_value",
                span_kind=trace.Span.SpanKind.INTERNAL,
            ))

        response = await client.create_span(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls)
        _, args, _ = call.mock_calls[0]

        assert args[0] == request

    # Establish that the response is the type that we expect.
    assert isinstance(response, trace.Span)

    assert response.name == "name_value"

    assert response.span_id == "span_id_value"

    assert response.parent_span_id == "parent_span_id_value"

    assert response.span_kind == trace.Span.SpanKind.INTERNAL
Esempio n. 5
0
    def create_span(
        self,
        request: trace.Span = None,
        *,
        retry: retries.Retry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> trace.Span:
        r"""Creates a new span.

        Args:
            request (google.cloud.trace_v2.types.Span):
                The request object. A span represents a single operation
                within a trace. Spans can be nested to form a trace
                tree. Often, a trace contains a root span that describes
                the end-to-end latency, and one or more subspans for its
                sub-operations. A trace can also contain multiple root
                spans, or none at all. Spans do not need to be
                contiguous—there may be gaps or overlaps between
                spans in a trace.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            google.cloud.trace_v2.types.Span:
                A span represents a single operation
                within a trace. Spans can be nested to
                form a trace tree. Often, a trace
                contains a root span that describes the
                end-to-end latency, and one or more
                subspans for its sub-operations. A trace
                can also contain multiple root spans, or
                none at all. Spans do not need to be
                contiguous—there may be gaps or
                overlaps between spans in a trace.

        """
        # Create or coerce a protobuf request object.
        # Minor optimization to avoid making a copy if the user passes
        # in a trace.Span.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, trace.Span):
            request = trace.Span(request)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = self._transport._wrapped_methods[self._transport.create_span]

        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (
            gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
        )

        # Send the request.
        response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

        # Done; return the response.
        return response
Esempio n. 6
0
    async def create_span(
            self,
            request: trace.Span = None,
            *,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> trace.Span:
        r"""Creates a new span.

        Args:
            request (:class:`~.trace.Span`):
                The request object. A span represents a single operation
                within a trace. Spans can be nested to form a trace
                tree. Often, a trace contains a root span that describes
                the end-to-end latency, and one or more subspans for its
                sub-operations. A trace can also contain multiple root
                spans, or none at all. Spans do not need to be
                contiguous—there may be gaps or overlaps between
                spans in a trace.

            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            ~.trace.Span:
                A span represents a single operation
                within a trace. Spans can be nested to
                form a trace tree. Often, a trace
                contains a root span that describes the
                end-to-end latency, and one or more
                subspans for its sub-operations. A trace
                can also contain multiple root spans, or
                none at all. Spans do not need to be
                contiguous—there may be gaps or
                overlaps between spans in a trace.

        """
        # Create or coerce a protobuf request object.

        request = trace.Span(request)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = gapic_v1.method_async.wrap_method(
            self._client._transport.create_span,
            default_retry=retries.Retry(
                initial=0.1,
                maximum=1.0,
                multiplier=1.2,
                predicate=retries.if_exception_type(
                    exceptions.DeadlineExceeded,
                    exceptions.ServiceUnavailable,
                ),
            ),
            default_timeout=120.0,
            client_info=DEFAULT_CLIENT_INFO,
        )

        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (gapic_v1.routing_header.to_grpc_metadata(
            (("name", request.name), )), )

        # Send the request.
        response = await rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response