Skip to content

Commit

Permalink
Update a bunch of converter messages and use standardized functions w…
Browse files Browse the repository at this point in the history
…here possible for validation.

Signed-off-by: erikbaranowski <[email protected]>
  • Loading branch information
erikbaranowski committed Oct 16, 2023
1 parent e4eed15 commit f84d2e5
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 167 deletions.
2 changes: 1 addition & 1 deletion converter/diag/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (ds Diagnostics) GenerateReport(writer io.Writer, reportType string) error
case Text:
return generateTextReport(writer, ds)
default:
return fmt.Errorf("unsupported diagnostic report type %q", reportType)
return fmt.Errorf("Invalid diagnostic report type %q", reportType)
}
}

Expand Down
40 changes: 37 additions & 3 deletions converter/internal/common/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,53 @@ func UnsupportedNotDeepEqualsMessage(a any, b any, name string, message string)
var diags diag.Diagnostics
if !reflect.DeepEqual(a, b) {
if message != "" {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("unsupported %s config was provided: %s", name, message))
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config: %s", name, message))
} else {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("unsupported %s config was provided.", name))
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config.", name))
}
}

return diags
}

func UnsupportedNotEquals(a any, b any, name string) diag.Diagnostics {
return UnsupportedNotEqualsMessage(a, b, name, "")
}

func UnsupportedNotEqualsMessage(a any, b any, name string, message string) diag.Diagnostics {
var diags diag.Diagnostics
if a != b {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("unsupported %s config was provided.", name))
if message != "" {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config: %s", name, message))
} else {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config.", name))
}
}

return diags
}

func UnsupportedEquals(a any, b any, name string) diag.Diagnostics {
return UnsupportedEqualsMessage(a, b, name, "")
}

func UnsupportedEqualsMessage(a any, b any, name string, message string) diag.Diagnostics {
var diags diag.Diagnostics
if a == b {
if message != "" {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config: %s", name, message))
} else {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config.", name))
}
}

return diags
}

func UnsupportedNotNil(a any, name string) diag.Diagnostics {
var diags diag.Diagnostics
if a != nil {
diags.Add(diag.SeverityLevelError, fmt.Sprintf("The converter does not support converting the provided %s config.", name))
}

return diags
Expand Down
20 changes: 5 additions & 15 deletions converter/internal/prometheusconvert/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
"github.com/grafana/river/rivertypes"
prom_config "github.com/prometheus/common/config"
)
Expand Down Expand Up @@ -31,21 +32,10 @@ func ToHttpClientConfig(httpClientConfig *prom_config.HTTPClientConfig) *config.
func ValidateHttpClientConfig(httpClientConfig *prom_config.HTTPClientConfig) diag.Diagnostics {
var diags diag.Diagnostics

if httpClientConfig.NoProxy != "" {
diags.Add(diag.SeverityLevelError, "unsupported HTTP Client config no_proxy was provided")
}

if httpClientConfig.ProxyFromEnvironment {
diags.Add(diag.SeverityLevelError, "unsupported HTTP Client config proxy_from_environment was provided")
}

if len(httpClientConfig.ProxyConnectHeader) > 0 {
diags.Add(diag.SeverityLevelError, "unsupported HTTP Client config proxy_connect_header was provided")
}

if httpClientConfig.TLSConfig.MaxVersion != 0 {
diags.Add(diag.SeverityLevelError, "unsupported HTTP Client config max_version was provided")
}
diags.AddAll(common.UnsupportedNotEquals(httpClientConfig.NoProxy, "", "HTTP Client no_proxy"))
diags.AddAll(common.UnsupportedEquals(httpClientConfig.ProxyFromEnvironment, true, "HTTP Client proxy_from_environment"))
diags.AddAll(common.UnsupportedEquals(len(httpClientConfig.ProxyConnectHeader) > 0, true, "HTTP Client proxy_connect_header"))
diags.AddAll(common.UnsupportedNotEquals(httpClientConfig.TLSConfig.MaxVersion, 0, "HTTP Client max_version"))

return diags
}
Expand Down
20 changes: 4 additions & 16 deletions converter/internal/prometheusconvert/digitalocean.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prometheusconvert

