cloudwatch-metrics 1.0.0 → 1.1.1

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: aae5940338cdbe6a8908f6330531daa21de3bebe3c63a84e5c8c30fbace3413e
4
+ data.tar.gz: 15cf0039191265a80066547276459f60f584bfd70924574b0719d65838b13bc3
5
5
  SHA512:
6
- metadata.gz: 33bcd4130b0fc125d24fae7827ffa16005d2b43217e457a817ac419f4560885d9a7410249e22a8aca85ab8358e3e012a2a9d86d38f58c8ace23265449db76fa5
7
- data.tar.gz: 1bd146a97750aa9be1b00f10491087a5b69933eda91f97821a467bcd2caf5415971641707bab97a9874843219ed9ddc51a6fefc0a3edce75f6754fab02667d7e
6
+ metadata.gz: 7c0a974cd1dd8a00473fabefa3be9721f0ce08ca832791c0e7fc764baf3d3111177d64583e30e822473392f746e5afb52a2908586d8ee87b39238283d9509556
7
+ data.tar.gz: 021f06249687423051df6bc8f0adf113be020666fa0b3184b16e36bb4c174d1333c4eb90043e2b9318b626f994b4fcb75a8514ec0715c439466ea18078fe99b6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cloudwatch-metrics (1.0.0)
4
+ cloudwatch-metrics (1.1.1)
5
5
  activesupport (~> 7.0)
6
6
  aws-sdk-cloudwatch (~> 1.73)
7
7
  rake (~> 13.0)
@@ -16,7 +16,7 @@ GEM
16
16
  tzinfo (~> 2.0)
17
17
  ast (2.4.2)
18
18
  aws-eventstream (1.2.0)
19
- aws-partitions (1.780.0)
19
+ aws-partitions (1.781.0)
20
20
  aws-sdk-cloudwatch (1.75.0)
21
21
  aws-sdk-core (~> 3, >= 3.174.0)
22
22
  aws-sigv4 (~> 1.1)
@@ -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.1'
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,13 @@ 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
+ rescue StandardError => e
49
+ handle_error(e)
48
50
  end
49
51
 
50
52
  def record_all(
51
- name:, values:, counts:, unit: nil, namespace: configuration.namespace, dimensions: nil, timestamp: nil
53
+ name:, values:, counts:, unit: nil, namespace: nil, dimensions: nil, timestamp: nil
52
54
  )
53
55
  metric_data = [{
54
56
  metric_name: name,
@@ -58,7 +60,9 @@ module CloudwatchMetrics
58
60
  dimensions: map_dimensions(dimensions),
59
61
  timestamp: timestamp
60
62
  }]
61
- put_data(namespace: namespace, metric_data: metric_data)
63
+ put_data(namespace: full_namespace(namespace), metric_data: metric_data)
64
+ rescue StandardError => e
65
+ handle_error(e)
62
66
  end
63
67
 
64
68
  private
@@ -71,9 +75,6 @@ module CloudwatchMetrics
71
75
  end
72
76
 
73
77
  nil
74
- rescue StandardError => e
75
- configuration.logger.error("CloudwatchMetrics error: #{e.message}")
76
- raise CloudwatchMetrics::Error, e.message
77
78
  end
78
79
 
79
80
  def cloudwatch_client
@@ -92,17 +93,38 @@ module CloudwatchMetrics
92
93
 
93
94
  # convert key/value hash to format expected by AWS SDK
94
95
  def map_dimensions(dimensions)
95
- return [] if dimensions.nil?
96
+ combined_dimensions = (configuration.default_dimensions || {}).merge(dimensions || {})
96
97
 
97
98
  mapped_values = []
98
- dimensions.keys.each do |key|
99
+ combined_dimensions.keys.each do |key|
99
100
  mapped_values << {
100
101
  name: key.to_s,
101
- value: dimensions[key]
102
+ value: combined_dimensions[key]
102
103
  }
103
104
  end
104
105
 
105
106
  mapped_values
106
107
  end
108
+
109
+ def handle_error(error)
110
+ if !configuration.error_handler.nil?
111
+ configuration.error_handler.call(error)
112
+ return
113
+ end
114
+
115
+ configuration.logger.error("CloudwatchMetrics error: #{error.message}")
116
+ raise CloudwatchMetrics::Error, error.message
117
+ end
118
+
119
+ def full_namespace(provided_namespace)
120
+ constructed_namespace = provided_namespace || configuration.namespace
121
+ raise CloudwatchMetrics::Error, 'Namespace must be provided' if constructed_namespace.blank?
122
+
123
+ if environment != 'production' && configuration.non_production_prefix.present?
124
+ constructed_namespace = configuration.non_production_prefix + constructed_namespace
125
+ end
126
+
127
+ constructed_namespace
128
+ end
107
129
  end
108
130
  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. If empty or nil no prefix will be applied. 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.1
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-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport