cloudwatch_Amazon CloudWatch指标示例

cloudwatch

Making sense of how Metrics are uniquely defined by a Name, a Namespace, and zero or more Dimensions.

了解如何通过名称,命名空间和零个或多个维度唯一定义度量标准。

I was surprised how long it took me to come to understand Amazon CloudWatch Metrics; thought to share my thinking.

我很惊讶我花了多长时间才了解Amazon CloudWatch指标; 想分享我的想法。

Metrics are the fundamental concept in CloudWatch. A metric represents a time-ordered set of data points that are published to CloudWatch. Think of a metric as a variable to monitor, and the data points as representing the values of that variable over time. For example, the CPU usage of a particular EC2 instance is one metric provided by Amazon EC2. The data points themselves can come from any application or business activity from which you collect data.…Metrics are uniquely defined by a name, a namespace, and zero or more dimensions.

指标是CloudWatch中的基本概念。 度量标准表示按时间顺序排列的一组数据点,这些数据点已发布到CloudWatch。 可以将指标视为要监视的变量,并将数据点表示为随时间推移该变量的值。 例如,特定EC2实例的CPU使用率是Amazon EC2提供的一项指标。 数据点本身可以​​来自您从中收集数据的任何应用程序或业务活动。…指标由名称,名称空间和零个或多个维度唯一定义。

— AWS — Amazon CloudWatch Concepts

— AWS — Amazon CloudWatch概念

It is relatively easy to understand how Names and Namespaces relate to each other and to Metrics; for example:

比较容易理解名称和命名空间之间的相互关系以及与度量之间的关系。 例如:

Image for post

Observations:

观察结果:

  • As Amazon CloudWatch is a regional service, Namespaces are unique within a Region, e.g., AWS/EC2 is distinct from CWAgent; Namespaces are also not hierarchical

    由于Amazon CloudWatch是区域服务,因此命名空间在区域内是唯一的,例如, AWS / EC2CWAgent不同 命名空间也不是分层的

  • Names are unique within a Namespace, e.g., CPUUtilization is distinct from DiskReadOps

    名称在名称空间内是唯一的,例如, CPUUtilizationDiskReadOps不同

  • Multiple Metrics, however, can share the same Name (this will make more sense shortly)

    但是,多个指标可以共享相同的名称(不久后,这将更有意义)
  • The AWS/EC2 Namespace is used by Amazon EC2 monitoring and CWAgent is used by Amazon CloudWatch Agent

    AWS / EC2命名空间所使用的亚马逊EC2监测CWAgent用于通过亚马逊CloudWatch的代理

note: It is interesting that the no-cost Amazon EC2 monitoring does not include either memory or disk utilization; for these, one must use Custom Metrics (starts at $0.30 per Custom Metric per month), e.g., generated by Amazon CloudWatch Agent.

注意 :有趣的是,免费的Amazon EC2监控不包括内存或磁盘使用率。 为此,您必须使用自定义指标(每个自定义指标每月0.30美元起),例如,由Amazon CloudWatch Agent生成。

The key to distinguishing between Metrics with the same Name are Dimensions:

区分名称相同的指标的关键是维度:

A dimension is a name/value pair that is part of the identity of a metric. You can assign up to 10 dimensions to a metric.

维是作为度量标识的一部分的名称/值对。 您最多可以为一个指标分配10个维度。

Every metric has specific characteristics that describe it, and you can think of dimensions as categories for those characteristics. Dimensions help you design a structure for your statistics plan. Because dimensions are part of the unique identifier for a metric, whenever you add a unique name/value pair to one of your metrics, you are creating a new variation of that metric.

每个度量标准都有描述它的特定特征,您可以将维度视为这些特征的类别。 维度可帮助您设计统计计划的结构。 因为维是度量标准唯一标识符的一部分,所以每当您向一个度量标准中添加唯一的名称/值对时,便会创建该度量标准的新变体。

— AWS — Amazon CloudWatch Concepts

— AWS — Amazon CloudWatch概念

Upon first reading this, I was confused. Looking back, part of my confusion arose due to the use of the word Dimension itself. IMHO, I think the term Label would have been a better choice. Let us look at some examples:

