Skip to content

Commit

Permalink
Add extra_labels object to BlackboxTarget block (#5157)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Clayton Cornell <[email protected]>
  • Loading branch information
spartan0x117 and clayton-cornell authored Sep 12, 2023
1 parent 36818bd commit aecd930
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Main (unreleased)
- Add a `file_watch` block in `loki.source.file` to configure how often to poll files from disk for changes via `min_poll_frequency` and `max_poll_frequency`.
In static mode it can be configured in the global `file_watch_config` via `min_poll_frequency` and `max_poll_frequency`. (@wildum)

- Flow: In `prometheus.exporter.blackbox`, allow setting labels for individual targets. (@spartan0x117)

### Enhancements

- Clustering: allow advertise interfaces to be configurable, with the possibility to select all available interfaces. (@wildum)
Expand Down
11 changes: 8 additions & 3 deletions component/prometheus/exporter/blackbox/blackbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func buildBlackboxTargets(baseTarget discovery.Target, args component.Arguments)
a := args.(Arguments)
for _, tgt := range a.Targets {
target := make(discovery.Target)
// Set extra labels first, meaning that any other labels will override
for k, v := range tgt.Labels {
target[k] = v
}
for k, v := range baseTarget {
target[k] = v
}
Expand All @@ -62,9 +66,10 @@ var DefaultArguments = Arguments{

// BlackboxTarget defines a target to be used by the exporter.
type BlackboxTarget struct {
Name string `river:",label"`
Target string `river:"address,attr"`
Module string `river:"module,attr,optional"`
Name string `river:",label"`
Target string `river:"address,attr"`
Module string `river:"module,attr,optional"`
Labels map[string]string `river:"labels,attr,optional"`
}

type TargetBlock []BlackboxTarget
Expand Down
44 changes: 44 additions & 0 deletions component/prometheus/exporter/blackbox/blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,47 @@ func TestBuildBlackboxTargets(t *testing.T) {
require.Equal(t, "http://example.com", targets[0]["__param_target"])
require.Equal(t, "http_2xx", targets[0]["__param_module"])
}

func TestBuildBlackboxTargetsWithExtraLabels(t *testing.T) {
baseArgs := Arguments{
ConfigFile: "modules.yml",
Targets: TargetBlock{{
Name: "target_a",
Target: "http://example.com",
Module: "http_2xx",
Labels: map[string]string{
"env": "test",
"foo": "bar",
},
}},
ProbeTimeoutOffset: 1.0,
}
baseTarget := discovery.Target{
model.SchemeLabel: "http",
model.MetricsPathLabel: "component/prometheus.exporter.blackbox.default/metrics",
"instance": "prometheus.exporter.blackbox.default",
"job": "integrations/blackbox",
"__meta_agent_integration_name": "blackbox",
"__meta_agent_integration_instance": "prometheus.exporter.blackbox.default",
}
args := component.Arguments(baseArgs)
targets := buildBlackboxTargets(baseTarget, args)
require.Equal(t, 1, len(targets))
require.Equal(t, "integrations/blackbox/target_a", targets[0]["job"])
require.Equal(t, "http://example.com", targets[0]["__param_target"])
require.Equal(t, "http_2xx", targets[0]["__param_module"])

require.Equal(t, "test", targets[0]["env"])
require.Equal(t, "bar", targets[0]["foo"])

// Check that the extra labels do not override existing labels
baseArgs.Targets[0].Labels = map[string]string{
"job": "test",
"instance": "test-instance",
}
args = component.Arguments(baseArgs)
targets = buildBlackboxTargets(baseTarget, args)
require.Equal(t, 1, len(targets))
require.Equal(t, "integrations/blackbox/target_a", targets[0]["job"])
require.Equal(t, "prometheus.exporter.blackbox.default", targets[0]["instance"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ debug metrics.
### Collect metrics using a blackbox exporter config file

This example uses a [`prometheus.scrape` component][scrape] to collect metrics
from `prometheus.exporter.blackbox`:
from `prometheus.exporter.blackbox`. It adds an extra label, `env="dev"`, to the metrics emitted by the `grafana` target. The `example` target does not have any added labels.

```river
prometheus.exporter.blackbox "example" {
Expand All @@ -101,6 +101,9 @@ prometheus.exporter.blackbox "example" {
target "grafana" {
address = "http://grafana.com"
module = "http_2xx"
labels = {
"env": "dev",
}
}
}
Expand Down Expand Up @@ -144,6 +147,9 @@ prometheus.exporter.blackbox "example" {
target "grafana" {
address = "http://grafana.com"
module = "http_2xx"
labels = {
"env": "dev",
}
}
}
Expand Down Expand Up @@ -171,4 +177,5 @@ Replace the following:
- `USERNAME`: The username to use for authentication to the remote_write API.
- `PASSWORD`: The password to use for authentication to the remote_write API.


[scrape]: {{< relref "./prometheus.scrape.md" >}}

0 comments on commit aecd930

Please sign in to comment.