

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CloudWatch 指標
<a name="cloudwatch-metrics-component"></a>

Amazon CloudWatch 指標元件 (`aws.greengrass.Cloudwatch`) 會將自訂指標從 Greengrass 核心裝置發佈至 Amazon CloudWatch。元件可讓元件發佈 CloudWatch 指標，您可以用來監控和分析 Greengrass 核心裝置的環境。如需詳細資訊，請參閱《[Amazon CloudWatch 使用者指南》中的使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com//AmazonCloudWatch/latest/monitoring/working_with_metrics.html)。 *Amazon CloudWatch *

若要使用此元件發佈 CloudWatch 指標，請將訊息發佈至此元件訂閱的主題。根據預設，此元件會訂閱`cloudwatch/metric/put`[本機發佈/訂閱](ipc-publish-subscribe.md)主題。您可以在部署此元件時指定其他主題，包括 AWS IoT Core MQTT 主題。

此元件會批次處理位於相同命名空間中的指標，並定期將其發佈至 CloudWatch。

**注意**  
此元件提供與 AWS IoT Greengrass V1 中 CloudWatch 指標連接器類似的功能。如需詳細資訊，請參閱《*AWS IoT Greengrass V1 開發人員指南*》中的 [CloudWatch 指標連接器](https://docs.aws.amazon.com/greengrass/latest/developerguide/cloudwatch-metrics-connector.html)。

**Topics**
+ [版本](#cloudwatch-metrics-component-versions)
+ [Type](#cloudwatch-metrics-component-type)
+ [作業系統](#cloudwatch-metrics-component-os-support)
+ [要求](#cloudwatch-metrics-component-requirements)
+ [相依性](#cloudwatch-metrics-component-dependencies)
+ [Configuration](#cloudwatch-metrics-component-configuration)
+ [輸入資料](#cloudwatch-metrics-component-input-data)
+ [輸出資料](#cloudwatch-metrics-component-output-data)
+ [授權](#cloudwatch-metrics-component-licenses)
+ [本機日誌檔案](#cloudwatch-metrics-component-log-file)
+ [變更記錄](#cloudwatch-metrics-component-changelog)
+ [另請參閱](#cloudwatch-metrics-component-see-also)

## 版本
<a name="cloudwatch-metrics-component-versions"></a>

此元件具有下列版本：
+ 4.0.x
+ 3.2.x
+ 3.1.x 版本
+ 3.0.x
+ 2.1.x
+ 2.0.x

如需有關每個元件版本變更的資訊，請參閱[變更日誌](#cloudwatch-metrics-component-changelog)。

## Type
<a name="cloudwatch-metrics-component-type"></a>

------
#### [ v4.x - v3.x ]

<a name="public-component-type-generic"></a>此<a name="public-component-type-generic-phrase"></a>元件是一般元件 (`aws.greengrass.generic`)。[Greengrass 核](greengrass-nucleus-component.md)會執行元件的生命週期指令碼。

------
#### [ v2.x ]

<a name="public-component-type-lambda"></a>此<a name="public-component-type-lambda-phrase"></a>元件是 Lambda 元件 (`aws.greengrass.lambda`)。[Greengrass 核](greengrass-nucleus-component.md)會使用 Lambda [啟動器元件執行此元件的 Lambda](lambda-launcher-component.md) 函數。

------

<a name="public-component-type-more-information"></a>如需詳細資訊，請參閱[元件類型](develop-greengrass-components.md#component-types)。

## 作業系統
<a name="cloudwatch-metrics-component-os-support"></a>

------
#### [ v4.x - v3.x ]

此元件可以安裝在執行下列作業系統的核心裝置上：
+ Linux
+ Windows

------
#### [ v2.x ]

此元件只能在 Linux 核心裝置上安裝。

------

## 要求
<a name="cloudwatch-metrics-component-requirements"></a>

此元件有下列需求：

------
#### [ v4.x - v3.x ]
+ <a name="public-component-python3-requirement"></a>安裝在核心裝置上並新增至 PATH 環境變數的 [Python](https://www.python.org/) 3.7 版。
+ 必須安裝下列 Python 程式庫，包括任何相依性，並可供執行 元件的使用者使用：
  + [AWS IoT Device SDK 適用於 Python 的 v2](https://github.com/aws/aws-iot-device-sdk-python-v2)
  + [適用於 Python (Boto3) 的 AWS SDK](http://boto.readthedocs.org/en/latest/ref/)
**注意**  
您可以將`UseInstaller`組態設定為 `true`，將這些程式庫自動安裝到提供的 Python 環境中。
+ [Greengrass 裝置角色](device-service-role.md)必須允許 `cloudwatch:PutMetricData`動作，如下列範例 IAM 政策所示。

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Action": [
          "cloudwatch:PutMetricData"
        ],
        "Effect": "Allow",
        "Resource": "*"
      }
    ]
  }
  ```

------

  如需詳細資訊，請參閱《[Amazon CloudWatch 使用者指南》中的 Amazon CloudWatch 許可參考](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/permissions-reference-cw.html)。 *Amazon CloudWatch *

------
#### [ 2.x ]
+ <a name="core-device-lambda-function-requirements"></a>您的核心裝置必須符合執行 Lambda 函數的要求。如果您希望核心裝置執行容器化 Lambda 函數，則裝置必須符合執行此作業的要求。如需詳細資訊，請參閱[Lambda 函數需求](setting-up.md#greengrass-v2-lambda-requirements)。
+ <a name="public-component-python3-requirement"></a>安裝在核心裝置上並新增至 PATH 環境變數的 [Python](https://www.python.org/) 3.7 版。
+ [Greengrass 裝置角色](device-service-role.md)必須允許 `cloudwatch:PutMetricData`動作，如下列範例 IAM 政策所示。

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Action": [
          "cloudwatch:PutMetricData"
        ],
        "Effect": "Allow",
        "Resource": "*"
      }
    ]
  }
  ```

------

  如需詳細資訊，請參閱《[Amazon CloudWatch 使用者指南》中的 Amazon CloudWatch 許可參考](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/permissions-reference-cw.html)。 *Amazon CloudWatch *
+ <a name="connector-component-legacy-subscription-router-dependency"></a>若要從此元件接收輸出資料，您必須在部署此元件時合併[舊版訂閱路由器元件](legacy-subscription-router-component.md) (`aws.greengrass.LegacySubscriptionRouter`) 的下列組態更新。此組態會指定此元件發佈回應的主題。

------
#### [ Legacy subscription router v2.1.x ]

  ```
  {
    "subscriptions": {
      "aws-greengrass-cloudwatch": {
        "id": "aws-greengrass-cloudwatch",
        "source": "component:aws.greengrass.Cloudwatch",
        "subject": "cloudwatch/metric/put/status",
        "target": "cloud"
      }
    }
  }
  ```

------
#### [ Legacy subscription router v2.0.x ]

  ```
  {
    "subscriptions": {
      "aws-greengrass-cloudwatch": {
        "id": "aws-greengrass-cloudwatch",
        "source": "arn:aws:lambda:region:aws:function:aws-greengrass-cloudwatch:version",
        "subject": "cloudwatch/metric/put/status",
        "target": "cloud"
      }
    }
  }
  ```<a name="connector-component-legacy-subscription-router-dependency-replace"></a>
  + 以您使用 AWS 區域 的 取代*區域*。
  + ** 以此元件執行的 Lambda 函數版本取代 版本。若要尋找 Lambda 函數版本，您必須檢視要部署之此元件版本的配方。在[AWS IoT Greengrass 主控台](https://console.aws.amazon.com//greengrass)中開啟此元件的詳細資訊頁面，並尋找 **Lambda 函數**鍵值對。此鍵/值對包含 Lambda 函數的名稱和版本。

**重要**  <a name="connector-component-legacy-subscription-router-dependency-note"></a>
每次部署此元件時，都必須更新舊版訂閱路由器上的 Lambda 函數版本。這可確保您為部署的元件版本使用正確的 Lambda 函數版本。

------

  <a name="connector-component-create-deployments"></a>如需詳細資訊，請參閱[建立部署](create-deployments.md)。

------

### 端點和連接埠
<a name="cloudwatch-metrics-component-endpoints"></a>

除了基本操作所需的端點和連接埠之外，此元件還必須能夠對下列端點和連接埠執行傳出請求。如需詳細資訊，請參閱[允許透過代理或防火牆的裝置流量](allow-device-traffic.md)。


| Endpoint | 站點 | 必要 | Description | 
| --- | --- | --- | --- | 
|  `monitoring.region.amazonaws.com`  | 443 | 是 |  上傳 CloudWatch 指標。  | 

## 相依性
<a name="cloudwatch-metrics-component-dependencies"></a>

當您部署元件時， AWS IoT Greengrass 也會部署其相依性的相容版本。這表示您必須符合元件及其所有相依性的要求，才能成功部署元件。本節列出此元件[發行版本的](#cloudwatch-metrics-component-changelog)相依性，以及為每個相依性定義元件版本的語意版本限制。您也可以在 [AWS IoT Greengrass 主控台](https://console.aws.amazon.com//greengrass)中檢視每個版本元件的相依性。在元件詳細資訊頁面上，尋找**相依性**清單。

------
#### [ 4.0.0 - 3.0.0 ]

下表列出此元件 4.0.0 到 3.0.0 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <3.0.0 | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | >=0.0.0 | 硬式 | 

------
#### [ 2.1.4 - 2.1.9 ]

下表列出此元件 2.1.4 至 2.1.9 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <3.0.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [字符交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.1.4 - 2.1.8 ]

下表列出此元件 2.1.4 和 2.1.8 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <3.0.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.1.2 - 2.1.3 ]

下表列出此元件 2.1.2 和 2.1.3 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.8.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.1.1 ]

下表列出此元件 2.1.1 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.7.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [字符交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.8 - 2.1.0 ]

下表列出此元件 2.0.8 至 2.1.0 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.6.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.7 ]

下表列出此元件 2.0.7 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.5.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.6 ]

下表列出此元件 2.0.6 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.4.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.5 ]

下表列出此元件 2.0.5 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.3.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [字符交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.4 ]

下表列出此元件 2.0.4 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.0 <2.2.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | ^2.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | ^2.0.0  | 軟式 | 
| [權杖交換服務](token-exchange-service-component.md) | ^2.0.0  | 硬式 | 

------
#### [ 2.0.3 ]

下表列出此元件 2.0.3 版的相依性。


| 相依性 | 相容版本 | 相依性類型 | 
| --- | --- | --- | 
| [Greengrass 核](greengrass-nucleus-component.md) | >=2.0.3 <2.1.0  | 硬式 | 
| [Lambda 啟動器](lambda-launcher-component.md) | >=1.0.0  | 硬式 | 
| [Lambda 執行階段](lambda-runtimes-component.md) | >=1.0.0  | 軟式 | 
| [字符交換服務](token-exchange-service-component.md) | >=1.0.0  | 硬式 | 

------

如需元件相依性的詳細資訊，請參閱[元件配方參考](component-recipe-reference.md#recipe-reference-component-dependencies)。

## Configuration
<a name="cloudwatch-metrics-component-configuration"></a>

此元件提供下列組態參數，您可以在部署元件時自訂這些參數。

------
#### [ v4.x ]

 `PublishInterval`   
（選用） 在元件發佈指定命名空間的批次指標之前等待的秒數上限。若要設定元件在接收指標時發佈指標，這表示無需批次處理，請指定 `0`。  
元件在相同命名空間中收到 20 個指標後，或在您指定的間隔後，會發佈至 CloudWatch。  
元件不會指定事件發佈的順序。
此值最多可達 900 秒。  
預設：10 秒

 `MaxMetricsToRetain`   
（選用） 元件將指標取代為較新的指標之前，所有命名空間中要儲存在記憶體中的指標數量上限。  
當核心裝置沒有網際網路連線時，就會套用此限制，因此元件會緩衝稍後發佈的指標。當緩衝區已滿時，元件會將最舊的指標取代為較新的指標。指定命名空間中的指標只會取代相同命名空間中的指標。  
如果元件的主機程序中斷，則元件不會儲存指標。例如，這可能會在部署期間或核心裝置重新啟動時發生。
此值必須至少為 2，000 個指標。  
預設：5，000 個指標

`InputTopic`  
（選用） 元件訂閱接收訊息的主題。如果您`true`為 指定 `PubSubToIoTCore`，則可以在本主題中使用 MQTT 萬用字元 (\$1 和 \$1)。  
預設：`cloudwatch/metric/put`

`OutputTopic`  
（選用） 元件發佈狀態回應的主題。  
預設：`cloudwatch/metric/put/status`

`PubSubToIoTCore`  
（選用） 字串值，定義是否發佈和訂閱 AWS IoT Core MQTT 主題。支援的值為 `true` 和 `false`。  
預設：`false`

`LogLevel`  
（選用） 元件的記錄層級。從下列日誌層級中進行選擇，此處依層級順序列出：  
+ `DEBUG`
+ `INFO`
+ `WARNING`
+ `ERROR`
+ `CRITICAL`
預設：`INFO`

 `UseInstaller`   
（選用） 布林值，定義是否使用安裝程式指令碼，將此元件的 Python 相依性安裝到提供的 Python 環境中。  
將此值設定為 `true`，以自動安裝此元件的 Python 相依性。設為 時`false`，您必須安裝下列程式庫，包括任何相依性，並將其提供給執行元件的使用者的 Python 環境。  
+ [AWS IoT Device SDK 適用於 Python 的 v2](https://github.com/aws/aws-iot-device-sdk-python-v2)
+ [適用於 Python (Boto3) 的 AWS SDK](http://boto.readthedocs.org/en/latest/ref/)
預設：`false`

`PublishRegion`  
（選用） 要發佈 CloudWatch 指標 AWS 區域 的 。此值會覆寫核心裝置的預設區域。只有跨區域指標需要此參數。

`accessControl`  
（選用） 包含[授權政策](interprocess-communication.md#ipc-authorization-policies)的物件，允許元件發佈和訂閱指定的主題。如果您為 `InputTopic`和 指定自訂值`OutputTopic`，則必須更新此物件中的資源值。  
預設：  

```
{
  "aws.greengrass.ipc.pubsub": {
    "aws.greengrass.Cloudwatch:pubsub:1": {
      "policyDescription": "Allows access to subscribe to input topics.",
      "operations": [
        "aws.greengrass#SubscribeToTopic"
      ],
      "resources": [
        "cloudwatch/metric/put"
      ]
    },
    "aws.greengrass.Cloudwatch:pubsub:2": {
      "policyDescription": "Allows access to publish to output topics.",
      "operations": [
        "aws.greengrass#PublishToTopic"
      ],
      "resources": [
        "cloudwatch/metric/put/status"
      ]
    }
  },
  "aws.greengrass.ipc.mqttproxy": {
    "aws.greengrass.Cloudwatch:mqttproxy:1": {
      "policyDescription": "Allows access to subscribe to input topics.",
      "operations": [
        "aws.greengrass#SubscribeToIoTCore"
      ],
      "resources": [
        "cloudwatch/metric/put"
      ]
    },
    "aws.greengrass.Cloudwatch:mqttproxy:2": {
      "policyDescription": "Allows access to publish to output topics.",
      "operations": [
        "aws.greengrass#PublishToIoTCore"
      ],
      "resources": [
        "cloudwatch/metric/put/status"
      ]
    }
  }
}
```

**Example 範例：組態合併更新**  

```
{
  "PublishInterval": 0,
  "PubSubToIoTCore": true
}
```

------
#### [ v3.x ]

 `PublishInterval`   
（選用） 在元件發佈指定命名空間的批次指標之前等待的秒數上限。若要設定元件在接收指標時發佈指標，這表示無需批次處理，請指定 `0`。  
元件在相同命名空間中收到 20 個指標後，或在您指定的間隔後，會發佈至 CloudWatch。  
元件不會指定事件發佈的順序。
此值最多可達 900 秒。  
預設：10 秒

 `MaxMetricsToRetain`   
（選用） 元件將指標取代為較新的指標之前，所有命名空間中要儲存在記憶體中的指標數量上限。  
當核心裝置沒有網際網路連線時，就會套用此限制，因此元件會緩衝稍後發佈的指標。當緩衝區已滿時，元件會將最舊的指標取代為較新的指標。指定命名空間中的指標只會取代相同命名空間中的指標。  
如果元件的主機程序中斷，則元件不會儲存指標。例如，這可能會在部署期間或核心裝置重新啟動時發生。
此值必須至少為 2，000 個指標。  
預設：5，000 個指標

`InputTopic`  
（選用） 元件訂閱接收訊息的主題。如果您`true`為 指定 `PubSubToIoTCore`，則可以在本主題中使用 MQTT 萬用字元 (\$1 和 \$1)。  
預設：`cloudwatch/metric/put`

`OutputTopic`  
（選用） 元件發佈狀態回應的主題。  
預設：`cloudwatch/metric/put/status`

`PubSubToIoTCore`  
（選用） 字串值，定義是否發佈和訂閱 AWS IoT Core MQTT 主題。支援的值為 `true` 和 `false`。  
預設：`false`

`LogLevel`  
（選用） 元件的記錄層級。從下列日誌層級中進行選擇，此處依層級順序列出：  
+ `DEBUG`
+ `INFO`
+ `WARNING`
+ `ERROR`
+ `CRITICAL`
預設：`INFO`

`UseInstaller`  
（選用） 布林值，定義是否使用安裝程式指令碼，將此元件的 Python 相依性安裝到提供的 Python 環境中。  
將此值設定為 `false`，以使用自訂指令碼或手動安裝相依性。設定為 時`false`，您必須安裝下列程式庫，包括任何相依性，並將其提供給執行元件的使用者的 Python 環境。  
+ [AWS IoT Device SDK 適用於 Python 的 v2](https://github.com/aws/aws-iot-device-sdk-python-v2)
+ [適用於 Python (Boto3) 的 AWS SDK](http://boto.readthedocs.org/en/latest/ref/)
預設：`true`

`PublishRegion`  
（選用） 要發佈 CloudWatch 指標 AWS 區域 的 。此值會覆寫核心裝置的預設區域。只有跨區域指標需要此參數。

`accessControl`  
（選用） 包含[授權政策](interprocess-communication.md#ipc-authorization-policies)的物件，允許元件發佈和訂閱指定的主題。如果您為 `InputTopic`和 指定自訂值`OutputTopic`，則必須更新此物件中的資源值。  
預設：  

```
{
  "aws.greengrass.ipc.pubsub": {
    "aws.greengrass.Cloudwatch:pubsub:1": {
      "policyDescription": "Allows access to subscribe to input topics.",
      "operations": [
        "aws.greengrass#SubscribeToTopic"
      ],
      "resources": [
        "cloudwatch/metric/put"
      ]
    },
    "aws.greengrass.Cloudwatch:pubsub:2": {
      "policyDescription": "Allows access to publish to output topics.",
      "operations": [
        "aws.greengrass#PublishToTopic"
      ],
      "resources": [
        "cloudwatch/metric/put/status"
      ]
    }
  },
  "aws.greengrass.ipc.mqttproxy": {
    "aws.greengrass.Cloudwatch:mqttproxy:1": {
      "policyDescription": "Allows access to subscribe to input topics.",
      "operations": [
        "aws.greengrass#SubscribeToIoTCore"
      ],
      "resources": [
        "cloudwatch/metric/put"
      ]
    },
    "aws.greengrass.Cloudwatch:mqttproxy:2": {
      "policyDescription": "Allows access to publish to output topics.",
      "operations": [
        "aws.greengrass#PublishToIoTCore"
      ],
      "resources": [
        "cloudwatch/metric/put/status"
      ]
    }
  }
}
```

**Example 範例：組態合併更新**  

```
{
  "PublishInterval": 0,
  "PubSubToIoTCore": true
}
```

------
#### [ v2.x ]

**注意**  <a name="connector-component-lambda-parameters"></a>
此元件的預設組態包含 Lambda 函數參數。建議您只編輯下列參數，以在裝置上設定此元件。

 `lambdaParams`   
包含此元件 Lambda 函數參數的物件。此物件包含下列資訊：    
 `EnvironmentVariables`   
包含 Lambda 函數參數的物件。此物件包含下列資訊：    
 `PUBLISH_INTERVAL`   
（選用） 在元件發佈指定命名空間的批次指標之前等待的秒數上限。若要設定元件在接收指標時發佈指標，這表示無需批次處理，請指定 `0`。  
在相同命名空間中收到 20 個指標或您指定的間隔之後，元件會發佈到 CloudWatch。  
元件不保證事件發佈的順序。
此值最多可達 900 秒。  
預設：10 秒  
 `MAX_METRICS_TO_RETAIN`   
（選用） 在元件將指標取代為較新的指標之前，所有命名空間中要儲存在記憶體中的指標數量上限。  
當核心裝置沒有網際網路連線時，就會套用此限制，因此元件會緩衝稍後發佈的指標。當緩衝區已滿時，元件會將最舊的指標取代為較新的指標。指定命名空間中的指標只會取代相同命名空間中的指標。  
如果元件的主機程序中斷，則元件不會儲存指標。例如，這可能會在部署期間或核心裝置重新啟動時發生。
此值必須至少為 2，000 個指標。  
預設：5，000 個指標  
 `PUBLISH_REGION`   
（選用） 要發佈 CloudWatch 指標 AWS 區域 的 。此值會覆寫核心裝置的預設區域。只有跨區域指標需要此參數。

`containerMode`  <a name="connector-component-container-mode-parameter"></a>
（選用） 此元件的容器化模式。您可以從以下選項中選擇：  <a name="lambda-function-component-container-mode-parameter"></a>
+ `NoContainer` – 元件不會在隔離的執行時間環境中執行。
+ `GreengrassContainer` – 元件會在 AWS IoT Greengrass 容器內的隔離執行時間環境中執行。
預設：`GreengrassContainer`

 `containerParams`   
<a name="connector-component-container-params-description"></a>（選用） 包含此元件容器參數的物件。如果您`GreengrassContainer`為 指定 ，元件會使用這些參數`containerMode`。  
此物件包含下列資訊：    
 `memorySize`   
<a name="connector-component-container-params-memory-size-description"></a>（選用） 要配置給元件的記憶體量 （以 KB 為單位）。  
預設為 64 MB (65，535 KB)。

`pubsubTopics`  <a name="connector-component-pubsub-topics-parameter"></a>
（選用） 物件，其中包含元件訂閱接收訊息的主題。您可以指定每個主題，以及元件是從 訂閱 MQTT 主題 AWS IoT Core 或本機發佈/訂閱主題。  
此物件包含下列資訊：    
`0` – 這是做為字串的陣列索引。  
包含下列資訊的物件：    
`type`  
（選用） 此元件用來訂閱訊息的發佈/訂閱訊息類型。您可以從以下選項中選擇：  
+ `PUB_SUB` - 訂閱本機發佈/訂閱訊息。如果您選擇此選項，主題不能包含 MQTT 萬用字元。如需在指定此選項時如何從自訂元件傳送訊息的詳細資訊，請參閱 [發佈/訂閱本機訊息](ipc-publish-subscribe.md)。
+ `IOT_CORE` – 訂閱 AWS IoT Core MQTT 訊息。如果您選擇此選項，主題可以包含 MQTT 萬用字元。如需在指定此選項時如何從自訂元件傳送訊息的詳細資訊，請參閱 [發佈/訂閱 AWS IoT Core MQTT 訊息](ipc-iot-core-mqtt.md)。
預設：`PUB_SUB`  
`topic`  
（選用） 元件訂閱接收訊息的主題。如果您`IotCore`為 指定 `type`，則可以在本主題中使用 MQTT 萬用字元 (`+` 和 `#`)。

**Example 範例：組態合併更新 （容器模式）**  

```
{
  "containerMode": "GreengrassContainer"
}
```

**Example 範例：組態合併更新 （無容器模式）**  

```
{
  "containerMode": "NoContainer"
}
```

------

## 輸入資料
<a name="cloudwatch-metrics-component-input-data"></a>

此元件接受下列主題的指標，並將指標發佈至 CloudWatch。<a name="connector-component-local-publish"></a>根據預設，此元件會訂閱本機發佈/訂閱訊息。如需如何從自訂元件發佈訊息至此元件的詳細資訊，請參閱 [發佈/訂閱本機訊息](ipc-publish-subscribe.md)。

從元件版本 v3.0.0 開始，您可以選擇將此元件設定為訂閱 MQTT 主題，方法是將`PubSubToIoTCore`組態參數設定為 `true`。如需將訊息發佈至自訂元件中 MQTT 主題的詳細資訊，請參閱 [發佈/訂閱 AWS IoT Core MQTT 訊息](ipc-iot-core-mqtt.md)。

 **預設主題：** `cloudwatch/metric/put`

<a name="connector-component-input-properties"></a>訊息接受下列屬性。輸入訊息必須是 JSON 格式。

 `request`   
此訊息中的指標。  
請求物件包含要發佈至 CloudWatch 的指標資料。指標值必須符合 [https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html)操作的規格。  
類型：`object`包含下列資訊：    
 `namespace`   
此請求中指標資料的使用者定義命名空間。CloudWatch 使用命名空間做為指標資料點的容器。  
您無法指定以預留字串 開頭的命名空間`AWS/`。
類型：`string`  
有效模式： `[^:].*`  
 `metricData`   
指標的資料。  
類型：`object`包含下列資訊：    
 `metricName`   
指標的名稱  
類型：`string`  
 `value`   
指標的值。  
CloudWatch 會拒絕太小或太大的值。值必須介於 `8.515920e-109`和 `1.174271e+108`(Base 10) 或 `2e-360`和 `2e360`(Base 2) 之間。CloudWatch 不支援特殊值，例如 `NaN`、 `+Infinity`和 `-Infinity`。
類型：`double`  
 `dimensions`   
（選用） 指標的維度。維度提供指標及其資料的其他資訊。一個指標可以定義最多 10 個維度。  
此元件會自動包含名為 的維度`coreName`，其中 值是核心裝置的名稱。  
類型： `array` 的物件，每個物件都包含下列資訊：    
 `name`   
（選用） 維度名稱。  
類型：`string`  
 `value`   
（選用） 維度值。  
類型：`string`  
 `timestamp`   
（選用） 收到指標資料的時間，以秒為單位，以 Unix epoch 時間表示。  
預設為元件接收訊息的時間。  
類型：`double`  
如果您在此元件的 2.0.3 和 2.0.7 版之間使用 ，我們建議您從單一來源傳送多個指標時，分別擷取每個指標的時間戳記。請勿使用變數來存放時間戳記。  
 `unit`   
（選用） 指標的單位。  
類型：`string`  
有效值：`Seconds`、`Microseconds`、`Milliseconds`、`Bytes`、`Kilobytes`、、`Megabytes``Gigabytes``Terabytes``Bits`、`Kilobits`、`Megabits`、、`Gigabits`、`Terabits``Percent`、`Count``Bytes/Second`、`Kilobytes/Second`、、、、`Megabytes/Second`、`Gigabytes/Second``Terabytes/Second``Bits/Second``Kilobits/Second`、、 `Megabits/Second` `Gigabits/Second` `Terabits/Second` `Count/Second` `None`  
預設為 `None`。

**注意**  
套用至 CloudWatch `PutMetricData` API 的所有配額都會套用至您使用此元件發佈的指標。下列配額特別重要：  
API 承載的 40 KB 限制
每一 API 請求 20 個指標
`PutMetricData` API 每秒 150 筆交易 (TPS)
如需詳細資訊，請參閱《[CloudWatch 使用者指南》中的 CloudWatch 服務配額](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html)。 *CloudWatch *

**Example 範例輸入**  

```
{
  "request": {
    "namespace": "Greengrass",
    "metricData": {
      "metricName": "latency",
      "dimensions": [
        {
          "name": "hostname",
          "value": "test_hostname"
        }
      ],
      "timestamp": 1539027324,
      "value": 123.0,
      "unit": "Seconds"
    }
  }
}
```

## 輸出資料
<a name="cloudwatch-metrics-component-output-data"></a>

 <a name="connector-component-output-data-pubsub"></a>此元件預設會將回應發佈為下列本機發佈/訂閱主題的輸出資料。如需如何在自訂元件中訂閱此主題訊息的詳細資訊，請參閱 [發佈/訂閱本機訊息](ipc-publish-subscribe.md)。

您可以選擇將此元件設定為發佈至 MQTT 主題，方法是將`PubSubToIoTCore`組態參數設定為 `true`。如需在自訂元件中訂閱 MQTT 主題訊息的詳細資訊，請參閱 [發佈/訂閱 AWS IoT Core MQTT 訊息](ipc-iot-core-mqtt.md)。

**注意**  
根據預設，元件版本 2.0.x 會將回應發佈為 MQTT 主題上的輸出資料。您必須在[舊版訂閱路由器元件](legacy-subscription-router-component.md)的組態`subject`中，將 主題指定為 。

 **預設主題：** `cloudwatch/metric/put/status`

**Example 範例輸出：成功**  
回應包含指標資料的命名空間，以及 CloudWatch 回應中的 `RequestId` 欄位。  

```
{
  "response": {
    "cloudwatch_rid": "70573243-d723-11e8-b095-75ff2EXAMPLE",
    "namespace": "Greengrass",
    "status": "success"
  }
}
```

**Example 範例輸出：失敗**  

```
{
  "response" : {
    "namespace": "Greengrass",
    "error": "InvalidInputException",
    "error_message": "cw metric is invalid",
    "status": "fail"
  }
}
```

**注意**  <a name="connector-component-retryable-error"></a>
如果元件偵測到可以重試的錯誤，例如連線錯誤，它會在下一個批次中重試發佈。

## 授權
<a name="cloudwatch-metrics-component-licenses"></a>

此元件包含下列第三方軟體/授權：<a name="boto-3-licenses"></a>
+ [適用於 Python (Boto3) 的 AWS SDK](https://pypi.org/project/boto3/)/Apache License 2.0
+ [botocore](https://pypi.org/project/botocore/)/Apache License 2.0
+ [dateutil](https://pypi.org/project/python-dateutil/1.4/)/PSF License
+ [docutils](https://pypi.org/project/docutils/)/BSD 授權、GNU 一般公有授權 (GPL)、Python 軟體基金會授權、公有網域
+ [jmespath](https://pypi.org/project/jmespath/)/MIT License
+ [s3transfer](https://pypi.org/project/s3transfer/)/Apache License 2.0
+ [urllib3](https://pypi.org/project/urllib3/)/MIT License

<a name="component-core-software-license"></a>此元件根據 [Greengrass 核心軟體授權合約](https://greengrass-release-license.s3.us-west-2.amazonaws.com/greengrass-license-v1.pdf)發行。

## 本機日誌檔案
<a name="cloudwatch-metrics-component-log-file"></a>

此元件使用以下日誌檔案。

------
#### [ Linux ]

```
/greengrass/v2/logs/aws.greengrass.Cloudwatch.log
```

------
#### [ Windows ]

```
C:\greengrass\v2\logs\aws.greengrass.Cloudwatch.log
```

------

**檢視此元件的日誌**
+ 在核心裝置上執行下列命令，以即時檢視此元件的日誌檔案。將 `/greengrass/v2`或 *C：\$1greengrass\$1v2* 取代為 AWS IoT Greengrass 根資料夾的路徑。

------
#### [ Linux ]

  ```
  sudo tail -f /greengrass/v2/logs/aws.greengrass.Cloudwatch.log
  ```

------
#### [ Windows (PowerShell) ]

  ```
  Get-Content C:\greengrass\v2\logs\aws.greengrass.Cloudwatch.log -Tail 10 -Wait
  ```

------

## 變更記錄
<a name="cloudwatch-metrics-component-changelog"></a>

下表說明每個版本元件的變更。

------
#### [ v4.x - v3.x ]


|  **版本**  |  **變更**  | 
| --- | --- | 
|  4.0.0  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 
|  3.2.0  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 
|  3.1.0  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 
|  3.0.0  |  <a name="changelog-cloudwatch-metrics-3.0.0-major-version-changes"></a>此版本的 CloudWatch 指標元件預期組態參數與 2.x 版不同。如果您使用 2.x 版的非預設組態，而且想要從 v2.x 升級到 v3.x，則必須更新元件的組態。如需詳細資訊，請參閱 [CloudWatch 指標元件組態](#cloudwatch-metrics-component-configuration)。 <a name="changelog-cloudwatch-metrics-3.0.0"></a>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 

------
#### [ v2.x ]


|  **版本**  |  **變更**  | 
| --- | --- | 
|  2.1.8  |  針對 Greengrass nucleus 2.13.0 版更新版本。  | 
|  2.1.3  |  針對 Greengrass nucleus 2.11.0 版更新版本。  | 
|  2.1.2  |  針對 Greengrass nucleus 2.7.0 版更新版本。  | 
|  2.1.1  |  針對 Greengrass nucleus 2.6.0 版更新版本。  | 
|  2.1.0  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 
|  2.0.8  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/greengrass/v2/developerguide/cloudwatch-metrics-component.html)  | 
|  2.0.7  |  針對 Greengrass nucleus 2.4.0 版更新版本。  | 
|  2.0.6  |  針對 Greengrass nucleus 2.3.0 版更新版本。  | 
|  2.0.5  |  針對 Greengrass nucleus 2.2.0 版更新版本。  | 
|  2.0.4  |  針對 Greengrass nucleus 2.1.0 版更新版本。  | 
|  2.0.3  |  初始版本。  | 

------

## 另請參閱
<a name="cloudwatch-metrics-component-see-also"></a>
+ 《Amazon CloudWatch 使用者指南》**中的[使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html)
+ *Amazon CloudWatch API 參考*中的 [PutMetricData](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html) 