Google Cloud Trace API client library for distributed tracing and performance analysis
—
Rich data structures for enhanced tracing with attributes, time events, stack traces, and structured metadata. The v2 API provides comprehensive data types that support detailed tracing information and flexible span representation.
Enhanced span representation with rich metadata, supporting complex tracing scenarios with detailed attributes, time events, and relationships.
class Span:
name: str # Resource name: "projects/{project}/traces/{trace}/spans/{span}"
span_id: str # Unique identifier for this span within a trace
parent_span_id: str # ID of the parent span, if any
display_name: TruncatableString # Human-readable name for this span
start_time: Timestamp # Start time of the span
end_time: Timestamp # End time of the span
attributes: Span.Attributes # Set of attributes for this span
stack_trace: StackTrace # Stack trace captured during span execution
time_events: Span.TimeEvents # Time-stamped events during span execution
links: Span.Links # Links to other spans
status: Status # Optional status for this span
same_process_as_parent_span: BoolValue # True if span and parent in same process
child_span_count: Int32Value # Number of child spans generated
span_kind: SpanKind # Type of span (INTERNAL, SERVER, CLIENT, etc.)
class Attributes:
attribute_map: Dict[str, AttributeValue] # Map of attributes
dropped_attributes_count: int # Number of dropped attributes
class TimeEvent:
time: Timestamp # Time when event occurred
annotation: Annotation # Text annotation with optional attributes
message_event: MessageEvent # Network message event
class Annotation:
description: TruncatableString # Description of the annotation
attributes: Attributes # Set of attributes for this annotation
class MessageEvent:
type: Type # Type of message (SENT, RECEIVED)
id: int # Identifier for this message
uncompressed_size_bytes: int # Uncompressed size in bytes
compressed_size_bytes: int # Compressed size in bytes
class Type(Enum):
TYPE_UNSPECIFIED = 0
SENT = 1
RECEIVED = 2
class TimeEvents:
time_event: List[TimeEvent] # Collection of time events
dropped_annotations_count: int # Number of dropped annotations
dropped_message_events_count: int # Number of dropped message events
class Link:
trace_id: str # Trace ID for linked span
span_id: str # Span ID for linked span
type: Type # Relationship type
attributes: Attributes # Link attributes
class Type(Enum):
TYPE_UNSPECIFIED = 0
CHILD_LINKED_SPAN = 1
PARENT_LINKED_SPAN = 2
class Links:
link: List[Link] # Collection of links
dropped_links_count: int # Number of dropped links
class SpanKind(Enum):
SPAN_KIND_UNSPECIFIED = 0 # Unspecified span kind
INTERNAL = 1 # Internal operation within application
SERVER = 2 # Synchronous RPC server span
CLIENT = 3 # Synchronous RPC client span
PRODUCER = 4 # Asynchronous producer span
CONSUMER = 5 # Asynchronous consumer spanUnion type for span attribute values, supporting string, integer, and boolean types with proper type safety.
class AttributeValue:
string_value: TruncatableString # String attribute value (oneof)
int_value: int # Integer attribute value (oneof)
bool_value: bool # Boolean attribute value (oneof)String representation with truncation metadata, allowing for efficient storage and transmission of potentially large string values.
class TruncatableString:
value: str # The truncated or untruncated original string
truncated_byte_count: int # Number of bytes removed from original stringCall stack information with detailed frame data, enabling precise debugging and performance analysis.
class StackTrace:
stack_frames: StackFrames # Stack frames in this stack trace
stack_trace_hash_id: int # Hash ID used to conserve network bandwidth
class StackFrame:
function_name: TruncatableString # Name of the function
original_function_name: TruncatableString # Unmangled function name
file_name: TruncatableString # Name of the source file
line_number: int # Line number in the source file
column_number: int # Column number in the source file
load_module: Module # Binary module this frame belongs to
source_version: TruncatableString # Source code version identifier
class StackFrames:
frame: List[StackFrame] # Stack frames in this call stack
dropped_frames_count: int # Number of stack frames droppedBinary module identification for stack trace frames, providing detailed information about loaded modules.
class Module:
module: TruncatableString # Binary module name (e.g., main.binary)
build_id: TruncatableString # Unique identifier for the moduleRequest message for batch span writing operations, enabling efficient transmission of multiple spans.
class BatchWriteSpansRequest:
name: str # Required. Resource name of the project: "projects/{project}"
spans: List[Span] # Required. Collection of spans to writeStandard RPC status representation for span completion status.
class Status:
code: int # Status code (0 = OK)
message: str # Developer-facing error message
details: List[Any] # Additional error detailsRepresents a point in time, independent of any time zone or calendar.
class Timestamp:
seconds: int # Seconds since Unix epoch
nanos: int # Non-negative fractions of a second at nanosecond resolutionfrom google.cloud import trace_v2
# Create a basic span
span = trace_v2.Span(
name="projects/my-project/traces/trace-123/spans/span-456",
span_id="span-456",
display_name=trace_v2.TruncatableString(value="database-query"),
start_time={"seconds": 1609459200, "nanos": 500000000},
end_time={"seconds": 1609459201, "nanos": 750000000},
span_kind=trace_v2.Span.SpanKind.INTERNAL
)from google.cloud import trace_v2
# Create span with attributes
attributes = trace_v2.Span.Attributes(
attribute_map={
"database.name": trace_v2.AttributeValue(
string_value=trace_v2.TruncatableString(value="users")
),
"database.rows_affected": trace_v2.AttributeValue(int_value=42),
"database.cached": trace_v2.AttributeValue(bool_value=True)
}
)
span = trace_v2.Span(
name="projects/my-project/traces/trace-123/spans/db-span",
span_id="db-span",
display_name=trace_v2.TruncatableString(value="SELECT users"),
start_time={"seconds": 1609459200},
end_time={"seconds": 1609459201},
attributes=attributes,
span_kind=trace_v2.Span.SpanKind.INTERNAL
)from google.cloud import trace_v2
# Create annotation
annotation = trace_v2.Span.TimeEvent.Annotation(
description=trace_v2.TruncatableString(value="Cache miss occurred"),
attributes=trace_v2.Span.Attributes(
attribute_map={
"cache.key": trace_v2.AttributeValue(
string_value=trace_v2.TruncatableString(value="user:123")
)
}
)
)
# Create time event
time_event = trace_v2.Span.TimeEvent(
time={"seconds": 1609459200, "nanos": 750000000},
annotation=annotation
)
# Create span with time events
time_events = trace_v2.Span.TimeEvents(
time_event=[time_event],
dropped_annotations_count=0
)
span = trace_v2.Span(
name="projects/my-project/traces/trace-123/spans/annotated-span",
span_id="annotated-span",
display_name=trace_v2.TruncatableString(value="cache-operation"),
start_time={"seconds": 1609459200},
end_time={"seconds": 1609459201},
time_events=time_events,
span_kind=trace_v2.Span.SpanKind.INTERNAL
)from google.cloud import trace_v2
# Create link to another span
link = trace_v2.Span.Link(
trace_id="other-trace-id",
span_id="other-span-id",
type=trace_v2.Span.Link.Type.CHILD_LINKED_SPAN,
attributes=trace_v2.Span.Attributes(
attribute_map={
"link.reason": trace_v2.AttributeValue(
string_value=trace_v2.TruncatableString(value="async-operation")
)
}
)
)
# Create span with links
links = trace_v2.Span.Links(
link=[link],
dropped_links_count=0
)
span = trace_v2.Span(
name="projects/my-project/traces/trace-123/spans/linked-span",
span_id="linked-span",
display_name=trace_v2.TruncatableString(value="linked-operation"),
start_time={"seconds": 1609459200},
end_time={"seconds": 1609459201},
links=links,
span_kind=trace_v2.Span.SpanKind.CLIENT
)Install with Tessl CLI
npx tessl i tessl/pypi-google-cloud-trace