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 1 commit
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
30 changes: 16 additions & 14 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,18 @@ type CloudWatch struct {
numConcurrentRequests int
}

type option func(*CloudWatch)
// Option is a function adapter to change config of the CloudWatch struct
type Option func(*CloudWatch)

func (s *CloudWatch) apply(opt option) {
func (cw *CloudWatch) apply(opt Option) {
if opt != nil {
opt(s)
opt(cw)
}
}

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 +62,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 +74,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 +91,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 +104,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 {
cw.apply(opt)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just remove the apply method altogether?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually more safer than a simple function. apply cannot be called outside of the package, but with the current Option type it's possible to modify the state of Cloudwatch after the initialization.

I'd instead change the signature of Option to this:

type Option interface{
    apply(*CloudWatch)
}

Copy link
Member

@peterbourgon peterbourgon Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd instead change the signature of Option to this:

Strong -1. Option interfaces are an antipattern, and especially those that take this form, i.e. an exported interface with an unexported method. type Option func(*Cloudwatch) keeps the Options grouped with the type in the docs, which is important.

with the current Option type it's possible to modify the state of Cloudwatch after the initialization.

I understand your point, but I don't think this is a meaningful risk, as only Options defined in package cloudwatch have access to the unexported fields of the struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the apply method, because cloudwatch.New(namespace, svc, nil) isn't norm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


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