Module: TCellAgent::Instrumentation::Rails::JSAgent

Defined in:
lib/tcell_agent/rails/js_agent_insert.rb

Constant Summary collapse

HEAD_SEARCH_REGEX =
Regexp.new('(<head>|<head( |\n).*?>)', Regexp::IGNORECASE)

Class Method Summary collapse

Class Method Details

.get_handler_and_script_insert(request, response_headers) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tcell_agent/rails/js_agent_insert.rb', line 56

def self.get_handler_and_script_insert(request, response_headers)
  js_agent_handler = nil
  script_insert = nil

  TCellAgent::Instrumentation.safe_block('JSAgent get handler and script insert') do
    return [nil, nil] unless insert_js_agent?(response_headers)

    js_agent_policy = TCellAgent.policy(TCellAgent::PolicyTypes::JSAGENTINJECTION)
    script_insert = js_agent_policy.get_js_agent_script_tag(
      request.env[TCellAgent::Instrumentation::TCELL_ID]
    )

    return [nil, nil] unless script_insert

    js_agent_handler = proc { |si, resp|
      handle_js_agent_insert(si, resp)
    }
  end

  [js_agent_handler, script_insert]
end

.handle_js_agent_insert(script_insert, response) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tcell_agent/rails/js_agent_insert.rb', line 44

def self.handle_js_agent_insert(script_insert, response)
  new_response = response
  TCellAgent::Instrumentation.safe_block('Handling JSAgent insert') do
    new_response = response.sub(
      TCellAgent::Instrumentation::Rails::JSAgent::HEAD_SEARCH_REGEX,
      "\\1#{script_insert}"
    )
  end

  new_response
end

.insert_js_agent?(response_headers) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tcell_agent/rails/js_agent_insert.rb', line 8

def self.insert_js_agent?(response_headers)
  content_disposition = response_headers['Content-Disposition']
  is_attachment = content_disposition && content_disposition =~ /^attachment/i

  content_type = response_headers['Content-Type']
  applicable_content_type = content_type &&
                            content_type.start_with?('text/html')

  content_encoding = response_headers['Content-Encoding']
  compressed_content_encoding = content_encoding &&
                                (content_encoding =~ /(br|gzip)/i)

  !is_attachment && applicable_content_type && !compressed_content_encoding
end

.insert_now(js_agent_handler, script_insert, rack_body, content_length) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tcell_agent/rails/js_agent_insert.rb', line 23

def self.insert_now(js_agent_handler, script_insert, rack_body, content_length)
  TCellAgent::Instrumentation.safe_block('Handling JSAgent Insert Now') do
    if js_agent_handler
      new_content_length = 0
      newbody = []
      rack_body.each do |str|
        modified_str = js_agent_handler.call(script_insert, str)

        newbody << modified_str
        new_content_length += modified_str.bytesize
      end
      rack_body.close if rack_body.respond_to?(:close)

      rack_body = newbody
      content_length = new_content_length
    end
  end

  [rack_body, content_length]
end