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
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
def create_span( self, name, span_id, display_name, start_time, end_time, parent_span_id=None, attributes=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, child_span_count=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates a new span. Example: >>> from google.cloud import trace_v2 >>> >>> client = trace_v2.TraceServiceClient() >>> >>> name = client.span_path('[PROJECT]', '[TRACE]', '[SPAN]') >>> >>> # TODO: Initialize `span_id`: >>> span_id = '' >>> >>> # TODO: Initialize `display_name`: >>> display_name = {} >>> >>> # TODO: Initialize `start_time`: >>> start_time = {} >>> >>> # TODO: Initialize `end_time`: >>> end_time = {} >>> >>> response = client.create_span(name, span_id, display_name, start_time, end_time) Args: name (str): The resource name of the span in the following format: :: projects/[PROJECT_ID]/traces/[TRACE_ID]/spans/[SPAN_ID] [TRACE\_ID] is a unique identifier for a trace within a project; it is a 32-character hexadecimal encoding of a 16-byte array. [SPAN\_ID] is a unique identifier for a span within a trace; it is a 16-character hexadecimal encoding of an 8-byte array. span_id (str): The [SPAN\_ID] portion of the span's resource name. display_name (Union[dict, ~google.cloud.trace_v2.types.TruncatableString]): A description of the span's operation (up to 128 bytes). Stackdriver Trace displays the description in the {% dynamic print site\_values.console\_name %}. For example, the display name can be a qualified method name or a file name and a line number where the operation is called. A best practice is to use the same display name within an application and at the same call point. This makes it easier to correlate spans in different traces. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.TruncatableString` start_time (Union[dict, ~google.cloud.trace_v2.types.Timestamp]): The start time of the span. On the client side, this is the time kept by the local machine where the span execution starts. On the server side, this is the time when the server's application handler starts running. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Timestamp` end_time (Union[dict, ~google.cloud.trace_v2.types.Timestamp]): The end time of the span. On the client side, this is the time kept by the local machine where the span execution ends. On the server side, this is the time when the server application handler stops running. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Timestamp` parent_span_id (str): The [SPAN\_ID] of this span's parent span. If this is a root span, then this field must be empty. attributes (Union[dict, ~google.cloud.trace_v2.types.Attributes]): A set of attributes on the span. You can have up to 32 attributes per span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Attributes` stack_trace (Union[dict, ~google.cloud.trace_v2.types.StackTrace]): Stack trace captured at the start of the span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.StackTrace` time_events (Union[dict, ~google.cloud.trace_v2.types.TimeEvents]): A set of time events. You can have up to 32 annotations and 128 message events per span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.TimeEvents` links (Union[dict, ~google.cloud.trace_v2.types.Links]): Links associated with the span. You can have up to 128 links per Span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Links` status (Union[dict, ~google.cloud.trace_v2.types.Status]): An optional final status for this span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Status` same_process_as_parent_span (Union[dict, ~google.cloud.trace_v2.types.BoolValue]): (Optional) Set this parameter to indicate whether this span is in the same process as its parent. If you do not set this parameter, Stackdriver Trace is unable to take advantage of this helpful information. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.BoolValue` child_span_count (Union[dict, ~google.cloud.trace_v2.types.Int32Value]): An optional number of child spans that were generated while this span was active. If set, allows implementation to detect missing child spans. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Int32Value` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.trace_v2.types.Span` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_span" not in self._inner_api_calls: self._inner_api_calls[ "create_span"] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_span, default_retry=self._method_configs["CreateSpan"].retry, default_timeout=self._method_configs["CreateSpan"].timeout, client_info=self._client_info, ) request = trace_pb2.Span( name=name, span_id=span_id, display_name=display_name, start_time=start_time, end_time=end_time, parent_span_id=parent_span_id, attributes=attributes, stack_trace=stack_trace, time_events=time_events, links=links, status=status, same_process_as_parent_span=same_process_as_parent_span, child_span_count=child_span_count, ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("name", name)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header) metadata.append(routing_metadata) return self._inner_api_calls["create_span"](request, retry=retry, timeout=timeout, metadata=metadata)
def create_span(self, name, span_id, display_name, start_time, end_time, parent_span_id=None, attributes=None, stack_trace=None, time_events=None, links=None, status=None, same_process_as_parent_span=None, child_span_count=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT): """ Creates a new Span. Example: >>> from google.cloud import trace_v2 >>> >>> client = trace_v2.TraceServiceClient() >>> >>> name = client.span_path('[PROJECT]', '[TRACE]', '[SPAN]') >>> span_id = '' >>> display_name = {} >>> start_time = {} >>> end_time = {} >>> >>> response = client.create_span(name, span_id, display_name, start_time, end_time) Args: name (str): The resource name of the span in the following format: :: projects/[PROJECT_ID]/traces/[TRACE_ID]/spans/[SPAN_ID] [TRACE_ID] is a unique identifier for a trace within a project. [SPAN_ID] is a unique identifier for a span within a trace, assigned when the span is created. span_id (str): The [SPAN_ID] portion of the span's resource name. The ID is a 16-character hexadecimal encoding of an 8-byte array. display_name (Union[dict, ~google.cloud.trace_v2.types.TruncatableString]): A description of the span's operation (up to 128 bytes). Stackdriver Trace displays the description in the {% dynamic print site_values.console_name %}. For example, the display name can be a qualified method name or a file name and a line number where the operation is called. A best practice is to use the same display name within an application and at the same call point. This makes it easier to correlate spans in different traces. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.TruncatableString` start_time (Union[dict, ~google.cloud.trace_v2.types.Timestamp]): The start time of the span. On the client side, this is the time kept by the local machine where the span execution starts. On the server side, this is the time when the server's application handler starts running. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Timestamp` end_time (Union[dict, ~google.cloud.trace_v2.types.Timestamp]): The end time of the span. On the client side, this is the time kept by the local machine where the span execution ends. On the server side, this is the time when the server application handler stops running. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Timestamp` parent_span_id (str): The [SPAN_ID] of this span's parent span. If this is a root span, then this field must be empty. attributes (Union[dict, ~google.cloud.trace_v2.types.Attributes]): A set of attributes on the span. There is a limit of 32 attributes per span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Attributes` stack_trace (Union[dict, ~google.cloud.trace_v2.types.StackTrace]): Stack trace captured at the start of the span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.StackTrace` time_events (Union[dict, ~google.cloud.trace_v2.types.TimeEvents]): The included time events. There can be up to 32 annotations and 128 message events per span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.TimeEvents` links (Union[dict, ~google.cloud.trace_v2.types.Links]): A maximum of 128 links are allowed per Span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Links` status (Union[dict, ~google.cloud.trace_v2.types.Status]): An optional final status for this span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Status` same_process_as_parent_span (Union[dict, ~google.cloud.trace_v2.types.BoolValue]): A highly recommended but not required flag that identifies when a trace crosses a process boundary. True when the parent_span belongs to the same process as the current span. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.BoolValue` child_span_count (Union[dict, ~google.cloud.trace_v2.types.Int32Value]): An optional number of child spans that were generated while this span was active. If set, allows implementation to detect missing child spans. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.trace_v2.types.Int32Value` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. Returns: A :class:`~google.cloud.trace_v2.types.Span` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ request = trace_pb2.Span( name=name, span_id=span_id, display_name=display_name, start_time=start_time, end_time=end_time, parent_span_id=parent_span_id, attributes=attributes, stack_trace=stack_trace, time_events=time_events, links=links, status=status, same_process_as_parent_span=same_process_as_parent_span, child_span_count=child_span_count) return self._create_span(request, retry=retry, timeout=timeout)