import (
"reflect"
"time"

"github.com/grafana/agent/component/common/config"
Expand All @@ -25,21 +24,10 @@ func appendDiscoveryDigitalOcean(pb *prometheusBlocks, label string, sdConfig *p
func validateDiscoveryDigitalOcean(sdConfig *prom_digitalocean.SDConfig) diag.Diagnostics {
var diags diag.Diagnostics

if sdConfig.HTTPClientConfig.BasicAuth != nil {
diags.Add(diag.SeverityLevelError, "unsupported basic_auth for digitalocean_sd_configs")
}

if sdConfig.HTTPClientConfig.Authorization != nil {
diags.Add(diag.SeverityLevelError, "unsupported authorization for digitalocean_sd_configs")
}

if sdConfig.HTTPClientConfig.OAuth2 != nil {
diags.Add(diag.SeverityLevelError, "unsupported oauth2 for digitalocean_sd_configs")
}

if !reflect.DeepEqual(prom_config.TLSConfig{}, sdConfig.HTTPClientConfig.TLSConfig) {
diags.Add(diag.SeverityLevelError, "unsupported oauth2 for digitalocean_sd_configs")
}
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.BasicAuth, nil, "digitalocean_sd_configs basic_auth"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.Authorization, nil, "digitalocean_sd_configs authorization"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.OAuth2, nil, "digitalocean_sd_configs oauth2"))
diags.AddAll(common.UnsupportedNotDeepEquals(prom_config.TLSConfig{}, sdConfig.HTTPClientConfig.TLSConfig, "digitalocean_sd_configs tls_config"))

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

Expand Down
10 changes: 5 additions & 5 deletions converter/internal/prometheusconvert/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"github.com/grafana/agent/component/discovery/docker"
"github.com/grafana/agent/converter/diag"
"github.com/grafana/agent/converter/internal/common"
prom_docker "github.com/prometheus/prometheus/discovery/moby"
prom_moby "github.com/prometheus/prometheus/discovery/moby"
)

func appendDiscoveryDocker(pb *prometheusBlocks, label string, sdConfig *prom_docker.DockerSDConfig) discovery.Exports {
func appendDiscoveryDocker(pb *prometheusBlocks, label string, sdConfig *prom_moby.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")
}

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

func toDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
func toDiscoveryDocker(sdConfig *prom_moby.DockerSDConfig) *docker.Arguments {
if sdConfig == nil {
return nil
}
Expand All @@ -37,7 +37,7 @@ func toDiscoveryDocker(sdConfig *prom_docker.DockerSDConfig) *docker.Arguments {
}
}

func toDockerFilters(filtersConfig []prom_docker.Filter) []docker.Filter {
func toDockerFilters(filtersConfig []prom_moby.Filter) []docker.Filter {
filters := make([]docker.Filter, 0)

for _, filter := range filtersConfig {
Expand Down
44 changes: 10 additions & 34 deletions converter/internal/prometheusconvert/ec2.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prometheusconvert

import (
"reflect"
"time"

"github.com/grafana/agent/component/discovery"
Expand All @@ -24,41 +23,18 @@ func appendDiscoveryEC2(pb *prometheusBlocks, label string, sdConfig *prom_aws.E
func validateDiscoveryEC2(sdConfig *prom_aws.EC2SDConfig) diag.Diagnostics {
var diags diag.Diagnostics

if sdConfig.HTTPClientConfig.BasicAuth != nil {
diags.Add(diag.SeverityLevelError, "unsupported basic_auth for ec2_sd_configs")
}

if sdConfig.HTTPClientConfig.Authorization != nil {
diags.Add(diag.SeverityLevelError, "unsupported authorization for ec2_sd_configs")
}

if sdConfig.HTTPClientConfig.OAuth2 != nil {
diags.Add(diag.SeverityLevelError, "unsupported oauth2 for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.BearerToken, prom_config.DefaultHTTPClientConfig.BearerToken) {
diags.Add(diag.SeverityLevelError, "unsupported bearer_token for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.BearerTokenFile, prom_config.DefaultHTTPClientConfig.BearerTokenFile) {
diags.Add(diag.SeverityLevelError, "unsupported bearer_token_file for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.FollowRedirects, prom_config.DefaultHTTPClientConfig.FollowRedirects) {
diags.Add(diag.SeverityLevelError, "unsupported follow_redirects for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.EnableHTTP2, prom_config.DefaultHTTPClientConfig.EnableHTTP2) {
diags.Add(diag.SeverityLevelError, "unsupported enable_http2 for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.ProxyConfig, prom_config.DefaultHTTPClientConfig.ProxyConfig) {
diags.Add(diag.SeverityLevelError, "unsupported proxy for ec2_sd_configs")
}
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.BasicAuth, nil, "ec2_sd_configs basic_auth"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.Authorization, nil, "ec2_sd_configs authorization"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.OAuth2, nil, "ec2_sd_configs oauth2"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.BearerToken, prom_config.DefaultHTTPClientConfig.BearerToken, "ec2_sd_configs bearer_token"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.BearerTokenFile, prom_config.DefaultHTTPClientConfig.BearerTokenFile, "ec2_sd_configs bearer_token_file"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.FollowRedirects, prom_config.DefaultHTTPClientConfig.FollowRedirects, "ec2_sd_configs follow_redirects"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.EnableHTTP2, prom_config.DefaultHTTPClientConfig.EnableHTTP2, "ec2_sd_configs enable_http2"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.ProxyConfig, prom_config.DefaultHTTPClientConfig.ProxyConfig, "ec2_sd_configs proxy"))

// Do a last check in case any of the specific checks missed anything.
if len(diags) == 0 && !reflect.DeepEqual(sdConfig.HTTPClientConfig, prom_config.DefaultHTTPClientConfig) {
diags.Add(diag.SeverityLevelError, "unsupported http_client_config for ec2_sd_configs")
if len(diags) == 0 {
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig, prom_config.DefaultHTTPClientConfig, "ec2_sd_configs http_client_config"))
}

return diags
Expand Down
44 changes: 10 additions & 34 deletions converter/internal/prometheusconvert/lightsail.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prometheusconvert

import (
"reflect"
"time"

"github.com/grafana/agent/component/discovery"
Expand All @@ -24,41 +23,18 @@ func appendDiscoveryLightsail(pb *prometheusBlocks, label string, sdConfig *prom
func validateDiscoveryLightsail(sdConfig *prom_aws.LightsailSDConfig) diag.Diagnostics {
var diags diag.Diagnostics

if sdConfig.HTTPClientConfig.BasicAuth != nil {
diags.Add(diag.SeverityLevelError, "unsupported basic_auth for ec2_sd_configs")
}

if sdConfig.HTTPClientConfig.Authorization != nil {
diags.Add(diag.SeverityLevelError, "unsupported authorization for ec2_sd_configs")
}

if sdConfig.HTTPClientConfig.OAuth2 != nil {
diags.Add(diag.SeverityLevelError, "unsupported oauth2 for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.BearerToken, prom_config.DefaultHTTPClientConfig.BearerToken) {
diags.Add(diag.SeverityLevelError, "unsupported bearer_token for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.BearerTokenFile, prom_config.DefaultHTTPClientConfig.BearerTokenFile) {
diags.Add(diag.SeverityLevelError, "unsupported bearer_token_file for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.FollowRedirects, prom_config.DefaultHTTPClientConfig.FollowRedirects) {
diags.Add(diag.SeverityLevelError, "unsupported follow_redirects for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.EnableHTTP2, prom_config.DefaultHTTPClientConfig.EnableHTTP2) {
diags.Add(diag.SeverityLevelError, "unsupported enable_http2 for ec2_sd_configs")
}

if !reflect.DeepEqual(sdConfig.HTTPClientConfig.ProxyConfig, prom_config.DefaultHTTPClientConfig.ProxyConfig) {
diags.Add(diag.SeverityLevelError, "unsupported proxy for ec2_sd_configs")
}
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.BasicAuth, nil, "lightsail_sd_configs basic_auth"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.Authorization, nil, "lightsail_sd_configs authorization"))
diags.AddAll(common.UnsupportedNotEquals(sdConfig.HTTPClientConfig.OAuth2, nil, "lightsail_sd_configs oauth2"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.BearerToken, prom_config.DefaultHTTPClientConfig.BearerToken, "lightsail_sd_configs bearer_token"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.BearerTokenFile, prom_config.DefaultHTTPClientConfig.BearerTokenFile, "lightsail_sd_configs bearer_token_file"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.FollowRedirects, prom_config.DefaultHTTPClientConfig.FollowRedirects, "lightsail_sd_configs follow_redirects"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.EnableHTTP2, prom_config.DefaultHTTPClientConfig.EnableHTTP2, "lightsail_sd_configs enable_http2"))
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig.ProxyConfig, prom_config.DefaultHTTPClientConfig.ProxyConfig, "lightsail_sd_configs proxy"))

// Do a last check in case any of the specific checks missed anything.
if len(diags) == 0 && !reflect.DeepEqual(sdConfig.HTTPClientConfig, prom_config.DefaultHTTPClientConfig) {
diags.Add(diag.SeverityLevelError, "unsupported http_client_config for ec2_sd_configs")
if len(diags) == 0 {
diags.AddAll(common.UnsupportedNotDeepEquals(sdConfig.HTTPClientConfig, prom_config.DefaultHTTPClientConfig, "lightsail_sd_configs http_client_config"))
}

return diags
Expand Down
6 changes: 2 additions & 4 deletions converter/internal/prometheusconvert/prometheusconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import (
prom_kubernetes "github.com/prometheus/prometheus/discovery/kubernetes"
prom_linode "github.com/prometheus/prometheus/discovery/linode"
prom_marathon "github.com/prometheus/prometheus/discovery/marathon"
prom_docker "github.com/prometheus/prometheus/discovery/moby"
prom_moby "github.com/prometheus/prometheus/discovery/moby"
prom_openstack "github.com/prometheus/prometheus/discovery/openstack"
prom_scaleway "github.com/prometheus/prometheus/discovery/scaleway"
prom_triton "github.com/prometheus/prometheus/discovery/triton"
prom_xds "github.com/prometheus/prometheus/discovery/xds"
prom_nerve "github.com/prometheus/prometheus/discovery/zookeeper"
prom_zk "github.com/prometheus/prometheus/discovery/zookeeper"
"github.com/prometheus/prometheus/storage"

Expand Down Expand Up @@ -148,7 +146,7 @@ func AppendServiceDiscoveryConfigs(pb *prometheusBlocks, serviceDiscoveryConfig
case *prom_dns.SDConfig:
labelCounts["dns"]++
exports = appendDiscoveryDns(pb, common.LabelWithIndex(labelCounts["dns"]-1, label), sdc)
case *prom_docker.DockerSDConfig:
case *prom_moby.DockerSDConfig:
labelCounts["docker"]++
exports = appendDiscoveryDocker(pb, common.LabelWithIndex(labelCounts["docker"]-1, label), sdc)
case *prom_aws.EC2SDConfig:
Expand Down Expand Up @@ -187,7 +185,7 @@ func AppendServiceDiscoveryConfigs(pb *prometheusBlocks, serviceDiscoveryConfig
case *prom_linode.SDConfig:
labelCounts["linode"]++
exports = appendDiscoveryLinode(pb, common.LabelWithIndex(labelCounts["linode"]-1, label), sdc)
case *prom_nerve.NerveSDConfig:
case *prom_zk.NerveSDConfig:
labelCounts["nerve"]++
exports = appendDiscoveryNerve(pb, common.LabelWithIndex(labelCounts["nerve"]-1, label), sdc)
case *prom_openstack.SDConfig:
Expand Down
5 changes: 1 addition & 4 deletions converter/internal/prometheusconvert/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func appendPrometheusScrape(pb *prometheusBlocks, scrapeConfig *prom_config.Scra
func validatePrometheusScrape(scrapeConfig *prom_config.ScrapeConfig) diag.Diagnostics {
var diags diag.Diagnostics

if scrapeConfig.NativeHistogramBucketLimit != 0 {
diags.Add(diag.SeverityLevelError, "unsupported native_histogram_bucket_limit for scrape_configs")
}

diags.AddAll(common.UnsupportedNotEquals(scrapeConfig.NativeHistogramBucketLimit != 0, true, "scrape_configs native_histogram_bucket_limit"))
diags.AddAll(ValidateHttpClientConfig(&scrapeConfig.HTTPClientConfig))

return diags
Expand Down
Loading

0 comments on commit f84d2e5

Please sign in to comment.