

# 将应用程序指标导出到 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 开发工具包进行分析。有关使用 OpenTelemetry 开发工具包分析您的应用程序的更多信息，请参阅 AWS Distro for OpenTelemetry 文档中的 [AWS Distro for OpenTelemetry 的介绍](https://aws-otel.github.io/docs/introduction)。

使用 Prometheus 库时，您的应用程序必须公开用于抓取指标数据的 `/metrics` 端点。有关使用 Prometheus 库分析您的应用程序的更多信息，请参阅 Prometheus 文档中的 [Prometheus 客户端库](https://prometheus.io/docs/instrumenting/clientlibs/)。

## 注意事项
<a name="application-metrics-prometheus-considerations"></a>

使用 Fargate 上的 Amazon ECS 与 AWS Distro for OpenTelemetry 的集成向 Amazon Managed Service for Prometheus 发送应用程序指标时，应考虑以下因素。
+ Fargate 和 Amazon EC2 实例上托管的 Amazon ECS 工作负载支持 AWS Distro for OpenTelemetry 集成。目前不支持外部实例。
+ 预设情况下，在向 Amazon Managed Service for Prometheus 导出时，AWS Distro for OpenTelemetry 包括可用于您的应用程序指标的所有可用的任务级别维度。您还可以对应用程序进行分析以添加其他维度。有关更多信息，请参阅 AWS Distro for OpenTelemetry 文档中的[开始将 Prometheus Remote Write Exporter 用于 Amazon Managed Service for 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 附加容器到您的任务定义中。以下任务定义示例显示了用于添加 AWS Distro for OpenTelemetry 附加以进行 Amazon Managed Service for Prometheus 集成的容器定义。

```
{
	"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"
}
```