otel-ottl
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenTelemetry Transformation Language (OTTL)
OpenTelemetry转换语言(OTTL)
Use OTTL to transform, filter, and manipulate telemetry data inside the OpenTelemetry Collector — without changing application code.
使用OTTL在OpenTelemetry Collector内部转换、过滤和操作遥测数据——无需修改应用代码。
Components that use OTTL
支持OTTL的组件
OTTL is not limited to the transform and filter processors.
The following Collector components accept OTTL expressions in their configuration.
OTTL不仅限于transform和filter处理器。以下Collector组件的配置中可使用OTTL表达式。
Processors
处理器
| Component | Use case |
|---|---|
| transform | Modify, enrich, or redact telemetry (set attributes, rename fields, truncate values) |
| filter | Drop telemetry entirely (discard metrics by name, drop spans by status, remove noisy logs) |
| attributes | Insert, update, delete, or hash resource and record attributes |
| span | Rename spans and set span status based on attribute values |
| tailsampling | Sample traces based on OTTL conditions (e.g., keep error traces, drop health checks) |
| cumulativetodelta | Convert cumulative metrics to delta temporality with OTTL-based metric selection |
| logdedup | Deduplicate log records using OTTL conditions |
| lookup | Enrich telemetry by looking up values from external tables using OTTL expressions |
| 组件 | 使用场景 |
|---|---|
| transform | 修改、丰富或脱敏遥测数据(设置属性、重命名字段、截断值) |
| filter | 完全丢弃遥测数据(按名称丢弃指标、按状态丢弃span、移除冗余日志) |
| attributes | 插入、更新、删除或哈希资源与记录属性 |
| span | 重命名span并根据属性值设置span状态 |
| tailsampling | 基于OTTL条件采样追踪(例如保留错误追踪、丢弃健康检查数据) |
| cumulativetodelta | 基于OTTL的指标选择将累积指标转换为增量时间序列 |
| logdedup | 使用OTTL条件对日志记录进行去重 |
| lookup | 使用OTTL表达式从外部表查询值来丰富遥测数据 |
Connectors
连接器
| Component | Use case |
|---|---|
| routing | Route telemetry to different pipelines based on OTTL conditions |
| count | Count spans, metrics, or logs matching OTTL conditions and emit as metrics |
| sum | Sum numeric values from telemetry matching OTTL conditions and emit as metrics |
| signaltometrics | Generate metrics from spans or logs using OTTL expressions for attribute extraction |
| 组件 | 使用场景 |
|---|---|
| routing | 基于OTTL条件将遥测数据路由到不同管道 |
| count | 统计匹配OTTL条件的span、指标或日志数量并作为指标输出 |
| sum | 对匹配OTTL条件的遥测数据中的数值求和并作为指标输出 |
| signaltometrics | 使用OTTL表达式提取属性,从span或日志生成指标 |
Receivers
接收器
| Component | Use case |
|---|---|
| hostmetrics | Filter host metrics at collection time using OTTL conditions |
| 组件 | 使用场景 |
|---|---|
| hostmetrics | 在采集阶段使用OTTL条件过滤主机指标 |
OTTL syntax
OTTL语法
Path expressions
路径表达式
Navigate telemetry data using dot notation:
span.name
span.attributes["http.method"]
resource.attributes["service.name"]Contexts (first path segment) map to OpenTelemetry signal structures:
- - Resource-level attributes
resource - - Instrumentation scope
scope - - Span data (traces)
span - - Span events
spanevent - - Metric metadata
metric - - Metric data points
datapoint - - Log records
log
使用点符号导航遥测数据:
span.name
span.attributes["http.method"]
resource.attributes["service.name"]上下文(路径的第一个段)对应OpenTelemetry信号结构:
- - 资源级属性
resource - - 仪器范围
scope - - Span数据(追踪)
span - - Span事件
spanevent - - 元数据指标
metric - - 指标数据点
datapoint - - 日志记录
log
Enumerations
枚举
Several fields accept int64 values exposed as global constants:
span.status.code == STATUS_CODE_ERROR
span.kind == SPAN_KIND_SERVER多个字段接受作为全局常量暴露的int64值:
span.status.code == STATUS_CODE_ERROR
span.kind == SPAN_KIND_SERVEROperators
运算符
| Category | Operators |
|---|---|
| Assignment | |
| Comparison | |
| Logical | |
| 类别 | 运算符 |
|---|---|
| 赋值 | |
| 比较 | |
| 逻辑 | |
Functions
函数
Converters (uppercase, pure functions that return values):
ToUpperCase(span.attributes["http.request.method"])
Substring(log.body.string, 0, 1024)
Concat(["prefix", span.attributes["request.id"]], "-")
IsMatch(metric.name, "^k8s\\..*$")Editors (lowercase, functions with side-effects that modify data):
set(span.attributes["region"], "us-east-1")
delete_key(resource.attributes, "internal.key")
limit(log.attributes, 10, [])转换器(大写,返回值的纯函数):
ToUpperCase(span.attributes["http.request.method"])
Substring(log.body.string, 0, 1024)
Concat(["prefix", span.attributes["request.id"]], "-")
IsMatch(metric.name, "^k8s\\..*$")编辑器(小写,有副作用的修改数据函数):
set(span.attributes["region"], "us-east-1")
delete_key(resource.attributes, "internal.key")
limit(log.attributes, 10, [])Conditional statements
条件语句
The clause applies transformations conditionally:
wherespan.attributes["db.statement"] = "REDACTED" where resource.attributes["service.name"] == "accounting"wherespan.attributes["db.statement"] = "REDACTED" where resource.attributes["service.name"] == "accounting"Nil checks
空值检查
OTTL uses for absence checking (not ):
nilnullresource.attributes["service.name"] != nilOTTL使用进行存在性检查(而非):
nilnullresource.attributes["service.name"] != nilCommon patterns
常见模式
Set attributes
设置属性
set(resource.attributes["k8s.cluster.name"], "prod-aws-us-west-2")set(resource.attributes["k8s.cluster.name"], "prod-aws-us-west-2")Redact sensitive data
脱敏敏感数据
set(span.attributes["http.request.header.authorization"], "REDACTED") where span.attributes["http.request.header.authorization"] != nilset(span.attributes["http.request.header.authorization"], "REDACTED") where span.attributes["http.request.header.authorization"] != nilDrop telemetry by pattern
按模式丢弃遥测数据
In a filter processor, matching expressions cause data to be dropped:
IsMatch(metric.name, "^k8s\\.replicaset\\..*$")在filter处理器中,匹配表达式会导致数据被丢弃:
IsMatch(metric.name, "^k8s\\.replicaset\\..*$")Drop stale data
丢弃过期数据
time_unix_nano < UnixNano(Now()) - 21600000000000time_unix_nano < UnixNano(Now()) - 21600000000000Backfill missing timestamps
回填缺失的时间戳
yaml
processors:
transform:
log_statements:
- context: log
statements:
- set(log.observed_time, Now()) where log.observed_time_unix_nano == 0
- set(log.time, log.observed_time) where log.time_unix_nano == 0yaml
processors:
transform:
log_statements:
- context: log
statements:
- set(log.observed_time, Now()) where log.observed_time_unix_nano == 0
- set(log.time, log.observed_time) where log.time_unix_nano == 0Filter processor example
Filter处理器示例
yaml
processors:
filter:
metrics:
datapoint:
- 'IsMatch(ConvertCase(String(metric.name), "lower"), "^k8s\\.replicaset\\.")'
service:
pipelines:
metrics:
receivers: [otlp]
processors: [filter, batch]
exporters: [debug]yaml
processors:
filter:
metrics:
datapoint:
- 'IsMatch(ConvertCase(String(metric.name), "lower"), "^k8s\\.replicaset\\.")'
service:
pipelines:
metrics:
receivers: [otlp]
processors: [filter, batch]
exporters: [debug]Transform processor example
Transform处理器示例
yaml
processors:
transform:
trace_statements:
- context: span
statements:
- set(span.status.code, STATUS_CODE_ERROR) where span.attributes["http.response.status_code"] >= 500
- set(span.attributes["env"], "production") where resource.attributes["deployment.environment"] == "prod"
service:
pipelines:
traces:
receivers: [otlp]
processors: [transform, batch]
exporters: [debug]yaml
processors:
transform:
trace_statements:
- context: span
statements:
- set(span.status.code, STATUS_CODE_ERROR) where span.attributes["http.response.status_code"] >= 500
- set(span.attributes["env"], "production") where resource.attributes["deployment.environment"] == "prod"
service:
pipelines:
traces:
receivers: [otlp]
processors: [transform, batch]
exporters: [debug]Defensive nil checks
防御性空值检查
Always check for before operating on optional attributes:
nilresource.attributes["service.namespace"] != nil
and
IsMatch(ConvertCase(String(resource.attributes["service.namespace"]), "lower"), "^platform.*$")在操作可选属性前始终检查:
nilresource.attributes["service.namespace"] != nil
and
IsMatch(ConvertCase(String(resource.attributes["service.namespace"]), "lower"), "^platform.*$")Error handling
错误处理
Compilation errors
编译错误
Occur during processor initialization and prevent Collector startup:
- Invalid syntax (missing quotes)
- Unknown functions
- Invalid path expressions
- Type mismatches
发生在处理器初始化阶段,会阻止Collector启动:
- 无效语法(缺少引号)
- 未知函数
- 无效路径表达式
- 类型不匹配
Runtime errors
运行时错误
Occur during telemetry processing:
- Accessing non-existent attributes
- Type conversion failures
- Function execution errors
发生在遥测数据处理阶段:
- 访问不存在的属性
- 类型转换失败
- 函数执行错误
Error mode configuration
错误模式配置
Always set explicitly.
The default () stops processing the current item on any error, which can silently drop telemetry in production.
error_modepropagate| Mode | Behavior | When to use |
|---|---|---|
| Stops processing current item | Development and strict environments where you want to catch every error |
| Logs error, continues processing | Production — set this unless you have a specific reason not to |
| Ignores errors without logging | High-volume pipelines with known-safe transforms where error logs are noise |
yaml
processors:
transform:
error_mode: ignore
trace_statements:
- context: span
statements:
- set(span.attributes["parsed"], ParseJSON(span.attributes["json_body"]))始终显式设置。默认值()会在发生任何错误时停止处理当前条目,这可能会在生产环境中静默丢弃遥测数据。
error_modepropagate| 模式 | 行为 | 使用场景 |
|---|---|---|
| 停止处理当前条目 | 开发环境和需要捕获所有错误的严格环境 |
| 记录错误,继续处理 | 生产环境——除非有特定原因,否则设置此模式 |
| 忽略错误且不记录 | 高流量管道且转换已知安全,错误日志会产生噪音的场景 |
yaml
processors:
transform:
error_mode: ignore
trace_statements:
- context: span
statements:
- set(span.attributes["parsed"], ParseJSON(span.attributes["json_body"]))Performance
性能
OTTL statements compile once at startup and execute as optimized function chains at runtime.
There is no need to optimize for compilation speed — focus on reducing the number of statements that evaluate per telemetry item.
Use clauses to skip items early rather than applying unconditional transforms.
whereOTTL语句在启动时编译一次,运行时作为优化的函数链执行。无需优化编译速度——重点减少每个遥测条目需要评估的语句数量。使用子句提前跳过条目,而非应用无条件转换。
whereFunction reference
函数参考
Editors
编辑器
Editors modify telemetry data in-place. They are lowercase.
| Function | Signature | Description |
|---|---|---|
| | Appends single or multiple values to a target field, converting scalars to arrays if needed |
| | Removes a key from a map |
| | Removes all keys matching a regex pattern |
| | Flattens nested maps to the root level |
| | Removes all keys NOT in the supplied list |
| | Keeps only keys matching a regex pattern |
| | Reduces map size to not exceed limit, preserving priority keys |
| | Merges source into target (strategy: |
| | Replaces matching string values using glob patterns |
| | Replaces segments matching regex (mode: |
| | Replaces entire string if it matches a glob pattern |
| | Replaces string sections matching a regex |
| | Sets a telemetry field to a value |
| | Truncates all string values in a map to a max length |
编辑器会就地修改遥测数据,采用小写命名。
| 函数 | 签名 | 描述 |
|---|---|---|
| | 向目标字段追加单个或多个值,必要时将标量转换为数组 |
| | 从映射中移除指定键 |
| | 移除所有匹配正则模式的键 |
| | 将嵌套映射展平到根级别 |
| | 移除所有不在提供列表中的键 |
| | 仅保留匹配正则模式的键 |
| | 将映射大小减少到不超过限制,保留优先级键 |
| | 将源映射合并到目标映射(策略: |
| | 使用通配符模式替换匹配的字符串值 |
| | 替换匹配正则的片段(模式: |
| | 如果字符串完全匹配通配符模式则替换整个字符串 |
| | 替换匹配正则的字符串部分 |
| | 将遥测字段设置为指定值 |
| | 将映射中的所有字符串值截断到最大长度 |
Converters: type checking
转换器:类型检查
| Function | Signature | Description |
|---|---|---|
| | Returns true if value is boolean |
| | Returns true if value is float64 |
| | Returns true if value is int64 |
| | Returns true if value is a map |
| | Returns true if value is a list |
| | Returns true if target matches regex pattern |
| | Returns true if span has no parent |
| | Returns true if value is a string |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 如果值是布尔类型则返回true |
| | 如果值是float64类型则返回true |
| | 如果值是int64类型则返回true |
| | 如果值是映射类型则返回true |
| | 如果值是列表类型则返回true |
| | 如果目标匹配正则模式则返回true |
| | 如果span没有父级则返回true |
| | 如果值是字符串类型则返回true |
Converters: type conversion
转换器:类型转换
| Function | Signature | Description |
|---|---|---|
| | Converts value to boolean |
| | Converts value to float64 |
| | Converts value to int64 |
| | Converts value to string |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 将值转换为布尔类型 |
| | 将值转换为float64类型 |
| | 将值转换为int64类型 |
| | 将值转换为字符串类型 |
Converters: string manipulation
转换器:字符串操作
| Function | Signature | Description |
|---|---|---|
| | Concatenates values with a delimiter |
| | Converts to |
| | Returns true if value starts with prefix |
| | Returns true if value ends with suffix |
| | Returns first index of value in target, or -1 |
| | Splits string into array by delimiter |
| | Extracts substring from start position |
| | Converts to CamelCase |
| | Converts to lowercase |
| | Converts to snake_case |
| | Converts to UPPERCASE |
| | Removes leading/trailing characters |
| | Removes leading prefix |
| | Removes trailing suffix |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 使用分隔符连接多个值 |
| | 转换为 |
| | 如果值以指定前缀开头则返回true |
| | 如果值以指定后缀结尾则返回true |
| | 返回值在目标中的第一个索引,不存在则返回-1 |
| | 按分隔符将字符串拆分为数组 |
| | 从起始位置提取子字符串 |
| | 转换为驼峰式命名 |
| | 转换为小写 |
| | 转换为蛇形命名 |
| | 转换为大写 |
| | 移除首尾指定字符 |
| | 移除指定前缀 |
| | 移除指定后缀 |
Converters: Hashing
转换器:哈希
| Function | Signature | Description |
|---|---|---|
| | Returns FNV hash as int64 |
| | Returns MD5 hash as hex string |
| | Returns 32-bit Murmur3 hash as hex string |
| | Returns 128-bit Murmur3 hash as hex string |
| | Returns SHA1 hash as hex string |
| | Returns SHA256 hash as hex string |
| | Returns SHA512 hash as hex string |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 返回FNV哈希值(int64类型) |
| | 返回MD5哈希值(十六进制字符串) |
| | 返回32位Murmur3哈希值(十六进制字符串) |
| | 返回128位Murmur3哈希值(十六进制字符串) |
| | 返回SHA1哈希值(十六进制字符串) |
| | 返回SHA256哈希值(十六进制字符串) |
| | 返回SHA512哈希值(十六进制字符串) |
Converters: encoding and decoding
转换器:编码与解码
| Function | Signature | Description |
|---|---|---|
| | Decodes string (base64, base64-raw, base64-url, IANA encodings) |
| | Returns hexadecimal representation |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 解码字符串(支持base64、base64-raw、base64-url、IANA编码) |
| | 返回十六进制表示 |
Converters: Parsing
转换器:解析
| Function | Signature | Description |
|---|---|---|
| | Extracts named regex capture groups into a map |
| | Parses unstructured data using grok patterns |
| | Parses CSV string to map |
| | Parses string as integer in given base (2-36) |
| | Parses JSON string to map or slice |
| | Parses key-value string to map |
| | Maps log level value to severity string |
| | Parses XML string to map (ignores attributes) |
| | Parses XML string to map (preserves structure) |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 将正则捕获组提取为映射 |
| | 使用grok模式解析非结构化数据 |
| | 将CSV字符串解析为映射 |
| | 将字符串解析为指定进制(2-36)的整数 |
| | 将JSON字符串解析为映射或切片 |
| | 将键值对字符串解析为映射 |
| | 将日志级别值映射为严重程度字符串 |
| | 将XML字符串解析为映射(忽略属性) |
| | 将XML字符串解析为映射(保留结构) |
Converters: Time and Date
转换器:时间与日期
| Function | Signature | Description |
|---|---|---|
| | Returns day component from time |
| | Parses duration string (e.g. |
| | Formats time to string using Go layout |
| | Returns hour component from time |
| | Returns duration as floating-point hours |
| | Returns minute component from time |
| | Returns duration as floating-point minutes |
| | Returns month component from time |
| | Returns nanosecond component from time |
| | Returns duration as nanosecond count |
| | Returns current time |
| | Returns second component from time |
| | Returns duration as floating-point seconds |
| | Parses string to time |
| | Truncates time to multiple of duration |
| | Creates time from Unix epoch |
| | Returns time as microseconds since epoch |
| | Returns time as milliseconds since epoch |
| | Returns time as nanoseconds since epoch |
| | Returns time as seconds since epoch |
| | Returns day of week from time |
| | Returns year component from time |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 从时间中提取日分量 |
| | 解析时长字符串(例如 |
| | 使用Go语言布局将时间格式化为字符串 |
| | 从时间中提取小时分量 |
| | 将时长转换为浮点型小时数 |
| | 从时间中提取分钟分量 |
| | 将时长转换为浮点型分钟数 |
| | 从时间中提取月份分量 |
| | 从时间中提取纳秒分量 |
| | 将时长转换为纳秒数 |
| | 返回当前时间 |
| | 从时间中提取秒分量 |
| | 将时长转换为浮点型秒数 |
| | 将字符串解析为时间 |
| | 将时间截断为指定时长的倍数 |
| | 从Unix纪元创建时间 |
| | 返回自纪元以来的微秒数 |
| | 返回自纪元以来的毫秒数 |
| | 返回自纪元以来的纳秒数 |
| | 返回自纪元以来的秒数 |
| | 从时间中提取星期几 |
| | 从时间中提取年份分量 |
Converters: Collections
转换器:集合
| Function | Signature | Description |
|---|---|---|
| | Returns true if item exists in slice |
| | Formats string using |
| | Returns all keys from a map |
| | Returns length of string, slice, or map |
| | Converts slice of objects to map |
| | Sorts array ( |
| | Converts map to key-value string |
| | Returns all values from a map |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 如果切片中存在指定项则返回true |
| | 使用 |
| | 返回映射中的所有键 |
| | 返回字符串、切片或映射的长度 |
| | 将对象切片转换为映射 |
| | 对数组进行排序( |
| | 将映射转换为键值对字符串 |
| | 返回映射中的所有值 |
Converters: IDs and Encoding
转换器:ID与编码
| Function | Signature | Description |
|---|---|---|
| | Creates ProfileID from 16 bytes or 32 hex chars |
| | Creates SpanID from 8 bytes or 16 hex chars |
| | Creates TraceID from 16 bytes or 32 hex chars |
| | Generates a new UUID |
| | Generates a new UUIDv7 |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 从16字节或32位十六进制字符创建ProfileID |
| | 从8字节或16位十六进制字符创建SpanID |
| | 从16字节或32位十六进制字符创建TraceID |
| | 生成新的UUID |
| | 生成新的UUIDv7 |
Converters: XML
转换器:XML
| Function | Signature | Description |
|---|---|---|
| | Converts XML attributes to child elements |
| | Wraps XML text content in elements |
| | Returns XML elements matching XPath |
| | Inserts XML at XPath locations |
| | Removes XML elements matching XPath |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 将XML属性转换为子元素 |
| | 将XML文本内容包装为元素 |
| | 返回匹配XPath的XML元素 |
| | 在XPath指定位置插入XML |
| | 移除匹配XPath的XML元素 |
Converters: Miscellaneous
转换器:其他
| Function | Signature | Description |
|---|---|---|
| | Generates network flow hash |
| | Returns true if value passes Luhn check |
| | Returns natural logarithm as float64 |
| | Parses URL into components (scheme, host, path, etc.) |
| | Parses user-agent string into map (name, version, OS) |
| 函数 | 签名 | 描述 |
|---|---|---|
| | 生成网络流哈希 |
| | 如果值通过Luhn校验则返回true |
| | 返回自然对数(float64类型) |
| | 将URL解析为组件(协议、主机、路径等) |
| | 将用户代理字符串解析为映射(名称、版本、操作系统) |