lamby 5.0.0 → 5.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: bed40f788bb3ef9eecb63040924c583bee423ac369179f76aa5fe43fe797e3ec
4
- data.tar.gz: 7fd33a98bb6cd19b202a16ef58f5a03f7f7e1e21f75e6342330b318ebb260bed
3
+ metadata.gz: 940f2329a4503c79e8c2c48908b3f1c2d22445c16ebb0dc71ee0802e54e7c560
4
+ data.tar.gz: 708b36a037b00b3f66dfa3239a8060ae6b00177628a1e1cd38ba0a8ed8988a94
5
5
  SHA512:
6
- metadata.gz: 8dca95a577c3afbcb3fbcb9715e54987b04cd830abeda28ac9c4df82f088a50ea866ce8a6f19212e003d80cb3d88c37370393e79c69f9f023c34ae81f967f8d5
7
- data.tar.gz: d795ac32b599d239ad6ccfde9da12e88b79bfcff854766c77c0630ca69acbaac9f2cbd5e5c56ee9eb48c46b70bd4d683b67fc7dbf1c2cef61c6dbf0f42a81728
6
+ metadata.gz: e2d135f4d5cab367171030c98c2126f57e9a9542b2b802535677450e47f8be6dc4c99d8865871978d73c184269c7a806d61c0b48f1fd54642ceda76289b96474
7
+ data.tar.gz: 472144850f8dd607c2709d74d6a240ea6949ce640a6e2ddc748e49a31db8f94b04cfa1b3aae842bb9fc465c798e47bc13020571dd8d611b01523e9f8378852cc
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  See this http://keepachangelog.com link for information on how we want this documented formatted.
4
4
 
5
+ ## v5.1.0
6
+
7
+ ### Added
8
+
9
+ - New CloudWatch cold start metrics. Defaults to off. Enable with `config.cold_start_metrics = true`.
10
+
5
11
  ## v5.0.0
6
12
 
7
13
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lamby (5.0.0)
4
+ lamby (5.1.0)
5
5
  lambda-console-ruby
6
6
  rack
7
7
 
@@ -133,6 +133,8 @@ GEM
133
133
  nio4r (2.5.9)
134
134
  nokogiri (1.14.3-aarch64-linux)
135
135
  racc (~> 1.4)
136
+ nokogiri (1.14.3-arm64-darwin)
137
+ racc (~> 1.4)
136
138
  pry (0.14.2)
137
139
  coderay (~> 1.1)
138
140
  method_source (~> 1.0)
@@ -169,6 +171,7 @@ GEM
169
171
  rake (13.0.6)
170
172
  ruby2_keywords (0.0.5)
171
173
  thor (1.2.1)
174
+ timecop (0.9.6)
172
175
  timeout (0.3.2)
173
176
  tzinfo (2.0.6)
174
177
  concurrent-ruby (~> 1.0)
@@ -180,6 +183,7 @@ GEM
180
183
 
181
184
  PLATFORMS
182
185
  aarch64-linux
186
+ arm64-darwin-22
183
187
 
184
188
  DEPENDENCIES
185
189
  aws-sdk-ssm
@@ -192,6 +196,7 @@ DEPENDENCIES
192
196
  pry
193
197
  rails
194
198
  rake
199
+ timecop
195
200
  webrick
196
201
 
197
202
  BUNDLED WITH
data/lamby.gemspec CHANGED
@@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'minitest-focus'
28
28
  spec.add_development_dependency 'mocha'
29
29
  spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'timecop'
30
31
  spec.add_development_dependency 'webrick'
31
32
  end
@@ -0,0 +1,83 @@
1
+ module Lamby
2
+ class ColdStartMetrics
3
+ NAMESPACE = 'Lamby'
4
+
5
+ @cold_start = true
6
+ @cold_start_time = (Time.now.to_f * 1000).to_i
7
+
8
+ class << self
9
+
10
+ def instrument!
11
+ return unless @cold_start
12
+ @cold_start = false
13
+ now = (Time.now.to_f * 1000).to_i
14
+ proactive_init = (now - @cold_start_time) > 10_000
15
+ new(proactive_init).instrument!
16
+ end
17
+
18
+ def clear!
19
+ @cold_start = true
20
+ @cold_start_time = (Time.now.to_f * 1000).to_i
21
+ end
22
+
23
+ end
24
+
25
+ def initialize(proactive_init)
26
+ @proactive_init = proactive_init
27
+ @metrics = []
28
+ @properties = {}
29
+ end
30
+
31
+ def instrument!
32
+ name = @proactive_init ? 'ProactiveInit' : 'ColdStart'
33
+ put_metric name, 1, 'Count'
34
+ puts JSON.dump(message)
35
+ end
36
+
37
+ private
38
+
39
+ def dimensions
40
+ [{ AppName: rails_app_name }]
41
+ end
42
+
43
+ def put_metric(name, value, unit = nil)
44
+ @metrics << { 'Name': name }.tap do |m|
45
+ m['Unit'] = unit if unit
46
+ end
47
+ set_property name, value
48
+ end
49
+
50
+ def set_property(name, value)
51
+ @properties[name] = value
52
+ self
53
+ end
54
+
55
+ def message
56
+ {
57
+ '_aws': {
58
+ 'Timestamp': timestamp,
59
+ 'CloudWatchMetrics': [
60
+ {
61
+ 'Namespace': NAMESPACE,
62
+ 'Dimensions': [dimensions.map(&:keys).flatten],
63
+ 'Metrics': @metrics
64
+ }
65
+ ]
66
+ }
67
+ }.tap do |m|
68
+ dimensions.each { |d| m.merge!(d) }
69
+ m.merge!(@properties)
70
+ end
71
+ end
72
+
73
+ def timestamp
74
+ Time.current.strftime('%s%3N').to_i
75
+ end
76
+
77
+ def rails_app_name
78
+ Lamby.config.metrics_app_name ||
79
+ Rails.application.class.name.split('::').first
80
+ end
81
+
82
+ end
83
+ end
data/lib/lamby/config.rb CHANGED
@@ -41,6 +41,8 @@ module Lamby
41
41
 
42
42
  def initialize_defaults
43
43
  @rack_app = nil
44
+ @cold_start_metrics = false
45
+ @metrics_app_name = nil
44
46
  @event_bridge_handler = lambda { |event, context| puts(event) }
45
47
  end
46
48
 
@@ -64,5 +66,21 @@ module Lamby
64
66
  @handled_proc = proc
65
67
  end
66
68
 
69
+ def cold_start_metrics?
70
+ @cold_start_metrics
71
+ end
72
+
73
+ def cold_start_metrics=(bool)
74
+ @cold_start_metrics = bool
75
+ end
76
+
77
+ def metrics_app_name
78
+ @metrics_app_name
79
+ end
80
+
81
+ def metrics_app_name=(name)
82
+ @metrics_app_name = name
83
+ end
84
+
67
85
  end
68
86
  end
data/lib/lamby/handler.rb CHANGED
@@ -4,6 +4,7 @@ module Lamby
4
4
  class << self
5
5
 
6
6
  def call(app, event, context, options = {})
7
+ Lamby::ColdStartMetrics.instrument! if Lamby.config.cold_start_metrics?
7
8
  new(app, event, context, options).call.response
8
9
  end
9
10
 
data/lib/lamby/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lamby
2
- VERSION = '5.0.0'
2
+ VERSION = '5.1.0'
3
3
  end
data/lib/lamby.rb CHANGED
@@ -9,6 +9,7 @@ require 'lamby/rack_alb'
9
9
  require 'lamby/rack_rest'
10
10
  require 'lamby/rack_http'
11
11
  require 'lamby/debug'
12
+ require 'lamby/cold_start_metrics'
12
13
  require 'lamby/handler'
13
14
 
14
15
  if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lamby
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2023-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: timecop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: webrick
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -177,6 +191,7 @@ files:
177
191
  - images/social2.png
178
192
  - lamby.gemspec
179
193
  - lib/lamby.rb
194
+ - lib/lamby/cold_start_metrics.rb
180
195
  - lib/lamby/config.rb
181
196
  - lib/lamby/debug.rb
182
197
  - lib/lamby/handler.rb