module TCellAgent::Instrumentation

Constants

TCELL_ID

Public Class Methods

get_safe_block_logger() click to toggle source

NOTE: mock for tests

# File lib/tcell_agent/instrumentation.rb, line 225
def self.get_safe_block_logger
  unless defined?(@safe_block_logger)
    @safe_block_logger = TCellAgent::ModuleLogger.new(TCellAgent.logger, name)
  end

  @safe_block_logger
end
grape_path_params(env, route) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 63
def self.grape_path_params(env, route)
  all_params = Grape::Request.new(env).params

  grape_route_params(route).keys.each_with_object({}) do |key, memo|
    memo[key] = all_params[key]
  end
end
grape_route?(route) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 5
def self.grape_route?(route)
  if defined?(Grape::API)
    begin
      return route.app < Grape::API if ::Rails::VERSION::MAJOR == 4 &&
                                       ::Rails::VERSION::MINOR < 2

      return route.app.app < Grape::API
    rescue StandardError
      # do nothing
    end
  end

  false
end
grape_route_info(route) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 38
def self.grape_route_info(route)
  major, minor, _tiny = Gem.loaded_specs['grape'].version.to_s.split('.')
  if major.to_i.zero? && minor.to_i < 16
    {
      :method => route.route_method,
      :path => route.route_path
    }
  else
    {
      :method => route.request_method,
      :path => route.path
    }
  end
end
grape_route_params(route) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 53
def self.grape_route_params(route)
  major, minor, _tiny = Gem.loaded_specs['grape'].version.to_s.split('.')
  if major.to_i.zero? && minor.to_i < 16
    route.route_params
  else
    route.params
  end
end
instrument_grape_api(grape_mount_endpoint, routes) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 20
def self.instrument_grape_api(grape_mount_endpoint, routes)
  return unless routes

  routes.each do |route|
    route_info = grape_route_info(route)

    route_path = "#{grape_mount_endpoint}#{route_info[:path]}"
    route_method = route_info[:method]

    route_id = TCellAgent::SensorEvents::Util.calculate_route_id(route_method, route_path)
    TCellAgent.send_event(
      TCellAgent::SensorEvents::AppRoutesSensorEvent.new(
        route_path, route_method, route_id, nil, nil
      )
    )
  end
end
safe_block(message, &block) click to toggle source
# File lib/tcell_agent/instrumentation.rb, line 233
def self.safe_block(message, &block)
  block.call
rescue StandardError => e
  logger = get_safe_block_logger
  logger.error("Error #{message} (#{e.class}): #{e.message}")
  logger.exception(e)
end
safe_block_no_log(_message, &block) click to toggle source
# File lib/tcell_agent/instrumentation.rb, line 241
def self.safe_block_no_log(_message, &block)
  block.call
rescue StandardError
  # do nothing
end

Public Instance Methods

call!(env) click to toggle source
# File lib/tcell_agent/rails/routes/grape.rb, line 73
def call!(env)
  if TCellAgent.configuration.should_intercept_requests?
    TCellAgent::Instrumentation.safe_block('Determining Rails Route ID') do
      tcell_context = env[TCellAgent::Instrumentation::TCELL_ID]
      if tcell_context && tcell_context.grape_mount_endpoint && respond_to?(:routes)
        route = routes.first

        if route
          route_info = TCellAgent::Instrumentation.grape_route_info(route)
          route_path = "#{tcell_context.grape_mount_endpoint}#{route_info[:path]}"

          if route_path
            tcell_context.path_parameters = TCellAgent::Instrumentation.grape_path_params(env, route)
            tcell_context.route_id =
              TCellAgent::SensorEvents::Util.calculate_route_id(tcell_context.request_method, route_path)
          end
        end
      end
    end
  end

  tcell_call!(env)
end