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

Refactor metrics/cloudwatch #1202

Merged
merged 2 commits into from
Nov 23, 2021
Merged
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
32 changes: 14 additions & 18 deletions metrics/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const (
maxValuesInABatch = 150
)

type Percentiles []struct {
s string
f float64
}

// CloudWatch receives metrics observations and forwards them to CloudWatch.
// Create a CloudWatch object, use it to create metrics, and pass those metrics as
// dependencies to the components that will use them.
Expand All @@ -46,15 +41,12 @@ type CloudWatch struct {
numConcurrentRequests int
}

type option func(*CloudWatch)

func (s *CloudWatch) apply(opt option) {
if opt != nil {
opt(s)
}
}
// Option is a function adapter to change config of the CloudWatch struct
type Option func(*CloudWatch)

func WithLogger(logger log.Logger) option {
// WithLogger sets the Logger that will receive error messages generated
// during the WriteLoop. By default, fmt logger is used.
func WithLogger(logger log.Logger) Option {
return func(c *CloudWatch) {
c.logger = logger
}
Expand All @@ -64,7 +56,7 @@ func WithLogger(logger log.Logger) option {
// existing/default values.
// Reason is that Cloudwatch makes you pay per metric, so you can save half the money
// by only using 2 metrics instead of the default 4.
func WithPercentiles(percentiles ...float64) option {
func WithPercentiles(percentiles ...float64) Option {
return func(c *CloudWatch) {
c.percentiles = make([]float64, 0, len(percentiles))
for _, p := range percentiles {
Expand All @@ -76,7 +68,11 @@ func WithPercentiles(percentiles ...float64) option {
}
}

func WithConcurrentRequests(n int) option {
// WithConcurrentRequests sets the upper limit on how many
// cloudwatch.PutMetricDataRequest may be under way at any
// given time. If n is greater than 20, 20 is used. By default,
// the max is set at 10 concurrent requests.
func WithConcurrentRequests(n int) Option {
return func(c *CloudWatch) {
if n > maxConcurrentRequests {
n = maxConcurrentRequests
Expand All @@ -89,7 +85,7 @@ func WithConcurrentRequests(n int) option {
// Namespace is applied to all created metrics and maps to the CloudWatch namespace.
// Callers must ensure that regular calls to Send are performed, either
// manually or with one of the helper methods.
func New(namespace string, svc cloudwatchiface.CloudWatchAPI, options ...option) *CloudWatch {
func New(namespace string, svc cloudwatchiface.CloudWatchAPI, options ...Option) *CloudWatch {
cw := &CloudWatch{
sem: nil, // set below
namespace: namespace,
Expand All @@ -102,8 +98,8 @@ func New(namespace string, svc cloudwatchiface.CloudWatchAPI, options ...option)
percentiles: []float64{0.50, 0.90, 0.95, 0.99},
}

for _, optFunc := range options {
optFunc(cw)
for _, opt := range options {
opt(cw)
}

cw.sem = make(chan struct{}, cw.numConcurrentRequests)
Expand Down