Sha256: 54b14c4756254ca471a18a0d682ba1eaceba21a46a118192e8a47e2fb8192eaf

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require_relative 'event_handler'

module OpenTelemetry
  module Instrumentation
    module Grape
      # Manages all subscriptions, both for custom subscribers and built-in notifications
      class Subscriber
        class << self
          # Subscribe to all notifications (except those specified in the :ignored_events configs)
          def subscribe
            subscriptions = filter_ignored_events(SUBSCRIPTIONS)
            subscriptions.each do |subscriber_method, event|
              ::ActiveSupport::Notifications.subscribe(event) do |*args|
                EventHandler.send(subscriber_method, *args)
              end
            end
          end

          private

          # ActiveSupport::Notifications that can be subscribed to using the documented `.subscribe` interface.
          # Reference: https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html#method-c-subscribe
          SUBSCRIPTIONS = {
            endpoint_run: 'endpoint_run.grape',
            endpoint_render: 'endpoint_render.grape',
            endpoint_run_filters: 'endpoint_run_filters.grape',
            format_response: 'format_response.grape'
          }.freeze

          def filter_ignored_events(subscriptions)
            # Do not ignore 'endpoint_run' event
            ignored_events = config[:ignored_events] - [:endpoint_run]
            subscriptions.reject { |event| ignored_events.include?(event) }
          end

          def config
            Grape::Instrumentation.instance.config
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-grape-0.2.0 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.7 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.6 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.5 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.4 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.3 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.2 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.1 lib/opentelemetry/instrumentation/grape/subscriber.rb
opentelemetry-instrumentation-grape-0.1.0 lib/opentelemetry/instrumentation/grape/subscriber.rb