Skip to content

Commit

Permalink
reorganize prometheus converter code and limit surface area of depend…
Browse files Browse the repository at this point in the history
…encies between converters (grafana#5406)

Signed-off-by: erikbaranowski <[email protected]>
  • Loading branch information
erikbaranowski authored Oct 17, 2023
1 parent 818191b commit 5d39145
Show file tree
Hide file tree
Showing 62 changed files with 480 additions and 479 deletions.
18 changes: 18 additions & 0 deletions converter/internal/common/convert_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import (
"github.com/grafana/river/token/builder"
)

// NewDiscoveryExports will return a new [discovery.Exports] with a specific
// key for converter component exports. The argument will be tokenized
// as a component export string rather than the standard [discovery.Target]
// RiverTokenize.
func NewDiscoveryExports(expr string) discovery.Exports {
return discovery.Exports{
Targets: NewDiscoveryTargets(expr),
}
}

// NewDiscoveryTargets will return a new [[]discovery.Target] with a specific
// key for converter component exports. The argument will be tokenized
// as a component export string rather than the standard [discovery.Target]
// RiverTokenize.
func NewDiscoveryTargets(expr string) []discovery.Target {
return []discovery.Target{map[string]string{"__expr__": expr}}
}

// ConvertTargets implements [builder.Tokenizer]. This allows us to set
// component.Arguments with an implementation that can be tokenized with
// custom behaviour for converting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package prometheusconvert
package common

import (
"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/river/rivertypes"
prom_config "github.com/prometheus/common/config"
Expand Down Expand Up @@ -108,21 +107,3 @@ func ToTLSConfig(tlsConfig *prom_config.TLSConfig) *config.TLSConfig {
MinVersion: config.TLSVersion(tlsConfig.MinVersion),
}
}

// NewDiscoveryExports will return a new [discovery.Exports] with a specific
// key for converter component exports. The argument will be tokenized
// as a component export string rather than the standard [discovery.Target]
// RiverTokenize.
func NewDiscoveryExports(expr string) discovery.Exports {
return discovery.Exports{
Targets: newDiscoveryTargets(expr),
}
}

// newDiscoveryTargets will return a new [[]discovery.Target] with a specific
// key for converter component exports. The argument will be tokenized
// as a component export string rather than the standard [discovery.Target]
// RiverTokenize.
func newDiscoveryTargets(expr string) []discovery.Target {
return []discovery.Target{map[string]string{"__expr__": expr}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package build

import (
"fmt"
Expand All @@ -8,24 +8,24 @@ import (
"github.com/grafana/river/token/builder"
)

// prometheusBlocks is a type for categorizing River Blocks before appending
// PrometheusBlocks is a type for categorizing River Blocks before appending
// them to a River File. This gives control over the order they are written
// versus appending them in the order the Blocks are created.
type prometheusBlocks struct {
discoveryBlocks []prometheusBlock
discoveryRelabelBlocks []prometheusBlock
prometheusScrapeBlocks []prometheusBlock
prometheusRelabelBlocks []prometheusBlock
prometheusRemoteWriteBlocks []prometheusBlock
type PrometheusBlocks struct {
DiscoveryBlocks []prometheusBlock
DiscoveryRelabelBlocks []prometheusBlock
PrometheusScrapeBlocks []prometheusBlock
PrometheusRelabelBlocks []prometheusBlock
PrometheusRemoteWriteBlocks []prometheusBlock
}

func NewPrometheusBlocks() *prometheusBlocks {
return &prometheusBlocks{
discoveryBlocks: []prometheusBlock{},
discoveryRelabelBlocks: []prometheusBlock{},
prometheusScrapeBlocks: []prometheusBlock{},
prometheusRelabelBlocks: []prometheusBlock{},
prometheusRemoteWriteBlocks: []prometheusBlock{},
func NewPrometheusBlocks() *PrometheusBlocks {
return &PrometheusBlocks{
DiscoveryBlocks: []prometheusBlock{},
DiscoveryRelabelBlocks: []prometheusBlock{},
PrometheusScrapeBlocks: []prometheusBlock{},
PrometheusRelabelBlocks: []prometheusBlock{},
PrometheusRemoteWriteBlocks: []prometheusBlock{},
}
}

Expand All @@ -37,47 +37,47 @@ func NewPrometheusBlocks() *prometheusBlocks {
// 3. Prometheus scrape component(s)
// 4. Prometheus relabel component(s) (if any)
// 5. Prometheus remote_write
func (pb *prometheusBlocks) AppendToFile(f *builder.File) {
for _, promBlock := range pb.discoveryBlocks {
func (pb *PrometheusBlocks) AppendToFile(f *builder.File) {
for _, promBlock := range pb.DiscoveryBlocks {
f.Body().AppendBlock(promBlock.block)
}

for _, promBlock := range pb.discoveryRelabelBlocks {
for _, promBlock := range pb.DiscoveryRelabelBlocks {
f.Body().AppendBlock(promBlock.block)
}

for _, promBlock := range pb.prometheusScrapeBlocks {
for _, promBlock := range pb.PrometheusScrapeBlocks {
f.Body().AppendBlock(promBlock.block)
}

for _, promBlock := range pb.prometheusRelabelBlocks {
for _, promBlock := range pb.PrometheusRelabelBlocks {
f.Body().AppendBlock(promBlock.block)
}

for _, promBlock := range pb.prometheusRemoteWriteBlocks {
for _, promBlock := range pb.PrometheusRemoteWriteBlocks {
f.Body().AppendBlock(promBlock.block)
}
}

func (pb *prometheusBlocks) getScrapeInfo() diag.Diagnostics {
func (pb *PrometheusBlocks) GetScrapeInfo() diag.Diagnostics {
var diags diag.Diagnostics

for _, promScrapeBlock := range pb.prometheusScrapeBlocks {
for _, promScrapeBlock := range pb.PrometheusScrapeBlocks {
detail := promScrapeBlock.detail

for _, promDiscoveryBlock := range pb.discoveryBlocks {
for _, promDiscoveryBlock := range pb.DiscoveryBlocks {
if strings.HasPrefix(promDiscoveryBlock.label, promScrapeBlock.label) {
detail = fmt.Sprintln(detail) + fmt.Sprintf(" A %s.%s component", strings.Join(promDiscoveryBlock.name, "."), promDiscoveryBlock.label)
}
}

for _, promDiscoveryRelabelBlock := range pb.discoveryRelabelBlocks {
for _, promDiscoveryRelabelBlock := range pb.DiscoveryRelabelBlocks {
if strings.HasPrefix(promDiscoveryRelabelBlock.label, promScrapeBlock.label) {
detail = fmt.Sprintln(detail) + fmt.Sprintf(" A %s.%s component", strings.Join(promDiscoveryRelabelBlock.name, "."), promDiscoveryRelabelBlock.label)
}
}

for _, promRelabelBlock := range pb.prometheusRelabelBlocks {
for _, promRelabelBlock := range pb.PrometheusRelabelBlocks {
if strings.HasPrefix(promRelabelBlock.label, promScrapeBlock.label) {
detail = fmt.Sprintln(detail) + fmt.Sprintf(" A %s.%s component", strings.Join(promRelabelBlock.name, "."), promRelabelBlock.label)
}
Expand All @@ -86,7 +86,7 @@ func (pb *prometheusBlocks) getScrapeInfo() diag.Diagnostics {
diags.AddWithDetail(diag.SeverityLevelInfo, promScrapeBlock.summary, detail)
}

for _, promRemoteWriteBlock := range pb.prometheusRemoteWriteBlocks {
for _, promRemoteWriteBlock := range pb.PrometheusRemoteWriteBlocks {
diags.AddWithDetail(diag.SeverityLevelInfo, promRemoteWriteBlock.summary, promRemoteWriteBlock.detail)
}

Expand All @@ -101,7 +101,7 @@ type prometheusBlock struct {
detail string
}

func newPrometheusBlock(block *builder.Block, name []string, label string, summary string, detail string) prometheusBlock {
func NewPrometheusBlock(block *builder.Block, name []string, label string, summary string, detail string) prometheusBlock {
return prometheusBlock{
block: block,
name: name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package component

import (
"time"
Expand All @@ -8,16 +8,17 @@ import (
"github.com/grafana/agent/component/discovery/azure"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"
"github.com/grafana/river/rivertypes"
prom_azure "github.com/prometheus/prometheus/discovery/azure"
)

func appendDiscoveryAzure(pb *prometheusBlocks, label string, sdConfig *prom_azure.SDConfig) discovery.Exports {
func appendDiscoveryAzure(pb *build.PrometheusBlocks, label string, sdConfig *prom_azure.SDConfig) discovery.Exports {
discoveryAzureArgs := toDiscoveryAzure(sdConfig)
name := []string{"discovery", "azure"}
block := common.NewBlockWithOverride(name, label, discoveryAzureArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return NewDiscoveryExports("discovery.azure." + label + ".targets")
pb.DiscoveryBlocks = append(pb.DiscoveryBlocks, build.NewPrometheusBlock(block, name, label, "", ""))
return common.NewDiscoveryExports("discovery.azure." + label + ".targets")
}

func toDiscoveryAzure(sdConfig *prom_azure.SDConfig) *azure.Arguments {
Expand All @@ -36,12 +37,12 @@ func toDiscoveryAzure(sdConfig *prom_azure.SDConfig) *azure.Arguments {
ProxyURL: config.URL(sdConfig.HTTPClientConfig.ProxyURL),
FollowRedirects: sdConfig.HTTPClientConfig.FollowRedirects,
EnableHTTP2: sdConfig.HTTPClientConfig.EnableHTTP2,
TLSConfig: *ToTLSConfig(&sdConfig.HTTPClientConfig.TLSConfig),
TLSConfig: *common.ToTLSConfig(&sdConfig.HTTPClientConfig.TLSConfig),
}
}

func validateDiscoveryAzure(sdConfig *prom_azure.SDConfig) diag.Diagnostics {
return ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
func ValidateDiscoveryAzure(sdConfig *prom_azure.SDConfig) diag.Diagnostics {
return common.ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
}

func toManagedIdentity(sdConfig *prom_azure.SDConfig) *azure.ManagedIdentity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package component

import (
"time"
Expand All @@ -7,20 +7,21 @@ import (
"github.com/grafana/agent/component/discovery/consul"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"
"github.com/grafana/river/rivertypes"
prom_consul "github.com/prometheus/prometheus/discovery/consul"
)

func appendDiscoveryConsul(pb *prometheusBlocks, label string, sdConfig *prom_consul.SDConfig) discovery.Exports {
func appendDiscoveryConsul(pb *build.PrometheusBlocks, label string, sdConfig *prom_consul.SDConfig) discovery.Exports {
discoveryConsulArgs := toDiscoveryConsul(sdConfig)
name := []string{"discovery", "consul"}
block := common.NewBlockWithOverride(name, label, discoveryConsulArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return NewDiscoveryExports("discovery.consul." + label + ".targets")
pb.DiscoveryBlocks = append(pb.DiscoveryBlocks, build.NewPrometheusBlock(block, name, label, "", ""))
return common.NewDiscoveryExports("discovery.consul." + label + ".targets")
}

func validateDiscoveryConsul(sdConfig *prom_consul.SDConfig) diag.Diagnostics {
return ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
func ValidateDiscoveryConsul(sdConfig *prom_consul.SDConfig) diag.Diagnostics {
return common.ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
}

func toDiscoveryConsul(sdConfig *prom_consul.SDConfig) *consul.Arguments {
Expand All @@ -43,6 +44,6 @@ func toDiscoveryConsul(sdConfig *prom_consul.SDConfig) *consul.Arguments {
ServiceTags: sdConfig.ServiceTags,
NodeMeta: sdConfig.NodeMeta,
RefreshInterval: time.Duration(sdConfig.RefreshInterval),
HTTPClientConfig: *ToHttpClientConfig(&sdConfig.HTTPClientConfig),
HTTPClientConfig: *common.ToHttpClientConfig(&sdConfig.HTTPClientConfig),
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package component

import (
"reflect"
Expand All @@ -9,20 +9,21 @@ import (
"github.com/grafana/agent/component/discovery/digitalocean"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"
"github.com/grafana/river/rivertypes"
prom_config "github.com/prometheus/common/config"
prom_digitalocean "github.com/prometheus/prometheus/discovery/digitalocean"
)

func appendDiscoveryDigitalOcean(pb *prometheusBlocks, label string, sdConfig *prom_digitalocean.SDConfig) discovery.Exports {
func appendDiscoveryDigitalOcean(pb *build.PrometheusBlocks, label string, sdConfig *prom_digitalocean.SDConfig) discovery.Exports {
discoveryDigitalOceanArgs := toDiscoveryDigitalOcean(sdConfig)
name := []string{"discovery", "digitalocean"}
block := common.NewBlockWithOverride(name, label, discoveryDigitalOceanArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return NewDiscoveryExports("discovery.digitalocean." + label + ".targets")
pb.DiscoveryBlocks = append(pb.DiscoveryBlocks, build.NewPrometheusBlock(block, name, label, "", ""))
return common.NewDiscoveryExports("discovery.digitalocean." + label + ".targets")
}

func validateDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) diag.Diagnostics {
func ValidateDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) diag.Diagnostics {
var diags diag.Diagnostics

if sdConfig.HTTPClientConfig.BasicAuth != nil {
Expand All @@ -41,7 +42,7 @@ func validateDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) diag.Di
diags.Add(diag.SeverityLevelError, "unsupported oauth2 for digitalocean_sd_configs")
}

diags.AddAll(ValidateHttpClientConfig(&sdConfig.HTTPClientConfig))
diags.AddAll(common.ValidateHttpClientConfig(&sdConfig.HTTPClientConfig))

return diags
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package component

import (
"time"
Expand All @@ -7,18 +7,19 @@ import (
"github.com/grafana/agent/component/discovery/dns"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"
prom_dns "github.com/prometheus/prometheus/discovery/dns"
)

func appendDiscoveryDns(pb *prometheusBlocks, label string, sdConfig *prom_dns.SDConfig) discovery.Exports {
func appendDiscoveryDns(pb *build.PrometheusBlocks, label string, sdConfig *prom_dns.SDConfig) discovery.Exports {
discoveryDnsArgs := toDiscoveryDns(sdConfig)
name := []string{"discovery", "dns"}
block := common.NewBlockWithOverride(name, label, discoveryDnsArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return NewDiscoveryExports("discovery.dns." + label + ".targets")
pb.DiscoveryBlocks = append(pb.DiscoveryBlocks, build.NewPrometheusBlock(block, name, label, "", ""))
return common.NewDiscoveryExports("discovery.dns." + label + ".targets")
}

func validateDiscoveryDns(sdConfig *prom_dns.SDConfig) diag.Diagnostics {
func ValidateDiscoveryDns(sdConfig *prom_dns.SDConfig) diag.Diagnostics {
return make(diag.Diagnostics, 0)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prometheusconvert
package component

import (
"time"
Expand All @@ -7,19 +7,20 @@ import (
"github.com/grafana/agent/component/discovery/docker"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert/build"
prom_docker "github.com/prometheus/prometheus/discovery/moby"
)

func appendDiscoveryDocker(pb *prometheusBlocks, label string, sdConfig *prom_docker.DockerSDConfig) discovery.Exports {
func appendDiscoveryDocker(pb *build.PrometheusBlocks, label string, sdConfig *prom_docker.DockerSDConfig) discovery.Exports {
discoveryDockerArgs := toDiscoveryDocker(sdConfig)
name := []string{"discovery", "docker"}
block := common.NewBlockWithOverride(name, label, discoveryDockerArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return NewDiscoveryExports("discovery.docker." + label + ".targets")
pb.DiscoveryBlocks = append(pb.DiscoveryBlocks, build.NewPrometheusBlock(block, name, label, "", ""))
return common.NewDiscoveryExports("discovery.docker." + label + ".targets")
}

func validateDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) diag.Diagnostics {
return ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
func ValidateDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) diag.Diagnostics {
return common.ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
}

func toDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
Expand All @@ -33,7 +34,7 @@ func toDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
HostNetworkingHost: sdConfig.HostNetworkingHost,
RefreshInterval: time.Duration(sdConfig.RefreshInterval),
Filters: toDockerFilters(sdConfig.Filters),
HTTPClientConfig: *ToHttpClientConfig(&sdConfig.HTTPClientConfig),
HTTPClientConfig: *common.ToHttpClientConfig(&sdConfig.HTTPClientConfig),
}
}

Expand Down
Loading

0 comments on commit 5d39145

Please sign in to comment.