|
| 1 | +--- |
| 2 | +title: OTTL contexts just got easier with context inference |
| 3 | +linkTitle: OTTL contexts just got easier |
| 4 | +date: 2025-02-20 |
| 5 | +author: '[Edmo Vamerlatti Costa](https://github.com/edmocosta) (Elastic)' |
| 6 | +issue: 6289 |
| 7 | +sig: Collector SIG |
| 8 | +cSpell:ignore: OTTL Vamerlatti |
| 9 | +--- |
| 10 | + |
| 11 | +Selecting the right OTTL context for running statements can be challenging, even |
| 12 | +for experienced users. Choosing the correct option impacts both accuracy and |
| 13 | +efficiency, as using higher-level OTTL contexts can avoid unnecessary iterations |
| 14 | +through nested lower-level contexts. |
| 15 | + |
| 16 | +To simplify this process, the OpenTelemetry community is excited to announce |
| 17 | +OTTL |
| 18 | +[context inference](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/transformprocessor/README.md#context-inference) |
| 19 | +support for the |
| 20 | +[transform processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/transformprocessor). |
| 21 | +This feature removes the need for users to understand the underlying context |
| 22 | +concept of OTTL, allowing them to focus solely on their data. It also improves |
| 23 | +statement processing efficiency by automatically selecting the most appropriate |
| 24 | +OTTL context. This optimization ensures that data transformations are both |
| 25 | +accurate and performant. |
| 26 | + |
| 27 | +## How does it work? |
| 28 | + |
| 29 | +Starting with version `0.120.0`, the transform processor supports two new |
| 30 | +context-inferred configuration styles. The first one offers a simpler and |
| 31 | +flatter approach, while the second closely resembles the existing configuration |
| 32 | +format. |
| 33 | + |
| 34 | +### Basic configuration |
| 35 | + |
| 36 | +The |
| 37 | +[basic configuration](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/transformprocessor/README.md#basic-config) |
| 38 | +style simplifies configuration by allowing users to list all statements |
| 39 | +together, without worrying about OTTL contexts or extra configuration |
| 40 | +structures. This style support statements from multiple OTTL contexts and does |
| 41 | +not require grouping them separately. |
| 42 | + |
| 43 | +To illustrate this, compare the following configuration: |
| 44 | + |
| 45 | +```yaml |
| 46 | +metric_statements: |
| 47 | + - context: resource |
| 48 | + statements: |
| 49 | + - keep_keys(attributes, ["host.name"]) |
| 50 | + - context: metric |
| 51 | + statements: |
| 52 | + - set(description, "Sum") where type == "Sum" |
| 53 | + - convert_sum_to_gauge() where name == "system.processes.count" |
| 54 | + - context: datapoint |
| 55 | + statements: |
| 56 | + - limit(attributes, 100, ["host.name"]) |
| 57 | +``` |
| 58 | +
|
| 59 | +With the new basic configuration style, the same logic is expressed more |
| 60 | +concisely by simply providing a list of statements: |
| 61 | +
|
| 62 | +```yaml |
| 63 | +metric_statements: |
| 64 | + - keep_keys(resource.attributes, ["host.name"]) |
| 65 | + - set(metric.description, "Sum") where metric.type == "Sum" |
| 66 | + - convert_sum_to_gauge() where metric.name == "system.processes.count" |
| 67 | + - limit(datapoint.attributes, 100, ["host.name"]) |
| 68 | +``` |
| 69 | +
|
| 70 | +This streamlined approach enhances readability and makes configuration more |
| 71 | +intuitive. To use this configuration style, all paths in the statements must be |
| 72 | +prefixed with their respective OTTL contexts. These prefixes are required for |
| 73 | +all context-inferred configurations and serve as hints for selecting the best |
| 74 | +match. It also makes statements unambiguous and portable between components |
| 75 | +using OTTL. |
| 76 | +
|
| 77 | +### Advanced configuration |
| 78 | +
|
| 79 | +The context-inferred |
| 80 | +[advanced configuration](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/transformprocessor/README.md#advanced-config) |
| 81 | +style closely resembles the existing format and allows users to leverage the |
| 82 | +benefits of context inference while providing granular control over statement |
| 83 | +configurations, such as `error_mode` and `conditions`. For example, consider the |
| 84 | +following configuration: |
| 85 | + |
| 86 | +```yaml |
| 87 | +metric_statements: |
| 88 | + - context: datapoint |
| 89 | + conditions: |
| 90 | + - resource.attributes["service.name"] == "my.service" |
| 91 | + statements: |
| 92 | + - set(metric.description, "counter") where attributes["my.attr"] == "some" |
| 93 | +``` |
| 94 | + |
| 95 | +The above can now be written as: |
| 96 | + |
| 97 | +<!-- prettier-ignore --> |
| 98 | +```yaml |
| 99 | +metric_statements: |
| 100 | + - conditions: |
| 101 | + - resource.attributes["service.name"] == "my.service" |
| 102 | + statements: |
| 103 | + - set(metric.description, "counter") where datapoint.attributes["my.attr"] == "some" |
| 104 | +``` |
| 105 | + |
| 106 | +In this example, the `context` value is omitted and is automatically inferred to |
| 107 | +`datapoint`, as it is the only OTTL context present in the statements that |
| 108 | +supports parsing both `datapoint` and `metric` data. |
| 109 | + |
| 110 | +If we update the above configuration removing the `datapoint` usage: |
| 111 | + |
| 112 | +```yaml |
| 113 | +metric_statements: |
| 114 | + - conditions: |
| 115 | + - resource.attributes["service.name"] == "my.service" |
| 116 | + statements: |
| 117 | + - set(metric.description, "counter") |
| 118 | +``` |
| 119 | + |
| 120 | +The context inferrer would select the `metric` OTTL context instead, since no |
| 121 | +data points are accessed. Although it would be possible to run the statements |
| 122 | +using the `datapoint` OTTL context, `metric` is the most efficient option. |
| 123 | + |
| 124 | +### Which configuration style should I choose? |
| 125 | + |
| 126 | +The [basic configuration](#basic-configuration) style is best suited for |
| 127 | +scenarios where simplicity and ease of use are paramount. It is ideal for simple |
| 128 | +use cases where your configuration needs are straightforward and do not require |
| 129 | +the use of additional configurations keys, allowing you to quickly set up your |
| 130 | +statements with minimal effort and without needing to understand the underlying |
| 131 | +concept of OTTL contexts. |
| 132 | + |
| 133 | +The [advanced configuration](#advanced-configuration) style is more detailed and |
| 134 | +allows the use of additional configuration keys such as `error_mode` and |
| 135 | +`conditions`. It supports statements from multiple OTTL contexts. However, |
| 136 | +unlike the basic configuration style, it may require splitting them into |
| 137 | +separate configuration groups (see |
| 138 | +[advanced config](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/transformprocessor#advanced-config)). |
| 139 | +In terms of performance, the advanced configuration is slightly faster than the |
| 140 | +basic configuration, making it a better choice for complex scenarios or |
| 141 | +configurations with a high number of statements. |
| 142 | + |
| 143 | +## Try it out |
| 144 | + |
| 145 | +As we wrap up, we encourage users to explore this new functionality and take |
| 146 | +advantage of its benefits in their telemetry pipelines! |
| 147 | + |
| 148 | +If you have any questions or suggestions, we’d love to hear from you! Join the |
| 149 | +conversation in the `#otel-collector` channel on the |
| 150 | +[CNCF Slack workspace](https://slack.cncf.io/). |
0 commit comments