Module: SignalFx::Lambda::Metrics
- Defined in:
- lib/signalfx/lambda/metrics.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- @@ephemeral_dimensions =
Set['aws_request_id', 'log_stream_name']
Class Attribute Summary collapse
-
.client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .init_client ⇒ Object
- .populate_dimensions(context) ⇒ Object
- .wrap_function(event:, context:) ⇒ Object
Class Attribute Details
.client ⇒ Object
Returns the value of attribute client.
12 13 14 |
# File 'lib/signalfx/lambda/metrics.rb', line 12 def client @client end |
Class Method Details
.init_client ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/signalfx/lambda/metrics.rb', line 83 def init_client access_token = ENV['SIGNALFX_ACCESS_TOKEN'] ingest_endpoint = ENV['SIGNALFX_METRICS_URL'] || ENV['SIGNALFX_ENDPOINT_URL'] || 'https://ingest.signalfx.com' timeout = ENV['SIGNALFX_SEND_TIMEOUT'] || 1 @client = SignalFx.new access_token, ingest_endpoint: ingest_endpoint, timeout: timeout end |
.populate_dimensions(context) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/signalfx/lambda/metrics.rb', line 74 def populate_dimensions(context) dimensions = SignalFx::Lambda.fields .reject {|key, _| @@ephemeral_dimensions.include?(key)} .map do |key, val| { :key => key, :value => val } end dimensions.push({ :key => 'metric_source', :value => SignalFx::Lambda::COMPONENT }) end |
.wrap_function(event:, context:) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/signalfx/lambda/metrics.rb', line 14 def wrap_function(event:, context:) cold_start = @client.nil? init_client unless @client counters = [] gauges = [] dimensions = populate_dimensions(context) # time execution of next block start_time = Time.now response = yield event: event, context: context end_time = Time.now duration = ((end_time - start_time) * 1000) # duration in ms end_time = end_time.strftime('%s%L') counters.push( { :metric => 'function.invocations', :value => 1, :timestamp => end_time, :dimensions => dimensions } ) counters.push( { :metric => 'function.cold_starts', :value => 1, :timestamp => end_time, :dimensions => dimensions } ) if cold_start gauges = [ { :metric => 'function.duration', :value => duration, :timestamp => end_time, :dimensions => dimensions } ] response rescue => error error_counter = { :metric => 'function.errors', :value => 1, :timestamp => end_time, :dimensions => dimensions } counters.push(error_counter) raise ensure # send metrics before leaving this block @client.send(gauges: gauges, counters: counters) if @client end |