Module: TCellAgent::Instrumentation

Defined in:
lib/tcell_agent/patches.rb,
lib/tcell_agent/rails/routes.rb,
lib/tcell_agent/rails/database.rb,
lib/tcell_agent/instrumentation.rb,
lib/tcell_agent/rails/routes/grape.rb,
lib/tcell_agent/instrumentation/lfi.rb,
lib/tcell_agent/rails/js_agent_insert.rb,
lib/tcell_agent/rails/routes/route_id.rb,
lib/tcell_agent/rails/tcell_body_proxy.rb,
lib/tcell_agent/rails/settings_reporter.rb,
lib/tcell_agent/rails/middleware/global_middleware.rb,
lib/tcell_agent/rails/middleware/context_middleware.rb,
lib/tcell_agent/rails/middleware/headers_middleware.rb,
lib/tcell_agent/rails/middleware/body_filter_middleware.rb

Defined Under Namespace

Modules: Lfi, Patches, Rails, RouteId Classes: TCellData

Constant Summary collapse

TCELL_ID =
'tcell.request_data'.freeze

Class Method Summary collapse

Class Method Details

.get_safe_block_loggerObject

NOTE: mock for tests



33
34
35
36
37
38
39
# File 'lib/tcell_agent/instrumentation.rb', line 33

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) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/tcell_agent/instrumentation.rb', line 41

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) ⇒ Object



49
50
51
52
53
# File 'lib/tcell_agent/instrumentation.rb', line 49

def self.safe_block_no_log(_message, &block)
  block.call
rescue StandardError
  # do nothing
end