

# 常见处理器使用案例
<a name="processor-examples"></a>

以下是用于组合处理器的常见场景和示例配置。

**日志管线示例**

**Example 标准化日志格式并添加元数据**  
解析 JSON 日志、标准化字段名称并添加环境信息：  

```
processor:
  - parse_json: {}
  - rename_keys:
      entries:
        - from_key: "timestamp"
          to_key: "@timestamp"
        - from_key: "log_level"
          to_key: "level"
  - add_entries:
      entries:
        - key: "environment"
          value: "production"
        - key: "application"
          value: "payment-service"
```

**Example 清理字段值并进行标准化处理**  
对状态代码进行标准化处理并移除敏感数据：  

```
processor:
  - uppercase_string:
      with_keys: ["status", "method"]
  - delete_entries:
      with_keys: ["credit_card", "password"]
  - substitute_string:
      entries:
        - source: "status"
          from: "SUCCESS"
          to: "OK"
```

**Example 提取并转换特定字段**  
提取用户信息和格式以进行分析：  

```
processor:
  - extract_value:
      entries:
        - source: "user_agent"
          target: "browser"
          from: "(?<browser>Chrome|Firefox|Safari)"
          to: "${browser}"
  - lowercase_string:
      with_keys: ["browser"]
  - move_keys:
      entries:
        - from_key: "browser"
          to_key: "user_data.browser"
```

**Example 使用条目级条件进行条件处理**  
使用条目级 `when` 条件，根据日志严重性添加不同的元数据：  

```
processor:
  - add_entries:
      entries:
        - key: "alert_level"
          value: "critical"
          when: "log.level == 'ERROR'"
        - key: "alert_level"
          value: "info"
          when_else: "log.level == 'ERROR'"
```

**Example 删除不需要的日志条目**  
筛选掉来自第三方来源的调试和跟踪日志条目，以降低噪音和存储成本：  

```
processor:
  - drop_events:
      when: "log.level in {'DEBUG', 'TRACE'}"
      handle_expression_failure: "skip"
```

**Example 带有 delete\_entris 的处理器级条件**  
仅在环境为生产环境时才删除敏感字段：  

```
processor:
  - delete_entries:
      with_keys: ["password", "api_key", "ssn"]
      when: "environment in {'prod', 'staging'}"
```

## 指标管线示例
<a name="metrics-processor-examples"></a>

以下示例展示了指标管线的处理器配置。指标处理器使用 OTTL 路径表达式来定位不同范围的属性。

**Example 为指标添加业务上下文**  
在指标数据点中添加团队所有权和环境标签：  

```
processor:
  - add_attributes:
      attributes:
        - key: resource.attributes["team"]
          value: "platform-engineering"
        - key: resource.attributes["cost_center"]
          value: "CC-1234"
```

**Example 移除高基数属性**  
删除导致存储成本上升的属性。不适用于累积指标或已出售指标：如果选择标准中的任何指标具有不支持的时间性，则管线会发出 `UnsupportedTemporality` 警告指标，您可以在 `AWS/Observability Admin` 命名空间中监控该指标：  

```
processor:
  - delete_attributes:
      with_keys:
        - resource.attributes["host.id"]
        - datapoint.attributes["http.request.id"]
```

**Example 标准化命名约定**  
重命名指标和属性，使其符合 OpenTelemetry 语义约定。不适用于累积指标或已出售指标：  

```
processor:
  - rename_metrics:
      metrics:
        - from: "cpu_usage_percent"
          to: "system.cpu.utilization"
  - rename_attributes:
      attributes:
        - from_key: resource.attributes["hostname"]
          to_key: resource.attributes["host.name"]
```