Class: TingYun::BrowserMonitoring
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"
Instrumentation::MiddlewareTracing::TXN_STARTED_KEY
Instance Attribute Summary
#category, #target, #transaction_options
Instance Method Summary
collapse
event_params, float, int, int_or_nil, log_failure, string, url_encode
#build_transaction_name, #initialize
#_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?
Instance Method Details
#auto_instrument_source(response, js_to_inject) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 107
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
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 93
def browser_timing_config(state)
timings = state.timings
data = {
:id => TingYun::Agent.config[:idSecret],
: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
135
136
137
138
139
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 135
def close_old_response(response)
if response.respond_to?(:close)
response.close
end
end
|
#find_tag_end(beginning_of_source) ⇒ Object
141
142
143
144
145
146
147
148
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 141
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
129
130
131
132
133
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 129
def gather_source(response)
source = nil
response.each {|fragment| source ? (source << fragment.to_s) : (source = fragment.to_s)}
source
end
|
#is_ajax?(env) ⇒ 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
#is_html?(headers) ⇒ Boolean
#manufacture_cookie ⇒ Object
#rum_enable? ⇒ 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
63
64
65
66
67
68
69
|
# File 'lib/ting_yun/middleware/browser_monitoring.rb', line 63
def should_instrument?(env, status, )
status == 200 &&
is_ajax?(env) &&
!env[ALREADY_INSTRUMENTED_KEY] &&
is_html?() &&
!is_attachment?()
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)
if should_instrument?(env, result[0], result[1])
if rum_enable? 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.
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
|