strands.telemetry
¶
Telemetry module.
This module provides metrics and tracing functionality.
strands.telemetry.metrics
¶
Utilities for collecting and reporting performance metrics in the SDK.
EventLoopMetrics
dataclass
¶
Aggregated metrics for an event loop's execution.
Attributes:
Name | Type | Description |
---|---|---|
cycle_count |
int
|
Number of event loop cycles executed. |
tool_metrics |
Dict[str, ToolMetrics]
|
Metrics for each tool used, keyed by tool name. |
cycle_durations |
List[float]
|
List of durations for each cycle in seconds. |
traces |
List[Trace]
|
List of execution traces. |
accumulated_usage |
Usage
|
Accumulated token usage across all model invocations. |
accumulated_metrics |
Metrics
|
Accumulated performance metrics across all model invocations. |
Source code in strands/telemetry/metrics.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
add_tool_usage(tool, duration, tool_trace, success, message)
¶
Record metrics for a tool invocation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool that was used. |
required |
duration
|
float
|
How long the tool call took in seconds. |
required |
tool_trace
|
Trace
|
The trace object for this tool call. |
required |
success
|
bool
|
Whether the tool call was successful. |
required |
message
|
Message
|
The message associated with the tool call. |
required |
Source code in strands/telemetry/metrics.py
end_cycle(start_time, cycle_trace, attributes=None)
¶
End the current event loop cycle and record its duration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_time
|
float
|
The timestamp when the cycle started. |
required |
cycle_trace
|
Trace
|
The trace object for this cycle. |
required |
attributes
|
Optional[Dict[str, Any]]
|
attributes of the metrics. |
None
|
Source code in strands/telemetry/metrics.py
get_summary()
¶
Generate a comprehensive summary of all collected metrics.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary containing summarized metrics data. |
Dict[str, Any]
|
This includes cycle statistics, tool usage, traces, and accumulated usage information. |
Source code in strands/telemetry/metrics.py
start_cycle(attributes=None)
¶
Start a new event loop cycle and create a trace for it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attributes
|
Optional[Dict[str, Any]]
|
attributes of the metrics. |
None
|
Returns:
Type | Description |
---|---|
Tuple[float, Trace]
|
A tuple containing the start time and the cycle trace object. |
Source code in strands/telemetry/metrics.py
update_metrics(metrics)
¶
Update the accumulated performance metrics with new metrics data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics
|
Metrics
|
The metrics data to add to the accumulated totals. |
required |
Source code in strands/telemetry/metrics.py
update_usage(usage)
¶
Update the accumulated token usage with new usage data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
usage
|
Usage
|
The usage data to add to the accumulated totals. |
required |
Source code in strands/telemetry/metrics.py
MetricsClient
¶
Singleton client for managing OpenTelemetry metrics instruments.
The actual metrics export destination (console, OTLP endpoint, etc.) is configured through OpenTelemetry SDK configuration by users, not by this client.
Source code in strands/telemetry/metrics.py
__init__()
¶
Initialize the MetricsClient.
This method only runs once due to the singleton pattern. Sets up the OpenTelemetry meter and creates metric instruments.
Source code in strands/telemetry/metrics.py
__new__()
¶
Create or return the singleton instance of MetricsClient.
Returns:
Type | Description |
---|---|
MetricsClient
|
The single MetricsClient instance. |
Source code in strands/telemetry/metrics.py
create_instruments()
¶
Create and initialize all OpenTelemetry metric instruments.
Source code in strands/telemetry/metrics.py
ToolMetrics
dataclass
¶
Metrics for a specific tool's usage.
Attributes:
Name | Type | Description |
---|---|---|
tool |
ToolUse
|
The tool being tracked. |
call_count |
int
|
Number of times the tool has been called. |
success_count |
int
|
Number of successful tool calls. |
error_count |
int
|
Number of failed tool calls. |
total_time |
float
|
Total execution time across all calls in seconds. |
Source code in strands/telemetry/metrics.py
add_call(tool, duration, success, metrics_client, attributes=None)
¶
Record a new tool call with its outcome.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool that was called. |
required |
duration
|
float
|
How long the call took in seconds. |
required |
success
|
bool
|
Whether the call was successful. |
required |
metrics_client
|
MetricsClient
|
The metrics client for recording the metrics. |
required |
attributes
|
Optional[Dict[str, Any]]
|
attributes of the metrics. |
None
|
Source code in strands/telemetry/metrics.py
Trace
¶
A trace representing a single operation or step in the execution flow.
Source code in strands/telemetry/metrics.py
__init__(name, parent_id=None, start_time=None, raw_name=None, metadata=None, message=None)
¶
Initialize a new trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Human-readable name of the operation being traced. |
required |
parent_id
|
Optional[str]
|
ID of the parent trace, if this is a child operation. |
None
|
start_time
|
Optional[float]
|
Timestamp when the trace started. If not provided, the current time will be used. |
None
|
raw_name
|
Optional[str]
|
System level name. |
None
|
metadata
|
Optional[Dict[str, Any]]
|
Additional contextual information about the trace. |
None
|
message
|
Optional[Message]
|
Message associated with the trace. |
None
|
Source code in strands/telemetry/metrics.py
add_child(child)
¶
Add a child trace to this trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
child
|
Trace
|
The child trace to add. |
required |
add_message(message)
¶
Add a message to the trace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
The message to add. |
required |
duration()
¶
Calculate the duration of this trace.
Returns:
Type | Description |
---|---|
Optional[float]
|
The duration in seconds, or None if the trace hasn't ended yet. |
end(end_time=None)
¶
Mark the trace as complete with the given or current timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
end_time
|
Optional[float]
|
Timestamp to use as the end time. If not provided, the current time will be used. |
None
|
Source code in strands/telemetry/metrics.py
to_dict()
¶
Convert the trace to a dictionary representation.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary containing all trace information, suitable for serialization. |
Source code in strands/telemetry/metrics.py
metrics_to_string(event_loop_metrics, allowed_names=None)
¶
Convert event loop metrics to a human-readable string representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_loop_metrics
|
EventLoopMetrics
|
The metrics to format. |
required |
allowed_names
|
Optional[Set[str]]
|
Set of names that are allowed to be displayed unmodified. |
None
|
Returns:
Type | Description |
---|---|
str
|
A formatted string representation of the metrics. |