|
| 1 | +# River: A Flow-optimized config language |
| 2 | + |
| 3 | +* Date: 2022-06-27 |
| 4 | +* Author: Robert Fratto (@rfratto), Matt Durham (@mattdurham) |
| 5 | +* PR: TODO |
| 6 | +* Status: Draft |
| 7 | + |
| 8 | +## Summary |
| 9 | + |
| 10 | +Grafana Agent developers have been working towards a feature called Grafana |
| 11 | +Agent Flow ([RFC-0004][]), a component-based re-imagining of Grafana Agent |
| 12 | +which compartmentalize the different configurable pieces of the agent, allowing |
| 13 | +users to more easily understand and debug configuration issues. Grafana Agent |
| 14 | +Flow was purposefully scoped broadly to allow for exploring many different |
| 15 | +component-based approaches for prototyping the experimental feature. |
| 16 | + |
| 17 | +The current implementation strategy focuses around expressions: settings for |
| 18 | +components can be derived from expressions which can reference and mutate the |
| 19 | +outputs of other components. Values can refer to arbitrary Go values like |
| 20 | +interfaces or channels, enabling component developers to easily allow users to |
| 21 | +construct data pipelines using Go APIs without requiring knowledge of the |
| 22 | +underlying implementation. |
| 23 | + |
| 24 | +The initial expressions prototype used [HCL][], which Flow's needs at the time. |
| 25 | +However, our growing dependency on passing around arbitrary Go values started |
| 26 | +to conflict with the limitations of HCL, making HCL increasingly insufficient |
| 27 | +for our specific use case. |
| 28 | + |
| 29 | +We examined alternatives to HCL such as YAML, CUE, Jsonnet, Lua, and Go itself. |
| 30 | +Eventually, we determined that the way we use arbitrary Go values in |
| 31 | +expressions for constructing pipelines is a new use case warranting a |
| 32 | +custom-built language. |
| 33 | + |
| 34 | +This document proposes River, an HCL-inspired declarative expressions-based |
| 35 | +language for continuous runtime evaluation. The decision to propose a new |
| 36 | +language is not taken lightly, and is seen as the last resort. As such, much of |
| 37 | +this proposal will focus on the rationale leading to this choice. |
| 38 | + |
| 39 | +## Goals |
| 40 | + |
| 41 | +* Minimize learning curve as much as possible to reduce friction |
| 42 | +* Make it easy for developers to consume for Flow components |
| 43 | +* Expose error messages in an easily understandable and actionable way |
| 44 | +* Natively support using Go values of any type |
| 45 | +* Natively support passing around and invoking real Go functions |
| 46 | + |
| 47 | +The language design will be scoped as small as possible, and new features will |
| 48 | +only be added over time as they are determined to be strictly necessary for |
| 49 | +Flow. |
| 50 | + |
| 51 | +## Non-Goals |
| 52 | + |
| 53 | +We are not aiming to create a general purpose configuration language. While it |
| 54 | +would be possible for River to eventually be used in different contexts by |
| 55 | +different projects, the primary goal today is specifically targeting Grafana |
| 56 | +Agent Flow. |
| 57 | + |
| 58 | +We will not provide a full specification for River here, only lightly |
| 59 | +describing it to allow implementation details to change over time. |
| 60 | + |
| 61 | +## Rationale |
| 62 | + |
| 63 | +### Why an expression language? Why not YAML? |
| 64 | + |
| 65 | +The entire rationale for creating a new language depends on the rationale that |
| 66 | +expressions provide a useful amount of capabilities to users. Expressions |
| 67 | +enable users to manipulate values to meet their own use cases in ways that |
| 68 | +otherwise would require dedicated feature work, such as: |
| 69 | + |
| 70 | +* Allowing users to merge metadata together from distinct sources when adding |
| 71 | + labels to metrics |
| 72 | + |
| 73 | +* Allowing users to chain Prometheus service discoveries (e.g., feed the output |
| 74 | + of Kubernetes Service Discovery into HTTP Service Discovery) |
| 75 | + |
| 76 | +* Allowing users to perform custom conditional logic, such as increasing rate |
| 77 | + limits during busier business months. |
| 78 | + |
| 79 | +Without expressions, we would need more components for common tasks. A |
| 80 | +`concat()` function call can be used to combine lists of discovered Prometheus |
| 81 | +targets, but without expressions, there would likely need to be a dedicated |
| 82 | +component for aggregating sets of targets together. |
| 83 | + |
| 84 | +The belief is that the work required to use and maintain an expression language |
| 85 | +is far less than the combined work to implement features that would be handled |
| 86 | +by expressions out of the box. |
| 87 | + |
| 88 | +YAML by itself does not support expressions. While expressions could be added |
| 89 | +to YAML through the use of templates (e.g., `field_a: {{ some_variable + 5 |
| 90 | +}}`), it is beyond the scope of what YAML was intended for and would be more |
| 91 | +cumbersome to use compared to a language where expressions are a first-class |
| 92 | +concept. |
| 93 | + |
| 94 | +### Why an embedded language? |
| 95 | + |
| 96 | +Embedded languages are typically known for the ability for maintainers of the |
| 97 | +project to expose APIs to users of the embedded language, such as the Lua API |
| 98 | +used by Neovim. Embedded languages typically imply tight integration with the |
| 99 | +application embedding them, as opposed to something like YAML which is a |
| 100 | +language consumed once at load time. |
| 101 | + |
| 102 | +An embedded language is a good fit for Flow: |
| 103 | + |
| 104 | +* It makes it easy for developers to expose APIs which users can interact with |
| 105 | + or pass around. These APIs can be opaque arbitrary Go types which the user |
| 106 | + doesn't need to know the detail of, only that it refers to something like a |
| 107 | + stream of metric samples. |
| 108 | + |
| 109 | +* It is well-suited for continuous evaluation (i.e., the core feature of Flow) |
| 110 | + so configuration can adapt to a changing environment. |
| 111 | + |
| 112 | +### Why a declarative language? Why not Lua? |
| 113 | + |
| 114 | +The language Flow relies on should have a minimal learning curve. While a |
| 115 | +language like Lua could likely be a decent fit for Flow, imperative languages |
| 116 | +have steeper learning curves compared to declarative languages. |
| 117 | + |
| 118 | +Declarative languages natively map to configuration files, since configuration |
| 119 | +files are used to tell the application the desired state, reducing the learning |
| 120 | +curve for the language and making it easier for users to reason about what the |
| 121 | +final config state should be. |
| 122 | + |
| 123 | +### Why not HCL? |
| 124 | + |
| 125 | +> For some background, it's important to note that HCL can be considered two |
| 126 | +> separate projects: hashicorp/hcl (the language and expression evaluator) and |
| 127 | +> zclconf/go-cty (the value and type system used by HCL). |
| 128 | +
|
| 129 | +HCL was the obvious first choice for the Flow prototype: it supports |
| 130 | +expressions, you can expose functions for users to call, and its syntax has a |
| 131 | +small learning curve. |
| 132 | + |
| 133 | +However, I found the [schema-driven processing][] API exposed by HCL to be |
| 134 | +difficult to work with for Flow, requiring a lot of boilerplate. While there is |
| 135 | +a library to interoperate with tagged Go structs, it was insufficient for |
| 136 | +passing around arbitrary Go values, requiring me to [fork][gohcl] both |
| 137 | +github.com/hashicorp/hcl/v2/gohcl and github.com/zclconf/go-cty/cty/gocty to |
| 138 | +reduce boilerplate. This fork contains a non-trivial amount of changes that |
| 139 | +would need to be maintained or contributed upstream to be tenable. |
| 140 | + |
| 141 | +Additionally, there is desired functionality that is not supported today in |
| 142 | +HCL/go-cty: |
| 143 | + |
| 144 | +1. A stronger focus on performance and memory usage, changing go-cty to operate |
| 145 | + around Go values instead of converting Go values to a custom representation. |
| 146 | +2. Ability to disable go-cty's requirement that strings are UTF-8 encoded |
| 147 | +3. Pass around functions as go-cty values (e.g., to allow a clustering |
| 148 | + component to expose a function to check for ownership of key against a hash |
| 149 | + ring) |
| 150 | +4. Ability to declare local variables in a scope without needing a `locals` |
| 151 | + block like as seen in Terraform. |
| 152 | + |
| 153 | +The combination of desired changes across gohcl and go-cty, the fork that was |
| 154 | +already necessary to make it easier to adopt HCL for Flow, and the desire to |
| 155 | +have a stronger interaction with arbitrary Go values led to the decision that a |
| 156 | +new Flow-specific language was warranted. |
| 157 | + |
| 158 | +### Why now? |
| 159 | + |
| 160 | +Grafana Agent Flow is already a dramatic change to the Agent. To avoid users |
| 161 | +being exhausted from the frequency of dramatic changes, it would be ideal for |
| 162 | +Grafana Agent Flow to ship with River instead of eventually migrating to River. |
| 163 | + |
| 164 | +## Minimizing impact |
| 165 | + |
| 166 | +New languages always have some amount of learning curve, and if the learning |
| 167 | +curve is too steep, the language will fail to be adopted. |
| 168 | + |
| 169 | +We will minimize this impact of a new language by: |
| 170 | + |
| 171 | +* Minimizing the learning curve as much as possible by not creating |
| 172 | + too many novel ideas at the language level. |
| 173 | + |
| 174 | +* Tend the syntax towards allowing users to copy-and-paste examples to learn as |
| 175 | + they go. |
| 176 | + |
| 177 | +* Heavily document the language so that all questions a user may have is |
| 178 | + answered. |
| 179 | + |
| 180 | +* Ensuring that error messages explain the problem and the resolution is |
| 181 | + obvious. |
| 182 | + |
| 183 | +## Proposal |
| 184 | + |
| 185 | +River is inspired by HCL. However, some of the syntax used by HCL will be |
| 186 | +changed to make River more easily identifiable as a different language. |
| 187 | + |
| 188 | +River focuses on expressions, attributes, and blocks. |
| 189 | + |
| 190 | +### Expressions |
| 191 | + |
| 192 | +Expressions resolve to values used by River. The type of expressions are: |
| 193 | + |
| 194 | +* Literal expressions: |
| 195 | + * Booleans: `true`, `false` |
| 196 | + * Numbers: `3`, `3.5`, `3e+10`, etc. |
| 197 | + * Strings: `"Hello, world!"` |
| 198 | +* Unary operations: |
| 199 | + * Logical NOT: `!true` |
| 200 | + * Negative: `-5` |
| 201 | +* Binary operations: |
| 202 | + * Math operators: `+`, `-`, `*`, `/`, `^` (pow) |
| 203 | + * Equality operators: `==`, `!=`, `<`, `<=`, `>`, `>=` |
| 204 | + * Logical operators: `||`, `&&` |
| 205 | +* Lists: `[1, 2, 3]` |
| 206 | +* Objects: `{ a = 5, b = 6 }` |
| 207 | +* Variable reference: `foobar` |
| 208 | +* Indexing: `some_list[0]` |
| 209 | +* Field access: `some_object.field_a` |
| 210 | +* Function calls: `concat([0, 1], [2, 3])` |
| 211 | +* Parenthesized expression: `(3 + 5)` |
| 212 | + |
| 213 | +### Attributes |
| 214 | + |
| 215 | +Attributes are key-value pairs which set individual settings, formatted as |
| 216 | +`<identifier> = <expression>`: |
| 217 | + |
| 218 | +``` |
| 219 | +log_level = "debug" |
| 220 | +log_format = "logfmt" |
| 221 | +``` |
| 222 | + |
| 223 | +### Blocks |
| 224 | + |
| 225 | +Blocks are named groupings of attributes, wrapping in curly braces. Blocks can |
| 226 | +also contain other blocks. |
| 227 | + |
| 228 | +``` |
| 229 | +server { |
| 230 | + http_address = "127.0.0.1:12345" |
| 231 | +} |
| 232 | +
|
| 233 | +prometheus.storage { |
| 234 | + remote_write { |
| 235 | + url = "http://localhost:9090/api/v1/write" |
| 236 | + } |
| 237 | +
|
| 238 | + remote_write { |
| 239 | + url = "http://localhost:9091/api/v1/write" |
| 240 | + } |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +Block names must consist of one or more identifiers separated by `.`. Blocks |
| 245 | +can also be given user-specified labels, denoted as a string wrapped in quotes: |
| 246 | + |
| 247 | +``` |
| 248 | +prometheus.storage "primary" { |
| 249 | + // ... |
| 250 | +} |
| 251 | +
|
| 252 | +prometheus.storage "secondary" { |
| 253 | + // ... |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +### Type system |
| 258 | + |
| 259 | +Values are categorized as being one of the following: |
| 260 | + |
| 261 | +* `number` |
| 262 | +* `bool` |
| 263 | +* `string` |
| 264 | +* `list` |
| 265 | + * Elements within the list do not have to be the same type. |
| 266 | +* `object` |
| 267 | +* `function` |
| 268 | + * Function values differentiate River from HCL/go-cty, which does not support |
| 269 | + passing around or invoking function values. |
| 270 | +* `capsule` |
| 271 | + * Capsule is a catch-all type which refers to some arbitrary Go value which |
| 272 | + is not one of the other types. For example, `<-chan int` would be |
| 273 | + represented as a capsule in River. |
| 274 | + |
| 275 | +River types map to Go types as follows: |
| 276 | + |
| 277 | +* `number`: Go `int*`, `uint*`, `float*` |
| 278 | +* `bool`: Go `bool` |
| 279 | +* `string`: Go `string`, `[]byte` |
| 280 | +* `list`: Go `[]T`, `[...]T`. |
| 281 | +* `object`: Go `map[string]T`, and structs with at least one River tag |
| 282 | +* `function`: Any Go function. |
| 283 | + * If the final return value of the Go function is an error, it will be |
| 284 | + checked on calling; a non-nil error will cause the evaluation of the |
| 285 | + function to fail. |
| 286 | +* `capsule`: All other Go values. |
| 287 | + |
| 288 | +River acts like a combination of a configuration language like HCL and an |
| 289 | +embedded language like Lua due to its focus on supporting all Go values, |
| 290 | +including values which cannot be directly represented by the user (such as Go |
| 291 | +interfaces). This enables developers to use native Go types for easily passing |
| 292 | +around business logic which users wire together through their configuration. |
| 293 | + |
| 294 | +### River struct tags |
| 295 | + |
| 296 | +River struct tags are used to converting between River values and Go structs. |
| 297 | +Tags take one of the following forms: |
| 298 | + |
| 299 | +* `river:"example,attr"`: required attribute named `example` |
| 300 | +* `river:"example,attr,optional"`: optional attribute named `example` |
| 301 | +* `river:"example,block"`: required block named `example` |
| 302 | +* `river:"example,block,optional"`: optional block named `example` |
| 303 | +* `river:",label"`: Used for decoding block labels into a `string`. |
| 304 | + |
| 305 | +Attribute and block names must be unique across the whole type. When encoding a |
| 306 | +Go struct, inner blocks are converted into objects. Attributes are converted |
| 307 | +into River values of the appropriate type. |
| 308 | + |
| 309 | +### Errors |
| 310 | + |
| 311 | +There are multiple types of errors which may occur: |
| 312 | + |
| 313 | +* Lexing / parsing errors |
| 314 | +* Evaluation errors (when evaluating an expression into a River value) |
| 315 | +* Decoding errors (when converting a River value into a Go value) |
| 316 | +* Validation errors (when Go code validates a value) |
| 317 | + |
| 318 | +Errors should be displayed to the user in a way that gives as much information |
| 319 | +as possible. Errors which involve unexpected values should print the value to |
| 320 | +ease debugging. |
| 321 | + |
| 322 | +For this `example.river` config file which expects the `targets` field to be a |
| 323 | +list of objects: |
| 324 | + |
| 325 | +``` |
| 326 | +prometheus.scrape "example1" { |
| 327 | + targets = 5 |
| 328 | +} |
| 329 | +
|
| 330 | +prometheus.scrape "example2" { |
| 331 | + targets = [5] |
| 332 | +} |
| 333 | +
|
| 334 | +prometheus.scrape "example3" { |
| 335 | + targets = some_list_of_objects + 5 |
| 336 | +} |
| 337 | +``` |
| 338 | + |
| 339 | +Errors could be shown to the user like: |
| 340 | + |
| 341 | +``` |
| 342 | +example.river:2:3: targets expects list value, got number |
| 343 | +
|
| 344 | + | targets = 5 |
| 345 | +
|
| 346 | + Value: |
| 347 | + 5 |
| 348 | +
|
| 349 | +example.river:6:3: list element 0 must be object, got number |
| 350 | +
|
| 351 | + | targets = [5] |
| 352 | +
|
| 353 | + Value: |
| 354 | + 5 |
| 355 | +
|
| 356 | +example.river:10:13: cannot perform `+` on types list and number |
| 357 | +
|
| 358 | + | some_list_of_objects + 5 |
| 359 | +
|
| 360 | + Expression: |
| 361 | + [{}] + 5 |
| 362 | +``` |
| 363 | + |
| 364 | +The errors print out the offending portion of the config file alongside the |
| 365 | +offending values. Printing out the offending values is useful when the values |
| 366 | +come from the result of referring to a variable or calling a function. |
| 367 | + |
| 368 | +### Concerns |
| 369 | + |
| 370 | +No existing tooling for River will exist from day one. While the initial |
| 371 | +implementation should include a formatter, tools like syntax highlighting or |
| 372 | +LSPs won't exist. |
| 373 | + |
| 374 | +## Alternatives considered |
| 375 | + |
| 376 | +### Handles |
| 377 | + |
| 378 | +Instead of passing around literal arbitrary Go values, handles could be used to |
| 379 | +_refer_ to arbitrary Go values. For example, a number could refer to some entry |
| 380 | +in an in-memory store which holds a Go channel or interface. |
| 381 | + |
| 382 | +Pros: |
| 383 | +* Works better with HCL in its current state without needing the gohcl fork |
| 384 | +* Would enable YAML, CUE, and Jsonnet to pass around arbitrary values |
| 385 | + |
| 386 | +Cons: |
| 387 | +* Still wouldn't allow HCL to pass around functions as values |
| 388 | +* More tedious for developers to work with (they now have to exchange handles |
| 389 | + for values). |
| 390 | +* Requires extra logic for making sure resources that handles refer to don't |
| 391 | + leak. |
| 392 | + |
| 393 | +[RFC-0004]: ./0004-agent-flow.md |
| 394 | +[HCL]: https://github.com/hashicorp/hcl |
| 395 | +[go-cty]: github.com/zclconf/go-cty |
| 396 | +[gohcl]: https://github.com/rfratto/gohcl |
| 397 | +[schema-driven processing]: https://github.com/hashicorp/hcl/blob/main/spec.md#schema-driven-processing |
0 commit comments