generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
292 lines (228 loc) · 11.4 KB
/
tools.py
File metadata and controls
292 lines (228 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
"""Tools for the context overflow demos.
All tools use the Memory Pointer Pattern: large data is stored outside
the LLM context window and accessed via a lightweight pointer key.
State store used depends on the agent architecture:
Single-agent (test_context_overflow.py, test_context_overflow.ipynb):
→ agent.state — data scoped to one agent instance
Tools: fetch_application_logs, analyze_error_patterns,
detect_latency_anomalies, generate_incident_report
Multi-agent / Swarm (swarm_demo.py, test_multiagent_context_overflow.ipynb):
→ invocation_state — data shared across all agents in the swarm
Tools: fetch_logs_swarm, analyze_errors_swarm,
detect_latency_swarm, generate_report_swarm, get_error_details_swarm
See:
https://strandsagents.com/latest/documentation/docs/user-guide/concepts/agents/state/
https://strandsagents.com/latest/documentation/docs/user-guide/concepts/multi-agent/multi-agent-patterns/
"""
from strands import tool, ToolContext
import json
from datetime import datetime, timedelta
import secrets
# ── Shared log generation ─────────────────────────────────────────────────────
def _generate_log_events(app_name: str, hours: int, include_trace: bool = True) -> list:
"""Generate synthetic log events. Used by both single-agent and swarm tools."""
log_levels = ["INFO", "WARN", "ERROR", "DEBUG"]
services = ["api-gateway", "auth-service", "db-connector", "cache-layer"]
logs = []
base_time = datetime.now() - timedelta(hours=hours)
for i in range(hours * 100):
level = log_levels[secrets.randbelow(len(log_levels))]
service = services[secrets.randbelow(len(services))]
event = {
"timestamp": (base_time + timedelta(seconds=i)).isoformat(),
"level": level,
"service": service,
"message": f"Event {i} from {service}",
"request_id": f"req-{i:08d}",
"duration_ms": secrets.randbelow(4991) + 10, # 10-5000
"status_code": [200, 201, 400, 404, 500, 503][secrets.randbelow(6)],
}
if include_trace and level == "ERROR":
event["stack_trace"] = "\n".join(
[f" at module{j}.function{j}(file{j}.py:{secrets.randbelow(100) + 1})" for j in range(15)]
)
logs.append(event)
return logs
# ── Single-agent tools (test_context_overflow.py) ────────────────────────────
# State store: agent.state — scoped to one agent instance
# Flow: fetch_application_logs → analyze_error_patterns
# → detect_latency_anomalies
# → generate_incident_report
@tool(context=True)
def fetch_application_logs(
app_name: str,
tool_context: ToolContext,
hours: int = 24,
include_trace: bool = True,
) -> str:
"""Fetch application logs from monitoring system.
Returns large dataset of log events that cannot be truncated without
losing critical information for anomaly detection.
Args:
app_name: Application name to fetch logs for
hours: Number of hours of logs to fetch (default 24)
include_trace: Include full stack traces (default True)
"""
logs = _generate_log_events(app_name, hours, include_trace)
result_str = json.dumps(logs, indent=2)
# Store in agent.state and return pointer instead of flooding context
if len(result_str) > 20000:
pointer = f"logs-{app_name}"
tool_context.agent.state.set(pointer, logs)
return f"Fetched {len(logs)} log events for {app_name} ({len(result_str):,} bytes). Data stored at: {pointer}"
return result_str
@tool(context=True)
def analyze_error_patterns(logs_pointer: str, tool_context: ToolContext, threshold: int = 10) -> str:
"""Analyze error patterns in application logs.
Requires complete log dataset to detect patterns accurately.
Cannot work with truncated data.
Args:
logs_pointer: Memory pointer to log data in agent.state
threshold: Minimum occurrences to report (default 10)
"""
logs = tool_context.agent.state.get(logs_pointer)
if not logs:
return f"Error: Invalid pointer '{logs_pointer}'"
error_logs = [log for log in logs if log["level"] == "ERROR"]
service_errors = {}
for log in error_logs:
service = log["service"]
service_errors[service] = service_errors.get(service, 0) + 1
patterns = {
"total_errors": len(error_logs),
"error_rate": len(error_logs) / len(logs) * 100,
"by_service": service_errors,
"high_frequency": {svc: count for svc, count in service_errors.items() if count >= threshold},
}
return json.dumps(patterns, indent=2)
@tool(context=True)
def detect_latency_anomalies(logs_pointer: str, tool_context: ToolContext, percentile: int = 95) -> str:
"""Detect latency anomalies in application logs.
Requires full dataset to calculate accurate percentiles.
Args:
logs_pointer: Memory pointer to log data in agent.state
percentile: Percentile threshold for anomalies (default 95)
"""
logs = tool_context.agent.state.get(logs_pointer)
if not logs:
return f"Error: Invalid pointer '{logs_pointer}'"
durations = sorted([log["duration_ms"] for log in logs])
p_index = int(len(durations) * percentile / 100)
p_value = durations[p_index]
anomalies = [
{"timestamp": log["timestamp"], "service": log["service"], "duration_ms": log["duration_ms"], "request_id": log["request_id"]}
for log in logs
if log["duration_ms"] > p_value
]
return json.dumps({"total_requests": len(logs), "p95_latency_ms": p_value, "anomalies_count": len(anomalies), "anomalies": anomalies[:20]}, indent=2)
@tool
def generate_incident_report(error_analysis: str, latency_analysis: str) -> str:
"""Generate incident report from error and latency analysis results.
Args:
error_analysis: JSON string with error analysis results
latency_analysis: JSON string with latency analysis results
"""
try:
errors = json.loads(error_analysis)
latency = json.loads(latency_analysis)
except json.JSONDecodeError as e:
return f"Error: could not parse analysis results — {e}"
report = {
"report_generated": datetime.now().isoformat(),
"summary": {
"total_errors": errors.get("total_errors", 0),
"error_rate_percent": round(errors.get("error_rate", 0), 2),
"latency_anomalies": latency.get("anomalies_count", 0),
"p95_latency_ms": latency.get("p95_latency_ms", 0),
},
"recommendations": [],
}
if errors.get("error_rate", 0) > 5:
report["recommendations"].append("HIGH: Error rate exceeds 5% - investigate immediately")
if latency.get("anomalies_count", 0) > 100:
report["recommendations"].append("MEDIUM: High number of latency anomalies detected")
return json.dumps(report, indent=2)
# ── Swarm / multi-agent tools (swarm_demo.py) ─────────────────────────────────
# State store: invocation_state — shared across all agents in the swarm
# Flow: fetch_logs_swarm → analyze_errors_swarm + detect_latency_swarm
# → generate_report_swarm
# get_error_details_swarm (follow-up investigation)
@tool(context=True)
def fetch_logs_swarm(app_name: str, tool_context: ToolContext, hours: int = 6) -> str:
"""Fetch application logs and store in shared invocation_state for other agents.
Args:
app_name: Application name
hours: Hours of logs to fetch
"""
logs = _generate_log_events(app_name, hours, include_trace=True)
pointer = f"logs-{app_name}"
tool_context.invocation_state[pointer] = logs
size = len(json.dumps(logs))
return f"Fetched {len(logs)} events ({size:,} bytes). Stored as '{pointer}' in shared state. Hand off to analyzer."
@tool(context=True)
def analyze_errors_swarm(logs_pointer: str, tool_context: ToolContext) -> str:
"""Analyze error patterns from logs in shared invocation_state.
Args:
logs_pointer: Pointer key (e.g. 'logs-payment-service')
"""
logs = tool_context.invocation_state.get(logs_pointer)
if not logs:
return f"Error: '{logs_pointer}' not found in shared state"
errors = [l for l in logs if l["level"] == "ERROR"]
by_service = {}
for e in errors:
by_service[e["service"]] = by_service.get(e["service"], 0) + 1
result = {"total_errors": len(errors), "error_rate": round(len(errors) / len(logs) * 100, 2), "by_service": by_service}
tool_context.invocation_state["error_analysis"] = result
return f"Error analysis complete. {json.dumps(result, indent=2)}"
@tool(context=True)
def detect_latency_swarm(logs_pointer: str, tool_context: ToolContext) -> str:
"""Detect latency anomalies from logs in shared invocation_state.
Args:
logs_pointer: Pointer key (e.g. 'logs-payment-service')
"""
logs = tool_context.invocation_state.get(logs_pointer)
if not logs:
return f"Error: '{logs_pointer}' not found in shared state"
durations = sorted([l["duration_ms"] for l in logs])
p95 = durations[int(len(durations) * 0.95)]
anomalies_count = sum(1 for l in logs if l["duration_ms"] > p95)
result = {"total_requests": len(logs), "p95_latency_ms": p95, "anomalies_count": anomalies_count}
tool_context.invocation_state["latency_analysis"] = result
return f"Latency analysis complete. {json.dumps(result, indent=2)}"
@tool(context=True)
def generate_report_swarm(tool_context: ToolContext) -> str:
"""Generate incident report from analyses in shared invocation_state."""
errors = tool_context.invocation_state.get("error_analysis")
latency = tool_context.invocation_state.get("latency_analysis")
if not errors or not latency:
return "Error: need both error_analysis and latency_analysis in shared state first"
report = {
"report_generated": datetime.now().isoformat(),
"summary": {
"total_errors": errors["total_errors"], "error_rate": errors["error_rate"],
"p95_latency_ms": latency["p95_latency_ms"], "anomalies": latency["anomalies_count"],
},
"by_service": errors["by_service"],
"recommendations": [],
}
if errors["error_rate"] > 5:
report["recommendations"].append("HIGH: Error rate exceeds 5%")
if latency["anomalies_count"] > 20:
report["recommendations"].append("MEDIUM: High latency anomaly count")
return json.dumps(report, indent=2)
@tool(context=True)
def get_error_details_swarm(logs_pointer: str, tool_context: ToolContext, service: str = None, limit: int = 5) -> str:
"""Get detailed error logs for a specific service from shared invocation_state.
Args:
logs_pointer: Pointer key (e.g. 'logs-payment-service')
service: Service name to filter (e.g. 'cache-layer')
limit: Max errors to return
"""
logs = tool_context.invocation_state.get(logs_pointer)
if not logs:
return f"Error: '{logs_pointer}' not found in shared state"
errors = [l for l in logs if l["level"] == "ERROR"]
if service:
errors = [e for e in errors if e["service"] == service]
return json.dumps(errors[:limit], indent=2)