Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed May 21, 2024
1 parent cd874ee commit 50b3c1d
Show file tree
Hide file tree
Showing 51 changed files with 190 additions and 410 deletions.
8 changes: 6 additions & 2 deletions internal/component/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ import (
_ "github.com/grafana/alloy/internal/component/remote/kubernetes/secret" // Import remote.kubernetes.secret
_ "github.com/grafana/alloy/internal/component/remote/s3" // Import remote.s3
_ "github.com/grafana/alloy/internal/component/remote/vault" // Import remote.vault

_ "github.com/grafana/alloy/internal/util/otelfeaturegatefix" // Gracefully handle duplicate OTEL feature gates
"github.com/grafana/alloy/internal/util/otelfeaturegatefix"
)

func init() {
// Gracefully handle already registered errors in the OpenTelemetry Collector feature gate.
otelfeaturegatefix.SetGracefulAlreadyRegisteredErrorHandler()
}
11 changes: 2 additions & 9 deletions internal/component/discovery/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
Args: EC2Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return NewEC2(opts, args.(EC2Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(EC2Arguments))
},
})
}
Expand All @@ -51,7 +51,7 @@ type EC2Arguments struct {
HTTPClientConfig config.HTTPClientConfig `alloy:",squash"`
}

func (args EC2Arguments) Convert() *promaws.EC2SDConfig {
func (args EC2Arguments) Convert() discovery.DiscovererConfig {
cfg := &promaws.EC2SDConfig{
Endpoint: args.Endpoint,
Region: args.Region,
Expand Down Expand Up @@ -106,10 +106,3 @@ func (args *EC2Arguments) Validate() error {
}
return nil
}

// NewEC2 creates a new discovery.ec2 component.
func NewEC2(opts component.Options, args EC2Arguments) (component.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
return args.(EC2Arguments).Convert(), nil
})
}
8 changes: 6 additions & 2 deletions internal/component/discovery/aws/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"net/url"
"testing"

"github.com/grafana/alloy/internal/component/common/config"
promaws "github.com/prometheus/prometheus/discovery/aws"
"github.com/stretchr/testify/require"
"gotest.tools/assert"

"github.com/grafana/alloy/internal/component/common/config"
)

func TestConvert(t *testing.T) {
Expand All @@ -27,7 +29,9 @@ func TestConvert(t *testing.T) {
}

// ensure values are set
promArgs := alloyArgs.Convert()
converted := alloyArgs.Convert()
promArgs, ok := converted.(*promaws.EC2SDConfig)
require.True(t, ok)
assert.Equal(t, "us-east-1", promArgs.Region)
assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())
}
12 changes: 2 additions & 10 deletions internal/component/discovery/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (a *Arguments) Validate() error {
return a.ProxyConfig.Validate()
}

func (a *Arguments) Convert() *prom_discovery.SDConfig {
func (a Arguments) Convert() discovery.DiscovererConfig {
var (
authMethod string
clientID string
Expand Down Expand Up @@ -116,11 +116,3 @@ func (a *Arguments) Convert() *prom_discovery.SDConfig {
HTTPClientConfig: *httpClientConfig.Convert(),
}
}

// New returns a new instance of a discovery.azure component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
newArgs := args.(Arguments)
return newArgs.Convert(), nil
})
}
14 changes: 10 additions & 4 deletions internal/component/discovery/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"testing"
"time"

"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/syntax"
"github.com/prometheus/common/model"
promdiscovery "github.com/prometheus/prometheus/discovery/azure"
"github.com/stretchr/testify/require"
"gotest.tools/assert"

"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/syntax"
)

func TestAlloyUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -130,7 +132,9 @@ func TestConvert(t *testing.T) {
},
}

promArgs := alloyArgsOAuth.Convert()
args := alloyArgsOAuth.Convert()
promArgs, ok := args.(*promdiscovery.SDConfig)
require.True(t, ok)
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
assert.Equal(t, 8080, promArgs.Port)
assert.Equal(t, "subid", promArgs.SubscriptionID)
Expand Down Expand Up @@ -161,7 +165,9 @@ func TestConvert(t *testing.T) {
},
}

promArgs = alloyArgsManagedIdentity.Convert()
args = alloyArgsManagedIdentity.Convert()
promArgs, ok = args.(*promdiscovery.SDConfig)
require.True(t, ok)
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
assert.Equal(t, 8080, promArgs.Port)
assert.Equal(t, "subid", promArgs.SubscriptionID)
Expand Down
12 changes: 2 additions & 10 deletions internal/component/discovery/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func (args *Arguments) Validate() error {
return args.HTTPClientConfig.Validate()
}

func (args *Arguments) Convert() *prom_discovery.SDConfig {
func (args Arguments) Convert() discovery.DiscovererConfig {
httpClient := &args.HTTPClientConfig

return &prom_discovery.SDConfig{
Expand All @@ -91,11 +91,3 @@ func (args *Arguments) Convert() *prom_discovery.SDConfig {
NodeMeta: args.NodeMeta,
}
}

// New returns a new instance of a discovery.consul component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
newArgs := args.(Arguments)
return newArgs.Convert(), nil
})
}
13 changes: 2 additions & 11 deletions internal/component/discovery/consulagent/consulagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -62,8 +62,7 @@ func (args *Arguments) Validate() error {
return args.TLSConfig.Validate()
}

// Convert converts Arguments into the SDConfig type.
func (args *Arguments) Convert() *SDConfig {
func (args Arguments) Convert() discovery.DiscovererConfig {
return &SDConfig{
RefreshInterval: model.Duration(args.RefreshInterval),
Server: args.Server,
Expand All @@ -78,11 +77,3 @@ func (args *Arguments) Convert() *SDConfig {
TLSConfig: *args.TLSConfig.Convert(),
}
}

// New returns a new instance of a discovery.consulagent component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
newArgs := args.(Arguments)
return newArgs.Convert(), nil
})
}
5 changes: 3 additions & 2 deletions internal/component/discovery/consulagent/consulagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"testing"
"time"

