Skip to content

Commit

Permalink
Add HTTPClientConfig to discovery.ec2 (grafana#5767)
Browse files Browse the repository at this point in the history
Co-authored-by: Paschalis Tsilias <[email protected]>
  • Loading branch information
2 people authored and BarunKGP committed Feb 20, 2024
1 parent 2e43e81 commit 496a40b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Main (unreleased)

- Added links between compatible components in the documentation to make it
easier to discover them. (@thampiotr)

- Allow defining `HTTPClientConfig` for `discovery.ec2`. (@cmbrad)

### Bugfixes

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())
}
3 changes: 3 additions & 0 deletions docs/sources/flow/reference/components/discovery.ec2.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Name | Type | Description | Default | Required
`role_arn` | `string` | AWS Role Amazon Resource Name (ARN), an alternative to using AWS API keys. | | no
`refresh_interval` | `string` | Refresh interval to re-read the instance list. | 60s | no
`port` | `int` | The port to scrape metrics from. If using the public IP address, this must instead be specified in the relabeling rule. | 80 | no
`proxy_url` | `string` | HTTP proxy to proxy requests through. | | no
`follow_redirects` | `bool` | Whether redirects returned by the server should be followed. | `true` | no
`enable_http2` | `bool` | Whether HTTP2 is supported for requests. | `true` | no

## Blocks

Expand Down

0 comments on commit 496a40b

Please sign in to comment.