Sha256: 5a0a2f18d78aad6293aa9d4f5ad773b5d236d1744b56248bb1f8da10702fa637

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module Skylight
  class Subscriber
    include Util::Logging

    attr_reader :config

    def initialize(config)
      @config      = config
      @subscriber  = nil
      @normalizers = Normalizers.build(config)
    end

    def register!
      unregister! if @subscriber
      @subscriber = ActiveSupport::Notifications.subscribe nil, self
    end

    def unregister!
      ActiveSupport::Notifications.unsubscribe @subscriber
      @subscriber = nil
    end

    #
    #
    # ===== ActiveSupport::Notifications API
    #
    #

    class Notification
      attr_reader :name, :span

      def initialize(name, span)
        @name, @span = name, span
      end
    end

    def start(name, id, payload)
      return unless trace = Instrumenter.current_trace

      cat, title, desc, annot = normalize(trace, name, payload)

      unless cat == :skip
        span = trace.instrument(cat, title, desc, annot)
      end

      trace.notifications << Notification.new(name, span)

    rescue Exception => e
      error "Subscriber#start error; msg=%s", e.message
      nil
    end

    def finish(name, id, payload)
      return unless trace = Instrumenter.current_trace

      while curr = trace.notifications.pop
        if curr.name == name
          curr.span.done if curr.span
          return
        end
      end

    rescue Exception => e
      error "Subscriber#finish error; msg=%s", e.message
      nil
    end

    def publish(name, *args)
      # Ignored for now because nothing in rails uses it
    end

  private

    def normalize(*args)
      @normalizers.normalize(*args)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
skylight-0.1.8 lib/skylight/subscriber.rb
skylight-0.1.7 lib/skylight/subscriber.rb
skylight-0.1.7.alpha1 lib/skylight/subscriber.rb
skylight-0.1.6 lib/skylight/subscriber.rb
skylight-0.1.6.alpha3 lib/skylight/subscriber.rb