Skip to content

Commit

Permalink
Add HTTPClientConfig to discovery.ec2 and add a simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbrad committed Nov 14, 2023
1 parent 65d35ec commit 73c0d1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Main (unreleased)

- Improved resilience of graph evaluation in presence of slow components. (@thampiotr)

- Allow overriding `HTTPClientConfig` fields such as `proxy_url` in `discovery.ec2`. (@cmbrad)

### Bugfixes

- Set exit code 1 on grafana-agentctl non-runnable command. (@fgouteroux)
Expand Down
20 changes: 12 additions & 8 deletions component/discovery/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/river/rivertypes"
promcfg "github.com/prometheus/common/config"
Expand Down Expand Up @@ -42,18 +43,21 @@ type EC2Arguments struct {
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
Port int `river:"port,attr,optional"`
Filters []*EC2Filter `river:"filter,block,optional"`

HTTPClientConfig config.HTTPClientConfig `river:",squash"`
}

func (args EC2Arguments) Convert() *promaws.EC2SDConfig {
cfg := &promaws.EC2SDConfig{
Endpoint: args.Endpoint,
Region: args.Region,
AccessKey: args.AccessKey,
SecretKey: promcfg.Secret(args.SecretKey),
Profile: args.Profile,
RoleARN: args.RoleARN,
RefreshInterval: model.Duration(args.RefreshInterval),
Port: args.Port,
Endpoint: args.Endpoint,
Region: args.Region,
AccessKey: args.AccessKey,
SecretKey: promcfg.Secret(args.SecretKey),
Profile: args.Profile,
RoleARN: args.RoleARN,
RefreshInterval: model.Duration(args.RefreshInterval),
Port: args.Port,
HTTPClientConfig: *args.HTTPClientConfig.Convert(),
}
for _, f := range args.Filters {
cfg.Filters = append(cfg.Filters, &promaws.EC2Filter{
Expand Down
29 changes: 29 additions & 0 deletions component/discovery/aws/ec2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package aws

import (
"net/url"
"testing"

"github.com/grafana/agent/component/common/config"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)

func TestConvert(t *testing.T) {
// parse example proxy
u, err := url.Parse("http://example:8080")
require.NoError(t, err)
httpClientConfig := config.DefaultHTTPClientConfig
httpClientConfig.ProxyURL = config.URL{URL: u}

// example configuration
riverArgs := EC2Arguments{
Region: "us-east-1",
HTTPClientConfig: httpClientConfig,
}

// ensure values are set
promArgs := riverArgs.Convert()
assert.Equal(t, "us-east-1", promArgs.Region)
assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())
}

0 comments on commit 73c0d1e

Please sign in to comment.