cloudwatch-metrics 0.1.14 → 0.1.16
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97790dd4bec2f067aeca87ba8aabaa580fbb98f793125258d4bdb53fea42da5d
|
4
|
+
data.tar.gz: 6ea60c0395cd756541de4c87bf81b74fd06f26f467be9bdd21d3cb2fb52381cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8767e36b91438b6178ebe96cac9a1a712629279afa37b20dd4624ae46319d6279544b3d63dc537fdf44272d286c790f9eb6f40b31e013391ace747e96b7c86bf
|
7
|
+
data.tar.gz: 16fb72d30d8f932f17e47281eee1c23d234a93fb222426c3b7423b9fb4fb30bff8746f366c619b7ac1d1a4f07e551684dfc05c1eea4d30864e04cdad0fdf3d71
|
data/README.md
CHANGED
@@ -23,7 +23,6 @@ The initializer contains descriptions for all available options. The only option
|
|
23
23
|
|
24
24
|
* **namespace**: This is the default namespace that all metrics will be published to. This should probably match the name of your application or service.
|
25
25
|
|
26
|
-
|
27
26
|
## Usage
|
28
27
|
|
29
28
|
Available methods are in the `CloudwatchMetrics` module. For convenience this is aliased to `CW` by default.
|
@@ -54,9 +53,15 @@ Both reporting methods accept optional parameters, which are detailed below, alo
|
|
54
53
|
|
55
54
|
* `unit`: The unit of measure for this data point. Units help provide conceptual meaning to your data, but CloudWatch attaches no significance to a unit internally. Common values are `Seconds`, `Kilobytes`, `Percent`, `Count`, `Kilobytes/Second`. Default value is `None`. A full list of available units is available at `CloudwatchMetrics.Units`.
|
56
55
|
* `namespace`: Override the namespace set in the initializer. Default is `nil`
|
57
|
-
* `dimensions`:
|
56
|
+
* `dimensions`: A hash of name/value pairs that form part of the identity of a metric. Dimensions allow you to group and filter data points within a particular metric. Default is `nil`.
|
58
57
|
* `timestamp`: The time associated with the data point. The timestamp can be up to two weeks in the past and up to two hours into the future. Default is the current time.
|
59
58
|
|
59
|
+
E.g. `CW.report(metric_name: 'search_result_count', value: 25, unit: CW.COUNT, namespace: 'custom_namespace', dimensions: { group: 'A', subgroup: 'B' }, timeStamp: Time.current - 1.day)`
|
60
|
+
|
61
|
+
### Local development
|
62
|
+
|
63
|
+
By default, `CloudWatchMetrics` only publishes to Cloudwatch in the production environment. In other environments it will write to the configured logger. These settings can be changed in the initializer.
|
64
|
+
|
60
65
|
### AWS Credentials
|
61
66
|
|
62
67
|
See [HOWTO Assume AWS role](https://docs.google.com/document/d/1NCXtlhddpJCttBPs2V4mv3J59V0C3ShTzaoSidBAVTU/edit#heading=h.kr4pxvf3gqqt) for info about setting up AWS credential locally. You can read more about AWS SDK configuration in the [AWS docs](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html).
|
@@ -68,6 +73,13 @@ aws_access_key_id = $AWS_ACCESS_KEY_ID
|
|
68
73
|
aws_secret_access_key = $AWS_SECRET_ACCESS_KEY
|
69
74
|
```
|
70
75
|
|
76
|
+
You'll also want an `~/.aws/config` file to set the default region
|
77
|
+
```
|
78
|
+
[default]
|
79
|
+
region = us-west-2
|
80
|
+
output = json
|
81
|
+
```
|
82
|
+
|
71
83
|
## Cloudwatch Concepts
|
72
84
|
|
73
85
|
Refer to the [Cloudwatch Documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html) for a general overview of custom metrics in CloudWatch.
|
data/lib/cloudwatch_metrics.rb
CHANGED
@@ -22,7 +22,7 @@ module CloudwatchMetrics
|
|
22
22
|
metric_name: metric_name,
|
23
23
|
value: value,
|
24
24
|
unit: unit,
|
25
|
-
dimensions: dimensions,
|
25
|
+
dimensions: map_dimensions(dimensions),
|
26
26
|
timestamp: timestamp
|
27
27
|
}]
|
28
28
|
put_data(namespace: namespace, metric_data: metric_data)
|
@@ -36,7 +36,7 @@ module CloudwatchMetrics
|
|
36
36
|
values: values,
|
37
37
|
counts: counts,
|
38
38
|
unit: unit,
|
39
|
-
dimensions: dimensions,
|
39
|
+
dimensions: map_dimensions(dimensions),
|
40
40
|
timestamp: timestamp
|
41
41
|
}]
|
42
42
|
put_data(namespace: namespace, metric_data: metric_data)
|
@@ -45,10 +45,16 @@ module CloudwatchMetrics
|
|
45
45
|
private
|
46
46
|
|
47
47
|
def put_data(namespace:, metric_data:)
|
48
|
-
|
49
|
-
namespace: namespace,
|
50
|
-
|
51
|
-
|
48
|
+
if configuration.publish_environments.include?(environment)
|
49
|
+
cloudwatch_client.put_metric_data({namespace: namespace, metric_data: metric_data})
|
50
|
+
else
|
51
|
+
configuration.logger.info("CloudwatchMetrics (#{namespace}): #{metric_data}")
|
52
|
+
end
|
53
|
+
|
54
|
+
nil
|
55
|
+
rescue StandardError => e
|
56
|
+
configuration.logger.error("CloudwatchMetrics error: #{e.message}")
|
57
|
+
raise CloudwatchMetrics::Error, e.message
|
52
58
|
end
|
53
59
|
|
54
60
|
def cloudwatch_client
|
@@ -64,6 +70,21 @@ module CloudwatchMetrics
|
|
64
70
|
def environment
|
65
71
|
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
66
72
|
end
|
73
|
+
|
74
|
+
# convert key/value hash to format expected by AWS SDK
|
75
|
+
def map_dimensions(dimensions)
|
76
|
+
return [] if dimensions.nil?
|
77
|
+
|
78
|
+
mapped_values = []
|
79
|
+
dimensions.keys.each do |key|
|
80
|
+
mapped_values << {
|
81
|
+
name: key,
|
82
|
+
value: dimensions[key]
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
mapped_values
|
87
|
+
end
|
67
88
|
end
|
68
89
|
end
|
69
90
|
|
@@ -14,14 +14,14 @@ CloudwatchMetrics.configure do |config|
|
|
14
14
|
|
15
15
|
# This is an optional configuration to set the region that
|
16
16
|
# the metrics will be published to. This is useful if you want to publish
|
17
|
-
# to a region other than the default region for your AWS
|
17
|
+
# to a region other than the default region for your AWS configuration.
|
18
18
|
# config.region = 'us-east-1'
|
19
19
|
|
20
20
|
# By default metrics will only be published in the production environment.
|
21
21
|
# In all other environments metrics will be logged.
|
22
22
|
# You can override this behavior by specifying an array of environments
|
23
23
|
# that you want to publish metrics in.
|
24
|
-
# config.publish_environments = %w[production
|
24
|
+
# config.publish_environments = %w[production development]
|
25
25
|
|
26
26
|
# This is an optional configuration to set the logger that
|
27
27
|
# the metrics will be logged to. Defaults to `Logger.new(STDOUT)`.
|