Sha256: ae570fceb36ac79d8699b5c61d4b0d5ab75e555398ab8f9bb7745c2bea78efb2

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

[[custom-instrumentation]]
=== Custom instrumentation

When <<introduction,installed>> and <<configuration,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.

See <<api-agent-start_transaction,`ElasticAPM.start_transaction`>> and
<<api-agent-start_span,`ElasticAPM.start_span`>>.

[float]
==== Helpers

ElasticAPM includes some nifty helpers if you just want to instrument a regular
method.

[source,ruby]
----
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
----

[float]
==== Custom span example

If you are already inside a Transaction (most likely) and you want to instrument
some work inside it, add a custom span:

[source,ruby]
----
class ThingsController < ApplicationController
  def index
    @result_of_work = ElasticAPM.with_span "Heavy work" do
      do_the_heavy_work
    end
  end
end
----

[float]
==== Custom transaction example

If you are **not** inside a Transaction already (eg. outside of your common web
application) start and manage your own transactions like so:

[source,ruby]
----
class Something
  def do_work
    transaction = ElasticAPM.start_transaction 'Something#do_work'

    begin
      Sequel[:users] # many third party libs will be automatically instrumented
    rescue Exception => e
      ElasticAPM.report(e)
      raise
    ensure
      ElasticAPM.end_transaction('result')
    end
  end
end
----

**Note:** If the agent isn't started beforehand this will do nothing.
See <<api-agent-start,ElasticAPM.start>>.

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
elastic-apm-2.3.1 docs/custom-instrumentation.asciidoc
elastic-apm-2.3.0 docs/custom-instrumentation.asciidoc
elastic-apm-2.2.0 docs/custom-instrumentation.asciidoc
elastic-apm-2.1.2 docs/custom-instrumentation.asciidoc
elastic-apm-2.1.1 docs/custom-instrumentation.asciidoc
elastic-apm-2.1.0 docs/custom-instrumentation.asciidoc
elastic-apm-2.0.1 docs/custom-instrumentation.asciidoc
elastic-apm-2.0.0 docs/custom-instrumentation.asciidoc