深入解析 AWS CLI 中 aws application-signals list-service-dependents:查询指定服务的调用方(Dependents)列表
【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli
本文以 list-service-dependents 官方示例 为主线,完整讲解aws application-signals list-service-dependents命令的参数、输出结构与实际输出样例,并结合 aws-cli 仓库内打包的 Application Signals API 模型(API 版本 2024-04-15)逐项印证参数约束、时间取整规则与分页机制。读完本文,你可以直接用该命令排查“在给定时间窗口内,到底有哪些服务、Synthetics 金丝雀或 RUM 客户端调用了我的服务”,并正确理解KeyAttributes、MaxResults、NextToken等关键参数。
1. ListServiceDependents 是做什么的
Application Signals(Amazon CloudWatch 的应用监控能力)通过自动发现应用拓扑,记录了实体之间的调用关系。list-service-dependents对应的 API 操作为ListServiceDependents,其官方定义(见 service-2.json 中operations.ListServiceDependents的documentation字段)为:
Returns the list of dependents that invoked the specified service during the provided time range. Dependents include other services, CloudWatch Synthetics canaries, and clients that are instrumented with CloudWatch RUM app monitors.
即:返回在指定时间范围内调用过某个服务的所有调用方(dependents)列表。这里的“调用方”不仅包括其他服务,还包括 CloudWatch Synthetics 金丝雀,以及接入了 CloudWatch RUM 应用监控的客户端。从 API 模型看,该操作还有几个值得注意的属性:
- HTTP 方法为
POST /service-dependents,成功响应码 200; - 标记为
"readonly": true,即只读操作,不会修改任何资源; - 可能抛出
ValidationException(参数校验失败)与ThrottlingException(请求限流)两类错误。
2. 官方示例命令逐行解读
官方示例文档 给出的完整命令如下:
aws application-signals list-service-dependents \ --start-time 1732021200 \ --end-time 1732107600 \ --key-attributes Environment=generic:default,Name=PetSite,Type=Service各参数含义如下:
| 参数 | 是否必填 | 说明 |
|---|---|---|
--start-time | 是 | 查询时间窗起点。CLI 按 epoch 秒传递(如1732021200)。API 模型明确说明“Your requested start time will be rounded to the nearest hour”——请求时间会被取整到最接近的整点 |
--end-time | 是 | 查询时间窗终点,同样是 epoch 秒(如1732107600),同样按整点取整 |
--key-attributes | 是 | 用来指定“要查询哪个服务”的字符串键值对。API 文档要求至少提供Type、Name、Environment三个属性 |
--max-results | 否 | 单次返回的最大结果数,缺省 50;模型约束为 1~100(见下文源码佐证) |
--next-token | 否 | 上一次调用返回的分页游标,传入后获取下一页结果 |
2.1--key-attributes的键值规则
KeyAttributes在 API 模型中是一个字符串到字符串的 map,其取值规则在 service-2.json 的ListServiceDependentsInput.KeyAttributes文档中写得很清楚,各键的用途互斥:
Type:标识对象类型。示例中使用Type=Service表示按“服务”定位目标;Name:对象名称,仅当Type为Service、RemoteService或AWS::Service时使用。示例中的Name=PetSite即目标服务名;Environment:对象所处的环境(服务部署位置/归属),示例中为generic:default;ResourceType:资源类型,仅当Type为Resource或AWS::Resource时使用;Identifier:资源对象的标识符,同样仅用于Resource/AWS::Resource类型。
从源码结构看,该 map 还有严格的基数约束:Attributesshape 定义"max": 4, "min": 1,即键值对数量必须为 1~4 个;键名KeyAttributeName的正则为[a-zA-Z]{1,50}(仅字母、最长 50),值KeyAttributeValue最长 1024。示例命令恰好传入了 3 个属性(Environment、Name、Type),符合最小必填集要求。
CLI 侧的Environment=generic:default,Name=PetSite,Type=Service写法是 aws-cli 的shorthand 语法(Key=Value逗号分隔的键值对简写),CLI 会将其解析为 API 要求的 map 结构后再发送。
2.2 时间参数的取整行为
示例中的两个时间戳均为 epoch 秒。API 模型对输入/输出时间的说明有一致且重要的细节:请求时间会被取整到整点,且输出中的StartTime/EndTime显示的是 Application Signals 实际采用的时间——“It might not match your request exactly, because it was rounded to the nearest hour.” 因此解读响应时应以输出中的起止时间为准,而非自己传入的原始值。
3. 完整输出结构与字段解析
官方示例给出的完整响应如下(原样继承自 示例文档):
{ "ServiceDependents": [{ "OperationName": "", "DependentKeyAttributes": { "Identifier": "pet-api-canary-hao", "ResourceType": "AWS::Synthetics::Canary", "Type": "AWS::Resource" }, "DependentOperationName": "", "MetricReferences": [] }, { "OperationName": "", "DependentKeyAttributes": { "Identifier": "PetSite", "ResourceType": "AWS::Synthetics::Canary", "Type": "AWS::Resource" }, "DependentOperationName": "", "MetricReferences": [] }], "StartTime": "2024-12-24T05:00:00+00:00", "EndTime": "2024-12-25T06:00:01+00:00" }对照 API 模型中ListServiceDependentsOutput的字段定义,各字段含义如下:
ServiceDependents(必填):调用方数组,模型约束为 0~100 个元素(ServiceDependentsshape 定义"max": 100)。示例中返回了两个调用方,均为 Synthetics 金丝雀——pet-api-canary-hao与PetSite,其Type为AWS::Resource、ResourceType为AWS::Synthetics::Canary。注意这里用的是Identifier而非Name,与第 2.1 节“Identifier仅用于AWS::Resource类型”的规则完全一致;OperationName:被调用实体上的操作名(模型约束 1~255 字符)。示例中为空字符串,因为金丝雀调用未携带操作粒度信息;DependentKeyAttributes:调用方自身的键值属性 map,规则与入参的KeyAttributes相同(1~4 个键值对);DependentOperationName:当调用方本身是一个服务、且从一个具体操作发起调用时,该操作名显示在此处;MetricReferences:Application Signals 为该调用方发现的 CloudWatch 指标引用数组。每个MetricReference必填Namespace、MetricType、MetricName三个字段,可选Dimensions(0~30 个维度)与AccountId。示例中该数组为空[],而相邻的 list-service-dependencies 示例 中则展示了非空的完整形态:Namespace=ApplicationSignals,MetricType分别为LATENCY/FAULT/ERROR,对应MetricName为Latency/Fault/Error,并带有Environment、Operation、RemoteEnvironment、RemoteOperation、RemoteService、Service六个维度。这说明:拿到MetricReferences后可以直接组合出 CloudWatch 指标查询条件;StartTime/EndTime(必填):服务端实际采用的时间窗口,按整点取整后回显。示例输出显示为2024-12-24T05:00:00+00:00至2024-12-25T06:00:01+00:00,注意返回的是 ISO 8601 格式,与输入时的 epoch 秒格式不同;NextToken(可选):当结果被截断时返回,用于翻页。
4. 源码级佐证:参数约束与分页实现
aws-cli 将 Application Signals 的服务模型完整打包在 awscli/botocore/data/application-signals/ 目录下(当前仓库收录的 API 版本为 2024-04-15,含 30 个操作)。从 service-2.json 可以确认以下实现事实:
必填参数:
ListServiceDependentsInput的"required"数组为["StartTime", "EndTime", "KeyAttributes"],与 CLI 必填参数一一对应。缺少任一必填参数会触发模型声明的ValidationException;MaxResults约束:ListServiceDependentsMaxResultsshape 定义为{"type": "integer", "box": true, "max": 100, "min": 1},即--max-results合法取值 1~100,缺省 50;分页定义:paginators-1.json 中明确注册了该操作的分页器:
"ListServiceDependents": { "input_token": "NextToken", "output_token": "NextToken", "limit_key": "MaxResults", "result_key": "ServiceDependents" }这带来两个实际用法:一是手动翻页,将上一次响应中的
NextToken作为下一次调用的--next-token传入;二是利用 aws-cli 的aws paginate子命令(该子命令正是读取paginators-1.json自动生成翻页逻辑),一次性拉取全部分页,无需手写 token 传递循环。
从源码结构看,result_key为ServiceDependents意味着paginate聚合时展开的正是该数组;limit_key为MaxResults意味着aws paginate application-signals list-service-dependents --max-items N可通过分页器内部自动换算每页大小。
5. 与 list-service-dependencies 的配合使用:上下游调用关系
排查调用链时,list-service-dependents(谁调用了我)通常需要与 list-service-dependencies 示例(我调用了谁)成对使用。两条命令的参数结构完全同构(--start-time、--end-time、--key-attributes及相同的分页参数),差异仅在结果方向:
list-service-dependents返回ServiceDependents数组,每项含DependentKeyAttributes/DependentOperationName;list-service-dependencies返回ServiceDependencies数组,每项含DependencyKeyAttributes/DependencyOperationName,且其MetricReferences通常非空(示例中返回了Latency、Fault、Error三条带完整维度的指标引用)。
因此一个典型的排查流程是:先用list-service-dependents锁定上游调用方,再用list-service-dependencies查看下游依赖及对应的ApplicationSignals命名空间指标,必要时把MetricReferences中的Namespace/MetricName/Dimensions直接用于 CloudWatch 指标查询做进一步下钻。
6. 使用注意事项小结
- 前提条件:目标服务必须已被 Application Signals 发现/接入(否则时间窗口内没有拓扑数据,
ServiceDependents可能为空数组——模型允许min: 0); - 时间格式:命令行传入 epoch 秒;输出回显为 ISO 8601 且按整点取整,核对窗口时以输出值为准;
- KeyAttributes 最小集:必须包含
Type、Name、Environment(服务类对象)或Type、Identifier、ResourceType(资源类对象),键值对总数不超过 4; - 分页:单页最多 100 条、默认 50 条;结果集较大时检查响应中是否出现
NextToken,并配合--next-token或aws paginate获取完整列表; - 限流:API 声明可能抛出
ThrottlingException,批量脚本中建议对 429 类错误做退避重试(aws-cli 内置的 retry 机制会自动处理部分限流场景)。
以上内容均以当前仓库中的示例文档与 API 模型为准:示例命令来自 awscli/examples/application-signals/list-service-dependents.rst,参数约束与分页器来自 awscli/botocore/data/application-signals/2024-04-15/service-2.json 与 awscli/botocore/data/application-signals/2024-04-15/paginators-1.json,可直接在仓库中查阅核对。
【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考