Skip to content

Commit

Permalink
converters: Default with localhost in static targets when none provid…
Browse files Browse the repository at this point in the history
…ed (#5546)

* promtail: Default with localhost in static targets in converter

* Add tests and changelog

* prettify

* feedback
  • Loading branch information
thampiotr authored Oct 20, 2023
1 parent 2352cba commit 8850660
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Main (unreleased)

- Fixed an issue where native histogram time series were being dropped silently. (@krajorama)

- Fix an issue with static mode and `promtail` converters, where static targets
did not correctly default to `localhost` when not provided. (@thampiotr)

### Enhancements

- The `loki.write` WAL now has snappy compression enabled by default. (@thepalbi)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package build

import (
"fmt"

"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"

"github.com/grafana/loki/clients/pkg/promtail/scrapeconfig"
"github.com/prometheus/common/model"
prom_discover "github.com/prometheus/prometheus/discovery"
)

Expand Down Expand Up @@ -107,6 +110,16 @@ func toDiscoveryConfig(cfg *scrapeconfig.Config) prom_discover.Configs {
}

if len(cfg.ServiceDiscoveryConfig.StaticConfigs) != 0 {
// Add source and default 'localhost' target to match Promtail's behaviour.
// See https://github.com/grafana/loki/blob/4a564456861c0ab7441b815aa49a7c0f22a05f4c/clients/pkg/promtail/targets/file/filetargetmanager.go#L102
for i, targetGroup := range cfg.ServiceDiscoveryConfig.StaticConfigs {
targetGroup.Source = fmt.Sprintf("%d", i)
if len(targetGroup.Targets) == 0 {
targetGroup.Targets = []model.LabelSet{
{model.AddressLabel: "localhost"},
}
}
}
sdConfigs = append(sdConfigs, cfg.ServiceDiscoveryConfig.StaticConfigs)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ discovery.relabel "example" {
__path__exclude__ = "/shelf/books/star_wars*",
category = "sf",
quality = "high",
}, {
__address__ = "localhost",
__path__ = "/var/log/this_is_used.log",
category = "important",
}, {
__address__ = "localhost",
}],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ scrape_configs:
category: sf
quality: high
- labels:
__path__: /var/log/this_is_not_used.log
category: superfluous
__path__: /var/log/this_is_used.log
category: important
- labels: { }
# Include k8s config to demonstrate how other discovery features differ from static_configs
kubernetes_sd_configs:
Expand Down

0 comments on commit 8850660

Please sign in to comment.