Module: TingYun::Agent

Extended by:
Agent
Included in:
Agent
Defined in:
lib/ting_yun/agent.rb,
lib/ting_yun/agent/agent.rb,
lib/ting_yun/agent/database.rb,
lib/ting_yun/agent/datastore.rb,
lib/ting_yun/agent/dispatcher.rb,
lib/ting_yun/agent/transaction.rb,
lib/ting_yun/agent/class_methods.rb,
lib/ting_yun/instrumentation/rake.rb,
lib/ting_yun/agent/datastore/mongo.rb,
lib/ting_yun/agent/event/event_loop.rb,
lib/ting_yun/agent/instance_methods.rb,
lib/ting_yun/agent/transaction/apdex.rb,
lib/ting_yun/agent/transaction/trace.rb,
lib/ting_yun/agent/database/statement.rb,
lib/ting_yun/agent/database/obfuscator.rb,
lib/ting_yun/agent/event/event_listener.rb,
lib/ting_yun/agent/collector/sql_sampler.rb,
lib/ting_yun/agent/method_tracer_helpers.rb,
lib/ting_yun/agent/collector/stats_engine.rb,
lib/ting_yun/agent/instance_methods/start.rb,
lib/ting_yun/agent/threading/agent_thread.rb,
lib/ting_yun/agent/transaction/attributes.rb,
lib/ting_yun/agent/transaction/exceptions.rb,
lib/ting_yun/agent/transaction/trace_node.rb,
lib/ting_yun/agent/datastore/metric_helper.rb,
lib/ting_yun/support/http_clients/uri_util.rb,
lib/ting_yun/agent/instance_methods/connect.rb,
lib/ting_yun/agent/transaction/class_method.rb,
lib/ting_yun/agent/collector/error_collector.rb,
lib/ting_yun/agent/cross_app/cross_app_monitor.rb,
lib/ting_yun/agent/cross_app/cross_app_tracing.rb,
lib/ting_yun/agent/database/connection_manager.rb,
lib/ting_yun/agent/transaction/instance_method.rb,
lib/ting_yun/agent/collector/transaction_sampler.rb,
lib/ting_yun/agent/database/explain_plan_helpers.rb,
lib/ting_yun/agent/transaction/transaction_state.rb,
lib/ting_yun/agent/collector/sql_sampler/slow_sql.rb,
lib/ting_yun/agent/instance_methods/handle_errors.rb,
lib/ting_yun/agent/transaction/request_attributes.rb,
lib/ting_yun/agent/collector/middle_ware_collector.rb,
lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb,
lib/ting_yun/agent/transaction/traced_method_stack.rb,
lib/ting_yun/agent/transaction/transaction_metrics.rb,
lib/ting_yun/agent/transaction/transaction_timings.rb,
lib/ting_yun/agent/collector/stats_engine/stats_hash.rb,
lib/ting_yun/agent/cross_app/inbound_request_monitor.rb,
lib/ting_yun/agent/collector/stats_engine/metric_stats.rb,
lib/ting_yun/agent/instance_methods/start_worker_thread.rb,
lib/ting_yun/agent/transaction/transaction_sample_builder.rb,
lib/ting_yun/agent/collector/error_collector/noticed_error.rb,
lib/ting_yun/agent/collector/middle_ware_collector/sampler.rb,
lib/ting_yun/agent/instance_methods/container_data_manager.rb,
lib/ting_yun/agent/collector/stats_engine/base_quantile_hash.rb,
lib/ting_yun/agent/collector/sql_sampler/transaction_sql_data.rb,
lib/ting_yun/agent/collector/transaction_sampler/class_method.rb,
lib/ting_yun/agent/collector/error_collector/error_trace_array.rb,
lib/ting_yun/agent/collector/middle_ware_collector/cpu_sampler.rb,
lib/ting_yun/agent/collector/middle_ware_collector/middle_ware.rb,
lib/ting_yun/agent/collector/middle_ware_collector/memory_sampler.rb,
lib/ting_yun/agent/collector/transaction_sampler/slowest_sample_buffer.rb,
lib/ting_yun/agent/collector/transaction_sampler/transaction_sample_buffer_base.rb

Defined Under Namespace

Modules: ClassMethods, Collector, CrossAppTracing, Database, Datastore, Event, HTTPClients, InstanceMethods, Instrumentation, MethodTracerHelpers, Threading Classes: Agent, CrossAppMonitor, Dispatcher, InboundRequestMonitor, TracedMethodFrame, TracedMethodStack, Transaction, TransactionMetrics, TransactionSampleBuilder, TransactionState, TransactionTimings

Constant Summary collapse

UNKNOWN_METRIC =
'(unknown)'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/ting_yun/agent.rb', line 19

def config
  @config
end

Instance Method Details

#add_custom_params(key, value) ⇒ Object

if you wanna call the method, you must make sure current_transaction is not nil at first if current_transaction

add_custom_params(:key1,:value1)
add_custom_params(:key2,:value2)

end public api



200
201
202
203
# File 'lib/ting_yun/agent.rb', line 200

def add_custom_params(key, value)
  txn = TingYun::Agent::TransactionState.tl_get.current_transaction
  txn.attributes.add_custom_params(key, value) if txn
end

#after_fork(options = {}) ⇒ Object

Register this method as a callback for processes that fork jobs.

If the master/parent connects to the agent prior to forking the agent in the forked process will use that agent_run. Otherwise the forked process will establish a new connection with the server.

Use this especially when you fork the process to run background jobs or other work. If you are doing this with a web dispatcher that forks worker processes then you will need to force the agent to reconnect, which it won’t do by default. Passenger and Rainbows and Unicorn are already handled, nothing special needed for them.

