# encoding: utf-8 module OneApm module Agent module Instrumentation module ActiveRecordHelper module_function def metric_for_name(name) return unless name && name.respond_to?(:split) parts = name.split(' ') if parts.size == 2 model = parts.first operation = parts.last.downcase case operation when 'load', 'count', 'exists' op_name = 'find' when 'indexes', 'columns' op_name = nil # fall back to DirectSQL when 'destroy', 'find', 'save', 'create' op_name = operation when 'update' op_name = 'save' else if model == 'Join' op_name = operation end end "ActiveRecord/#{model}/#{op_name}" if op_name end end def metric_for_sql(sql) #THREAD_LOCAL_ACCESS txn = OneApm::Transaction.tl_current metric = txn && txn.database_metric_name if metric.nil? operation = OneApm::Agent::Database.parse_operation_from_query(sql) if operation # Could not determine the model/operation so use a fallback metric metric = "Database/SQL/#{operation}" else metric = "Database/SQL/other" end end metric end # Given a metric name such as "ActiveRecord/model/action" this # returns an array of rollup metrics: # [ "Database/all", "ActiveRecord/all", "ActiveRecord/action" ] # If the metric name is in the form of "ActiveRecord/action" # this returns merely: [ "Database/all", "ActiveRecord/all" ] def rollup_metrics_for(metric) metrics = ["Database/all"] # If we're outside of a web transaction, don't record any rollup # database metrics. This is to prevent metrics from background tasks # from polluting the metrics used to drive overview graphs. if OneApm::Transaction.recording_web_transaction? metrics << "ActiveRecord/all" else metrics << "Database/allOther" end metrics << "ActiveRecord/#{$1}" if metric =~ /ActiveRecord\/[\w|\:]+\/(\w+)/ metrics end # Given a database adapter name and a database server host # this returns a metric name in the form: # "RemoteService/sql/adapter/host" # Host defaults to "localhost". def remote_service_metric(adapter, host) host ||= 'localhost' type = adapter.to_s.sub(/\d*/, '') "RemoteService/sql/#{type}/#{host}" end end end end end