When installed and properly configured ElasticAPM will automatically wrap your app’s request/responses in transactions and report its errors. It also wraps each background job if you use Sidekiq or DelayedJob.
But it is possible to create your own transactions as well as provide spans for any automatic or custom transaction.
ElasticAPM includes some nifty helpers if you just want to instrument a regular method.
class Thing include SpanHelpers def do_the_work # ... end span_method :do_hard_work # takes optional `name` and `type` def self.do_all_the_work # ... end span_class_method :do_hard_work, 'Custom name', 'custom.work_thing' end
Custom span exampleedit
If you are already inside a Transaction (most likely) and you want to intrument some work inside it, add a custom span:
class ThingsController < ApplicationController def index @result_of_work = ElasticAPM.span "Heavy work" do do_the_heavy_work end end end
Custom transaction exampleedit
If you are not inside a Transaction already (eg. outside of your common web application) start and manage your own transactions like so:
class Something def do_work transaction = ElasticAPM.transaction 'Something#do_work' begin Sequel[:users] # many third party libs will be automatically instrumented transaction.submit('success') if transaction rescue Exception => e ElasticAPM.report(e) transaction.submit('error') if transaction raise ensure transaction.release end end end
Note: If the agent isn’t started beforehand this will do nothing. See ElasticAPM.start.