Sha256: 669d3ab401400b6287f13791b54c61da81f825c6f32caf745fa9ccc46d1e5774

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'rails/railtie'
require 'lograge'

module GetaroundUtils; end
module GetaroundUtils::Railties; end

class GetaroundUtils::Railties::Lograge < Rails::Railtie
  module LogrageActionController
    def append_info_to_payload(payload)
      super
      payload[:lograge] = {}
      payload[:lograge][:host] = request.host
      payload[:lograge][:params] = request.filtered_parameters.except(:action, :controller)
      payload[:lograge][:remote_ip] = request.remote_ip
      payload[:lograge][:user_agent] = request.user_agent
      payload[:lograge][:referer] = request.referer
      payload[:lograge][:session_id] = session.is_a?(Hash) ? session[:id] : session.id.to_s if defined?(session)
      payload[:lograge][:user_id] = current_user&.id if defined?(current_user)
      payload[:lograge][:origin] = 'lograge'

      if defined?(NewRelic::Agent::Tracer)
        if span_id = NewRelic::Agent::Tracer.span_id
          payload[:lograge]['span.id'] = span_id
        end
        if trace_id = NewRelic::Agent::Tracer.trace_id
          payload[:lograge]['trace.id'] = trace_id
        end
        payload[:lograge]['entity.type'] = 'SERVICE'
        payload[:lograge]['entity.guid'] = NewRelic::Agent.config[:entity_guid]
        payload[:lograge]['entity.name'] = NewRelic::Agent.config[:app_name]&.first
      end

      if defined?(NewRelic::Agent::Hostname)
        payload[:lograge]['hostname'] = NewRelic::Agent::Hostname.get
      end

      nil
    end
  end

  initializer 'getaround_utils.action_controller' do
    ActionController::Base.prepend LogrageActionController
  end

  HTTP_PARAMS = [:method, :path, :host, :remote_ip,
                 :status, :duration, :location,
                 :user_agent, :referer].freeze

  config.lograge.enabled = true
  config.lograge.custom_options = ->(event) {
    event.payload[:lograge]
  }
  config.lograge.before_format = ->(data, _) {
    data.except(*HTTP_PARAMS).merge(http: data.slice(*HTTP_PARAMS))
  }
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
getaround_utils-0.2.19 lib/getaround_utils/railties/lograge.rb