Sha256: c0c1eecfc53619a65ae6ce2831734666b467d33b4066d79a7c92f103cc09aa0f

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module Logtail
  module Integrations
    module Rack
      # The HTTP server response event tracks outgoing HTTP responses that you send
      # to clients.

      class HTTPResponse
        attr_reader :body, :content_length, :headers, :headers_json, :http_context, :request_id, :service_name,
          :status, :duration_ms

        def initialize(attributes)
          @body = attributes[:body]
          @content_length  = attributes[:content_length]
          @headers = attributes[:headers]
          @http_context = attributes[:http_context]
          @request_id = attributes[:request_id]
          @service_name = attributes[:service_name]
          @status = attributes[:status]
          @duration_ms = attributes[:duration_ms]

          if @headers
            @headers_json = @headers.to_json
          end
        end

        # Returns the human readable log message for this event.
        def message
          if http_context
            message = "#{http_context[:method]} #{http_context[:path]} completed with " \
              "#{status} #{status_description} "

            if content_length
              message << ", #{content_length} bytes, "
            end

            message << "in #{duration_ms}ms"
          else
            message = "Completed #{status} #{status_description} "

            if content_length
              message << ", #{content_length} bytes, "
            end

            message << "in #{duration_ms}ms"
          end
        end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
logtail-rack-0.1.4 lib/logtail-rack/http_response.rb
logtail-rack-0.1.3 lib/logtail-rack/http_response.rb
logtail-rack-0.1.2 lib/logtail-rack/http_response.rb
logtail-rack-0.1.1 lib/logtail-rack/http_response.rb
logtail-rack-0.1.0 lib/logtail-rack/http_response.rb