Codebase list golang-github-go-kit-kit / a073a09
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
1 changed file(s) with 15 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2121 maxConcurrentRequests = 20
2222 maxValuesInABatch = 150
2323 )
24
25 type Percentiles []struct {
26 s string
27 f float64
28 }
2924
3025 // CloudWatch receives metrics observations and forwards them to CloudWatch.
3126 // Create a CloudWatch object, use it to create metrics, and pass those metrics as
4540 numConcurrentRequests int
4641 }
4742
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 {
5749 return func(c *CloudWatch) {
5850 c.logger = logger
5951 }
6355 // existing/default values.
6456 // Reason is that Cloudwatch makes you pay per metric, so you can save half the money
6557 // by only using 2 metrics instead of the default 4.
66 func WithPercentiles(percentiles ...float64) option {
58 func WithPercentiles(percentiles ...float64) Option {
6759 return func(c *CloudWatch) {
6860 c.percentiles = make([]float64, 0, len(percentiles))
6961 for _, p := range percentiles {
7567 }
7668 }
7769
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 {
7975 return func(c *CloudWatch) {
8076 if n > maxConcurrentRequests {
8177 n = maxConcurrentRequests
8884 // Namespace is applied to all created metrics and maps to the CloudWatch namespace.
8985 // Callers must ensure that regular calls to Send are performed, either
9086 // 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 {
9288 cw := &CloudWatch{
9389 sem: nil, // set below
9490 namespace: namespace,
10197 percentiles: []float64{0.50, 0.90, 0.95, 0.99},
10298 }
10399
104 for _, optFunc := range options {
105 optFunc(cw)
100 for _, opt := range options {
101 opt(cw)
106102 }
107103
108104 cw.sem = make(chan struct{}, cw.numConcurrentRequests)