

# CloudWatch 指标（OTel）
<a name="metrics-pipeline-selection-criteria"></a>

选择标准确定哪些 OTel 指标进入管线进行处理。每个标准都是形式为 `<path> == "<value>"` 的表达式，它与传入指标的特定属性相匹配。至少需要一项选择标准。

标准被分组到一个带有 AND 语义的 `match_all` 块中，指标必须与组中的每个表达式都匹配才能进入管线。

## 支持的路径
<a name="selection-criteria-paths"></a>

以下 OTTL 路径在选择标准中受支持：


**选择标准路径**  

| 路径 | 说明 | 
| --- | --- | 
| `resource.attributes["key"]` | 资源级别属性 | 
| `instrumentation_scope.name` | 检测范围名称 | 
| `instrumentation_scope.version` | 检测范围版本 | 
| `instrumentation_scope.attributes["key"]` | 检测范围属性 | 
| `metric.name` | 指标名称 | 
| `datapoint.attributes["key"]` | 数据点级别属性 | 
| `attributes["key"]` | `datapoint.attributes["key"]` 的简称 | 

## 配置
<a name="selection-criteria-configuration"></a>

选择标准在管线配置的 `source` 部分中定义：

```
pipeline:
  source:
    cloudwatch_metrics:
      format: otlp
      selection_criteria:
        - match_all:
            - 'resource.attributes["service.name"] == "my-service"'
            - 'metric.name == "http.server.request.duration"'
  processor:
    - add_attributes:
        attributes:
          - key: resource.attributes["team"]
            value: "platform-engineering"
  sink:
    - cloudwatch_metrics: {}
```

下面的示例使用所有支持的路径类型：

```
pipeline:
  source:
    cloudwatch_metrics:
      format: otlp
      selection_criteria:
        - match_all:
            - 'resource.attributes["service.name"] == "my-service"'
            - 'instrumentation_scope.name == "my-scope"'
            - 'instrumentation_scope.version == "1.0.0"'
            - 'instrumentation_scope.attributes["library"] == "otel-java"'
            - 'metric.name == "http.server.request.duration"'
            - 'datapoint.attributes["status_code"] == "200"'
            - 'attributes["environment"] == "production"'
  processor:
    - add_attributes:
        attributes:
          - key: resource.attributes["team"]
            value: "observability"
  sink:
    - cloudwatch_metrics: {}
```

## 使用 PromQL 进行验证
<a name="selection-criteria-promql"></a>

CloudWatch 使用 `@` 前缀约定将 OTLP 属性范围映射到 PromQL 标签。使用此映射可在 Query Studio 中验证管线是否按预期处理指标：


**OTTL 路径到 PromQL 标签映射**  

| 管线 OTTL 路径 | PromQL 标签前缀 | 示例 | 
| --- | --- | --- | 
| `resource.attributes["key"]` | `@resource.` | `@resource.service.name` | 
| `instrumentation_scope.name` | `@instrumentation.@name` | `@instrumentation.@name` | 
| `instrumentation_scope.attributes["key"]` | `@instrumentation.` | `@instrumentation.library` | 
| `datapoint.attributes["key"]` / `attributes["key"]` | `@datapoint.` 或裸露 | `status_code` | 

例如，如果管线添加了值为 `"platform-engineering"` 的 `resource.attributes["team"]`，则可以确认它已应用：

```
{"CPUUtilization", "@resource.team"="platform-engineering"}
```

## 要求和限制
<a name="selection-criteria-requirements"></a>

单个 `match_all` 组  
每个管线的 `selection_criteria` 中仅支持一个 `match_all` 组。不能在单个管线中定义多个 `match_all` 组。

最低标准  
`match_all` 组中至少需要一个表达式。

最大标准  
一个 `match_all` 组最多可包含 20 个表达式。

AND 语义  
`match_all` 组中的所有表达式都必须匹配，指标才能进入管线。

精确字符串匹配  
值必须是静态字符串。不支持通配符、正则表达式和部分匹配。每个表达式都必须使用 `==` 运算符。

各管线之间不存在重叠的标准  
每个指标数据点最多只能匹配一个管线。如果一个数据点与新的和现有的管线标准都匹配，则管线创建会失败。为防止重叠，每个管线的选择标准中至少应包含一条具有不同值的属性路径。  
例如，以下两个选择标准发生重叠，因为管线 A 选择 `payment-service` 中的所有指标，其中包括管线 B 所针对的特定指标：  

```
# Pipeline A — selects ALL metrics from payment-service
selection_criteria:
  - match_all:
      - 'resource.attributes["service.name"] == "payment-service"'

# Pipeline B — FAILS: a datapoint with service.name="payment-service"
# and metric.name="http.server.request.duration" matches both pipelines
selection_criteria:
  - match_all:
      - 'metric.name == "http.server.request.duration"'
```