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
sina safari authored 2 years ago
GitHub committed 2 years ago
21 | 21 | maxConcurrentRequests = 20 |
22 | 22 | maxValuesInABatch = 150 |
23 | 23 | ) |
24 | ||
25 | type Percentiles []struct { | |
26 | s string | |
27 | f float64 | |
28 | } | |
29 | 24 | |
30 | 25 | // CloudWatch receives metrics observations and forwards them to CloudWatch. |
31 | 26 | // Create a CloudWatch object, use it to create metrics, and pass those metrics as |
45 | 40 | numConcurrentRequests int |
46 | 41 | } |
47 | 42 | |
48 | type option func(*CloudWatch) | |
49 | ||
50 | func (s *CloudWatch) apply(opt option) { | |
51 | if opt != nil { | |
52 | opt(s) | |
53 | } | |
54 | } | |
55 | ||
56 | func WithLogger(logger log.Logger) option { | |
43 | // Option is a function adapter to change config of the CloudWatch struct | |
44 | type Option func(*CloudWatch) | |
45 | ||
46 | // WithLogger sets the Logger that will receive error messages generated | |
47 | // during the WriteLoop. By default, fmt logger is used. | |
48 | func WithLogger(logger log.Logger) Option { | |
57 | 49 | return func(c *CloudWatch) { |
58 | 50 | c.logger = logger |
59 | 51 | } |
63 | 55 | // existing/default values. |
64 | 56 | // Reason is that Cloudwatch makes you pay per metric, so you can save half the money |
65 | 57 | // by only using 2 metrics instead of the default 4. |
66 | func WithPercentiles(percentiles ...float64) option { | |
58 | func WithPercentiles(percentiles ...float64) Option { | |
67 | 59 | return func(c *CloudWatch) { |
68 | 60 | c.percentiles = make([]float64, 0, len(percentiles)) |
69 | 61 | for _, p := range percentiles { |
75 | 67 | } |
76 | 68 | } |
77 | 69 | |
78 | func WithConcurrentRequests(n int) option { | |
70 | // WithConcurrentRequests sets the upper limit on how many | |
71 | // cloudwatch.PutMetricDataRequest may be under way at any | |
72 | // given time. If n is greater than 20, 20 is used. By default, | |
73 | // the max is set at 10 concurrent requests. | |
74 | func WithConcurrentRequests(n int) Option { | |
79 | 75 | return func(c *CloudWatch) { |
80 | 76 | if n > maxConcurrentRequests { |
81 | 77 | n = maxConcurrentRequests |
88 | 84 | // Namespace is applied to all created metrics and maps to the CloudWatch namespace. |
89 | 85 | // Callers must ensure that regular calls to Send are performed, either |
90 | 86 | // manually or with one of the helper methods. |
91 | func New(namespace string, svc cloudwatchiface.CloudWatchAPI, options ...option) *CloudWatch { | |
87 | func New(namespace string, svc cloudwatchiface.CloudWatchAPI, options ...Option) *CloudWatch { | |
92 | 88 | cw := &CloudWatch{ |
93 | 89 | sem: nil, // set below |
94 | 90 | namespace: namespace, |
101 | 97 | percentiles: []float64{0.50, 0.90, 0.95, 0.99}, |
102 | 98 | } |
103 | 99 | |
104 | for _, optFunc := range options { | |
105 | optFunc(cw) | |
100 | for _, opt := range options { | |
101 | opt(cw) | |
106 | 102 | } |
107 | 103 | |
108 | 104 | cw.sem = make(chan struct{}, cw.numConcurrentRequests) |