Sha256: 253a06f6693dc9e12a00d844c7fb7089f3840717b5fdb87dd5679ce8b9ac8795

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'active_support/notifications'

module Rails
  module Instrumentation
    module Utils
      class << self
        # calls a handler function with name 'event' on the handler module.
        # For example, if the handler module is ActionViewSubscriber and the
        # event hook is 'render_template.action_controller', full_name is
        # 'render_template.action_controller' and event_name is 'render_template'
        def register_subscriber(full_name: '', event_name: '', handler_module: nil)
          ::ActiveSupport::Notifications.subscribe(full_name) do |*args|
            event = ::ActiveSupport::Notifications::Event.new(*args)
            handler_module.send(event_name, event)
          end
        end

        # takes and event and some set of tags from a handler, and creates a
        # span with the event's name and the start and finish times.
        def trace_notification(event:, tags: [])
          tags = ::Rails::Instrumentation::TAGS.clone.merge(tags)

          span = ::Rails::Instrumentation.tracer.start_span(event.name,
                                                            tags: tags,
                                                            start_time: event.time)

          # tag transaction_id
          span.set_tag('transaction.id', event.transaction_id)
          tag_error(span, event.payload) if event.payload.key? :exception_object

          span.finish(end_time: event.end)
        end

        # according to the ActiveSupport::Notifications documentation, exceptions
        # will be indicated with the presence of the :exception and :exception_object
        # keys. These will be tagged and logged according to the OpenTracing
        # specification.
        def tag_error(span, payload)
          span.record_exception(payload[:exception_object])
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signalfx-rails-instrumentation-0.2.0 lib/rails/instrumentation/utils.rb