Sha256: 9b8207df9b4dad5d7bbff9035d94c2719d8e2d8d83530abdb86bda7e717889bd

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Labkit
  module Tracing
    module Rails
      # ActiveRecordSubscriber bridges active record notifications to
      # the distributed tracing subsystem
      class ActiveRecordSubscriber
        include RailsCommon

        ACTIVE_RECORD_NOTIFICATION_TOPIC = "sql.active_record"
        OPERATION_NAME_PREFIX = "active_record:"
        DEFAULT_OPERATION_NAME = "sqlquery"

        # Instruments Rails ActiveRecord events for opentracing.
        # Returns a lambda, which, when called will unsubscribe from the notifications
        def self.instrument
          subscriber = new

          subscription =
            ActiveSupport::Notifications.subscribe(ACTIVE_RECORD_NOTIFICATION_TOPIC) do |_, start, finish, _, payload|
              subscriber.notify(start, finish, payload)
            end

          create_unsubscriber [subscription]
        end

        # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
        def notify(start, finish, payload)
          generate_span_for_notification(notification_name(payload), start, finish, payload, tags_for_notification(payload))
        end

        private

        def notification_name(payload)
          OPERATION_NAME_PREFIX + (payload[:name].presence || DEFAULT_OPERATION_NAME)
        end

        def tags_for_notification(payload)
          {
            "component" => "ActiveRecord",
            "span.kind" => "client",
            "db.type" => "sql",
            "db.connection_id" => payload[:connection_id],
            "db.cached" => payload[:cached] || false,
            "db.statement" => payload[:sql],
          }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
gitlab-labkit-0.10.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.9.1 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.9.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.8.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.7.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.6.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.5.2 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.5.1 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.5.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.4.2 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.4.1 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.4.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.3.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.2.0 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.1.2 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.1.0.pre.1.pre.gcb57c95 lib/labkit/tracing/rails/active_record_subscriber.rb
gitlab-labkit-0.1.0 lib/labkit/tracing/rails/active_record_subscriber.rb