Components
Components are the building blocks of Alloy. Each component performs a single task, such as retrieving secrets, collecting metrics, or processing data.
You learned about blocks, attributes, and expressions in the previous section. Components use the same syntax structure. They’re special blocks that Alloy knows how to execute. When you define a component block in your configuration, Alloy creates a running instance that performs the component’s specific task.
Components are the key to the flexibility of Alloy. You can combine them to create data pipelines that collect, transform, and send telemetry data exactly how you need.
Component structure
Every component has two main parts:
- Arguments: Settings that configure the component’s behavior. These are the attributes and blocks you define inside the component block.
- Exports: Values that the component makes available to other components. The running component generates these exports and they can change over time.
When Alloy loads your configuration:
- It reads the arguments you provided in each component block.
- It creates a running instance of the component with those arguments.
- The component starts its work and may update its exports as it runs.
- Other components can reference these exports in their arguments.
Component syntax
Components use the block syntax you learned about earlier. The general pattern is:
COMPONENT_NAME "LABEL" {
// Arguments (attributes and nested blocks)
attribute_name = "value"
nested_block {
setting = "value"
}
}The COMPONENT_NAME tells Alloy which type of component to create.
The "LABEL" is a unique identifier you choose to distinguish between multiple instances of the same component type.
Component names
Each component has a name that describes its purpose. For example:
local.fileretrieves file contents from disk.prometheus.scrapecollects Prometheus metrics.loki.writesends log data to Loki.
You define components by specifying the component name with a user-defined label:
local.file "my_config" {
filename = "/etc/app/config.yaml"
}
prometheus.scrape "api_metrics" {
targets = [{"__address__" = "localhost:8080"}]
forward_to = [prometheus.remote_write.default.receiver]
}Component references
You reference components by combining their name with their label.
For example, you can reference the local.file component labeled my_config as local.file.my_config.
The combination of component name and label must be unique in your configuration. This allows you to define multiple instances of the same component type:
prometheus.scrape "api" {
targets = [{"__address__" = "api.example.com:8080"}]
forward_to = [prometheus.remote_write.production.receiver]
}
prometheus.scrape "database" {
targets = [{"__address__" = "db.example.com:9090"}]
forward_to = [prometheus.remote_write.production.receiver]
}Component exports
Components can share data through exports. When one component references another component’s export, it creates a connection between them. You’ll learn more about these component references in Expressions.
local.file "api_key" {
filename = "/etc/secrets/api.key"
}
prometheus.remote_write "production" {
endpoint {
url = "https://prometheus.example.com/api/v1/write"
basic_auth {
username = "metrics"
password = local.file.api_key.content // Reference the file content
}
}
}In this example:
- The
local.filecomponent reads a file and exports its content. - The
prometheus.remote_writecomponent uses that content as a password. - When the file changes, Alloy automatically updates the
local.filecomponent’s exports. - This causes Alloy to re-evaluate the
prometheus.remote_writecomponent with the new password.
You’ll learn the details of how to write these references in Expressions.
Next steps
Now that you understand what components are and how they work, learn how to use them effectively:
- Configure components - Learn how to define and configure components with arguments and blocks
- Build data pipelines - Connect components together to create data processing workflows
- Component controller - Understand how Alloy manages components at runtime
To extend component functionality:
- Custom components - Create reusable custom components for your specific needs
- Community components - Use components shared by the Alloy community
For hands-on learning:
- Tutorials - Build complete data collection pipelines step by step
For reference:
- Expressions - Use component exports in dynamic configurations
- Alloy syntax - Explore advanced syntax features and patterns
- Component reference - Browse all available components and their options