"github.com/grafana/alloy/syntax"
promcfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/alloy/syntax"
)

func TestConvert(t *testing.T) {
Expand All @@ -32,7 +33,7 @@ func TestConvert(t *testing.T) {
err := syntax.Unmarshal([]byte(exampleAlloyConfig), &args)
require.NoError(t, err)

converted := args.Convert()
converted := args.Convert().(*SDConfig)
assert.Equal(t, "localhost:8500", converted.Server)
assert.Equal(t, promcfg.Secret("token"), converted.Token)
assert.Equal(t, "dc", converted.Datacenter)
Expand Down
12 changes: 2 additions & 10 deletions internal/component/discovery/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func (a *Arguments) Validate() error {
return a.ProxyConfig.Validate()
}

func (a *Arguments) Convert() *prom_discovery.SDConfig {
func (a Arguments) Convert() discovery.DiscovererConfig {
httpClientConfig := config.DefaultHTTPClientConfig
httpClientConfig.BearerToken = a.BearerToken
httpClientConfig.BearerTokenFile = a.BearerTokenFile
Expand All @@ -80,11 +80,3 @@ func (a *Arguments) Convert() *prom_discovery.SDConfig {
HTTPClientConfig: *httpClientConfig.Convert(),
}
}

// New returns a new instance of a discovery.digitalocean component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
newArgs := args.(Arguments)
return newArgs.Convert(), nil
})
}
12 changes: 7 additions & 5 deletions internal/component/discovery/digitalocean/digitalocean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"testing"
"time"

"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/syntax"
prom_common_config "github.com/prometheus/common/config"
promcommonconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promdiscovery "github.com/prometheus/prometheus/discovery/digitalocean"
"github.com/stretchr/testify/require"
"gotest.tools/assert"

"github.com/grafana/alloy/internal/component/common/config"
"github.com/grafana/alloy/syntax"
)

func TestAlloyUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -81,10 +83,10 @@ func TestConvert(t *testing.T) {
EnableHTTP2: false,
}

converted := args.Convert()
converted := args.Convert().(*promdiscovery.SDConfig)
assert.Equal(t, model.Duration(5*time.Minute), converted.RefreshInterval)
assert.Equal(t, 8181, converted.Port)
assert.Equal(t, prom_common_config.Secret("token"), converted.HTTPClientConfig.BearerToken)
assert.Equal(t, promcommonconfig.Secret("token"), converted.HTTPClientConfig.BearerToken)
assert.Equal(t, "http://example:8080", converted.HTTPClientConfig.ProxyURL.String())
assert.Equal(t, false, converted.HTTPClientConfig.FollowRedirects)
assert.Equal(t, false, converted.HTTPClientConfig.EnableHTTP2)
Expand Down
15 changes: 14 additions & 1 deletion internal/component/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ func New(o component.Options, args component.Arguments, creator Creator) (*Compo
return c, c.Update(args)
}

// ConvertibleConfig is used to more conveniently convert a configuration struct into a DiscovererConfig.
type ConvertibleConfig interface {
// Convert converts the struct into a DiscovererConfig.
Convert() DiscovererConfig
}

// NewFromConvertibleConfig creates a discovery component given a ConvertibleConfig. Convenience function for New.
func NewFromConvertibleConfig[T ConvertibleConfig](opts component.Options, conf T) (component.Component, error) {
return New(opts, conf, func(args component.Arguments) (DiscovererConfig, error) {
return args.(T).Convert(), nil
})
}

// Run implements component.Component.
func (c *Component) Run(ctx context.Context) error {
var (
Expand Down Expand Up @@ -219,7 +232,7 @@ func (c *Component) runDiscovery(ctx context.Context, d DiscovererWithMetrics) {
return
case groups := <-ch:
for _, group := range groups {
// DiscovererConfig will send an empty target set to indicate the group (keyed by Source field)
// Discoverer will send an empty target set to indicate the group (keyed by Source field)
// should be removed
if len(group.Targets) == 0 {
delete(cache, group.Source)
Expand Down
15 changes: 3 additions & 12 deletions internal/component/discovery/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -59,20 +59,11 @@ func (args *Arguments) Validate() error {
return nil
}

// Convert converts Arguments to the upstream Prometheus SD type.
func (args Arguments) Convert() dns.SDConfig {
return dns.SDConfig{
func (args Arguments) Convert() discovery.DiscovererConfig {
return &dns.SDConfig{
Names: args.Names,
RefreshInterval: model.Duration(args.RefreshInterval),
Type: args.Type,
Port: args.Port,
}
}

// New returns a new instance of a discovery.dns component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.DiscovererConfig, error) {
conf := args.(Arguments).Convert()
return &conf, nil
})
}
6 changes: 4 additions & 2 deletions internal/component/discovery/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"testing"
"time"

"github.com/grafana/alloy/syntax"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/stretchr/testify/require"
"gotest.tools/assert"

"github.com/grafana/alloy/syntax"
)

func TestAlloyUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -71,7 +73,7 @@ func TestConvert(t *testing.T) {
Names: []string{"example.com", "example2.com"},
}

converted := args.Convert()
converted := args.Convert().(*dns.SDConfig)
assert.Equal(t, model.Duration(5*time.Minute), converted.RefreshInterval)
assert.Equal(t, 8181, converted.Port)
assert.Equal(t, "A", converted.Type)
Expand Down
Loading

0 comments on commit 50b3c1d

Please sign in to comment.