Grafana Cloud

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:

  1. Calculate values: Perform arithmetic operations on component exports and literals.
  2. Compare data: Make decisions based on component states or configuration values.
  3. Combine strings: Build dynamic identifiers, paths, or configuration strings.
  4. 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

OperatorDescription
+Adds two numbers.
-Subtracts two numbers.
*Multiplies two numbers.
/Divides two numbers.
%Computes the remainder after dividing two numbers.
^Raises a number to the specified power.

String operators

OperatorDescription
+Concatenate two strings or two secrets, or a string and a secret.

String concatenation is particularly useful for building dynamic values from component exports:

Alloy
prometheus.scrape "app" {
  targets = [{ "__address__" = local.file.host.content + ":" + local.file.port.content }]
  job_name = "app-" + sys.env("ENVIRONMENT")
}

Comparison operators

OperatorDescription
==Returns true when two values are equal.
!=Returns true when two values aren’t equal.
<Returns true when the left value is less than the right value.
<=Returns true when the left value is less than or equal to the right value.
>Returns true when the left value is greater than the right value.
>=Returns true when the left value is greater than or equal to the right value.

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:

  1. Boolean values are equal if both are either true or false.
  2. Numerical (integer and floating-point) values follow the usual ordering.
  3. String values follow lexical ordering, byte-wise.
  4. Objects are equal if all their fields are equal.
  5. Arrays are equal if their corresponding elements are equal.

Comparison operators are commonly used for conditional configuration:

Alloy
// Check if environment matches
remote_write_enabled = sys.env("ENVIRONMENT") == "production"

Logical operators

OperatorDescription
&&Returns true when both the left and right values are true.
||Returns true when either the left or right value is true.
!Negates a boolean value.

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: