Skip to content

Instantly share code, notes, and snippets.

@somic
Created June 5, 2019 21:55
Show Gist options
  • Save somic/987cc079fc4d0d74f0c2bdab079dc3a4 to your computer and use it in GitHub Desktop.
Save somic/987cc079fc4d0d74f0c2bdab079dc3a4 to your computer and use it in GitHub Desktop.
lambda
require 'aws-xray-sdk'
def random_sleep_under_1s
r = Random.rand
sleep r
r
end
def xray(segment_name, method_name, with_subsegments: true)
parent_id = ENV['_X_AMZN_TRACE_ID'].gsub(/.*Parent=/, '').split(';').first
trace_id = ENV['_X_AMZN_TRACE_ID'].gsub(/.*Root=/, '').split(';').first
# this is how you start a segment
segment = XRay.recorder.begin_segment(segment_name, trace_id: trace_id, parent_id: parent_id)
if with_subsegments
1.upto(2).each do |i|
XRay.recorder.capture("subsegment_#{i}_#{segment_name}", segment: segment) do |subsegment|
ret = send(method_name) # here we do some work that we want to measure
subsegment.annotations[:return_value] = ret
ret
end
end
else
ret = send(method_name) # here we do some work that we want to measure
segment.annotations[:return_value] = ret # this annotation will be applied to segment
ret
end
XRay.recorder.end_segment
end
def handler(event:, context:)
xray('random_sleep_1', :random_sleep_under_1s)
xray('random_sleep_2', :random_sleep_under_1s)
{ body: 'hello world!' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment