This module contains most of the public API methods for the Ruby Agent.
For adding custom instrumentation to method invocations, see the docs for {NewRelic::Agent::MethodTracer} and {NewRelic::Agent::MethodTracer::ClassMethods}.
For information on how to trace transactions in non-Rack contexts, see {NewRelic::Agent::Instrumentation::ControllerInstrumentation}.
For general documentation about the Ruby agent, see: docs.newrelic.com/docs/agents/ruby-agent
@api public
| LOG_LEVELS | = | { "debug" => ::Logger::DEBUG, "info" => ::Logger::INFO, "warn" => ::Logger::WARN, "error" => ::Logger::ERROR, "fatal" => ::Logger::FATAL, } | ||
| SUPPORTED_VERSIONS | = | { # Rubies :mri => { :type => :ruby, :name => "MRI", :supported => ["1.8.7", "1.9.2", "1.9.3", "2.0.0", "~> 2.1.0", "~> 2.2.0", "~> 2.3.0"], :experimental=> ["2.4.0-preview3"], :url => "https://www.ruby-lang.org", :feed => "https://www.ruby-lang.org/en/feeds/news.rss", :notes => [ "1.8.7 includes support for Ruby Enterprise Edition (REE).", "1.8.7 & REE require the 'json' gem to be present in your Gemfile/operating environment.", "Last supported agent on 1.8.6 was 3.6.8.168."] | ||
| UNKNOWN_METRIC | = | '(unknown)'.freeze | placeholder name used when we cannot determine a transaction‘s name | |
| ADD_CUSTOM_ATTRIBUTES | = | "NewRelic::Agent.add_custom_attributes".freeze | ||
| ADD_CUSTOM_PARAMETERS | = | "NewRelic::Agent.add_custom_parameters".freeze | ||
| ADD_REQUEST_PARAMETERS | = | "NewRelic::Agent.add_request_parameters".freeze | ||
| SET_USER_ATTRIBUTES | = | "NewRelic::Agent.set_user_attributes".freeze |
| agent | -> | instance |
| config | [R] |
Cancel the collection of the current transaction in progress, if any. Only affects the transaction started on this thread once it has started and before it has completed.
This method has been deprecated in favor of ignore_transaction, which does what people expect this method to do.
@api public @deprecated
Add attributes to the transaction trace, Insights Transaction event, and any traced errors recorded for the current transaction.
If Browser Monitoring is enabled, and the browser_monitoring.attributes.enabled configuration setting is true, these custom attributes will also be present in the script injected into the response body, making them available on Insights PageView events.
@api public
Add instrumentation files to the agent. The argument should be a glob matching ruby scripts which will be executed at the time instrumentation is loaded. Since instrumentation is not loaded when the agent is not running it‘s better to use this method to register instrumentation than just loading the files directly, although that probably also works.
@api public
Deprecated. Use add_custom_attributes instead.
@deprecated @api public
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:
@api public
In previous agent releases, this method was required for manual RUM instrumentation. That work is now all done by the browser_timing_header method, but this is left for compatibility.
@api public @deprecated
This method returns a string suitable for inclusion in a page - known as ‘manual instrumentation’ for Real User Monitoring. Can return either a script tag with associated javascript, or in the case of disabled Real User Monitoring, an empty string
This is the header string - it should be placed as high in the page as is reasonably possible - that is, before any style or javascript inclusions, but after any header-related meta tags
In previous agents there was a corresponding footer required, but all the work is now done by this single method.
@api public
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.
@api public
This method sets the state of sql recording in the transaction sampler feature. Within the given block, no sql will be recorded
usage:
NewRelic::Agent.disable_sql_recording do
...
end
@api public
This method disables the recording of transaction traces in the given block. See also disable_all_tracing
@api public
Get or create a statistics gatherer that will aggregate numerical data under a metric name.
metric_name should follow a slash separated path convention. Application specific metrics should begin with "Custom/".
Return a NewRelic::Agent::Stats that accepts data via calls to add_data_point(value).
This method is deprecated in favor of record_metric and increment_metric, and is not thread-safe.
@api public @deprecated
Get the name of the current running transaction. This is useful if you want to modify the default name.
@api public
This method disables browser monitoring javascript injection in the current transaction.
@api public
Set a filter to be applied to errors that the Ruby Agent will track. The block should evalute to the exception to track (which could be different from the original exception) or nil to ignore this exception.
The block is yielded to with the exception to filter.
Return the new block or the existing filter Proc if no block is passed.
@api public
This method disables the recording of the current transaction. No metrics, traced errors, transaction traces, Insights events, slow SQL traces, or RUM injection will happen for this transaction.
@api public
Increment a simple counter metric.
metric_name should follow a slash separated path convention. Application specific metrics should begin with "Custom/".
This method is safe to use from any thread.
@api public
Primary interface to logging is fronted by this accessor Access via ::NewRelic::Agent.logger
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 newrelic.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 newrelic.yml section to use (ie, RAILS_ENV) can be overridden with an :env argument.
@api public
Send an error to New Relic.
@param [Exception] exception Error you wish to send @param [Hash] options Modify how New Relic processes the error @option options [Hash] :custom_params Custom parameters to attach to the trace @option options [Boolean] :trace_only Only record the error trace
(do not affect error rate or Apdex status)
@option options [String] :uri Request path, minus request params or query string
(usually not needed)
@option options [String] :metric Metric name associated with the transaction
(usually not needed)
Any option keys other than the ones listed here are treated as :custom_params.
*Note:* Previous versions of the agent allowed passing :request_params, but those are now ignored. If you need to record the request parameters, call this method inside a transaction or pass the information in :custom_params.
Most of the time, you do not need to specify the :uri or :metric options; only pass them if you are calling notice_error outside a transaction.
@api public
Record a custom event to be sent to New Relic Insights. The recorded event will be buffered in memory until the next time the agent sends data to New Relic‘s servers.
If you want to be able to tie the information recorded via this call back to the web request or background job that it happened in, you may want to instead use the add_custom_attributes API call to attach attributes to the Transaction event that will automatically be generated for the request.
A timestamp will be automatically added to the recorded event when this method is called.
@param [Symbol or String] event_type The name of the event type to record. Event
types must consist of only alphanumeric
characters, '_', ':', or ' '.
@param [Hash] event_attrs A Hash of attributes to be attached to the event.
Keys should be strings or symbols, and values
may be strings, symbols, numeric values or
booleans.
@api public
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.
@api public
This method sets the block sent to this method as a sql obfuscator. The block will be called with a single String SQL statement to obfuscate. The method must return the obfuscated String SQL. If chaining of obfuscators is required, use type = :before or :after
type = :before, :replace, :after
Example:
NewRelic::Agent.set_sql_obfuscator(:replace) do |sql|
my_obfuscator(sql)
end
@api public
Set the name of the current running transaction. The agent will apply a reasonable default based on framework routing, but in cases where this is insufficient, this can be used to manually control the name of the transaction. The category of transaction can be specified via the +:category+ option:
The default category is the same as the running transaction.
@api public
Shutdown the agent. Call this before exiting. Sends any queued data and kills the background thread.
@param options [Hash] Unused options Hash, for back compatibility only
@api public
helper method to check the thread local to determine whether the transaction in progress is traced or not
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.
@param [String,Class,to_s] model the DB model class
@param [String] method the name of the finder method or other method to identify the operation with.