初读这篇文章时,我很困惑。 回顾过去,由于使用了Dimension本身一词,引起了我的部分困惑。 恕我直言,我认为标签一词会是一个更好的选择。 让我们看一些例子:

Image for post

Observations:

观察结果:

  • Under the mem_used_percent Name, Metrics are distinguished using three name/value pairs (Dimensions); with names InstanceId, ImageId, and InstanceType. Here the two Metrics refer to memory utilization of two different AWS EC2 Instances

    mem_used_percent名称下,使用三个名称/值对(维度)来区分指标。 名称为InstanceIdImageIdInstanceType。 这里的两个指标指的是两个不同的AWS EC2实例的内存利用率

  • If we were to change an Instance’s Type, Amazon EC2 monitoring would create a new Metric with the updated InstanceType Dimension (but with the same InstanceId). This, in particular, is understandable as mem_used_percent would not be comparable across different Instance Types (different memory values in the denominator)

    如果要更改实例的类型,Amazon EC2监视将使用更新的InstanceType维度(但具有相同的InstanceId )创建一个新的指标。 尤其可以理解,因为mem_used_percent在不同的实例类型(分母中的不同内存值)之间不具有可比性

  • Under the disk_used_percent, Metrics are distinguished using six Dimensions; with names InstanceId, ImageId, InstanceType, fstype, device and path. Here the two Metrics refer to disk utilization of two different volumes for the same AWS EC2 Instance; same InstanceId and different paths

    disk_used_percent下 ,使用六个维度来区分指标; 名称InstanceIdImageIdInstanceTypefstype设备路径 。 这里的两个指标指的是同一个AWS EC2实例的两个不同卷的磁盘利用率。 相同的InstanceId和不同的路径

note: It seems that the Metrics are labeled with some irrelevant (not useful in distinguishing Metrics) Dimensions, e.g., we would never have two metrics with the same InstanceId and different ImageId Dimensions (one cannot change an Instance’s AMI Id).

注意 :度量标准似乎标记有一些不相关的标签(在区分度量标准时无用),例如,我们永远不会拥有两个具有相同InstanceId和不同ImageId维度的度量标准(一个不能更改实例的AMI ID)。

note: Metrics, and their identifiers, can be listed using the AWS CLI tool, e.g.,

注意 :指标及其标识符可以使用AWS CLI工具列出,例如,

$ aws cloudwatch list-metrics \
--namespace AWS/EC2
{
"Metrics": [
{
"Namespace": "AWS/EC2",
"MetricName": "NetworkPacketsIn",
"Dimensions": [
{
"Name": "InstanceId",
"Value": "i-08d99a369f572dbdb"
}
]
},
...

Amazon EC2 monitoring generates aggregated Metrics across Amazon EC2 Instances with the same AMI Images, same Instance Types, and across all Instances. This is accomplished by using appropriate Dimensions:

Amazon EC2监控会在具有相同AMI映像,相同实例类型和所有实例的Amazon EC2实例之间生成聚合指标。 这可以通过使用适当的尺寸来实现:

note: This is something specific to how Amazon EC2 monitoring generates Metrics; i.e,. it is not something that Amazon CloudWatch does automatically.

注意 :这是Amazon EC2监控如何生成指标的特定内容; 即。 它不是Amazon CloudWatch自动执行的操作。

Image for post

Observations:

观察结果:

  • The Metrics with an InstanceId Dimension are the Per-Instance Metrics (as shown in the AWS Console)

    具有InstanceId维度的指标是按实例指标 (如AWS控制台所示)

  • The Metrics with an ImageId Dimension are the By Image (AMI) Id Metrics

    具有ImageId维度的指标是按图像(AMI)ID指标

  • The Metrics with the InstanceType Dimension are the Aggregated by Instance Type Metrics

    具有InstanceType维度的指标按实例类型指标进行汇总

  • The Metrics with no Dimensions (EMPTY) are the Across All Instances Metrics

    没有维度的指标( EMPTY )是所有实例指标

Now that we know how to uniquely identify a Metric by supplying a Namespace, Name, and Dimensions, let us explore how to calculate statistics across Data Points in a Metric.

现在,我们知道了如何通过提供名称空间,名称和维度来唯一标识度量标准,让我们探索如何在度量标准中跨数据点计算统计信息。

First, we calculate an average across all Data Points in five minute periods over the course of an hour for a CPU utilization Per-Instance Metric using the AWS CLI tool.

首先,我们使用AWS CLI工具在一个小时的过程中,在五分钟内计算所有数据点的平均值,以得出每个实例的CPU利用率指标。

$ aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-08d99a369f572dbdb \
--start-time 2020-08-11T16:00:00Z \
--end-time 2020-08-11T17:00:00Z \
--period 300 \
--statistics Average

{
"Label": "CPUUtilization",
"Datapoints": [
{
"Timestamp": "2020-08-11T16:30:00+00:00",
"Average": 0.0666666666666676,
"Unit": "Percent"
},
...

Observations:

观察结果:

  • We are required to provide all of the identifiers, Namespace, Name, and Dimensions to uniquely identify a single Metric

    我们需要提供所有标识符,名称空间,名称和维度,以唯一地标识单个指标
  • Here we get twelve (60 divided by 5) values representing the five minute periods over an hour

    在这里,我们获得了十二个值(60除以5)代表一个小时的五个分钟周期
  • While one cannot access the raw Data Points themselves, one can effectively get at them by calculating any statistic, e.g., Average, over a period that matches up with the period of the service generating the Data Points in the Metric (there is only one value is used to calculate the statistic)

    尽管无法访问原始数据点本身,但可以通过在与度量标准中生成数据点的服务周期相匹配的时间段内计算任何统计数据(例如, 平均值)来有效地获取它们(只有一个值用于计算统计信息)

We get the same data using the Amazon CloudWatch service in the AWS Console:

我们使用AWS控制台中的Amazon CloudWatch服务获得相同的数据:

Image for post

note: After exploring the Basic (recorded every five minutes) Metrics generated by Amazon EC2 monitoring, I noticed that it records five Data Points at the same point in time every five minutes (I expected one). We can see this by using the SampleCount (number of Data Points in period) statistic. This threw me off for quite some time.

注意 :在探索了由Amazon EC2监控生成的基本(每五分钟记录一次)指标之后,我注意到它每五分钟在同一时间点记录五个数据点(我希望一个)。 我们可以通过使用SampleCount (周期中的数据点数)统计信息来看到这一点。 这让我离开了很长一段时间。

Image for post

Wrap Up

结语

Nothing too fancy here, but at least Dimensions make sense to me now.

这里没什么好看的,但是至少Dimensions现在对我有意义。

翻译自: https://codeburst.io/amazon-cloudwatch-metrics-by-example-249826b1404d

cloudwatch

好的,以下是一个简单的 Java 代码示例,用于将 CloudWatch Logs 事件发送到 Lambda 表达式进行处理: 首先,在 AWS Lambda 控制台中创建一个新的 Lambda 函数,并将运行时设置为 Java 8 或 Java 11。 然后,将以下代码复制并粘贴到 Lambda 函数的代码编辑器中: ```java import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent; public class CloudWatchLogsHandler implements RequestHandler<CloudWatchLogsEvent, Void> { public Void handleRequest(CloudWatchLogsEvent event, Context context) { // 从 CloudWatch Logs 事件中获取日志数据 String logData = event.getAwsLogs().getData(); // 在 Lambda 日志中打印日志数据 context.getLogger().log("Received log data: " + logData); // 在这里可以编写自己的日志处理逻辑 return null; } } ``` 此代码使用 AWS SDK for Java v2 中提供的 `RequestHandler` 接口来处理 CloudWatch Logs 事件。当 Lambda 函数被 CloudWatch 触发时,它将调用 `handleRequest` 方法,并将 `CloudWatchLogsEvent` 对象作为输入参数传递给该方法。 在 `handleRequest` 方法中,我们可以使用 `event.getAwsLogs().getData()` 方法获取传入的日志数据。然后,我们可以在 Lambda 日志中打印这些数据,并在这里编写自己的日志处理逻辑。 最后,在 Lambda 控制台中将此函数部署为一个新的函数,并将 CloudWatch Logs 触发器添加到该函数中以将日志数据发送到此函数进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值