Skip to content

Commit a073a09

Browse files
authored
Refactor metrics/cloudwatch (#1202)
* Refactor metrics/cloudwatch: - Export `option` type for better documentation - Add documentation for exported option functions - Use `CloudWatch.apply` method and remove unused `Percentiles` type * Remove the `apply` method
1 parent ed56ff7 commit a073a09

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

metrics/cloudwatch/cloudwatch.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ const (
2323
maxValuesInABatch = 150
2424
)
2525

26-
type Percentiles []struct {
27-
s string
28-
f float64
29-
}
30-
3126
// CloudWatch receives metrics observations and forwards them to CloudWatch.
3227
// Create a CloudWatch object, use it to create metrics, and pass those metrics as
3328
// dependencies to the components that will use them.
@@ -46,15 +41,12 @@ type CloudWatch struct {
4641
numConcurrentRequests int
4742
}
4843

49-
type option func(*CloudWatch)
50-
51-
func (s *CloudWatch) apply(opt option) {
52-
if opt != nil {
53-
opt(s)
54-
}
55-
}
44+
// Option is a function adapter to change config of the CloudWatch struct
45+
type Option func(*CloudWatch)
5646

57-
func WithLogger(logger log.Logger) option {
47+
// WithLogger sets the Logger that will receive error messages generated
48+
// during the WriteLoop. By default, fmt logger is used.
49+
func WithLogger(logger log.Logger) Option {
5850
return func(c *CloudWatch) {
5951
c.logger = logger
6052
}
@@ -64,7 +56,7 @@ func WithLogger(logger log.Logger) option {
6456
// existing/default values.
6557
// Reason is that Cloudwatch makes you pay per metric, so you can save half the money
6658
// by only using 2 metrics instead of the default 4.
67-
func WithPercentiles(percentiles ...float64) option {
59+
func WithPercentiles(percentiles ...float64) Option {
6860
return func(c *CloudWatch) {
6961
c.percentiles = make([]float64, 0, len(percentiles))
7062
for _, p := range percentiles {
@@ -76,7 +68,11 @@ func WithPercentiles(percentiles ...float64) option {
7668
}
7769
}
7870

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

105-
for _, optFunc := range options {
106-
optFunc(cw)
101+
for _, opt := range options {
102+
opt(cw)
107103
}
108104

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

0 commit comments

Comments
 (0)