Module: SignalFx::Lambda::Metrics

Defined in:
lib/signalfx/lambda/metrics.rb

Defined Under Namespace

Classes: Error

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/signalfx/lambda/metrics.rb', line 9

def client
  @client
end

Class Method Details

.init_clientObject



78
79
80
81
82
83
# File 'lib/signalfx/lambda/metrics.rb', line 78

def init_client
  access_token = ENV['SIGNALFX_ACCESS_TOKEN']
  ingest_endpoint = ENV['SIGNALFX_METRICS_URL'] || ENV['SIGNALFX_ENDPOINT_URL'] || 'https://ingest.signalfx.com'

  @client = SignalFx.new access_token, ingest_endpoint: ingest_endpoint
end

.populate_dimensions(context) ⇒ Object



71
72
73
74
75
76
# File 'lib/signalfx/lambda/metrics.rb', line 71

def populate_dimensions(context)
  dimensions = SignalFx::Lambda.fields.map do |key, val|
    { :key => key, :value => val }
  end
  dimensions.push({ :key => 'metric_source', :value => SignalFx::Lambda::COMPONENT })
end

.wrap_function(event:, context:) ⇒ Object



11
12
13
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
# File 'lib/signalfx/lambda/metrics.rb', line 11

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