Skip to content

Commit

Permalink
initial wiring for a converter to receive extra arguments (#5602)
Browse files Browse the repository at this point in the history
* initial wiring for a converter to receive extra arguments

Signed-off-by: erikbaranowski <[email protected]>

---------

Signed-off-by: erikbaranowski <[email protected]>
  • Loading branch information
erikbaranowski authored Oct 26, 2023
1 parent 3d96a2c commit 561aa90
Show file tree
Hide file tree
Showing 47 changed files with 165 additions and 91 deletions.
2 changes: 1 addition & 1 deletion cmd/internal/flowmode/cmd_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func convert(r io.Reader, fc *flowConvert) error {
return err
}

riverBytes, diags := converter.Convert(inputBytes, converter.Input(fc.sourceFormat))
riverBytes, diags := converter.Convert(inputBytes, converter.Input(fc.sourceFormat), []string{})
err = generateConvertReport(diags, fc)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/flowmode/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func loadFlowSource(path string, converterSourceFormat string, converterBypassEr
}
if converterSourceFormat != "flow" {
var diags convert_diag.Diagnostics
bb, diags = converter.Convert(bb, converter.Input(converterSourceFormat))
bb, diags = converter.Convert(bb, converter.Input(converterSourceFormat), []string{})
hasError := hasErrorLevel(diags, convert_diag.SeverityLevelError)
hasCritical := hasErrorLevel(diags, convert_diag.SeverityLevelCritical)
if hasCritical || (!converterBypassErrors && hasError) {
Expand Down
12 changes: 8 additions & 4 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var SupportedFormats = []string{
// Convert generates a Grafana Agent Flow config given an input configuration
// file.
//
// extraArgs are supported to be passed along to a converter such as enabling
// integrations-next for the static converter. Converters that do not support
// extraArgs will return a critical severity diagnostic if any are passed.
//
// Conversions are made as literally as possible, so the resulting config files
// may be unoptimized (i.e., lacking component reuse). A converted config file
// should just be the starting point rather than the final destination.
Expand All @@ -41,14 +45,14 @@ var SupportedFormats = []string{
// because of mismatched functionality, an error is returned with no resulting
// config. If the conversion completed successfully but generated warnings, an
// error is returned alongside the resulting config.
func Convert(in []byte, kind Input) ([]byte, diag.Diagnostics) {
func Convert(in []byte, kind Input, extraArgs []string) ([]byte, diag.Diagnostics) {
switch kind {
case InputPrometheus:
return prometheusconvert.Convert(in)
return prometheusconvert.Convert(in, extraArgs)
case InputPromtail:
return promtailconvert.Convert(in)
return promtailconvert.Convert(in, extraArgs)
case InputStatic:
return staticconvert.Convert(in)
return staticconvert.Convert(in, extraArgs)
}

var diags diag.Diagnostics
Expand Down
10 changes: 9 additions & 1 deletion converter/internal/prometheusconvert/prometheusconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ import (
)

// Convert implements a Prometheus config converter.
func Convert(in []byte) ([]byte, diag.Diagnostics) {
//
// extraArgs are supported to mirror the other converter params due to shared
// testing code but they should be passed empty to this converter.
func Convert(in []byte, extraArgs []string) ([]byte, diag.Diagnostics) {
var diags diag.Diagnostics

if len(extraArgs) > 0 {
diags.Add(diag.SeverityLevelCritical, fmt.Sprintf("extra arguments are not supported for the prometheus converter: %s", extraArgs))
return nil, diags
}

promConfig, err := prom_config.Load(string(in), false, log.NewNopLogger())
if err != nil {
diags.Add(diag.SeverityLevelCritical, fmt.Sprintf("failed to parse Prometheus config: %s", err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

func TestConvert(t *testing.T) {
test_common.TestDirectory(t, "testdata", ".yaml", true, prometheusconvert.Convert)
test_common.TestDirectory(t, "testdata", ".yaml", true, []string{}, prometheusconvert.Convert)
}
10 changes: 9 additions & 1 deletion converter/internal/promtailconvert/promtailconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ func (c *Config) Clone() flagext.Registerer {
}

// Convert implements a Promtail config converter.
func Convert(in []byte) ([]byte, diag.Diagnostics) {
//
// extraArgs are supported to mirror the other converter params due to shared
// testing code but they should be passed empty to this converter.
func Convert(in []byte, extraArgs []string) ([]byte, diag.Diagnostics) {
var (
diags diag.Diagnostics
cfg Config
)

if len(extraArgs) > 0 {
diags.Add(diag.SeverityLevelCritical, fmt.Sprintf("extra arguments are not supported for the promtail converter: %s", extraArgs))
return nil, diags
}

// Set default values first.
flagSet := flag.NewFlagSet("", flag.PanicOnError)
err := lokicfgutil.Unmarshal(&cfg,
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/promtailconvert/promtailconvert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

func TestConvert(t *testing.T) {
test_common.TestDirectory(t, "testdata", ".yaml", true, promtailconvert.Convert)
test_common.TestDirectory(t, "testdata", ".yaml", true, []string{}, promtailconvert.Convert)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
agent_exporter "github.com/grafana/agent/pkg/integrations/agent"
)

func (b *IntegrationsV1ConfigBuilder) appendAgentExporter(config *agent_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendAgentExporter(config *agent_exporter.Config) discovery.Exports {
args := toAgentExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/apache_http"
)

func (b *IntegrationsV1ConfigBuilder) appendApacheExporter(config *apache_http.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendApacheExporter(config *apache_http.Config) discovery.Exports {
args := toApacheExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/azure_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendAzureExporter(config *azure_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendAzureExporter(config *azure_exporter.Config) discovery.Exports {
args := toAzureExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendBlackboxExporter(config *blackbox_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendBlackboxExporter(config *blackbox_exporter.Config) discovery.Exports {
args := toBlackboxExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
27 changes: 21 additions & 6 deletions converter/internal/staticconvert/internal/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,40 @@ import (
prom_config "github.com/prometheus/prometheus/config"
)

type IntegrationsV1ConfigBuilder struct {
type IntegrationsConfigBuilder struct {
f *builder.File
diags *diag.Diagnostics
cfg *config.Config
globalCtx *GlobalContext
}

func NewIntegrationsV1ConfigBuilder(f *builder.File, diags *diag.Diagnostics, cfg *config.Config, globalCtx *GlobalContext) *IntegrationsV1ConfigBuilder {
return &IntegrationsV1ConfigBuilder{
func NewIntegrationsConfigBuilder(f *builder.File, diags *diag.Diagnostics, cfg *config.Config, globalCtx *GlobalContext) *IntegrationsConfigBuilder {
return &IntegrationsConfigBuilder{
f: f,
diags: diags,
cfg: cfg,
globalCtx: globalCtx,
}
}

func (b *IntegrationsV1ConfigBuilder) Build() {
func (b *IntegrationsConfigBuilder) Build() {
b.appendLogging(b.cfg.Server)
b.appendServer(b.cfg.Server)
b.appendIntegrations()
}

func (b *IntegrationsV1ConfigBuilder) appendIntegrations() {
func (b *IntegrationsConfigBuilder) appendIntegrations() {
switch b.cfg.Integrations.Version {
case config.IntegrationsVersion1:
b.appendV1Integrations()
case config.IntegrationsVersion2:
b.appendV2Integrations()
default:
panic(fmt.Sprintf("unknown integrations version %d", b.cfg.Integrations.Version))
}
}

func (b *IntegrationsConfigBuilder) appendV1Integrations() {
for _, integration := range b.cfg.Integrations.ConfigV1.Integrations {
if !integration.Common.Enabled {
continue
Expand Down Expand Up @@ -140,7 +151,11 @@ func (b *IntegrationsV1ConfigBuilder) appendIntegrations() {
}
}

func (b *IntegrationsV1ConfigBuilder) appendExporter(commonConfig *int_config.Common, name string, extraTargets []discovery.Target) {
func (b *IntegrationsConfigBuilder) appendV2Integrations() {

}

func (b *IntegrationsConfigBuilder) appendExporter(commonConfig *int_config.Common, name string, extraTargets []discovery.Target) {
scrapeConfig := prom_config.DefaultScrapeConfig
scrapeConfig.JobName = fmt.Sprintf("integrations/%s", name)
scrapeConfig.RelabelConfigs = commonConfig.RelabelConfigs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
cadvisor_integration "github.com/grafana/agent/pkg/integrations/cadvisor"
)

func (b *IntegrationsV1ConfigBuilder) appendCadvisorExporter(config *cadvisor_integration.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendCadvisorExporter(config *cadvisor_integration.Config) discovery.Exports {
args := toCadvisorExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/cloudwatch_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendCloudwatchExporter(config *cloudwatch_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendCloudwatchExporter(config *cloudwatch_exporter.Config) discovery.Exports {
args := toCloudwatchExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/consul_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendConsulExporter(config *consul_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendConsulExporter(config *consul_exporter.Config) discovery.Exports {
args := toConsulExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/dnsmasq_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendDnsmasqExporter(config *dnsmasq_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendDnsmasqExporter(config *dnsmasq_exporter.Config) discovery.Exports {
args := toDnsmasqExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/elasticsearch_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendElasticsearchExporter(config *elasticsearch_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendElasticsearchExporter(config *elasticsearch_exporter.Config) discovery.Exports {
args := toElasticsearchExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/gcp_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendGcpExporter(config *gcp_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendGcpExporter(config *gcp_exporter.Config) discovery.Exports {
args := toGcpExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendGithubExporter(config *github_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendGithubExporter(config *github_exporter.Config) discovery.Exports {
args := toGithubExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/kafka_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendKafkaExporter(config *kafka_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendKafkaExporter(config *kafka_exporter.Config) discovery.Exports {
args := toKafkaExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/staticconvert/internal/build/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/grafana/agent/pkg/server"
)

func (b *IntegrationsV1ConfigBuilder) appendLogging(config *server.Config) {
func (b *IntegrationsConfigBuilder) appendLogging(config *server.Config) {
args := toLogging(config)
if !reflect.DeepEqual(*args, logging.DefaultOptions) {
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/memcached_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendMemcachedExporter(config *memcached_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendMemcachedExporter(config *memcached_exporter.Config) discovery.Exports {
args := toMemcachedExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendMongodbExporter(config *mongodb_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendMongodbExporter(config *mongodb_exporter.Config) discovery.Exports {
args := toMongodbExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendMssqlExporter(config *mssql_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendMssqlExporter(config *mssql_exporter.Config) discovery.Exports {
args := toMssqlExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendMysqldExporter(config *mysqld_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendMysqldExporter(config *mysqld_exporter.Config) discovery.Exports {
args := toMysqldExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/grafana/agent/pkg/integrations/node_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendNodeExporter(config *node_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendNodeExporter(config *node_exporter.Config) discovery.Exports {
args := toNodeExporter(config)
b.f.Body().AppendBlock(common.NewBlockWithOverride(
[]string{"prometheus", "exporter", "unix"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendOracledbExporter(config *oracledb_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendOracledbExporter(config *oracledb_exporter.Config) discovery.Exports {
args := toOracledbExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendPostgresExporter(config *postgres_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendPostgresExporter(config *postgres_exporter.Config) discovery.Exports {
args := toPostgresExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/grafana/agent/pkg/integrations/process_exporter"
)

func (b *IntegrationsV1ConfigBuilder) appendProcessExporter(config *process_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendProcessExporter(config *process_exporter.Config) discovery.Exports {
args := toProcessExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendRedisExporter(config *redis_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendRedisExporter(config *redis_exporter.Config) discovery.Exports {
args := toRedisExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
2 changes: 1 addition & 1 deletion converter/internal/staticconvert/internal/build/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/grafana/agent/service/http"
)

func (b *IntegrationsV1ConfigBuilder) appendServer(config *server.Config) {
func (b *IntegrationsConfigBuilder) appendServer(config *server.Config) {
args := toServer(config)
if !reflect.DeepEqual(*args.TLS, http.TLSArguments{}) {
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
snmp_config "github.com/prometheus/snmp_exporter/config"
)

func (b *IntegrationsV1ConfigBuilder) appendSnmpExporter(config *snmp_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendSnmpExporter(config *snmp_exporter.Config) discovery.Exports {
args := toSnmpExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendSnowflakeExporter(config *snowflake_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendSnowflakeExporter(config *snowflake_exporter.Config) discovery.Exports {
args := toSnowflakeExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grafana/river/rivertypes"
)

func (b *IntegrationsV1ConfigBuilder) appendSquidExporter(config *squid_exporter.Config) discovery.Exports {
func (b *IntegrationsConfigBuilder) appendSquidExporter(config *squid_exporter.Config) discovery.Exports {
args := toSquidExporter(config)
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name())
b.f.Body().AppendBlock(common.NewBlockWithOverride(
Expand Down
Loading

0 comments on commit 561aa90

Please sign in to comment.