使用AWS Web控制台,只需导航到API的相关阶段并勾选CloudWatch Settings下的框。 但是我使用aws-sdk来构建我的API,我看不到以编程方式执行此操作。在createStage,createMethod或我看过的任何其他地方都没有。 我可以使用aws-sdk为我的API启用CloudWatch吗?
答案 0 :(得分:1)
假设您正在使用Java SDK,请按照以下步骤进行操作:
RestApi restApi = amazonApiGateway.getRestApis()。getItem()。get(0)
Stage stage = restApi.getStageByName(“”);
PatchDocument patchDocument = PatchUtils.createPatchDocument( PatchUtils.createReplaceOperation(“/ * / * / metrics / enabled”,“true”), PatchUtils.createReplaceOperation(“/ * / * / logging / dataTrace”,“true”), PatchUtils.createReplaceOperation(“/ * / * / logging / loglevel”,“INFO”) ); stage.updateStage(patchDocument);
答案 1 :(得分:0)
在aws-java-sdk-api-gateway中,版本:' 1.11.264'
经过大量时间和Patrice的帮助,我能够使用以下代码启动CloudWatch Logging。
如果要导入yaml / json格式的swagger来创建API,则在导入完成后,您将获得apiID,然后部署它,然后根据需要更新舞台设置。因此,下面的代码从部署过程开始 - 一旦部署,您就可以更改阶段设置
private void deployAPI(String name, String apiID) throws IOException, InterruptedException {
CreateDeploymentRequest createDeploymentRequest = new CreateDeploymentRequest();
createDeploymentRequest.setRestApiId(apiID);
createDeploymentRequest.setStageName(getEnvironment());
for (int i = 0; i < 5; i++) {
try {
client.createDeployment(createDeploymentRequest);
break;
} catch (TooManyRequestsException e) {
log.error(e);
sleepToAvoidAWSTooManyRequestException();
}
}
log.info("API deployed : " + apiID);
System.out.println("API Stage Updating");
client.updateStage(new UpdateStageRequest().withRestApiId(apiID).withStageName(getEnvironment())
.withPatchOperations(getpatchOperations()));
System.out.println("API Stage Updated");
}
private List<PatchOperation> getpatchOperations() {
List<PatchOperation> list = new ArrayList<PatchOperation>();
list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/metrics/enabled").withValue("true"));
list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/logging/dataTrace").withValue("true"));
list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/logging/loglevel").withValue("ERROR"));
return list;
}