Options:

  • :force_reconnect => true to force the spawned process to establish a new connection, such as when forking a long running process. The default is false–it will only connect to the server if the parent had not connected.

  • :keep_retrying => false if we try to initiate a new connection, this tells me to only try it once so this method returns quickly if there is some kind of latency with the server.



158
159
160
# File 'lib/ting_yun/agent.rb', line 158

def after_fork(options={})
  agent.after_fork(options) if agent
end

#agentObject Also known as: instance



23
24
25
26
27
# File 'lib/ting_yun/agent.rb', line 23

def agent
  return @agent if @agent
  TingYun::Agent.logger.warn("Agent unavailable as it hasn't been started.")
  nil
end

#agent=(new_instance) ⇒ Object



31
32
33
# File 'lib/ting_yun/agent.rb', line 31

def agent=(new_instance)
  @agent = new_instance
end

#disable_all_tracingObject

Yield to the block without collecting any metrics or traces in any of the subsequent calls. If executed recursively, will keep track of the first entry point and turn on tracing again after leaving that block. This uses the thread local TransactionState.



170
171
172
173
174
175
176
177
178
# File 'lib/ting_yun/agent.rb', line 170

def disable_all_tracing
  return yield unless agent
  begin
    agent.push_trace_execution_flag(false)
    yield
  ensure
    agent.pop_trace_execution_flag
  end
end

#loggerObject



36
37
38
# File 'lib/ting_yun/agent.rb', line 36

def logger
  @logger || ::TingYun::Logger::StartupLogger.instance
end

#logger=(log) ⇒ Object



40
41
42
# File 'lib/ting_yun/agent.rb', line 40

def logger=(log)
  @logger = log
end

#manual_start(options = {}) ⇒ Object

Call this to manually start the Agent in situations where the Agent does not auto-start.

When the app environment loads, so does the Agent. However, the Agent will only connect to the service if a web front-end is found. If you want to selectively monitor ruby processes that don’t use web plugins, then call this method in your code and the Agent will fire up and start reporting to the service.

Options are passed in as overrides for values in the tingyun.yml, such as app_name. In addition, the option log will take a logger that will be used instead of the standard file logger. The setting for the tingyun.yml section to use (ie, RAILS_ENV) can be overridden with an :env argument.



95
96
97
98
# File 'lib/ting_yun/agent.rb', line 95

def manual_start(options={})
  raise "Options must be a hash" unless Hash === options
  TingYun::Frameworks.init_start({ :'nbs.agent_enabled' => true, :sync_startup => true }.merge(options))
end

#notice_error(exception, options = {:type =>:exception}) ⇒ Object

Notice the error with the given available options:

  • :uri => Request path, minus request params or query string

  • :metric => The metric name associated with the transaction

  • :custom_params => Custom parameters



126
127
128
129
# File 'lib/ting_yun/agent.rb', line 126

def notice_error(exception, options={:type =>:exception})
  TingYun::Agent::Transaction.notice_error(exception, options)
  nil # don't return a noticed error datastructure. it can only hurt.
end

#record_metric(metric_name, value, is_scoped = false) ⇒ Object

Record a value for the given metric name.

This method should be used to record event-based metrics such as method calls that are associated with a specific duration or magnitude.

metric_name should follow a slash separated path convention. Application specific metrics should begin with “Custom/”.

value should be either a single Numeric value representing the duration/ magnitude of the event being recorded, or a Hash containing :count, :total, :min, :max, and :sum_of_squares keys. The latter form is useful for recording pre-aggregated metrics collected externally.

This method is safe to use from any thread.



66
67
68
69
70
71
72
73
74
# File 'lib/ting_yun/agent.rb', line 66

def record_metric(metric_name, value, is_scoped=false ) #THREAD_LOCAL_ACCESS
  return unless agent
  stats = TingYun::Metrics::Stats.create_from_hash(value) if value.is_a?(Hash)
  if is_scoped
    agent.stats_engine.tl_record_scoped_metrics(metric_name, stats || value)
  else
    agent.stats_engine.tl_record_unscoped_metrics(metric_name, stats || value)
  end
end

#reset_configObject



44
45
46
# File 'lib/ting_yun/agent.rb', line 44

def reset_config
  @config.reset_to_defaults
end

#shutdownObject

Shutdown the agent. Call this before exiting. Sends any queued data and kills the background thread.

Parameters:

  • options (Hash)

    Unused options Hash, for back compatibility only



190
191
192
# File 'lib/ting_yun/agent.rb', line 190

def shutdown
  agent.shutdown if agent
end

#tl_is_execution_traced?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/ting_yun/agent.rb', line 205

def tl_is_execution_traced?
  TingYun::Agent::TransactionState.tl_get.execution_traced?
end

#with_database_metric_name(model, method = nil, product = nil, &block) ⇒ Object

Yield to a block that is run with a database metric name context. This means the Database instrumentation will use this for the metric name if it does not otherwise know about a model. This is re-entrant.

identify the operation with.

Parameters:

  • model (String, Class, #to_s)

    the DB model class

  • method (String) (defaults to: nil)

    the name of the finder method or other method to



109
110
111
112
113
114
115
# File 'lib/ting_yun/agent.rb', line 109

def with_database_metric_name(model, method = nil, product = nil, &block) #THREAD_LOCAL_ACCESS
  if txn = TingYun::Agent::TransactionState.tl_get.current_transaction
    txn.with_database_metric_name(model, method, product, &block)
  else
    yield
  end
end