require 'base64' require 'new_relic/agent/beacon_configuration' module NewRelic module Agent module BrowserMonitoring def browser_timing_header return "" if NewRelic::Agent.instance.beacon_configuration.nil? return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced? NewRelic::Agent.instance.beacon_configuration.browser_timing_header end def browser_timing_footer config = NewRelic::Agent.instance.beacon_configuration return "" if config.nil? || !config.rum_enabled || config.browser_monitoring_key.nil? return "" if !NewRelic::Agent.is_transaction_traced? || !NewRelic::Agent.is_execution_traced? generate_footer_js end private def generate_footer_js if browser_monitoring_start_time config = NewRelic::Agent.instance.beacon_configuration application_id = config.application_id beacon = config.beacon license_key = config.browser_monitoring_key footer_js_string(beacon, license_key, application_id) else '' end end def browser_monitoring_transaction_name Thread.current[:newrelic_most_recent_transaction] || "" end def browser_monitoring_start_time Thread.current[:newrelic_start_time] end def clamp_to_positive(value) return 0.0 if value < 0 value end def browser_monitoring_app_time clamp_to_positive(((Time.now - browser_monitoring_start_time).to_f * 1000.0).round) end def browser_monitoring_queue_time clamp_to_positive((Thread.current[:newrelic_queue_time].to_f * 1000.0).round) end def footer_js_string(beacon, license_key, application_id) obfuscated_transaction_name = obfuscate(browser_monitoring_transaction_name) html_safe_if_needed("") end def html_safe_if_needed(string) if string.respond_to?(:html_safe) string.html_safe else string end end def obfuscate(text) obfuscated = "" key_bytes = NewRelic::Agent.instance.beacon_configuration.license_bytes index = 0 text.each_byte{|byte| obfuscated.concat((byte ^ key_bytes[index % 13].to_i)) index+=1 } [obfuscated].pack("m0").gsub("\n", '') end end end end