

# Amazon CloudWatch 代理
<a name="CloudWatch-OTLPCloudWatchAgent"></a>

CloudWatch 代理基于 OpenTelemetry Collector 构建，因此您可以使用它来接收 OpenTelemetry 数据并将其发送到 CloudWatch OTLP 端点。对于大多数客户而言，这是向 CloudWatch 发送遥测数据的推荐方式，因为单个代理还可以支持 CloudWatch Application Signals 和 CloudWatch Enhanced Container Insights 等精选体验。

目前，要通过代理发送 OpenTelemetry 数据，需要以 YAML 格式提供 OpenTelemetry 收集器配置，并将其附加到代理自身的配置中。使用 CloudWatch 代理配置文件启动代理，然后附加 OpenTelemetry YAML 文件：

```
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -c file:/tmp/agent.json -s
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a append-config -c file:{{/tmp/otel.yaml}} -s
```

代理会在启动时合并这两个配置，并记录已解析的配置。为避免与代理自动创建的管道发生合并冲突，请在 OpenTelemetry YAML 中为每个组件和管道名称添加自定义后缀（例如 `otlphttp/cwagent`）。

## 支持的 OpenTelemetry 组件
<a name="CloudWatch-OTLPCloudWatchAgent-Components"></a>

以下 OpenTelemetry 组件可供您在附加的 YAML 配置中进行配置。请使用此处显示的组件类型名称作为 YAML 中的键。


| 组件类型 | 可用组件 | 
| --- | --- | 
| 接收器 | `otlp`, `prometheus`, `statsd`, `collectd`, `jmx`, `hostmetrics`, `filelog`, `tcplog`, `udplog`, `jaeger`, `zipkin`, `kafka`, `kubeletstats` | 
| 处理器 | `batch`, `memory_limiter`, `filter`, `attributes`, `resource`, `resourcedetection`, `metricstransform`, `transform`, `cumulativetodelta`, `deltatocumulative`, `deltatorate`, `groupbyattrs`, `groupbytrace`, `k8sattributes`, `metricsgeneration`, `metricstarttime`, `probabilistic_sampler`, `span`, `tail_sampling` | 
| 导出器 | `otlphttp`, `awsemf`, `awscloudwatchlogs`, `awsxray`, `prometheusremotewrite`, `debug` | 
| 扩展程序 | `sigv4auth`, `headers_setter`, `file_storage`, `health_check`, `pprof`, `zpages` | 

CloudWatch 代理仅支持向 AWS 目的地写入遥测数据。

## 配置示例
<a name="CloudWatch-OTLPCloudWatchAgent-Examples"></a>

以下示例使用 `otlphttp` 导出器和 `sigv4auth` 扩展，将每个信号发送到相应的 CloudWatch OTLP 端点。每个组件和管道名称都使用 `/cwagent` 后缀，以避免与代理自动创建的管道发生冲突。将 {{region}} 替换为 AWS 区域。

**Metrics**

```
receivers:
  otlp/cwagent:
    protocols:
      http:
        endpoint: 0.0.0.0:4318
processors:
  batch/cwagent: {}
exporters:
  otlphttp/cwagent:
    metrics_endpoint: https://monitoring.{{region}}.amazonaws.com/v1/metrics
    auth:
      authenticator: sigv4auth/cwagent
extensions:
  sigv4auth/cwagent:
    region: "{{region}}"
    service: "monitoring"
service:
  extensions: [sigv4auth/cwagent]
  pipelines:
    metrics/cwagent:
      receivers: [otlp/cwagent]
      processors: [batch/cwagent]
      exporters: [otlphttp/cwagent]
```

**日志**

```
receivers:
  otlp/cwagent:
    protocols:
      http:
        endpoint: 0.0.0.0:4318
exporters:
  otlphttp/cwagent:
    logs_endpoint: https://logs.{{region}}.amazonaws.com/v1/logs
    headers:
      x-aws-log-group: {{my-log-group}}
      x-aws-log-stream: default
    auth:
      authenticator: sigv4auth/cwagent
extensions:
  sigv4auth/cwagent:
    region: "{{region}}"
    service: "logs"
service:
  extensions: [sigv4auth/cwagent]
  pipelines:
    logs/cwagent:
      receivers: [otlp/cwagent]
      exporters: [otlphttp/cwagent]
```

**跟踪**

```
receivers:
  otlp/cwagent:
    protocols:
      http:
        endpoint: 0.0.0.0:4318
exporters:
  otlphttp/cwagent:
    traces_endpoint: https://xray.{{region}}.amazonaws.com/v1/traces
    auth:
      authenticator: sigv4auth/cwagent
extensions:
  sigv4auth/cwagent:
    region: "{{region}}"
    service: "xray"
service:
  extensions: [sigv4auth/cwagent]
  pipelines:
    traces/cwagent:
      receivers: [otlp/cwagent]
      exporters: [otlphttp/cwagent]
```

**注意**  
在向 OTLP 跟踪数据端点发送跟踪数据之前，请确保启用 Transaction Search。