Sha256: 4d27fb9ef102c128e1a436378778b8f237712d9f9f0fbe0f4e3eaae25e8aa3c0

Contents?: true

Size: 1.73 KB

Versions: 19

Compression:

Stored size: 1.73 KB

Contents

require "timber/event"
require "timber/util"

module Timber
  module Events
    # The HTTP server response event tracks outgoing HTTP responses that you send
    # to clients.
    #
    # @note This event should be installed automatically through integrations,
    #   such as the {Integrations::ActionController::LogSubscriber} integration.
    class HTTPResponse < Timber::Event
      attr_reader :body, :headers, :http_context, :request_id, :service_name, :status, :time_ms

      def initialize(attributes)
        @body = attributes[:body] && Util::HTTPEvent.normalize_body(attributes[:body])
        @headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
        @http_context = attributes[:http_context]
        @request_id = attributes[:request_id]
        @status = attributes[:status] || raise(ArgumentError.new(":status is required"))
        @time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required"))
        @time_ms = @time_ms.round(6)
      end

      def to_hash
        {body: body, headers: headers, request_id: request_id, status: status, 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 = {})
        {:http_response => to_hash}
      end

      # Returns the human readable log message for this event.
      def message
        if http_context
          "#{http_context[:method]} #{http_context[:path]} sent #{status} #{status_description} " \
            "in #{time_ms}ms"
        else
          "Completed #{status} #{status_description} in #{time_ms}ms"
        end
      end

      def status_description
        Rack::Utils::HTTP_STATUS_CODES[status]
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

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