Help build the future of open source observability software Open positions

Check out the open source projects we support Downloads

Grot cannot remember your choice unless you click the consent notice at the bottom.

Grafana Agent v0.40: custom components and modules for easier configuration

Grafana Agent v0.40: custom components and modules for easier configuration

19 Mar, 2024 3 min

Grafana Agent v0.40 is now available.

You can take a look at the changelog for the full list of changes as well as our upgrade guide for breaking changes, but we’ve compiled some of the highlights in the latest Agent release below.

Custom components

To make Flow, the component-based revision of Grafana Agent, even more customizable, we’ve introduced the ability to declare custom components. Custom components may use existing components and the standard library in order to allow you to create your own reusable components.

For example, say you have a URL somewhere in a JSON file that needs to be reused in different ways throughout your pipeline, you could do something like:

river
// This component takes a json blob, looks up the "url"
// key, and concatenates "/api/v1/write"
declare "prom_url" {
	argument "json" { }

	export "output" {
		value = format("%s%s", json_path(argument.json.value, ".url")[0], "/api/v1/write")
	}
}

local.file "blob" {
	filename = "/tmp/blob.json"
}

prom_url "example" {
	json = local.file.blob.content
}

// Your pipeline here

prometheus.remote_write "demo" {
	endpoint {
		url = prom_url.example.output
	}
}

Now, you can reference prom_url.example.output, rather than having to copy/paste concat(json_path(local.file.blob.content, “.url”), “/api/v1/write”) each time.

Modules 2.0

With the release of Grafana Agent 0.40 comes a new way of configuring modules!

Custom components can be used to reduce configuration within a single config file, but if you’d like to reuse configuration among multiple pipelines, that’s what modules are for.

Imagine you are running a suite of applications in a Kubernetes environment. If you wanted to standardize the set of labels sent by all of your monitored apps and infrastructure, you could create a module to make it easy!

kubernetes_helper.river

river
// This component will add the "node" and "example_custom_label"
// labels to all passed-in targets
declare "k8s_node_relabel" {
	argument "write_to" {
		optional = false
	}

	argument "targets" {
		optional = false
	}

	discovery.relabel "add_labels" {
		rule {
			source_labels = ["__meta_kubernetes_node_name"]
			target_label  = "node"
		}

		rule {
			source_labels = ["__meta_kubernetes_node_label_example"]
			target_label  = "example_custom_label"
		}
	}

	export "output" {
		value = discovery.relabel.add_labels.output
	}
}

// This component will add the "namespace", "pod", and "ready" labels
// to all passed-in targets
declare "k8s_pod_relabel" {
	argument "write_to" {
		optional = false
	}

	argument "targets" {
		optional = false
	}

	discovery.relabel "add_labels" {
		rule {
			source_labels = ["__meta_kubernetes_namespace"]
			target_label  = "namespace"
		}

		rule {
			source_labels = ["__meta_kubernetes_pod_name"]
			target_label  = "pod"
		}

		rule {
			source_labels = ["__meta_kubernetes_pod_ready"]
			target_label  = "ready"
		}
	}

	export "output" {
		value = discovery.relabel.add_labels.output
	}
}

In your main configuration, you could then use these predefined components:

river
// Import the helper module under the 'k8s_helpers' namespace
import.file "k8s_helpers" {
	filename = "kubernetes_helper.river"
}

// Set up discovery for pods in our cluster
discovery.kubernetes "pods" {
	role = "pod"

	// Connection config goes here
}

// Apply the standardized relabeling from the k8s_helpers module
k8s_helpers.k8s_pod_relabel "demo" {
	targets = discovery.kubernetes.pods.targets
}

// Scrape the relabeled targets
prometheus.scrape "k8s" {
	targets    = k8s_helpers.k8s_pod_relabel.demo.output
	forward_to = [prometheus.remote_write.cloud.receiver]
}

// Remote write to our Prometheus endpoint
prometheus.remote_write "cloud" {
	endpoint {
		url = "https://prometheus:9090/api/v1/write"
	}
}

Now if you need to add more Kubernetes service discovery components, you can reuse the k8s_helpers.* components to perform the relabeling. If you would like to update these remotely, you can make use of import.http or import.git and the Grafana Agent will poll for changes to the module and automatically load them!

As always, we’d love to hear from you, so feel free to drop by our Grafana Labs Community Slack or check out the Agent repo directly. We look forward to your comments and feedback!

Grafana Cloud is the easiest way to get started with metrics, logs, traces, dashboards, and more. We have a generous forever-free tier and plans for every use case. Sign up for free now!

On this page
Scroll for more