Skip to content

Commit

Permalink
get rid of static SD boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed Sep 5, 2023
1 parent b990eb5 commit e283f00
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
6 changes: 5 additions & 1 deletion converter/internal/prometheusconvert/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"time"

"golang.org/x/exp/maps"

"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/component/prometheus/scrape"
"github.com/grafana/agent/converter/diag"
Expand Down Expand Up @@ -68,7 +70,9 @@ func getScrapeTargets(staticConfig prom_discovery.StaticConfig) []discovery.Targ
for _, labelSet := range target.Targets {
for labelName, labelValue := range labelSet {
targetMap[string(labelName)] = string(labelValue)
targets = append(targets, targetMap)
newMap := map[string]string{}
maps.Copy(newMap, targetMap)
targets = append(targets, newMap)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/prometheusconvert/testdata/scrape.river
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
prometheus.scrape "prometheus1" {
targets = concat(
[{
__address__ = "localhost:9091",
__address__ = "localhost:9090",
app = "foo",
}],
[{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package build

import (
"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/loki/clients/pkg/promtail/scrapeconfig"
prom_discover "github.com/prometheus/prometheus/discovery"
)
Expand All @@ -17,8 +20,27 @@ func (s *ScrapeConfigBuilder) AppendSDs() {
targets := prometheusconvert.AppendServiceDiscoveryConfigs(pb, sdConfigs, common.LabelForParts(s.globalCtx.LabelPrefix, s.cfg.JobName))
pb.AppendToFile(s.f)

targetLiterals := make([]discovery.Target, 0)
for _, target := range targets {
s.allTargetsExps = append(s.allTargetsExps, target["__expr__"])
if expr, ok := target["__expr__"]; ok {
// use the expression if __expr__ is set
s.allTargetsExps = append(s.allTargetsExps, expr)
} else {
// otherwise, use the target as a literal
targetLiterals = append(targetLiterals, target)
}
}

// write the target literals as a string if there are any
if len(targetLiterals) != 0 {
literalsStr, err := toRiverExpression(targetLiterals)
if err != nil { // should not happen, unless we have a bug
s.diags.Add(
diag.SeverityLevelCritical,
"failed to write static SD targets as valid River expression: "+err.Error(),
)
}
s.allTargetsExps = append(s.allTargetsExps, literalsStr)
}

s.diags.AddAll(prometheusconvert.ValidateServiceDiscoveryConfigs(sdConfigs))
Expand Down Expand Up @@ -87,6 +109,10 @@ func toDiscoveryConfig(cfg *scrapeconfig.Config) prom_discover.Configs {
sdConfigs = append(sdConfigs, sd)
}

if len(cfg.ServiceDiscoveryConfig.StaticConfigs) != 0 {
sdConfigs = append(sdConfigs, cfg.ServiceDiscoveryConfig.StaticConfigs)
}

for _, sd := range cfg.ServiceDiscoveryConfig.TritonSDConfigs {
sdConfigs = append(sdConfigs, sd)
}
Expand Down
33 changes: 0 additions & 33 deletions converter/internal/promtailconvert/internal/build/static_sd.go

This file was deleted.

1 change: 0 additions & 1 deletion converter/internal/promtailconvert/promtailconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func appendScrapeConfig(
b.Sanitize()

// Append all the SD components
b.AppendStaticSDs()
b.AppendSDs()

// Append loki.source.file to process all SD components' targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ discovery.kubernetes "example" {

discovery.relabel "example" {
targets = concat(
discovery.kubernetes.example.targets,
[{
__address__ = "this",
__path__ = "/var/log/captain_scott_last_expedition.log",
Expand Down Expand Up @@ -32,7 +33,6 @@ discovery.relabel "example" {
category = "sf",
quality = "high",
}],
discovery.kubernetes.example.targets,
)

rule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ discovery.relabel "metrics_agent_promobee" {
prometheus.scrape "metrics_agent_prometheus" {
targets = concat(
[{
__address__ = "localhost:9101",
__address__ = "localhost:9099",
}],
[{
__address__ = "localhost:9101",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ prometheus.scrape "metrics_name_jobName" {
targets = concat(
discovery.consul.metrics_name_jobName.targets,
[{
__address__ = "localhost:9101",
__address__ = "localhost:9099",
}],
[{
__address__ = "localhost:9101",
Expand Down

0 comments on commit e283f00

Please sign in to comment.