awslabs.cloudwatch-applicationsignals-mcp-server 0.1.21__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- awslabs/__init__.py +17 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/__init__.py +17 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/audit_presentation_utils.py +288 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/audit_utils.py +912 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/aws_clients.py +120 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/canary_utils.py +910 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ec2/ec2-dotnet-enablement.md +435 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ec2/ec2-java-enablement.md +321 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ec2/ec2-nodejs-enablement.md +420 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ec2/ec2-python-enablement.md +598 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ecs/ecs-dotnet-enablement.md +264 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ecs/ecs-java-enablement.md +193 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ecs/ecs-nodejs-enablement.md +198 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/ecs/ecs-python-enablement.md +236 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/eks/eks-dotnet-enablement.md +166 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/eks/eks-java-enablement.md +166 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/eks/eks-nodejs-enablement.md +166 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/eks/eks-python-enablement.md +169 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/lambda/lambda-dotnet-enablement.md +336 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/lambda/lambda-java-enablement.md +336 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/lambda/lambda-nodejs-enablement.md +336 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_guides/templates/lambda/lambda-python-enablement.md +336 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/enablement_tools.py +147 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/server.py +1505 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/service_audit_utils.py +231 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/service_tools.py +659 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/sli_report_client.py +333 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/slo_tools.py +386 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/trace_tools.py +784 -0
- awslabs/cloudwatch_applicationsignals_mcp_server/utils.py +172 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/METADATA +808 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/RECORD +36 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/WHEEL +4 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/entry_points.txt +2 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/licenses/LICENSE +174 -0
- awslabs_cloudwatch_applicationsignals_mcp_server-0.1.21.dist-info/licenses/NOTICE +2 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Service-specific utilities for service audit tool."""
|
|
16
|
+
|
|
17
|
+
from datetime import datetime, timezone
|
|
18
|
+
from loguru import logger
|
|
19
|
+
from typing import Any, List, Optional
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _ci_get(d: dict, *names) -> Optional[Any]:
|
|
23
|
+
"""Case-insensitive dictionary getter."""
|
|
24
|
+
for n in names:
|
|
25
|
+
if n in d:
|
|
26
|
+
return d[n]
|
|
27
|
+
lower = {k.lower(): v for k, v in d.items()}
|
|
28
|
+
for n in names:
|
|
29
|
+
if n.lower() in lower:
|
|
30
|
+
return lower[n.lower()]
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _need(d: dict, *names):
|
|
35
|
+
"""Get required field from dictionary."""
|
|
36
|
+
v = _ci_get(d, *names)
|
|
37
|
+
if v is None:
|
|
38
|
+
raise ValueError(f'Missing required field: one of {", ".join(names)}')
|
|
39
|
+
return v
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def coerce_service_target(t: dict) -> dict:
|
|
43
|
+
"""Convert common shorthand inputs into canonical service target.
|
|
44
|
+
|
|
45
|
+
Emits: {"Type":"service","Data":{"Service":{"Type":"Service","Name":...,"Environment":...,"AwsAccountId?":...}}}
|
|
46
|
+
|
|
47
|
+
Shorthands accepted:
|
|
48
|
+
{"Type":"service","Service":"<name>"}
|
|
49
|
+
{"Type":"service","Data":{"Service":"<name>"}}
|
|
50
|
+
{"Type":"service","Data":{"Service":{"Name":"<name>"}}}
|
|
51
|
+
{"target_type":"service","service":"<name>"}
|
|
52
|
+
"""
|
|
53
|
+
ttype = (_ci_get(t, 'Type', 'type', 'target_type') or '').lower()
|
|
54
|
+
if ttype != 'service':
|
|
55
|
+
raise ValueError('not a service target')
|
|
56
|
+
|
|
57
|
+
data = _ci_get(t, 'Data', 'data') or {}
|
|
58
|
+
service = _ci_get(data, 'Service', 'service') or _ci_get(t, 'Service', 'service')
|
|
59
|
+
|
|
60
|
+
if isinstance(service, str):
|
|
61
|
+
entity = {'Name': service}
|
|
62
|
+
elif isinstance(service, dict):
|
|
63
|
+
entity = dict(service)
|
|
64
|
+
elif isinstance(data, dict) and _ci_get(data, 'Name', 'name'):
|
|
65
|
+
entity = {'Name': _ci_get(data, 'Name', 'name')}
|
|
66
|
+
else:
|
|
67
|
+
raise ValueError("service target missing 'Service' payload")
|
|
68
|
+
|
|
69
|
+
if 'Type' not in entity and 'type' not in entity:
|
|
70
|
+
entity['Type'] = 'Service'
|
|
71
|
+
|
|
72
|
+
name = _ci_get(entity, 'Name', 'name')
|
|
73
|
+
env = _ci_get(entity, 'Environment', 'environment')
|
|
74
|
+
acct = _ci_get(entity, 'AwsAccountId', 'awsAccountId', 'aws_account_id')
|
|
75
|
+
|
|
76
|
+
out = {'Type': 'Service'}
|
|
77
|
+
if name:
|
|
78
|
+
out['Name'] = name
|
|
79
|
+
if env:
|
|
80
|
+
out['Environment'] = env
|
|
81
|
+
if acct:
|
|
82
|
+
out['AwsAccountId'] = acct
|
|
83
|
+
|
|
84
|
+
return {'Type': 'service', 'Data': {'Service': out}}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def normalize_service_entity(entity: dict) -> dict:
|
|
88
|
+
"""Normalize service entity structure."""
|
|
89
|
+
out = {
|
|
90
|
+
'Type': _ci_get(entity, 'Type', 'type') or 'Service',
|
|
91
|
+
'Name': _need(entity, 'Name', 'name'),
|
|
92
|
+
'Environment': _ci_get(entity, 'Environment', 'environment'),
|
|
93
|
+
}
|
|
94
|
+
acct = _ci_get(entity, 'AwsAccountId', 'awsAccountId', 'aws_account_id')
|
|
95
|
+
if acct:
|
|
96
|
+
out['AwsAccountId'] = acct
|
|
97
|
+
return out
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def normalize_service_target(item: dict) -> dict:
|
|
101
|
+
"""Normalize service target structure."""
|
|
102
|
+
data = _need(item, 'Data', 'data')
|
|
103
|
+
svc = _ci_get(data, 'Service', 'service')
|
|
104
|
+
svc_entity = normalize_service_entity(svc if isinstance(svc, dict) else data)
|
|
105
|
+
return {'Type': 'service', 'Data': {'Service': svc_entity}}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def normalize_service_targets(raw: List[dict]) -> List[dict]:
|
|
109
|
+
"""Normalize and validate service targets."""
|
|
110
|
+
if not isinstance(raw, list):
|
|
111
|
+
raise ValueError('`service_targets` must be a JSON array')
|
|
112
|
+
if len(raw) == 0:
|
|
113
|
+
raise ValueError('`service_targets` must contain at least 1 item')
|
|
114
|
+
|
|
115
|
+
out = []
|
|
116
|
+
for i, t in enumerate(raw, 1):
|
|
117
|
+
if not isinstance(t, dict):
|
|
118
|
+
raise ValueError(f'service_targets[{i}] must be an object')
|
|
119
|
+
|
|
120
|
+
maybe_type = (_ci_get(t, 'Type', 'type', 'target_type') or '').lower()
|
|
121
|
+
if maybe_type == 'service':
|
|
122
|
+
try:
|
|
123
|
+
t = coerce_service_target(t) # tolerant upgrade
|
|
124
|
+
except ValueError as e:
|
|
125
|
+
raise ValueError(f'service_targets[{i}] invalid service target: {e}')
|
|
126
|
+
|
|
127
|
+
ttype = (_ci_get(t, 'Type', 'type') or '').lower()
|
|
128
|
+
if ttype == 'service':
|
|
129
|
+
out.append(normalize_service_target(t))
|
|
130
|
+
else:
|
|
131
|
+
raise ValueError(
|
|
132
|
+
f"service_targets[{i}].type must be 'service' (this tool only handles service targets)"
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
return out
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def validate_and_enrich_service_targets(
|
|
139
|
+
normalized_targets: List[dict], applicationsignals_client, unix_start: int, unix_end: int
|
|
140
|
+
) -> List[dict]:
|
|
141
|
+
"""If a service target exists without Environment, fetch from the API.
|
|
142
|
+
|
|
143
|
+
NOTE: This function should only be called AFTER wildcard expansion has been completed.
|
|
144
|
+
Wildcard patterns should be expanded before calling this function.
|
|
145
|
+
"""
|
|
146
|
+
enriched_targets = []
|
|
147
|
+
|
|
148
|
+
for idx, t in enumerate(normalized_targets, 1):
|
|
149
|
+
target_type = (t.get('Type') or '').lower()
|
|
150
|
+
|
|
151
|
+
if target_type == 'service':
|
|
152
|
+
svc = (t.get('Data') or {}).get('Service') or {}
|
|
153
|
+
service_name = svc.get('Name')
|
|
154
|
+
|
|
155
|
+
# Check if this is still a wildcard pattern - this should not happen after proper expansion
|
|
156
|
+
if service_name and '*' in service_name:
|
|
157
|
+
raise ValueError(
|
|
158
|
+
f"service_targets[{idx}]: Wildcard pattern '{service_name}' found in validation phase. "
|
|
159
|
+
f'Wildcard expansion should have been completed before validation. '
|
|
160
|
+
f'This indicates an internal processing error.'
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
if not svc.get('Environment') and service_name:
|
|
164
|
+
# Fetch service details from API to get environment
|
|
165
|
+
logger.debug(f'Fetching environment for service: {service_name}')
|
|
166
|
+
try:
|
|
167
|
+
# Get all services to find the one we want
|
|
168
|
+
services_response = applicationsignals_client.list_services(
|
|
169
|
+
StartTime=datetime.fromtimestamp(unix_start, tz=timezone.utc),
|
|
170
|
+
EndTime=datetime.fromtimestamp(unix_end, tz=timezone.utc),
|
|
171
|
+
MaxResults=100,
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
# Find the service with matching name
|
|
175
|
+
target_service = None
|
|
176
|
+
for service in services_response.get('ServiceSummaries', []):
|
|
177
|
+
key_attrs = service.get('KeyAttributes', {})
|
|
178
|
+
if key_attrs.get('Name') == service_name:
|
|
179
|
+
target_service = service
|
|
180
|
+
break
|
|
181
|
+
|
|
182
|
+
if target_service:
|
|
183
|
+
key_attrs = target_service.get('KeyAttributes', {})
|
|
184
|
+
environment = key_attrs.get('Environment')
|
|
185
|
+
if environment:
|
|
186
|
+
# Enrich the service target with the found environment
|
|
187
|
+
enriched_svc = dict(svc)
|
|
188
|
+
enriched_svc['Environment'] = environment
|
|
189
|
+
enriched_target = {
|
|
190
|
+
'Type': 'service',
|
|
191
|
+
'Data': {'Service': enriched_svc},
|
|
192
|
+
}
|
|
193
|
+
enriched_targets.append(enriched_target)
|
|
194
|
+
logger.debug(
|
|
195
|
+
f'Enriched service {service_name} with environment: {environment}'
|
|
196
|
+
)
|
|
197
|
+
continue
|
|
198
|
+
else:
|
|
199
|
+
raise ValueError(
|
|
200
|
+
f"service_targets[{idx}]: Service '{service_name}' found but has no Environment. "
|
|
201
|
+
f'This service may not be properly configured in Application Signals.'
|
|
202
|
+
)
|
|
203
|
+
else:
|
|
204
|
+
raise ValueError(
|
|
205
|
+
f"service_targets[{idx}]: Service '{service_name}' not found in Application Signals. "
|
|
206
|
+
f'Use list_monitored_services() to see available services.'
|
|
207
|
+
)
|
|
208
|
+
except Exception as e:
|
|
209
|
+
if 'not found' in str(e) or 'Service' in str(e):
|
|
210
|
+
raise e # Re-raise our custom error messages
|
|
211
|
+
else:
|
|
212
|
+
raise ValueError(
|
|
213
|
+
f'service_targets[{idx}].Data.Service.Environment is required for service targets. '
|
|
214
|
+
f"Provide Environment (e.g., 'eks:top-observations/default') or ensure the service exists in Application Signals. "
|
|
215
|
+
f'API error: {str(e)}'
|
|
216
|
+
)
|
|
217
|
+
elif not svc.get('Environment'):
|
|
218
|
+
raise ValueError(
|
|
219
|
+
f'service_targets[{idx}].Data.Service.Environment is required for service targets. '
|
|
220
|
+
f"Provide Environment (e.g., 'eks:top-observations/default')."
|
|
221
|
+
)
|
|
222
|
+
else:
|
|
223
|
+
# Non-service targets should not be here since this tool only handles services
|
|
224
|
+
logger.warning(
|
|
225
|
+
f"Unexpected target type '{target_type}' in service audit tool - ignoring"
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
# Add the target as-is if it doesn't need enrichment
|
|
229
|
+
enriched_targets.append(t)
|
|
230
|
+
|
|
231
|
+
return enriched_targets
|