-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95054f1
commit a7012c8
Showing
10 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module Lamby | ||
class ColdStartMetrics | ||
NAMESPACE = 'Lamby' | ||
|
||
@cold_start = true | ||
@cold_start_time = (Time.now.to_f * 1000).to_i | ||
|
||
class << self | ||
|
||
def instrument! | ||
return unless @cold_start | ||
@cold_start = false | ||
now = (Time.now.to_f * 1000).to_i | ||
proactive_init = (now - @cold_start_time) > 10_000 | ||
new(proactive_init).instrument! | ||
end | ||
|
||
def clear! | ||
@cold_start = true | ||
@cold_start_time = (Time.now.to_f * 1000).to_i | ||
end | ||
|
||
end | ||
|
||
def initialize(proactive_init) | ||
@proactive_init = proactive_init | ||
@metrics = [] | ||
@properties = {} | ||
end | ||
|
||
def instrument! | ||
name = @proactive_init ? 'ProactiveInit' : 'ColdStart' | ||
put_metric name, 1, 'Count' | ||
puts JSON.dump(message) | ||
end | ||
|
||
private | ||
|
||
def dimensions | ||
[{ AppName: rails_app_name }] | ||
end | ||
|
||
def put_metric(name, value, unit = nil) | ||
@metrics << { 'Name': name }.tap do |m| | ||
m['Unit'] = unit if unit | ||
end | ||
set_property name, value | ||
end | ||
|
||
def set_property(name, value) | ||
@properties[name] = value | ||
self | ||
end | ||
|
||
def message | ||
{ | ||
'_aws': { | ||
'Timestamp': timestamp, | ||
'CloudWatchMetrics': [ | ||
{ | ||
'Namespace': NAMESPACE, | ||
'Dimensions': [dimensions.map(&:keys).flatten], | ||
'Metrics': @metrics | ||
} | ||
] | ||
} | ||
}.tap do |m| | ||
dimensions.each { |d| m.merge!(d) } | ||
m.merge!(@properties) | ||
end | ||
end | ||
|
||
def timestamp | ||
Time.current.strftime('%s%3N').to_i | ||
end | ||
|
||
def rails_app_name | ||
Lamby.config.metrics_app_name || | ||
Rails.application.class.name.split('::').first | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Lamby | ||
VERSION = '5.0.0' | ||
VERSION = '5.1.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'test_helper' | ||
|
||
class ColdStartMetricsSpec < LambySpec | ||
|
||
before { Lamby::ColdStartMetrics.clear! } | ||
|
||
it 'has a config that defaults to false' do | ||
refute Lamby.config.cold_start_metrics? | ||
end | ||
|
||
it 'calling instrument for the first time will output a CloudWatch count metric for ColdStart' do | ||
out = capture(:stdout) { Lamby::ColdStartMetrics.instrument! } | ||
metric = JSON.parse(out) | ||
expect(metric['AppName']).must_equal 'Dummy' | ||
expect(metric['ColdStart']).must_equal 1 | ||
metrics = metric['_aws']['CloudWatchMetrics'] | ||
expect(metrics.size).must_equal 1 | ||
expect(metrics.first['Namespace']).must_equal 'Lamby' | ||
expect(metrics.first['Dimensions']).must_equal [['AppName']] | ||
expect(metrics.first['Metrics']).must_equal [{'Name' => 'ColdStart', 'Unit' => 'Count'}] | ||
end | ||
|
||
it 'only ever sends one metric for the lifespan of the function' do | ||
assert_output(/ColdStart/) { Lamby::ColdStartMetrics.instrument! } | ||
assert_output('') { Lamby::ColdStartMetrics.instrument! } | ||
Timecop.travel(Time.now + 10) { assert_output('') { Lamby::ColdStartMetrics.instrument! } } | ||
Timecop.travel(Time.now + 50000000) { assert_output('') { Lamby::ColdStartMetrics.instrument! } } | ||
end | ||
|
||
it 'will record a ProactiveInit metric if the function is called after 10 seconds' do | ||
Timecop.travel(Time.now + 11) do | ||
out = capture(:stdout) { Lamby::ColdStartMetrics.instrument! } | ||
metric = JSON.parse(out) | ||
expect(metric['AppName']).must_equal 'Dummy' | ||
expect(metric['ProactiveInit']).must_equal 1 | ||
metrics = metric['_aws']['CloudWatchMetrics'] | ||
expect(metrics.size).must_equal 1 | ||
expect(metrics.first['Namespace']).must_equal 'Lamby' | ||
expect(metrics.first['Dimensions']).must_equal [['AppName']] | ||
expect(metrics.first['Metrics']).must_equal [{'Name' => 'ProactiveInit', 'Unit' => 'Count'}] | ||
end | ||
end | ||
|
||
it 'will not record a ProactiveInit metric if the function is called before 10 seconds' do | ||
Timecop.travel(Time.now + 9) do | ||
assert_output(/ColdStart/) { Lamby::ColdStartMetrics.instrument! } | ||
end | ||
end | ||
|
||
private | ||
|
||
def now_ms | ||
(Time.now.to_f * 1000).to_i | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters