Skip to content

Commit

Permalink
Promtail config conversion - part 2 (#4500)
Browse files Browse the repository at this point in the history
* wip

* fixes

* static SD

* file discovery

* consul

* consul, DO, docker swarm

* other unsupported

* GCE

* format-yaml

* ec2

* azure

* todos

* server validation

* loki push api

* goimports

* fix issue in prometheus/discovery/relabel

* fix lint

* fix another test case

* use random port

* move loki.write to own file
  • Loading branch information
thampiotr authored and clayton-cornell committed Aug 14, 2023
1 parent e4b61eb commit d10f5ff
Show file tree
Hide file tree
Showing 63 changed files with 1,216 additions and 124 deletions.
81 changes: 81 additions & 0 deletions converter/internal/common/weaveworks_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package common

import (
"github.com/grafana/agent/converter/diag"
"github.com/weaveworks/common/server"
)

func DefaultWeaveWorksServerCfg() server.Config {
cfg := server.Config{}
// NOTE: due to a bug in promtail, the default server values for loki_push_api are not applied currently, so
// we need to comment out the following line.
//cfg.RegisterFlags(flag.NewFlagSet("", flag.PanicOnError))
return cfg
}

func ValidateWeaveWorksServerCfg(cfg server.Config) diag.Diagnostics {
var (
diags diag.Diagnostics
defaultCfg = DefaultWeaveWorksServerCfg()
)

if cfg.HTTPListenNetwork != defaultCfg.HTTPListenNetwork {
diags.Add(diag.SeverityLevelError, "http_listen_network is not supported in server config")
}
if cfg.GRPCListenNetwork != defaultCfg.GRPCListenNetwork {
diags.Add(diag.SeverityLevelError, "grpc_listen_network is not supported in server config")
}
if cfg.CipherSuites != defaultCfg.CipherSuites {
diags.Add(diag.SeverityLevelError, "tls_cipher_suites is not supported in server config")
}
if cfg.MinVersion != defaultCfg.MinVersion {
diags.Add(diag.SeverityLevelError, "tls_min_version is not supported in server config")
}
if cfg.HTTPTLSConfig != defaultCfg.HTTPTLSConfig {
diags.Add(diag.SeverityLevelError, "http_tls_config is not supported in server config")
}
if cfg.GRPCTLSConfig != defaultCfg.GRPCTLSConfig {
diags.Add(diag.SeverityLevelError, "grpc_tls_config is not supported in server config")
}
if cfg.RegisterInstrumentation {
diags.Add(diag.SeverityLevelError, "register_instrumentation is not supported in server config")
}
if cfg.ServerGracefulShutdownTimeout != defaultCfg.ServerGracefulShutdownTimeout {
diags.Add(diag.SeverityLevelError, "graceful_shutdown_timeout is not supported in server config")
}
if cfg.GRPCServerTime != defaultCfg.GRPCServerTime {
diags.Add(diag.SeverityLevelError, "grpc_server_keepalive_time is not supported in server config")
}
if cfg.GRPCServerTimeout != defaultCfg.GRPCServerTimeout {
diags.Add(diag.SeverityLevelError, "grpc_server_keepalive_timeout is not supported in server config")
}
if cfg.GRPCServerMinTimeBetweenPings != defaultCfg.GRPCServerMinTimeBetweenPings {
diags.Add(diag.SeverityLevelError, "grpc_server_min_time_between_pings is not supported in server config")
}
if cfg.GRPCServerPingWithoutStreamAllowed != defaultCfg.GRPCServerPingWithoutStreamAllowed {
diags.Add(diag.SeverityLevelError, "grpc_server_ping_without_stream_allowed is not supported in server config")
}
if cfg.LogFormat != defaultCfg.LogFormat {
diags.Add(diag.SeverityLevelError, "log_format is not supported in server config")
}
if cfg.LogLevel.String() != defaultCfg.LogLevel.String() {
diags.Add(diag.SeverityLevelError, "log_level is not supported in server config")
}
if cfg.LogSourceIPs != defaultCfg.LogSourceIPs {
diags.Add(diag.SeverityLevelError, "log_source_ips_enabled is not supported in server config")
}
if cfg.LogSourceIPsHeader != defaultCfg.LogSourceIPsHeader {
diags.Add(diag.SeverityLevelError, "log_source_ips_header is not supported in server config")
}
if cfg.LogSourceIPsRegex != defaultCfg.LogSourceIPsRegex {
diags.Add(diag.SeverityLevelError, "log_source_ips_regex is not supported in server config")
}
if cfg.LogRequestAtInfoLevel != defaultCfg.LogRequestAtInfoLevel {
diags.Add(diag.SeverityLevelError, "log_request_at_info_level_enabled is not supported in server config")
}
if cfg.PathPrefix != defaultCfg.PathPrefix {
diags.Add(diag.SeverityLevelError, "http_path_prefix is not supported in server config")
}

return diags
}
6 changes: 3 additions & 3 deletions converter/internal/prometheusconvert/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

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

func toDiscoveryAzure(sdConfig *prom_azure.SDConfig) *azure.Arguments {
func ToDiscoveryAzure(sdConfig *prom_azure.SDConfig) *azure.Arguments {
if sdConfig == nil {
return nil
}
Expand All @@ -40,7 +40,7 @@ func toDiscoveryAzure(sdConfig *prom_azure.SDConfig) *azure.Arguments {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions converter/internal/prometheusconvert/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func appendDiscoveryConsul(pb *prometheusBlocks, label string, sdConfig *prom_consul.SDConfig) discovery.Exports {
discoveryConsulArgs := toDiscoveryConsul(sdConfig)
discoveryConsulArgs := ToDiscoveryConsul(sdConfig)
name := []string{"discovery", "consul"}
block := common.NewBlockWithOverride(name, label, discoveryConsulArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
Expand All @@ -23,7 +23,7 @@ func validateDiscoveryConsul(sdConfig *prom_consul.SDConfig) diag.Diagnostics {
return ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
}

func toDiscoveryConsul(sdConfig *prom_consul.SDConfig) *consul.Arguments {
func ToDiscoveryConsul(sdConfig *prom_consul.SDConfig) *consul.Arguments {
if sdConfig == nil {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions converter/internal/prometheusconvert/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
)

func appendDiscoveryDigitalOcean(pb *prometheusBlocks, label string, sdConfig *prom_digitalocean.SDConfig) discovery.Exports {
discoveryDigitalOceanArgs := toDiscoveryDigitalOcean(sdConfig)
discoveryDigitalOceanArgs := ToDiscoveryDigitalOcean(sdConfig)
name := []string{"discovery", "digitalocean"}
block := common.NewBlockWithOverride(name, label, discoveryDigitalOceanArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return newDiscoverExports("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 @@ -47,7 +47,7 @@ func validateDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) diag.Di
return diags
}

func toDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) *digitalocean.Arguments {
func ToDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) *digitalocean.Arguments {
if sdConfig == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions converter/internal/prometheusconvert/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func appendDiscoveryDocker(pb *prometheusBlocks, label string, sdConfig *prom_docker.DockerSDConfig) discovery.Exports {
discoveryDockerArgs := toDiscoveryDocker(sdConfig)
discoveryDockerArgs := ToDiscoveryDocker(sdConfig)
name := []string{"discovery", "docker"}
block := common.NewBlockWithOverride(name, label, discoveryDockerArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
Expand All @@ -22,7 +22,7 @@ func validateDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) diag.Diagnost
return ValidateHttpClientConfig(&sdConfig.HTTPClientConfig)
}

func toDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
func ToDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
if sdConfig == nil {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions converter/internal/prometheusconvert/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
)

func appendDiscoveryEC2(pb *prometheusBlocks, label string, sdConfig *prom_aws.EC2SDConfig) discovery.Exports {
discoveryec2Args := toDiscoveryEC2(sdConfig)
discoveryec2Args := ToDiscoveryEC2(sdConfig)
name := []string{"discovery", "ec2"}
block := common.NewBlockWithOverride(name, label, discoveryec2Args)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return newDiscoverExports("discovery.ec2." + label + ".targets")
}

func validateDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) diag.Diagnostics {
func ValidateDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) diag.Diagnostics {
var diags diag.Diagnostics

if sdConfig.HTTPClientConfig.BasicAuth != nil {
Expand Down Expand Up @@ -64,7 +64,7 @@ func validateDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) diag.Diagnostics {
return diags
}

func toDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) *aws.EC2Arguments {
func ToDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) *aws.EC2Arguments {
if sdConfig == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions converter/internal/prometheusconvert/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func appendDiscoveryFile(pb *prometheusBlocks, label string, sdConfig *prom_file.SDConfig) discovery.Exports {
discoveryFileArgs := toDiscoveryFile(sdConfig)
discoveryFileArgs := ToDiscoveryFile(sdConfig)
name := []string{"discovery", "file"}
block := common.NewBlockWithOverride(name, label, discoveryFileArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
Expand All @@ -22,7 +22,7 @@ func validateDiscoveryFile(sdConfig *prom_file.SDConfig) diag.Diagnostics {
return make(diag.Diagnostics, 0)
}

func toDiscoveryFile(sdConfig *prom_file.SDConfig) *file.Arguments {
func ToDiscoveryFile(sdConfig *prom_file.SDConfig) *file.Arguments {
if sdConfig == nil {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions converter/internal/prometheusconvert/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
)

func appendDiscoveryGCE(pb *prometheusBlocks, label string, sdConfig *prom_gce.SDConfig) discovery.Exports {
discoveryGCEArgs := toDiscoveryGCE(sdConfig)
discoveryGCEArgs := ToDiscoveryGCE(sdConfig)
name := []string{"discovery", "gce"}
block := common.NewBlockWithOverride(name, label, discoveryGCEArgs)
pb.discoveryBlocks = append(pb.discoveryBlocks, newPrometheusBlock(block, name, label, "", ""))
return newDiscoverExports("discovery.gce." + label + ".targets")
}

func validateDiscoveryGce(sdConfig *prom_gce.SDConfig) diag.Diagnostics {
func ValidateDiscoveryGCE(sdConfig *prom_gce.SDConfig) diag.Diagnostics {
return make(diag.Diagnostics, 0)
}

func toDiscoveryGCE(sdConfig *prom_gce.SDConfig) *gce.Arguments {
func ToDiscoveryGCE(sdConfig *prom_gce.SDConfig) *gce.Arguments {
if sdConfig == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/prometheusconvert/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func appendDiscoveryRelabel(pb *prometheusBlocks, relabelConfigs []*prom_relabel
pb.discoveryRelabelBlocks = append(pb.discoveryRelabelBlocks, newPrometheusBlock(block, name, label, "", ""))

return &disc_relabel.Exports{
Output: newDiscoveryTargets(fmt.Sprintf("discovery.relabel.%s.targets", label)),
Output: newDiscoveryTargets(fmt.Sprintf("discovery.relabel.%s.output", label)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ discovery.relabel "prometheus1" {
}

prometheus.scrape "prometheus1" {
targets = discovery.relabel.prometheus1.targets
targets = discovery.relabel.prometheus1.output
forward_to = [prometheus.relabel.prometheus1.receiver]
job_name = "prometheus1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ discovery.relabel "prometheus2" {
}

prometheus.scrape "prometheus1" {
targets = discovery.relabel.prometheus1.targets
targets = discovery.relabel.prometheus1.output
forward_to = [prometheus.remote_write.default.receiver]
job_name = "prometheus1"
}

prometheus.scrape "prometheus2" {
targets = discovery.relabel.prometheus2.targets
targets = discovery.relabel.prometheus2.output
forward_to = [prometheus.remote_write.default.receiver]
job_name = "prometheus2"
}
Expand Down
8 changes: 4 additions & 4 deletions converter/internal/prometheusconvert/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ func validateScrapeConfigs(scrapeConfigs []*prom_config.ScrapeConfig) diag.Diagn
case prom_discover.StaticConfig:
newDiags = validateScrapeTargets(sdc)
case *prom_azure.SDConfig:
newDiags = validateDiscoveryAzure(sdc)
newDiags = ValidateDiscoveryAzure(sdc)
case *prom_consul.SDConfig:
newDiags = validateDiscoveryConsul(sdc)
case *prom_digitalocean.SDConfig:
newDiags = validateDiscoveryDigitalOcean(sdc)
newDiags = ValidateDiscoveryDigitalOcean(sdc)
case *prom_dns.SDConfig:
newDiags = validateDiscoveryDns(sdc)
case *prom_docker.DockerSDConfig:
newDiags = validateDiscoveryDocker(sdc)
case *prom_aws.EC2SDConfig:
newDiags = validateDiscoveryEC2(sdc)
newDiags = ValidateDiscoveryEC2(sdc)
case *prom_file.SDConfig:
newDiags = validateDiscoveryFile(sdc)
case *prom_gce.SDConfig:
newDiags = validateDiscoveryGce(sdc)
newDiags = ValidateDiscoveryGCE(sdc)
case *prom_kubernetes.SDConfig:
newDiags = validateDiscoveryKubernetes(sdc)
case *prom_aws.LightsailSDConfig:
Expand Down
26 changes: 26 additions & 0 deletions converter/internal/promtailconvert/internal/build/azure_sd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package build

import (
"fmt"

"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert"
)

func (s *ScrapeConfigBuilder) AppendAzureSDs() {
if len(s.cfg.ServiceDiscoveryConfig.AzureSDConfigs) == 0 {
return
}
for i, sd := range s.cfg.ServiceDiscoveryConfig.AzureSDConfigs {
s.diags.AddAll(prometheusconvert.ValidateDiscoveryAzure(sd))
compName := fmt.Sprintf("%s_%d", s.cfg.JobName, i)

args := prometheusconvert.ToDiscoveryAzure(sd)
s.f.Body().AppendBlock(common.NewBlockWithOverride(
[]string{"discovery", "azure"},
compName,
args,
))
s.allTargetsExps = append(s.allTargetsExps, "discovery.azure."+compName+".targets")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (s *ScrapeConfigBuilder) AppendCloudFlareConfig() {
}
}
s.f.Body().AppendBlock(common.NewBlockWithOverrideFn(
[]string{"loki", "source", "cloudfare"},
[]string{"loki", "source", "cloudflare"},
s.cfg.JobName,
args,
override,
Expand Down
13 changes: 13 additions & 0 deletions converter/internal/promtailconvert/internal/build/consul_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package build

import "github.com/grafana/agent/converter/diag"

func (s *ScrapeConfigBuilder) AppendConsulAgentSDs() {
// TODO: implement this
if s.cfg.ServiceDiscoveryConfig.ConsulAgentSDConfigs != nil {
s.diags.Add(
diag.SeverityLevelError,
"consul_agent SDs are not currently supported in Grafana Agent Flow - see https://github.com/grafana/agent/issues/2261",
)
}
}
25 changes: 25 additions & 0 deletions converter/internal/promtailconvert/internal/build/consul_sd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package build

import (
"fmt"

"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert"
)

func (s *ScrapeConfigBuilder) AppendConsulSDs() {
if len(s.cfg.ServiceDiscoveryConfig.ConsulSDConfigs) == 0 {
return
}

for i, sd := range s.cfg.ServiceDiscoveryConfig.ConsulSDConfigs {
args := prometheusconvert.ToDiscoveryConsul(sd)
compLabel := fmt.Sprintf("consul_sd_%d", i)
s.f.Body().AppendBlock(common.NewBlockWithOverride(
[]string{"discovery", "consul"},
compLabel,
args,
))
s.allTargetsExps = append(s.allTargetsExps, "discovery.consul."+compLabel+".targets")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package build

import (
"fmt"

"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/agent/converter/internal/prometheusconvert"
)

func (s *ScrapeConfigBuilder) AppendDigitalOceanSDs() {
if len(s.cfg.ServiceDiscoveryConfig.DigitalOceanSDConfigs) == 0 {
return
}
for i, sd := range s.cfg.ServiceDiscoveryConfig.DigitalOceanSDConfigs {
s.diags.AddAll(prometheusconvert.ValidateDiscoveryDigitalOcean(sd))
compName := fmt.Sprintf("%s_%d", s.cfg.JobName, i)

args := prometheusconvert.ToDiscoveryDigitalOcean(sd)
s.f.Body().AppendBlock(common.NewBlockWithOverride(
[]string{"discovery", "digitalocean"},
compName,
args,
))
s.allTargetsExps = append(s.allTargetsExps, "discovery.digitalocean."+compName+".targets")
}
}
Loading

0 comments on commit d10f5ff

Please sign in to comment.