From 8850660790ac5e615d13647752fbb49b4b50ea37 Mon Sep 17 00:00:00 2001 From: Piotr <17101802+thampiotr@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:51:18 +0100 Subject: [PATCH] converters: Default with localhost in static targets when none provided (#5546) * promtail: Default with localhost in static targets in converter * Add tests and changelog * prettify * feedback --- CHANGELOG.md | 3 +++ .../internal/build/service_discovery.go | 13 +++++++++++++ .../testdata/static_pipeline_example.river | 6 ++++++ .../testdata/static_pipeline_example.yaml | 4 ++-- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3b26e1f86f9..52c1a6db8bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/converter/internal/promtailconvert/internal/build/service_discovery.go b/converter/internal/promtailconvert/internal/build/service_discovery.go index 37489bc66c59..6219bc2b121d 100644 --- a/converter/internal/promtailconvert/internal/build/service_discovery.go +++ b/converter/internal/promtailconvert/internal/build/service_discovery.go @@ -1,6 +1,8 @@ package build import ( + "fmt" + "github.com/grafana/agent/component/discovery" "github.com/grafana/agent/converter/diag" "github.com/grafana/agent/converter/internal/common" @@ -8,6 +10,7 @@ import ( "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" ) @@ -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) } diff --git a/converter/internal/promtailconvert/testdata/static_pipeline_example.river b/converter/internal/promtailconvert/testdata/static_pipeline_example.river index d105b315036a..3b7e4b20b436 100644 --- a/converter/internal/promtailconvert/testdata/static_pipeline_example.river +++ b/converter/internal/promtailconvert/testdata/static_pipeline_example.river @@ -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", }], ) diff --git a/converter/internal/promtailconvert/testdata/static_pipeline_example.yaml b/converter/internal/promtailconvert/testdata/static_pipeline_example.yaml index 2608cfb8825e..80239d45eada 100644 --- a/converter/internal/promtailconvert/testdata/static_pipeline_example.yaml +++ b/converter/internal/promtailconvert/testdata/static_pipeline_example.yaml @@ -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: