cloudwatch-metrics 0.1.13 → 0.1.15

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: f81642066454cacb68f5e5d76f7cee30aae9cb09d9db2dd9a1026a102c974f3f
4
- data.tar.gz: 33ad13c41b6caa0fb8da1a538ca11cc2a11a688f012daee58ba39eeb82e6c441
3
+ metadata.gz: 1dff409be424bcafe2081ebe6583808b9577f957db434dcf6d0101f4e61c8eb9
4
+ data.tar.gz: bc45bed975955dc25846b8a427f8ed28b62f26f4910a8fef8262374ccd21eede
5
5
  SHA512:
6
- metadata.gz: d676d266e1d6bb2f9468b2fd6b9bf3b348cec4a59bcea379201b68762487849de78fdab7acdde876a0b7df1aa57440938e8657bb0f3d0870cf64a983d4c7263d
7
- data.tar.gz: 56e203e9bdad56fe89626309ec86ee2bfcae8879a262471034b9ebaad0c4e6093f5d7b96767108d91468a28dbee7db4db0f6bd7e4202eb26adaebd96ccf736d0
6
+ metadata.gz: c63953bc1565b6aa922d8521067193d25c680667a09a1361dd5ef8dfaacdbbf31acf50ea0b11f552355372146a27dad2df59b6314f3cb70d330e538c084c8740
7
+ data.tar.gz: 4c93c5b6b9a38d6a570e86c271d0855bea2321b6c3474a9231119bfa3798a6e8af3a4158604738fc6e0cfc7c6cf681c1bdcd26ab7fe62494def64beeb639eea0
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.
@@ -57,6 +56,10 @@ Both reporting methods accept optional parameters, which are detailed below, alo
57
56
  * `dimensions`: An array 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
+ ### Local development
60
+
61
+ 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.
62
+
60
63
  ### AWS Credentials
61
64
 
62
65
  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 +71,13 @@ aws_access_key_id = $AWS_ACCESS_KEY_ID
68
71
  aws_secret_access_key = $AWS_SECRET_ACCESS_KEY
69
72
  ```
70
73
 
74
+ You'll also want an `~/.aws/config` file to set the default region
75
+ ```
76
+ [default]
77
+ region = us-west-2
78
+ output = json
79
+ ```
80
+
71
81
  ## Cloudwatch Concepts
72
82
 
73
83
  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.
@@ -3,10 +3,18 @@
3
3
  module CloudwatchMetrics
4
4
  class Configuration
5
5
  attr_accessor :namespace, :region
6
- attr_writer :no_cw_alias
6
+ attr_writer :no_cw_alias, :publish_environments, :logger
7
7
 
8
8
  def no_cw_alias
9
9
  @no_cw_alias ||= false
10
10
  end
11
+
12
+ def publish_environments
13
+ @publish_environments ||= %w[production]
14
+ end
15
+
16
+ def logger
17
+ @logger ||= Logger.new(STDOUT)
18
+ end
11
19
  end
12
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudwatchMetrics
4
- VERSION = '0.1.13'
4
+ VERSION = '0.1.15'
5
5
  end
@@ -44,15 +44,31 @@ module CloudwatchMetrics
44
44
 
45
45
  private
46
46
 
47
+ def put_data(namespace:, metric_data:)
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
58
+ end
59
+
47
60
  def cloudwatch_client
48
- @cloudwatch_client ||= Aws::CloudWatch::Client.new(region: configuration.region)
61
+ return @cloudwatch_client unless @cloudwatch_client.nil?
62
+
63
+ @cloudwatch_client = if !configuration.region.nil?
64
+ Aws::CloudWatch::Client.new(region: configuration.region)
65
+ else
66
+ Aws::CloudWatch::Client.new
67
+ end
49
68
  end
50
69
 
51
- def put_data(namespace:, metric_data:)
52
- resp = cloudwatch_client.put_metric_data({
53
- namespace: namespace,
54
- metric_data: metric_data
55
- })
70
+ def environment
71
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
56
72
  end
57
73
  end
58
74
  end
@@ -16,4 +16,15 @@ CloudwatchMetrics.configure do |config|
16
16
  # the metrics will be published to. This is useful if you want to publish
17
17
  # to a region other than the default region for your AWS credentials.
18
18
  # config.region = 'us-east-1'
19
+
20
+ # By default metrics will only be published in the production environment.
21
+ # In all other environments metrics will be logged.
22
+ # You can override this behavior by specifying an array of environments
23
+ # that you want to publish metrics in.
24
+ # config.publish_environments = %w[production staging]
25
+
26
+ # This is an optional configuration to set the logger that
27
+ # the metrics will be logged to. Defaults to `Logger.new(STDOUT)`.
28
+ # For rails applications you can set this to `Rails.logger`.
29
+ # config.logger = Rails.logger
19
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudwatch-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Puckett