cloudwatch-metrics 1.0.0 → 1.1.0

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: 7e76f596fe51773a068e074190d2b73a050772e565f57a9728d7e05e8d0dc426
4
- data.tar.gz: 1be98427ad0a9f15b9090bf0275d53d929d47d461c873fe574960895a3d47fb1
3
+ metadata.gz: fb532d3ef368646603acb0246884a36c398dd42b8bcd17cfe9aad3c69dabe8b4
4
+ data.tar.gz: b3f06ecce07db061df5d992eb847c82ae28ff704f4649d5f6bdd8bc720519c3f
5
5
  SHA512:
6
- metadata.gz: 33bcd4130b0fc125d24fae7827ffa16005d2b43217e457a817ac419f4560885d9a7410249e22a8aca85ab8358e3e012a2a9d86d38f58c8ace23265449db76fa5
7
- data.tar.gz: 1bd146a97750aa9be1b00f10491087a5b69933eda91f97821a467bcd2caf5415971641707bab97a9874843219ed9ddc51a6fefc0a3edce75f6754fab02667d7e
6
+ metadata.gz: 39104d7d1796b1fd74a65ad98df38f81f84d635370d71aefb27bc851de97543b07061efa4a51ddb7468bc3190305517dde60f251d383e24a03e9abfadfc59931
7
+ data.tar.gz: ba0a2202fde50efd5fe08e3cd641653901955281b60b30d4a9dbca488268183c3f2520b3dcf265cb7afb69213292f36b72c113b9680afa6a745c84d007505ac0
@@ -2,8 +2,8 @@
2
2
 
3
3
  module CloudwatchMetrics
4
4
  class Configuration
5
- attr_accessor :namespace, :region, :environment
6
- attr_writer :no_cw_alias, :publish_environments, :logger
5
+ attr_accessor :namespace, :region, :environment, :error_handler, :default_dimensions
6
+ attr_writer :no_cw_alias, :publish_environments, :logger, :non_production_prefix
7
7
 
8
8
  def no_cw_alias
9
9
  @no_cw_alias ||= false
@@ -16,5 +16,9 @@ module CloudwatchMetrics
16
16
  def logger
17
17
  @logger ||= Logger.new($stdout)
18
18
  end
19
+
20
+ def non_production_prefix
21
+ @non_production_prefix ||= 'test-'
22
+ end
19
23
  end
20
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudwatchMetrics
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -35,7 +35,7 @@ module CloudwatchMetrics
35
35
  end
36
36
 
37
37
  def record(
38
- name:, value:, unit: nil, namespace: configuration.namespace, dimensions: nil, timestamp: nil
38
+ name:, value:, unit: nil, namespace: nil, dimensions: nil, timestamp: nil
39
39
  )
40
40
  metric_data = [{
41
41
  metric_name: name,
@@ -44,11 +44,11 @@ module CloudwatchMetrics
44
44
  dimensions: map_dimensions(dimensions),
45
45
  timestamp: timestamp
46
46
  }]
47
- put_data(namespace: namespace, metric_data: metric_data)
47
+ put_data(namespace: full_namespace(namespace), metric_data: metric_data)
48
48
  end
49
49
 
50
50
  def record_all(
51
- name:, values:, counts:, unit: nil, namespace: configuration.namespace, dimensions: nil, timestamp: nil
51
+ name:, values:, counts:, unit: nil, namespace: nil, dimensions: nil, timestamp: nil
52
52
  )
53
53
  metric_data = [{
54
54
  metric_name: name,
@@ -58,7 +58,7 @@ module CloudwatchMetrics
58
58
  dimensions: map_dimensions(dimensions),
59
59
  timestamp: timestamp
60
60
  }]
61
- put_data(namespace: namespace, metric_data: metric_data)
61
+ put_data(namespace: full_namespace(namespace), metric_data: metric_data)
62
62
  end
63
63
 
64
64
  private
@@ -72,8 +72,7 @@ module CloudwatchMetrics
72
72
 
73
73
  nil
74
74
  rescue StandardError => e
75
- configuration.logger.error("CloudwatchMetrics error: #{e.message}")
76
- raise CloudwatchMetrics::Error, e.message
75
+ handle_error(e)
77
76
  end
78
77
 
79
78
  def cloudwatch_client
@@ -92,17 +91,38 @@ module CloudwatchMetrics
92
91
 
93
92
  # convert key/value hash to format expected by AWS SDK
94
93
  def map_dimensions(dimensions)
95
- return [] if dimensions.nil?
94
+ combined_dimensions = (configuration.default_dimensions || {}).merge(dimensions || {})
96
95
 
97
96
  mapped_values = []
98
- dimensions.keys.each do |key|
97
+ combined_dimensions.keys.each do |key|
99
98
  mapped_values << {
100
99
  name: key.to_s,
101
- value: dimensions[key]
100
+ value: combined_dimensions[key]
102
101
  }
103
102
  end
104
103
 
105
104
  mapped_values
106
105
  end
106
+
107
+ def handle_error(error)
108
+ if !configuration.error_handler.nil?
109
+ configuration.error_handler.call(error)
110
+ return
111
+ end
112
+
113
+ configuration.logger.error("CloudwatchMetrics error: #{error.message}")
114
+ raise CloudwatchMetrics::Error, error.message
115
+ end
116
+
117
+ def full_namespace(provided_namespace)
118
+ constructed_namespace = provided_namespace || configuration.namespace
119
+ raise CloudwatchMetrics::Error, 'Namespace must be provided' if constructed_namespace.nil?
120
+
121
+ if environment != 'production' && configuration.non_production_prefix.present?
122
+ constructed_namespace = configuration.non_production_prefix + constructed_namespace
123
+ end
124
+
125
+ constructed_namespace
126
+ end
107
127
  end
108
128
  end
@@ -32,4 +32,21 @@ CloudwatchMetrics.configure do |config|
32
32
  # This is useful if the gem is being used in a non-rails application.
33
33
  # Defaults to ENV['RAILS_ENV'] if present or 'development'` otherwise.
34
34
  # config.environment = 'production'
35
+
36
+ # This is an optional configuration to set the namespace prefix for non-production
37
+ # environments. This is useful if you want to avoid polluting your production
38
+ # metrics with test data. Defaults to 'test-'.
39
+ # config.non_production_prefix = 'test-'
40
+
41
+ # This is an optional configuration to set an error handler proc.
42
+ # If not set the default error handler will log the error
43
+ # and raise a CloudwatchMetrics::Error
44
+ # config.error_handler = proc do |exception|
45
+ # Rails.logger.error(exception)
46
+ # Sentry.capture_exception(exception)
47
+ # end
48
+
49
+ # This is an optional configuration to set default dimensions for all metrics.
50
+ # Dimensions passed to the `record` or `record_all` methods will be merged with these.
51
+ # config.default_dimensions = {name: 'value'}
35
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudwatch-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Puckett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2023-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport