# encoding: utf-8 require 'base64' require 'one_apm/support/obfuscator' require 'one_apm/transaction/transaction_timings' module OneApm module Agent class JavascriptInstrumentor include OneApm::Coerce RUM_KEY_LENGTH = 13 def initialize(event_listener) event_listener.subscribe(:finished_configuring, &method(:log_configuration)) end def log_configuration OneApm::Manager.logger.debug("JS agent loader requested: #{OneApm::Manager.config[:'browser_monitoring.loader']}", "JS agent loader debug: #{OneApm::Manager.config[:'browser_monitoring.debug']}", "JS agent loader version: #{OneApm::Manager.config[:'browser_monitoring.loader_version']}") if !OneApm::Manager.config[:'rum.enabled'] OneApm::Manager.logger.debug("Real User Monitoring is disabled for this agent. Edit your configuration to change this.") end end def enabled? OneApm::Manager.config[:'rum.enabled'] && !!Manager.config[:beacon] end def obfuscator @obfuscator ||= OneApm::Agent::Obfuscator.new(OneApm::Manager.config[:license_key], RUM_KEY_LENGTH) end def js_enabled_and_ready? if !enabled? OneApm::Manager.logger.log_once(:debug, :js_agent_disabled, "JS agent instrumentation is disabled.") false elsif missing_config?(:js_agent_loader) OneApm::Manager.logger.log_once(:debug, :missing_js_agent_loader, "Missing :js_agent_loader. Skipping browser instrumentation.") false elsif missing_config?(:beacon) OneApm::Manager.logger.log_once(:debug, :missing_beacon, "Beacon configuration not received (yet?). Skipping browser instrumentation.") false elsif missing_config?(:browser_key) OneApm::Manager.logger.log_once(:debug, :missing_browser_key, "Browser key is not set. Skipping browser instrumentation.") false else true end rescue => e OneApm::Manager.logger.debug "Failure during 'js_enabled_and_ready?'", e false end def insert_js?(state) if !state.current_transaction OneApm::Manager.logger.debug "Not in transaction. Skipping browser instrumentation." false elsif !state.is_transaction_traced? OneApm::Manager.logger.debug "Transaction is not traced. Skipping browser instrumentation." false elsif !state.is_execution_traced? OneApm::Manager.logger.debug "Execution is not traced. Skipping browser instrumentation." false elsif state.current_transaction.ignore_enduser? OneApm::Manager.logger.debug "Ignore end user for this transaction is set. Skipping browser instrumentation." false else true end rescue => e OneApm::Manager.logger.debug "Failure during insert_js", e false end def missing_config?(key) value = OneApm::Manager.config[key] value.nil? || value.empty? end def browser_timing_header return '' unless js_enabled_and_ready? # fast exit state = OneApm::TransactionState.tl_get return '' unless insert_js?(state) # slower exit bt_config = browser_timing_config(state) return '' if bt_config.empty? bt_config + browser_timing_loader rescue => e OneApm::Manager.logger.debug "Failure during RUM browser_timing_header construction", e '' end def browser_timing_loader html_safe_if_needed("\n") end def browser_timing_config(state) txn = state.current_transaction return '' if txn.nil? txn.freeze_name_and_execute_if_not_ignored do data = data_for_js_agent(state) json = OneApm::JSONWrapper.dump(data) return html_safe_if_needed("\n") end '' end BEACON_KEY = "beacon".freeze ERROR_BEACON_KEY = "errorBeacon".freeze LICENSE_KEY_KEY = "licenseKey".freeze APPLICATIONID_KEY = "applicationID".freeze TRANSACTION_NAME_KEY = "transactionName".freeze QUEUE_TIME_KEY = "queueTime".freeze APPLICATION_TIME_KEY = "applicationTime".freeze AGENT_KEY = "agent".freeze USER_ATTRIBUTES_KEY = "userAttributes".freeze SSL_FOR_HTTP_KEY = "sslForHttp".freeze # NOTE: Internal prototyping may override this, so leave name stable! def data_for_js_agent(state) timings = state.timings data = { BEACON_KEY => OneApm::Manager.config[:beacon], ERROR_BEACON_KEY => OneApm::Manager.config[:error_beacon], LICENSE_KEY_KEY => OneApm::Manager.config[:browser_key], APPLICATIONID_KEY => OneApm::Manager.config[:application_id], TRANSACTION_NAME_KEY => obfuscator.obfuscate(timings.transaction_name_or_unknown), QUEUE_TIME_KEY => timings.queue_time_in_millis, APPLICATION_TIME_KEY => timings.app_time_in_millis, AGENT_KEY => OneApm::Manager.config[:js_agent_file] } add_ssl_for_http(data) add_user_attributes(data, state.current_transaction) data end def add_ssl_for_http(data) ssl_for_http = OneApm::Manager.config[:'browser_monitoring.ssl_for_http'] unless ssl_for_http.nil? data[SSL_FOR_HTTP_KEY] = ssl_for_http end end def add_user_attributes(data, txn) return unless include_custom_parameters?(txn) params = event_params(txn.custom_parameters) json = OneApm::JSONWrapper.dump(params) data[USER_ATTRIBUTES_KEY] = obfuscator.obfuscate(json) end def include_custom_parameters?(txn) has_custom_parameters?(txn) && OneApm::Manager.config[:'browser_monitoring.capture_attributes'] end def has_custom_parameters?(txn) txn && txn.custom_parameters && !txn.custom_parameters.empty? end def html_safe_if_needed(string) string = string.html_safe if string.respond_to?(:html_safe) string end end end end