Sha256: a88ffb8d7284801500791caedc60c48a332d2a7cdf5cb3f2fd5563cc2dffbc45

Contents?: true

Size: 1.05 KB

Versions: 21

Compression:

Stored size: 1.05 KB

Contents

require "timber/event"

module Timber
  module Events
    # The SQL query event tracks sql queries to your database.
    #
    # @note This event should be installed automatically through integrations,
    #   such as the {Integrations::ActiveRecord::LogSubscriber} integration.
    class SQLQuery < Timber::Event
      MAX_QUERY_BYTES = 1024.freeze

      attr_reader :sql, :time_ms, :message

      def initialize(attributes)
        @sql = attributes[:sql] || raise(ArgumentError.new(":sql is required"))
        @sql = @sql.byteslice(0, MAX_QUERY_BYTES)
        @time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required"))
        @time_ms = @time_ms.round(6)
        @message = attributes[:message] || raise(ArgumentError.new(":message is required"))
      end

      def to_hash
        {sql: sql, time_ms: time_ms}
      end
      alias to_h to_hash

      # Builds a hash representation containing simple objects, suitable for serialization (JSON).
      def as_json(_options = {})
        {:sql_query => to_hash}
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
timber-2.5.1 lib/timber/events/sql_query.rb
timber-2.5.0 lib/timber/events/sql_query.rb
timber-2.4.0 lib/timber/events/sql_query.rb
timber-2.3.4 lib/timber/events/sql_query.rb
timber-2.3.3 lib/timber/events/sql_query.rb
timber-2.3.2 lib/timber/events/sql_query.rb
timber-2.3.1 lib/timber/events/sql_query.rb
timber-2.3.0 lib/timber/events/sql_query.rb
timber-2.2.3 lib/timber/events/sql_query.rb
timber-2.2.2 lib/timber/events/sql_query.rb
timber-2.2.1 lib/timber/events/sql_query.rb
timber-2.2.0 lib/timber/events/sql_query.rb
timber-2.1.10 lib/timber/events/sql_query.rb
timber-2.1.9 lib/timber/events/sql_query.rb
timber-2.1.8 lib/timber/events/sql_query.rb
timber-2.1.7 lib/timber/events/sql_query.rb
timber-2.1.6 lib/timber/events/sql_query.rb
timber-2.1.5 lib/timber/events/sql_query.rb
timber-2.1.4 lib/timber/events/sql_query.rb
timber-2.1.3 lib/timber/events/sql_query.rb