otel-ottl
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenTelemetry Transformation Language (OTTL)
OpenTelemetry Transformation Language (OTTL)
OTTL transforms or selects telemetry inside Collector components. This skill is pinned to
collector-contrib v0.158.0. Function, path, default, and feature-gate availability varies by
release; when the user's version differs, verify against the matching upstream tag.
OTTL用于在Collector组件内部转换或选择遥测数据。本技能基于collector-contrib v0.158.0版本。函数、路径、默认值和特性开关的可用性因版本而异;如果用户使用的版本不同,请对照对应的上游标签进行验证。
Workflow
工作流程
- Choose the component. rewrites,
transformdrops,filterdecides whether to retain traces, andtail_samplingsends telemetry to pipelines. A component controls its available contexts and functions.routing - Choose the lowest usable context. Lower contexts can read their parents (for example, a span
can read ), but parents cannot read children. Use
resource.attributesfor point attributes instead of traversingdatapoint.metric.data_points - Verify every emitted function, then write the statement. Confirm each function's exact
identifier and signature in references/functions.md. If an identifier
is absent, treat it as unsupported instead of deriving or substituting a plausible name. An
editor such as or
setmutates data and may have adelete_keycondition. Converters such aswhereandParseJSONreturn values; they do not mutate.IsMatch - Set error behavior deliberately. logs statement errors and continues;
ignorecontinues without logging;silentreturns the error and can cause the component to drop the payload. In v0.158, transform and filter default topropagate; routing also defaults toignorewhile its beta default-error feature gate is enabled. For routing,ignoresends an errored payload toignore; configure that fallback or the payload is dropped.default_pipelines - Verify end to end. Validate the exact Collector version, then send known telemetry and inspect file-exporter output. Use the telemetrygen recipe.
ottl
set(span.attributes["env"], "prod") where resource.attributes["env"] == nil- 选择组件。用于重写数据,
transform用于丢弃数据,filter用于决定是否保留链路追踪数据,tail_sampling用于将遥测数据发送到不同流水线。每个组件会管控其可用的上下文和函数。routing - 选择可用的最低层级上下文。低层级上下文可以读取其父级上下文(例如,span可以读取),但父级上下文无法读取子级上下文。对于指标点属性,请使用
resource.attributes上下文,而非遍历datapoint。metric.data_points - 验证每个要使用的函数,再编写语句。请在references/functions.md中确认每个函数的确切标识符和签名。如果某个标识符不存在,则视为不支持该函数,不要自行推导或替换为看似合理的名称。或
set这类编辑函数会修改数据,并且可搭配delete_key条件使用。where和ParseJSON这类转换函数仅返回值,不会修改数据。IsMatch - 明确设置错误处理行为。会记录语句错误并继续执行;
ignore会继续执行但不记录错误;silent会返回错误,可能导致组件丢弃数据载荷。在v0.158版本中,transform和filter的默认错误处理行为是propagate;当routing的beta版默认错误特性开关启用时,其默认行为也为ignore。对于routing组件,ignore会将处理出错的载荷发送到ignore;请配置该回退机制,否则载荷会被丢弃。default_pipelines - 端到端验证。确认Collector的准确版本,然后发送已知的遥测数据并检查file-exporter的输出。可使用telemetrygen 方案。
ottl
set(span.attributes["env"], "prod") where resource.attributes["env"] == nilLoad only what the task needs
仅加载任务所需内容
- Contexts — exact paths, hierarchy, enums, and request metadata.
- Functions — editor/converter signatures and release availability.
- Quick reference — component YAML, recipes, escaping, troubleshooting, and safe skeletons.
For a single path or function, read only the relevant section instead of loading the full catalogs.
- 上下文 — 精确路径、层级结构、枚举值和请求元数据。
- 函数 — 编辑/转换函数的签名及版本可用性。
- 快速参考 — 组件YAML配置、方案、转义规则、故障排查和安全模板。
如果仅需查看单个路径或函数,请只阅读相关章节,无需加载完整目录。
Safety and correctness gates
安全与正确性校验
- Guard optional or polymorphic input before conversion: ,
where x != nil, or the appropriate type check.IsString(x) - For JSON-object-only work, guard both the type and shape before calling , for example
ParseJSON. The RE2IsString(log.body) and IsMatch(log.body.string, "(?s)^\\s*\\{.*\\}\\s*$")flag admits pretty-printed objects containing newlines. Checking(?s)after parsing does not prevent arrays or scalar JSON from being parsed.IsMap - On a version-pinned request, confirm every chosen path and function against that release tag;
do not assume a function listed for this skill's v0.158 anchor exists in an older release.
For v0.156 JSON-object parsing, ,
ParseJSON, andIsStringare available without the v0.157 alpha lambda feature gate.IsMatch - Request metadata is read-only and may contain credentials. Copy only explicitly allowlisted,
non-sensitive keys. OTLP metadata routing requires on the receiver. HTTP/client header spelling may retain its form (
include_metadata: true); gRPC metadata keys are lowercase (otelcol.client.metadata["X-Tenant"][0]).otelcol.grpc.metadata["x-tenant"][0] - The routing context is deprecated as of v0.156; use
requestorotelcol.client.metadata.otelcol.grpc.metadata - Log-record-specific rewrites of shared resource or scope data require and the alpha
flatten_data: truegate. This copies and regroups data; do not enable it accidentally.transform.flatten.logs - Hashing an identifier does not necessarily anonymize it. Apply the organization's data-handling policy before retaining deterministic hashes of personal data.
- 在转换前对可选或多态输入进行校验:使用、
where x != nil或相应的类型检查。IsString(x) - 仅处理JSON对象时,调用前需同时校验类型和格式,例如
ParseJSON。RE2的IsString(log.body) and IsMatch(log.body.string, "(?s)^\\s*\\{.*\\}\\s*$")标志允许包含换行符的格式化JSON对象。解析后检查(?s)无法阻止数组或标量JSON被解析。IsMap - 如果请求指定了版本,请对照该版本标签确认所选的每个路径和函数;不要假设本技能基于v0.158版本列出的函数在旧版本中也存在。对于v0.156版本的JSON对象解析,、
ParseJSON和IsString无需启用v0.157的alpha lambda特性开关即可使用。IsMatch - 请求元数据为只读状态,可能包含凭证信息。仅复制明确允许的非敏感键。OTLP元数据路由要求接收器设置。HTTP/客户端请求头的拼写会保留原有格式(如
include_metadata: true);gRPC元数据键为小写(如otelcol.client.metadata["X-Tenant"][0])。otelcol.grpc.metadata["x-tenant"][0] - 从v0.156版本开始,routing的上下文已被弃用;请使用
request或otelcol.client.metadata。otelcol.grpc.metadata - 针对日志记录重写共享资源或范围数据时,需要设置并启用alpha版
flatten_data: true特性开关。此操作会复制并重新分组数据,请勿意外启用。transform.flatten.logs - 对标识符进行哈希处理并不一定能实现匿名化。在保留个人数据的确定性哈希值前,请遵循组织的数据处理政策。
Frequent syntax traps
常见语法陷阱
- In Collector YAML, write an OTTL replacement backreference as
${1}. A replacement such as$${1}is literal and silently fails to substitute the capture.$1REDACTED - Go RE2 rejects large counted repetitions such as ; use
(.{1024}).*with a nil/type guard andSubstring, orLenfor a map.truncate_all - Current span-event paths use , not
spanevent.*. Cache paths are context-qualified, such asspan_event.*.span.cache["parsed"] - Use ;
Decode(value, "base64")is deprecated.Base64Decode - Regex escapes inside OTTL strings are doubled (,
\\d,\\s).\\.
- 在Collector的YAML配置中,OTTL替换的反向引用需要写为
${1}。像$${1}这样的替换会被视为字面量,无法静默替换捕获的内容。$1REDACTED - Go RE2不支持大数量的重复匹配,如;请结合空值/类型校验和
(.{1024}).*函数使用Len,或对映射使用Substring。truncate_all - 当前的span事件路径使用,而非
spanevent.*。缓存路径需要带上上下文限定,例如span_event.*。span.cache["parsed"] - 请使用;
Decode(value, "base64")已被弃用。Base64Decode - OTTL字符串中的正则表达式转义符需要写两次(如、
\\d、\\s)。\\.