Operators
You learned how to reference component exports to connect your pipelines in the previous topic. Now you’ll learn how to use operators to manipulate, transform, and combine values in your expressions.
Operators are essential for creating dynamic configurations that can:
- Calculate values: Perform arithmetic operations on component exports and literals.
- Compare data: Make decisions based on component states or configuration values.
- Combine strings: Build dynamic identifiers, paths, or configuration strings.
- Access nested data: Extract values from complex objects and arrays.
The component controller evaluates all operators following the standard PEMDAS order of mathematical operations.
Arithmetic operators
String operators
String concatenation is particularly useful for building dynamic values from component exports:
prometheus.scrape "app" {
targets = [{ "__address__" = local.file.host.content + ":" + local.file.port.content }]
job_name = "app-" + sys.env("ENVIRONMENT")
}Comparison operators
You can use the equality operators == and != with any operands.
The operands in ordering operators <, <=, >, and >= must be orderable and of the same type.
The results of these comparisons are:
- Boolean values are equal if both are either
trueorfalse. - Numerical (integer and floating-point) values follow the usual ordering.
- String values follow lexical ordering, byte-wise.
- Objects are equal if all their fields are equal.
- Arrays are equal if their corresponding elements are equal.
Comparison operators are commonly used for conditional configuration:
// Check if environment matches
remote_write_enabled = sys.env("ENVIRONMENT") == "production"Logical operators
Logical operators work with boolean values and return a boolean result.
Assignment operator
The Alloy configuration syntax uses = as the assignment operator.
An assignment statement can only assign a single value. Each value must be assignable to the attribute or object key: