lamby 4.3.3 → 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: dce401866a14fbce98621f6c677ab0f17df796c1c9c0792d4a1030adc69eadbd
4
- data.tar.gz: 593c2f449b053f4bdc6ae2dbe74602f0debd93d57a57a1811d7f791bc1b40fc9
3
+ metadata.gz: 940f2329a4503c79e8c2c48908b3f1c2d22445c16ebb0dc71ee0802e54e7c560
4
+ data.tar.gz: 708b36a037b00b3f66dfa3239a8060ae6b00177628a1e1cd38ba0a8ed8988a94
5
5
  SHA512:
6
- metadata.gz: 4f119c574a97288c37eb7c5c97643650d2d730fe16ab7b7e6b06a777088e069b6050c4ab023856e32c10c7da0029d74067eb8db37492b0dc0c1a0ed3948f7ee9
7
- data.tar.gz: 1bf90e93726e78a75f408801976f65c7dafe369cc14d567fb4ddb337832a0b3dfb10fb1d8e2f91b9b7cf20d42c176f914f0b33c3641accbbc8fd757ec3d56ba6
6
+ metadata.gz: e2d135f4d5cab367171030c98c2126f57e9a9542b2b802535677450e47f8be6dc4c99d8865871978d73c184269c7a806d61c0b48f1fd54642ceda76289b96474
7
+ data.tar.gz: 472144850f8dd607c2709d74d6a240ea6949ce640a6e2ddc748e49a31db8f94b04cfa1b3aae842bb9fc465c798e47bc13020571dd8d611b01523e9f8378852cc
data/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
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
+
11
+ ## v5.0.0
12
+
13
+ ### Changed
14
+
15
+ - ⚠️ Breaking Changes ⚠️
16
+ - Remove Lamby::Runner & Lamby::Command in favor of LambdaConsole Ruby gem.
17
+ - New runner pattern exceptions from above. Now: LambdaConsole::Run::UnknownCommandPattern
18
+ - Switch your own runner events to new https://github.com/rails-lambda/lambda-console spec.
19
+
5
20
  ## v4.3.1, v4.3.2
6
21
 
7
22
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lamby (4.3.3)
4
+ lamby (5.1.0)
5
+ lambda-console-ruby
5
6
  rack
6
7
 
7
8
  GEM
@@ -98,6 +99,7 @@ GEM
98
99
  i18n (1.12.0)
99
100
  concurrent-ruby (~> 1.0)
100
101
  jmespath (1.6.2)
102
+ lambda-console-ruby (1.0.0)
101
103
  lambdakiq (2.2.0)
102
104
  activejob
103
105
  aws-sdk-sqs
@@ -131,6 +133,8 @@ GEM
131
133
  nio4r (2.5.9)
132
134
  nokogiri (1.14.3-aarch64-linux)
133
135
  racc (~> 1.4)
136
+ nokogiri (1.14.3-arm64-darwin)
137
+ racc (~> 1.4)
134
138
  pry (0.14.2)
135
139
  coderay (~> 1.1)
136
140
  method_source (~> 1.0)
@@ -167,6 +171,7 @@ GEM
167
171
  rake (13.0.6)
168
172
  ruby2_keywords (0.0.5)
169
173
  thor (1.2.1)
174
+ timecop (0.9.6)
170
175
  timeout (0.3.2)
171
176
  tzinfo (2.0.6)
172
177
  concurrent-ruby (~> 1.0)
@@ -178,6 +183,7 @@ GEM
178
183
 
179
184
  PLATFORMS
180
185
  aarch64-linux
186
+ arm64-darwin-22
181
187
 
182
188
  DEPENDENCIES
183
189
  aws-sdk-ssm
@@ -190,6 +196,7 @@ DEPENDENCIES
190
196
  pry
191
197
  rails
192
198
  rake
199
+ timecop
193
200
  webrick
194
201
 
195
202
  BUNDLED WITH
data/lamby.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
  spec.add_dependency 'rack'
22
+ spec.add_dependency 'lambda-console-ruby'
22
23
  spec.add_development_dependency 'aws-sdk-ssm'
23
24
  spec.add_development_dependency 'bundler'
24
25
  spec.add_development_dependency 'rake'
@@ -26,5 +27,6 @@ Gem::Specification.new do |spec|
26
27
  spec.add_development_dependency 'minitest-focus'
27
28
  spec.add_development_dependency 'mocha'
28
29
  spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'timecop'
29
31
  spec.add_development_dependency 'webrick'
30
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
 
@@ -53,7 +55,7 @@ module Lamby
53
55
  end
54
56
 
55
57
  def runner_patterns
56
- Runner::PATTERNS
58
+ LambdaConsole::Run::PATTERNS
57
59
  end
58
60
 
59
61
  def handled_proc
@@ -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
 
@@ -92,10 +93,8 @@ module Lamby
92
93
  Lambdakiq.cmd event: @event, context: @context
93
94
  elsif lambda_cable?
94
95
  LambdaCable.cmd event: @event, context: @context
95
- elsif command?
96
- Lamby::Command.cmd event: @event, context: @context
97
- elsif runner?
98
- Lamby::Runner.cmd event: @event, context: @context
96
+ elsif LambdaConsole.handle?(@event)
97
+ LambdaConsole.handle(@event)
99
98
  elsif event_bridge?
100
99
  Lamby.config.event_bridge_handler.call @event, @context
101
100
  else
@@ -121,14 +120,6 @@ module Lamby
121
120
  defined?(::Lambdakiq) && ::Lambdakiq.jobs?(@event)
122
121
  end
123
122
 
124
- def runner?
125
- Lamby::Runner.handle?(@event)
126
- end
127
-
128
- def command?
129
- Lamby::Command.handle?(@event)
130
- end
131
-
132
123
  def lambda_cable?
133
124
  defined?(::LambdaCable) && ::LambdaCable.handle?(@event, @context)
134
125
  end
data/lib/lamby/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lamby
2
- VERSION = '4.3.3'
2
+ VERSION = '5.1.0'
3
3
  end
data/lib/lamby.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'lamby/logger'
2
2
  require 'rack'
3
3
  require 'base64'
4
+ require 'lambda-console-ruby'
4
5
  require 'lamby/version'
5
6
  require 'lamby/config'
6
7
  require 'lamby/rack'
@@ -8,8 +9,7 @@ require 'lamby/rack_alb'
8
9
  require 'lamby/rack_rest'
9
10
  require 'lamby/rack_http'
10
11
  require 'lamby/debug'
11
- require 'lamby/runner'
12
- require 'lamby/command'
12
+ require 'lamby/cold_start_metrics'
13
13
  require 'lamby/handler'
14
14
 
15
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: 4.3.3
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-15 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
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lambda-console-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: aws-sdk-ssm
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,20 @@ dependencies:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
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'
125
153
  - !ruby/object:Gem::Dependency
126
154
  name: webrick
127
155
  requirement: !ruby/object:Gem::Requirement
@@ -163,7 +191,7 @@ files:
163
191
  - images/social2.png
164
192
  - lamby.gemspec
165
193
  - lib/lamby.rb
166
- - lib/lamby/command.rb
194
+ - lib/lamby/cold_start_metrics.rb
167
195
  - lib/lamby/config.rb
168
196
  - lib/lamby/debug.rb
169
197
  - lib/lamby/handler.rb
@@ -175,7 +203,6 @@ files:
175
203
  - lib/lamby/rack_http.rb
176
204
  - lib/lamby/rack_rest.rb
177
205
  - lib/lamby/railtie.rb
178
- - lib/lamby/runner.rb
179
206
  - lib/lamby/ssm_parameter_store.rb
180
207
  - lib/lamby/tasks.rake
181
208
  - lib/lamby/version.rb
data/lib/lamby/command.rb DELETED
@@ -1,43 +0,0 @@
1
- module Lamby
2
- class Command
3
-
4
- class << self
5
-
6
- def handle?(event)
7
- event.dig('x-lambda-console', 'command') ||
8
- event.dig('lamby', 'command')
9
- end
10
-
11
- def cmd(event:, context:)
12
- new(event).call
13
- end
14
-
15
- end
16
-
17
- def initialize(event)
18
- @event = event
19
- @body = ''
20
- end
21
-
22
- def call
23
- begin
24
- body = eval(command, TOPLEVEL_BINDING).inspect
25
- { statusCode: 200, headers: {}, body: body }
26
- rescue Exception => e
27
- body = "#<#{e.class}:#{e.message}>".tap do |b|
28
- if e.backtrace
29
- b << "\n"
30
- b << e.backtrace.join("\n")
31
- end
32
- end
33
- { statusCode: 422, headers: {}, body: body }
34
- end
35
- end
36
-
37
- def command
38
- @event.dig('x-lambda-console', 'command') ||
39
- @event.dig('lamby', 'command')
40
- end
41
-
42
- end
43
- end
data/lib/lamby/runner.rb DELETED
@@ -1,64 +0,0 @@
1
- require 'open3'
2
-
3
- module Lamby
4
- class Runner
5
- class Error < StandardError ; end
6
- class UnknownCommandPattern < Error ; end
7
-
8
- PATTERNS = [%r{.*}]
9
-
10
- class << self
11
-
12
- def handle?(event)
13
- event.dig('x-lambda-console', 'runner') ||
14
- event.dig('lamby', 'runner')
15
- end
16
-
17
- def cmd(event:, context:)
18
- new(event).call
19
- end
20
-
21
- end
22
-
23
- def initialize(event)
24
- @event = event
25
- @body = ''
26
- end
27
-
28
- def call
29
- validate!
30
- status = Open3.popen3(env, command, chdir: chdir) do |_stdin, stdout, stderr, thread|
31
- @body << stdout.read
32
- @body << stderr.read
33
- puts @body
34
- thread.value.exitstatus
35
- end
36
- { statusCode: status, headers: {}, body: @body }
37
- end
38
-
39
- def command
40
- @event.dig('x-lambda-console', 'runner') ||
41
- @event.dig('lamby', 'runner')
42
- end
43
-
44
- private
45
-
46
- def chdir
47
- defined?(::Rails) ? ::Rails.root : Dir.pwd
48
- end
49
-
50
- def validate!
51
- return if pattern?
52
- raise UnknownCommandPattern.new(command)
53
- end
54
-
55
- def pattern?
56
- PATTERNS.any? { |p| p === command }
57
- end
58
-
59
- def env
60
- Hash[ENV.to_hash.map { |k,v| [k, ENV[k]] }]
61
- end
62
-
63
- end
64
- end