Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow defining a rate limit to call the newrelic api #2741

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ require (
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
64 changes: 47 additions & 17 deletions newrelic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,58 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/mitchellh/go-homedir"
"golang.org/x/sync/semaphore"
"golang.org/x/time/rate"

insights "github.com/newrelic/go-insights/client"
nr "github.com/newrelic/newrelic-client-go/v2/newrelic"
)

// Config contains New Relic provider settings
type Config struct {
AdminAPIKey string
PersonalAPIKey string
Region string
APIURL string
CACertFile string
InfrastructureAPIURL string
InsecureSkipVerify bool
InsightsAccountID string
InsightsInsertKey string
InsightsInsertURL string
InsightsQueryKey string
InsightsQueryURL string
NerdGraphAPIURL string
SyntheticsAPIURL string
userAgent string
serviceName string
AdminAPIKey string
PersonalAPIKey string
Region string
APIURL string
CACertFile string
InfrastructureAPIURL string
InsecureSkipVerify bool
InsightsAccountID string
InsightsInsertKey string
InsightsInsertURL string
InsightsQueryKey string
InsightsQueryURL string
NerdGraphAPIURL string
SyntheticsAPIURL string
userAgent string
serviceName string
MaxRequestsPerSecond int
MaxConcurrentRequests int
}

type ThrottledRoundTripper struct {
original http.RoundTripper
ratelimiter *rate.Limiter
concurrency *semaphore.Weighted
}

func NewThrottledRoundTripper(rt http.RoundTripper, maxRequestsPerSecond int, maxConcurrentRequests int) *ThrottledRoundTripper {
return &ThrottledRoundTripper{
original: rt,
ratelimiter: rate.NewLimiter(rate.Limit(maxRequestsPerSecond), maxRequestsPerSecond),
concurrency: semaphore.NewWeighted(int64(maxConcurrentRequests)),
}
}

func (t *ThrottledRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := req.Context()

if err := t.concurrency.Acquire(ctx, 1); err != nil {
return nil, err
}
defer t.concurrency.Release(1)
t.ratelimiter.Wait(ctx)
return t.original.RoundTrip(req)
}

// Client returns a new client for accessing New Relic
Expand Down Expand Up @@ -75,7 +104,8 @@ func (c *Config) Client() (*nr.NewRelic, error) {
t = logging.NewTransport("newrelic", t)
}

options = append(options, nr.ConfigHTTPTransport(t))
throttledTransport := NewThrottledRoundTripper(t, c.MaxRequestsPerSecond, c.MaxConcurrentRequests)
options = append(options, nr.ConfigHTTPTransport(throttledTransport))

if c.APIURL != "" {
options = append(options, nr.ConfigBaseURL(c.APIURL))
Expand Down
34 changes: 23 additions & 11 deletions newrelic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEW_RELIC_API_CACERT", ""),
},
"max_requests_per_second": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEW_RELIC_MAX_REQUESTS_PER_SECOND", 600),
},
"max_concurrent_requests": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NEW_RELIC_MAX_CONCURRENT_REQUESTS", 25),
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -234,17 +244,19 @@ func providerConfigure(data *schema.ResourceData, terraformVersion string) (inte
log.Printf("[INFO] UserAgent: %s", userAgent)

cfg := Config{
AdminAPIKey: adminAPIKey,
PersonalAPIKey: personalAPIKey,
Region: data.Get("region").(string),
APIURL: data.Get("api_url").(string),
SyntheticsAPIURL: data.Get("synthetics_api_url").(string),
NerdGraphAPIURL: data.Get("nerdgraph_api_url").(string),
InfrastructureAPIURL: getInfraAPIURL(data),
userAgent: userAgent,
InsecureSkipVerify: data.Get("insecure_skip_verify").(bool),
CACertFile: data.Get("cacert_file").(string),
serviceName: userAgentServiceName,
AdminAPIKey: adminAPIKey,
PersonalAPIKey: personalAPIKey,
Region: data.Get("region").(string),
APIURL: data.Get("api_url").(string),
SyntheticsAPIURL: data.Get("synthetics_api_url").(string),
NerdGraphAPIURL: data.Get("nerdgraph_api_url").(string),
InfrastructureAPIURL: getInfraAPIURL(data),
userAgent: userAgent,
InsecureSkipVerify: data.Get("insecure_skip_verify").(bool),
CACertFile: data.Get("cacert_file").(string),
serviceName: userAgentServiceName,
MaxRequestsPerSecond: data.Get("max_requests_per_second").(int),
MaxConcurrentRequests: data.Get("max_concurrent_requests").(int),
}
log.Println("[INFO] Initializing newrelic-client-go")

Expand Down