Skip to content

Commit

Permalink
Dynamic blackbox module (grafana#13)
Browse files Browse the repository at this point in the history
* Added dynamic blackbox module
  • Loading branch information
djcode authored Sep 25, 2023
1 parent 820dbbb commit 730df0b
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parameterize a configuration to create reusable pipelines.
| ---- | ----------- | ------------- |
| [OTLP to LGTM](./modules/otlp/otlp-to-lgtm/) | Module to ingest OTLP data and then send it to Loki, Mimir and Tempo stacks locally or in GrafanaCloud. | `>= v0.33`
| [Grafana Agent Telemetry to LGTM](./modules/grafana-agent/telemetry-to-lgtm/) | Module to forward the Grafana Agent's own telemetry data to Loki, Mimir and Tempo stacks locally or in Grafana Cloud. | `>= v0.33`
| [Grafana Agent Dynamic Blackbox Exporter](./modules/grafana-agent/dynamic-blackbox/) | Module to use blackbox exporter with dynamic targets. | `>= v0.35`
| [Grafana Cloud Autoconfigure](./modules/grafana-cloud/autoconfigure/) | Module to automatically configure receivers for Grafana Cloud. | `>= v0.34`
| [Host Filtering](./modules/host-filter/) | The host filtering module provides a Flow mode equivalent to static mode's host filtering functionality. | `>= v0.34`

Expand Down
85 changes: 85 additions & 0 deletions modules/grafana-agent/dynamic-blackbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Grafana Agent Dynamic Blackbox Exporter

A module to add a dynamic target list to blackbox exporter.

The `prometheus.exporter.blackbox` component expects a series of target blocks as part of its configuration and doesn't accept a list of targets from another component.

This module allows a list of targets from another component to be used by blackbox exporter for scraping.

## Agent Version

`>= v0.35`

## Module arguments

The following arguments are supported when passing arguments to the module loader:

| Name | Type | Description | Default | Required
| ---- | ---- | ----------- | ------- | --------
| `targets` | `list(map(string))` | List of targets for blackbox | | yes
| `target_label` | `string` | Metric label the original target label will be added to | `"address"` | no
| `config_file` | `string` or `secret` | blackbox_exporter configuration file path | | no
| `config` | `secret` | blackbox_exporter configuration as inline string | | no
| `probe_timeout_offset` | `duration` | Offset in seconds to subtract from timeout when probing targets | `"0.5s"` | no

The arguments `config` and `config_file` are mutually exclusive. If neither are specified, a default config is used.

The `config_file` argument points to a YAML file defining which blackbox_exporter modules to use.
The `config` argument must be a YAML document as string defining which blackbox_exporter modules to use.
`config` is typically loaded by using the exports of another component. For example,

- `local.file.LABEL.content`
- `remote.http.LABEL.content`
- `remote.s3.LABEL.content`

See [blackbox_exporter]( https://github.com/prometheus/blackbox_exporter/blob/master/example.yml) for details on how to generate a config file.

## Module exports

The following fields are exported by the module:

| Name | Type | Description
| ---- | ---- | -----------
| `targets` | `list(map(string))` | The targets that can be used to collect blackbox metrics.

## Example

```
discovery.file "targets" {
files = ["targets.yml"]
}
module.git "blackbox" {
repository = "https://github.com/grafana/agent-modules.git"
revision = "main"
path = "modules/grafana-agent/dynamic-blackbox/module.river"
arguments {
config = "{ modules: { tcps: { prober: tcp, tcp: { tls: true, tls_config: { insecure_skip_verify: true } } } } }"
targets = discovery.file.targets.targets
}
}
prometheus.scrape "scrape" {
targets = module.git.blackbox.exports.targets
forward_to = [ prometheus.remote_write.main.receiver ]
}
prometheus.remote_write "main" {
endpoint {
url = "http://url/to/push"
}
}
```

Add a `__param_module` label in the targets.yml to select the blackbox module to use with a given target.

```
---
- labels:
type: external
__param_module: tcps
targets:
- grafana.com:443
- prometheus.io:443
```
65 changes: 65 additions & 0 deletions modules/grafana-agent/dynamic-blackbox/module.river
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/********************************************
* ARGUMENTS
********************************************/
argument "targets" { }

argument "target_label" {
optional = true
default = "address"
}

argument "config_file" {
optional = true
}

argument "config" {
optional = true
}

argument "probe_timeout_offset" {
optional = true
}

/********************************************
* EXPORTS
********************************************/
export "targets" {
value = discovery.relabel.default.output
}

/********************************************
* LOGIC
********************************************/

prometheus.exporter.blackbox "base" {
config_file = argument.config_file.value
config = argument.config.value
probe_timeout_offset = argument.probe_timeout_offset.value
target "dummy" {
address = "dummy"
}
}

discovery.relabel "default" {
targets = argument.targets.value

rule {
source_labels = ["__address__"]
target_label = "__param_target"
}

rule {
source_labels = ["__address__"]
target_label = argument.target_label.value
}

rule {
target_label = "__address__"
replacement = prometheus.exporter.blackbox.base.targets[0].__address__
}

rule {
target_label = "__metrics_path__"
replacement = prometheus.exporter.blackbox.base.targets[0].__metrics_path__
}
}

0 comments on commit 730df0b

Please sign in to comment.