

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

# 將應用程式指標匯出至 Amazon Managed Service for Prometheus
<a name="application-metrics-prometheus"></a>

Amazon ECS 支援將您的任務層級 CPU、記憶體、網路和儲存指標，以及自訂應用程式指標匯出至 Amazon Managed Service for Prometheus。方法是將 AWS Distro for OpenTelemetry 附屬容器新增至您的任務定義。Amazon ECS 主控台透過在建立新任務定義時新增**使用指標收集**選項來簡化此流程。如需詳細資訊，請參閱[使用主控台建立 Amazon ECS 任務定義](create-task-definition.md)。

這些指標將匯出至 Amazon Managed Service for Prometheus，並可以使用 Amazon Managed Grafana 儀表板查看。您必須使用 Prometheus 程式庫或 OpenTelemetry SDK 檢測應用程式。如需有關使用 OpenTelemetry SDK 檢測應用程式的詳細資訊，請參閱 [AWS Distro for OpenTelemetry 文件](https://aws-otel.github.io/docs/introduction)中的 AWS Distro for OpenTelemetry 簡介。

使用 Prometheus 程式庫時，您的應用程式必須公開 `/metrics` 端點，用於抓取指標資料。如需有關使用 Prometheus 程式庫檢測應用程式的詳細資訊，請參閱 Prometheus 文件中的 [Prometheus 用戶端程式庫](https://prometheus.io/docs/instrumenting/clientlibs/)。

## 考量事項
<a name="application-metrics-prometheus-considerations"></a>

使用 Amazon ECS on Fargate 與 AWS Distro for OpenTelemetry 整合，將應用程式指標傳送至 Amazon Managed Service for Prometheus 時，應考慮下列事項。
+ 託管在 Fargate 上的 AWS Amazon ECS 工作負載和託管在 Amazon EC2 執行個體上的 Amazon ECS 工作負載支援 Distro for OpenTelemetry 整合。目前不支援外部執行個體。
+ 根據預設， AWS Distro for OpenTelemetry 會在匯出至 Amazon Managed Service for Prometheus 時包含應用程式指標的所有可用任務層級維度。您還可以檢測應用程式以增加其他維度。如需詳細資訊，請參閱 AWS Distro for OpenTelemetry 文件中的[適用於 Amazon Managed Service for Prometheus 的 Prometheus 遠端寫入匯出器入門](https://aws-otel.github.io/docs/getting-started/prometheus-remote-write-exporter)。

## AWS Distro for OpenTelemetry 與 Amazon Managed Service for Prometheus 整合所需的 IAM 許可
<a name="application-metrics-prometheus-iam"></a>

Amazon ECS 與使用 AWS Distro for OpenTelemetry 附屬的 Amazon Managed Service for Prometheus 整合需要您建立任務 IAM 角色，並在任務定義中指定角色。註冊任務定義之前，必須手動建立此任務的 IAM 角色。如需建立任務角色的詳細資訊，請參閱 [Amazon ECS 任務 IAM 角色](task-iam-roles.md)。

建議您也將 AWS Distro for OpenTelemetry 附屬項目設定為將容器日誌路由至 CloudWatch Logs，這也需要在任務定義中建立和指定任務執行 IAM 角色。Amazon ECS 主控台會代表您負責處理任務執行 IAM 角色，但任務 IAM 角色必須手動建立。如需有關為任務建立 IAM 角色的詳細資訊，請參閱 [Amazon ECS 任務執行 IAM 角色](task_execution_IAM_role.md)。

**重要**  
如果您也使用 AWS Distro for OpenTelemetry 整合收集應用程式追蹤資料，請確定您的任務 IAM 角色也包含該整合所需的許可。如需詳細資訊，請參閱[使用應用程式追蹤資料識別 Amazon ECS 最佳化機會](trace-data.md)。

 AWS Distro for OpenTelemetry 與 Amazon Managed Service for Prometheus 整合需要下列許可：
+ logs:PutLogEvents
+ logs:CreateLogGroup
+ logs:CreateLogStream
+ logs:DescribeLogStreams
+ logs:DescribeLogGroups
+ cloudwatch:PutMetricData

## 在任務定義中指定 AWS Distro for OpenTelemetry 附屬項目
<a name="application-metrics-prometheus-containerdefinitions"></a>

Amazon ECS 主控台使用**使用指標收集**選項，簡化了建立 AWS Distro for OpenTelemetry 附屬容器的體驗。如需詳細資訊，請參閱[使用主控台建立 Amazon ECS 任務定義](create-task-definition.md)。

如果您不是使用 Amazon ECS 主控台，則可以手動將 AWS Distro for OpenTelemetry 附屬容器新增至任務定義。下列任務定義範例顯示為 Amazon Managed Service for Prometheus 整合新增 AWS Distro for OpenTelemetry 附屬項目的容器定義。

```
{
	"family": "otel-using-cloudwatch",
	"taskRoleArn": "arn:aws:iam::111122223333:role/AmazonECS_OpenTelemetryCloudWatchRole",
	"executionRoleArn": "arn:aws:iam::111122223333:role/ecsTaskExecutionRole",
	"containerDefinitions": [{
			"name": "aws-otel-emitter",
			"image": "application-image",
			"logConfiguration": {
				"logDriver": "awslogs",
				"options": {
					"awslogs-create-group": "true",
					"awslogs-group": "/ecs/aws-otel-emitter",
					"awslogs-region": "aws-region",
					"awslogs-stream-prefix": "ecs"
				}
			},
			"dependsOn": [{
				"containerName": "aws-otel-collector",
				"condition": "START"
			}]
		},
		{
			"name": "aws-otel-collector",
			"image": "public.ecr.aws/aws-observability/aws-otel-collector:v0.30.0",
			"essential": true,
			"command": [
				"--config=/etc/ecs/ecs-amp.yaml"
			],
			"environment": [{
				"name": "AWS_PROMETHEUS_ENDPOINT",
				"value": "https://aps-workspaces.aws-region.amazonaws.com/workspaces/ws-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111/api/v1/remote_write"
			}],
			"logConfiguration": {
				"logDriver": "awslogs",
				"options": {
					"awslogs-create-group": "True",
					"awslogs-group": "/ecs/ecs-aws-otel-sidecar-collector",
					"awslogs-region": "aws-region",
					"awslogs-stream-prefix": "ecs"
				}
			}
		}
	],
	"networkMode": "awsvpc",
	"requiresCompatibilities": [
		"FARGATE"
	],
	"cpu": "1024",
	"memory": "3072"
}
```