Class: TingYun::BrowserMonitoring

Inherits:
AgentMiddleware show all
Includes:
Support::Coerce
Defined in:
lib/ting_yun/middleware/browser_monitoring.rb

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze
TEXT_HTML =
'text/html'.freeze
CONTENT_DISPOSITION =
'Content-Disposition'.freeze
ATTACHMENT =
'attachment'.freeze
SCAN_LIMIT =
64_000
TITLE_END =
'</title>'.freeze
TITLE_END_CAPITAL =
'</TITLE>'.freeze
HEAD_END =
'<head>'.freeze
HEAD_END_CAPITAL =
'<HEAD>'.freeze
GT =
'>'.freeze
ALREADY_INSTRUMENTED_KEY =
"tingyun.browser_monitoring_already_instrumented"

Constants included from Instrumentation::MiddlewareTracing

Instrumentation::MiddlewareTracing::TXN_STARTED_KEY

Instance Attribute Summary

Attributes inherited from AgentMiddleware

#category, #target, #transaction_options

Instance Method Summary collapse

Methods included from Support::Coerce

event_params, float, int, int_or_nil, log_failure, string, url_encode

Methods inherited from AgentMiddleware

#build_transaction_name, #initialize

Methods included from Instrumentation::MiddlewareTracing

#_nr_has_middleware_tracing, #build_transaction_options, #call, #capture_http_response_code, #capture_response_content_type, #events, #merge_first_middleware_options, #note_transaction_started, #sinatra_static?

Constructor Details

This class inherits a constructor from TingYun::AgentMiddleware

Instance Method Details

#auto_instrument_source(response, js_to_inject) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 106

def auto_instrument_source(response, js_to_inject)
  source = gather_source(response)
  close_old_response(response)
  return nil unless source

  beginning_of_source = source[0..SCAN_LIMIT]
  insertion_index = find_tag_end(beginning_of_source)

  if insertion_index
    source = source[0...insertion_index] <<
        js_to_inject <<
        source[insertion_index..-1]
  else
    TingYun::Agent.logger.debug "Skipping RUM instrumentation. Could not properly determine location to inject script."
  end

  source
rescue => e
  TingYun::Agent.logger.debug "Skipping RUM instrumentation on exception.", e
  nil
end

#browser_timing_config(state) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 92

def browser_timing_config(state)
  timings = state.timings

  data = {
      :id => TingYun::Agent.config[:tingyunIdSecret],
      :n => state.transaction_name ,
      :a => timings.app_time_in_millis,
      :q => timings.queue_time_in_millis,
      :tid => state.trace_id
  }
  data
end

#close_old_response(response) ⇒ Object



134
135
136
137
138
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 134

def close_old_response(response)
  if response.respond_to?(:close)
    response.close
  end
end

#find_tag_end(beginning_of_source) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 140

def find_tag_end(beginning_of_source)
  tag_end = beginning_of_source.index(TITLE_END) ||
      beginning_of_source.index(HEAD_END) ||
      beginning_of_source.index(TITLE_END_CAPITAL) ||
      beginning_of_source.index(HEAD_END_CAPITAL)

  beginning_of_source.index(GT, tag_end) + 1 if tag_end
end

#gather_source(response) ⇒ Object



128
129
130
131
132
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 128

def gather_source(response)
  source = nil
  response.each {|fragment| source ? (source << fragment.to_s) : (source = fragment.to_s)}
  source
end

#is_ajax?(env) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 75

def is_ajax?(env)
  env["HTTP_X_REQUESTED_WITH"].nil?
end

#is_attachment?(headers) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 79

def is_attachment?(headers)
  headers[CONTENT_DISPOSITION] && headers[CONTENT_DISPOSITION].include?(ATTACHMENT)
end

#is_html?(headers) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 71

def is_html?(headers)
  headers[CONTENT_TYPE] && headers[CONTENT_TYPE].include?(TEXT_HTML)
end


87
88
89
90
91
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 87

def manufacture_cookie
  state = TingYun::Agent::TransactionState.tl_get
  timings = state.timings
  "%7B%22id%22%3A%22#{TingYun::Support::Coerce.url_encode(TingYun::Agent.config[:tingyunIdSecret].to_s)}%22%2C%22n%22%3A%22#{TingYun::Support::Coerce.url_encode(state.transaction_name.to_s)}%22%2C%22tid%22%3A%22#{state.trace_id}%22%2C%22q%22%3A#{timings.queue_time_in_millis}%2C%22a%22%3A#{timings.app_time_in_millis}%7D"
end

#rum_enable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 83

def rum_enable?
  TingYun::Agent.config[:'nbs.rum.enabled']
end

#should_instrument?(env, status, headers) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 63

def should_instrument?(env, status, headers)
  status == 200 &&
      is_ajax?(env) &&
      !env[ALREADY_INSTRUMENTED_KEY] &&
      is_html?(headers) &&
      !is_attachment?(headers)
end

#traced_call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 28

def traced_call(env)
  result = @app.call(env)   # [status, headers, response]

  if should_instrument?(env, result[0], result[1])
    if rum_enable? # unsupport insert script
      if TingYun::Agent.config[:'nbs.rum.mix_enabled']
        result[1]["Set-Cookie"] = "TINGYUN_DATA=#{manufacture_cookie}"
        env[ALREADY_INSTRUMENTED_KEY] = true
        result
      else
        js_to_inject = TingYun::Instrumentation::Support::JavascriptInstrument.browser_timing_header
        if (js_to_inject != '')
          response_string = auto_instrument_source(result[2], js_to_inject)

          env[ALREADY_INSTRUMENTED_KEY] = true
          if response_string
            response = Rack::Response.new(response_string, result[0], result[1])
            response.finish
          else
            result
          end
        else
          result
        end
      end
    else
      result
    end
  else
    result
  end
end