帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub
这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
从 Amazon CloudWatch Watch 获取指标
此 Node.js 代码示例演示:
如何检索已发布 CloudWatch 指标的列表。
如何将数据点发布到 CloudWatch 指标。
场景
指标是关于您的系统性能的数据。您可以启用对某些资源(例如 Amazon EC2 实例)或您自己的应用程序指标的详细监控。
在本示例中,使用一系列 Node.js 模块从 CloudWatch 获取指标并将事件发送到 Amazon CloudWatch Events。Node.js 模块使用适用于 JavaScript 的开发工具包,通过以下方法从 CloudWatch 获取指标。CloudWatch
客户端类:
有关 CloudWatch 指标的更多信息,请参阅 Amazon CloudWatch 用户指南 中的使用 Amazon CloudWatch 指标。
先决条件任
要设置和运行此示例,您必须先完成以下任务:
-
设置项目环境以运行这些 Node TypeScript 示例,然后安装所需的AWS SDK for JavaScript和第三方模块。按照上的说明操作GitHub
. 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅从共享凭证文件加载 Node.js 中的凭证。
这些示例使用 ECMASCRIPT6 (ES6)。这需要 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅。Node.js 下载内容。
但是,如果你更喜欢使用 CommonJS 语法,请参阅JavaScript ES6/常用JS 语法
列出指标
创建libs
创建文件名为的 Node.js 模块cloudWatchClient.js
. 将下面的代码复制并粘贴到其中,这将创建 CloudWatch 客户端对象。Replace领域
使用您的AWSregion region。
import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });
这个代码是可用的GitHub 上的位置
创建文件名为 listMetrics.js
的 Node.js 模块。确保按前面所示配置开发工具包,包括下载 CloudWatch 客户端。创建一个包含列出指标所需的参数的 JSON 对象。调用 ListMetricsCommand
方法以列出 IncomingLogEvents
指标。
// Import required AWS SDK clients and commands for Node.js import { ListMetricsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { Dimensions: [ { Name: "LogGroupName" /* required */, }, ], MetricName: "IncomingLogEvents", Namespace: "AWS/Logs", }; export const run = async () => { try { const data = await cwClient.send(new ListMetricsCommand(params)); console.log("Success. Metrics:", JSON.stringify(data.Metrics)); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
要运行示例,请在命令提示符处输入以下内容。
node listMetrics.js
可以找到这个示例代码GitHub 上的位置
提交自定义指标
创建libs
创建文件名为的 Node.js 模块cloudWatchClient.js
. 将下面的代码复制并粘贴到其中,这将创建 CloudWatch 客户端对象。Replace领域
使用您的AWSregion region。
import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });
这个代码是可用的GitHub 上的位置
创建文件名为 putMetricData.js
的 Node.js 模块。确保按前面所示配置开发工具包,包括下载 CloudWatch 客户端。创建一个 JSON 对象,其中包含为 PAGES_VISITED
自定义指标提交数据点所需的参数。调用 PutMetricDataCommand
方法。
// Import required AWS SDK clients and commands for Node.js import { PutMetricDataCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { MetricData: [ { MetricName: "PAGES_VISITED", Dimensions: [ { Name: "UNIQUE_PAGES", Value: "URLS", }, ], Unit: "None", Value: 1.0, }, ], Namespace: "SITE/TRAFFIC", }; export const run = async () => { try { const data = await cwClient.send(new PutMetricDataCommand(params)); console.log("Success", data.$metadata.requestId); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
要运行示例,请在命令提示符处输入以下内容。
node putMetricData.js
可以找到这个示例代码GitHub 上的位置