예제 #1
0
    def test_batch_write_spans(self):
        project_id = os.environ["PROJECT_ID"]

        client = trace_v2.TraceServiceClient()
        name = f"projects/{project_id}"
        spans = []
        client.batch_write_spans(name=name, spans=spans)
    def test_create_span(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        span_id_2 = 'spanId2-643891741'
        parent_span_id = 'parentSpanId-1757797477'
        expected_response = {
            'name': name_2,
            'span_id': span_id_2,
            'parent_span_id': parent_span_id
        }
        expected_response = trace_pb2.Span(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = trace_v2.TraceServiceClient(channel=channel)

        # Setup Request
        name = client.span_path('[PROJECT]', '[TRACE]', '[SPAN]')
        span_id = 'spanId-2011840976'
        display_name = {}
        start_time = {}
        end_time = {}

        response = client.create_span(name, span_id, display_name, start_time,
                                      end_time)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = trace_pb2.Span(name=name,
                                          span_id=span_id,
                                          display_name=display_name,
                                          start_time=start_time,
                                          end_time=end_time)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #3
0
    def test_batch_write_spans(self):
        project_id = os.environ["PROJECT_ID"]

        client = trace_v2.TraceServiceClient()
        name = client.project_path(project_id)
        spans = []
        client.batch_write_spans(name, spans)
    def test_batch_write_spans_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = trace_v2.TraceServiceClient(channel=channel)

        # Setup request
        name = client.project_path('[PROJECT]')
        spans = []

        with pytest.raises(CustomException):
            client.batch_write_spans(name, spans)
    def test_batch_write_spans(self):
        client = trace_v2.TraceServiceClient()

        proejct_inside = client.project_path(PROJECT_INSIDE)
        proejct_outside = client.project_path(PROJECT_OUTSIDE)
        spans = []

        write_inside = lambda: client.batch_write_spans(proejct_inside, spans)
        write_outside = lambda: client.batch_write_spans(
            proejct_outside, spans)

        assert not TestVPCServiceControlV2._is_rejected(write_inside)
        assert TestVPCServiceControlV2._is_rejected(write_outside)
    def test_batch_write_spans_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = trace_v2.TraceServiceClient()

        # Setup request
        name = client.project_path("[PROJECT]")
        spans = []

        with pytest.raises(CustomException):
            client.batch_write_spans(name, spans)
    def test_batch_write_spans(self):
        channel = ChannelStub()
        client = trace_v2.TraceServiceClient(channel=channel)

        # Setup Request
        name = client.project_path('[PROJECT]')
        spans = []

        client.batch_write_spans(name, spans)

        assert len(channel.requests) == 1
        expected_request = tracing_pb2.BatchWriteSpansRequest(name=name,
                                                              spans=spans)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_span_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = trace_v2.TraceServiceClient(channel=channel)

        # Setup request
        name = client.span_path('[PROJECT]', '[TRACE]', '[SPAN]')
        span_id = 'spanId-2011840976'
        display_name = {}
        start_time = {}
        end_time = {}

        with pytest.raises(CustomException):
            client.create_span(name, span_id, display_name, start_time,
                               end_time)
예제 #9
0
    def test_batch_write_spans(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = trace_v2.TraceServiceClient()

        # Setup Request
        name = client.project_path("[PROJECT]")
        spans = []

        client.batch_write_spans(name, spans)

        assert len(channel.requests) == 1
        expected_request = tracing_pb2.BatchWriteSpansRequest(name=name, spans=spans)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #10
0
    def test_create_span_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = trace_v2.TraceServiceClient()

        # Setup request
        name = client.span_path("[PROJECT]", "[TRACE]", "[SPAN]")
        span_id = "spanId-2011840976"
        display_name = {}
        start_time = {}
        end_time = {}

        with pytest.raises(CustomException):
            client.create_span(name, span_id, display_name, start_time, end_time)
    def test_create_span(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        span_id_2 = "spanId2-643891741"
        parent_span_id = "parentSpanId-1757797477"
        expected_response = {
            "name": name_2,
            "span_id": span_id_2,
            "parent_span_id": parent_span_id,
        }
        expected_response = trace_pb2.Span(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = trace_v2.TraceServiceClient()

        # Setup Request
        name = client.span_path("[PROJECT]", "[TRACE]", "[SPAN]")
        span_id = "spanId-2011840976"
        display_name = {}
        start_time = {}
        end_time = {}

        response = client.create_span(name, span_id, display_name, start_time,
                                      end_time)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = trace_pb2.Span(
            name=name,
            span_id=span_id,
            display_name=display_name,
            start_time=start_time,
            end_time=end_time,
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
예제 #12
0
def create_span(
    project_id: str,
    trace_id: str,
    span_id: str,
    display_name: str,
    start_time: datetime.datetime,
    end_time: datetime.datetime,
):
    """Create a new span.

    Args:
        project_id: The name of the project.
        trace_id: A unique identifier for a trace within a
            project; it is a 32-character hexadecimal encoding of a
            16-byte array.
        span_id: A unique identifier for a span within a trace;
            it is a 16-character hexadecimal encoding of an 8-byte
        display_name: A description of the span's
            operation (up to 128 bytes)
        start_time: The start time of the span.
        end_time: The end time of the span.
    """

    client = trace_v2.TraceServiceClient()

    name = client.span_path(project_id, trace_id, span_id)
    display_name = trace_v2.TruncatableString(value=display_name)

    request = trace_v2.Span(
        name=name,
        span_id=span_id,
        display_name=display_name,
        start_time=start_time,
        end_time=end_time,
    )

    span = client.create_span(request=request)

    print(span)
    return span
예제 #13
0
def client():
    return trace_v2.TraceServiceClient()