# encoding: utf-8 require 'one_apm/probe' require 'one_apm/support/collection_helper' require 'one_apm/support/timer_lib' require 'one_apm/support/json_wrapper' require 'one_apm/support/chained_call' require 'one_apm/support/rules_engine' require 'one_apm/support/http_clients/uri_util' require 'one_apm/support/system_info' require 'one_apm/metrics/metric_spec' require 'one_apm/metrics/metric_data' require 'one_apm/metrics/stats' require 'one_apm/errors/noticed_error' require 'one_apm/errors/agent_errors' require 'one_apm/agent' require 'one_apm/agent/cross_app/cross_app_monitor' require 'one_apm/support/method_tracer' require 'one_apm/support/event/worker_loop' require 'one_apm/support/event/event_loop' require 'one_apm/transaction/transaction_sample' require 'one_apm/collector/containers/stats_engine' require 'one_apm/collector/containers/transaction_sampler' require 'one_apm/collector/containers/sql_sampler' require 'one_apm/collector/containers/error_collector' require 'one_apm/agent/busy_calculator' require 'one_apm/agent/database' require 'one_apm/support/forked_process_channel' require 'one_apm/configuration' require 'one_apm/agent/sampler' require 'one_apm/agent/samplers/cpu_sampler' require 'one_apm/agent/samplers/memory_sampler' require 'one_apm/agent/samplers/object_sampler' require 'one_apm/agent/samplers/delayed_job_sampler' require 'one_apm/agent/samplers/vm_sampler' module OneApm module Manager extend self @agent = nil def agent return @agent if @agent OneApm::Manager.logger.warn("Agent unavailable as it hasn't been started.") OneApm::Manager.logger.warn(caller.join("\n")) nil end def agent=(new_agent) @agent = new_agent end def start(options = {}) raise "Options must be a hash" unless Hash === options OneApm::Support::ForkedProcessChannel.listener.start if options[:start_channel_listener] OneApm::Probe.init({:agent_enabled => true, :sync_startup => true}.merge(options)) end alias_method :manual_start, :start def shutdown(options={}) agent.shutdown(options) if agent end def restart shutdown agent.harvest_samplers.clear agent.agent_variable_set(:@connect_state, :pending) agent.agent_variable_set(:@worker_thread, nil) agent.harvester.agent_variable_set(:@starting_pid, nil) OneApm::Probe.init({:agent_enabled => true, :sync_startup => true}) end def logger @logger || OneApm::Logger::StartupLogger.instance end def logger=(log) @logger = log end def config @config ||= OneApm::Configuration::Manager.new end def reset_config @config.reset_to_defaults end def revert_to_default_configuration @config.remove_config_type(:manual) @config.remove_config_type(:server) end def after_fork(options = {}) agent.after_fork(options) end def record_metric(metric_name, value) value_to_store = value value_to_store = OneApm::Metrics::Stats.create_from_hash(value) if value.is_a?(Hash) agent.stats_engine.tl_record_unscoped_metrics(metric_name, value_to_store) end def increment_metric(metric_name, amount = 1) agent.stats_engine.tl_record_unscoped_metrics(metric_name) do |stats| stats.increment_count(amount) end end def drop_buffered_data agent.drop_buffered_data end def add_instrumentation(file_pattern) OneApm::Probe.instance.add_instrumentation file_pattern end def set_sql_obfuscator(type = :replace, &block) OneApm::Agent::Database.set_sql_obfuscator(type, &block) end def disable_sql_recording state = agent.set_record_sql(false) begin yield ensure agent.set_record_sql(state) end end def disable_transaction_tracing state = agent.set_record_tt(false) begin yield ensure agent.set_record_tt(state) end end def ignore_transaction txn = OneApm::Transaction.tl_current txn.ignore! if txn end def ignore_apdex txn = OneApm::Transaction.tl_current txn.ignore_apdex! if txn end def ignore_enduser txn = OneApm::Transaction.tl_current txn.ignore_enduser! if txn end def disable_all_tracing agent.push_trace_execution_flag(false) yield ensure agent.pop_trace_execution_flag end def record_custom_event(event_type, event_attrs) if agent && OneApm::Manager.config[:'custom_insights_events.enabled'] agent.custom_event_aggregator.record(event_type, event_attrs) end nil end def tl_is_execution_traced? OneApm::TransactionState.tl_get.is_execution_traced? end def tl_is_transaction_traced? OneApm::TransactionState.tl_get.is_transaction_traced? end def tl_is_sql_recorded? OneApm::TransactionState.tl_get.is_sql_recorded? end def ignore_error_filter(&block) if block OneApm::Collector::ErrorCollector.ignore_error_filter = block else OneApm::Collector::ErrorCollector.ignore_error_filter end end def notice_error(exception, options = {}) Transaction.notice_error(exception, options) nil end def notice_sql(sql, metric, config, elapsed_time, state, &explainer) agent.transaction_sampler.notice_sql(sql, config, elapsed_time, state, &explainer) agent.sql_sampler.notice_sql(sql, metric, config, elapsed_time, state, &explainer) end def add_custom_parameters(params) if params.is_a? Hash txn = Transaction.tl_current txn.add_custom_parameters(params) if txn else ::OneApm::Manager.logger.warn("Bad argument passed to #add_custom_parameters. Expected Hash but got #{params.class}") end end def set_transaction_name(name, options={}) Transaction.set_overriding_transaction_name(name, options[:category]) end def get_transaction_name txn = Transaction.tl_current if txn txn.best_name.sub(Regexp.new("\\A#{Regexp.escape(OneApm::TransactionNamer.prefix_for_category(txn))}"), '') end end def subscribe(event_type, &handler) agent.events.subscribe( event_type, &handler ) end def notify(event_type, *args) agent.events.notify( event_type, *args ) rescue OneApm::Manager.logger.debug "Ignoring exception during %p event notification" % [event_type] end def browser_timing_header agent.javascript_instrumentor.browser_timing_header end def require_test_helper path = File.join(__FILE__, '..', '..', '..', 'test', 'agent_helper') require File.expand_path(path) end end end