Sha256: ff7db78b03fc4771c9a92f5a70ef704341257171db775b43579489262e56bdc5

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

module ActiveSupport
  module Cache
    module Tracer
      class Subscriber
        attr_reader :tracer, :active_span, :event, :operation_name

        def initialize(tracer: OpenTracing.global_tracer, active_span: nil, event:)
          @tracer = tracer
          @active_span = active_span
          @event = event
          @operation_name = "cache.#{event}"
        end

        # For compatibility with Rails 3.2
        def call(*args)
          _, start, finish, _, payload = *args

          span = Tracer.start_span(operation_name,
                                    event: event,
                                    tracer: tracer,
                                    active_span: active_span,
                                    start_time: start,
                                    **payload)

          if payload[:exception]
            Rails::Tracer::SpanHelpers.set_error(span, payload[:exception_object] || payload[:exception])
          end

          span.finish(end_time: finish)
        end

        def start(name, _, payload)
          span = tracer.start_span(operation_name,
                                   child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
                                   tags: {
                                    'component' => 'ActiveSupport::Cache',
                                    'span.kind' => 'client'
                                   })

          payload[:__OT_SPAN__] = span
        end

        def finish(name, _, payload)
          span = payload[:__OT_SPAN__]
          return unless span

          span.set_tag('cache.key', payload.fetch(:key, 'unknown'))

          if event == 'read'
            span.set_tag('cache.hit', payload.fetch(:hit, false))
          end

          if payload[:exception]
            Rails::Tracer::SpanHelpers.set_error(span, payload[:exception_object] || payload[:exception])
          end

          span.finish
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
signalfx-rails-instrumentation-0.1.3 lib/rails/active_support/cache/subscriber.rb
signalfx-rails-instrumentation-0.1.2 lib/rails/active_support/cache/subscriber.rb
signalfx-rails-instrumentation-0.1.1 lib/rails/active_support/cache/subscriber.rb
signalfx-rails-instrumentation-0.1.0 lib/rails/active_support/cache/subscriber.rb
rails-tracer-0.5.0 lib/rails/active_support/cache/subscriber.rb