# 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 'find', 'load', 'count', 'exists' op_name = 'select' when 'destroy' op_name = 'delete' when 'create' op_name = 'insert' when 'update', 'save' op_name = 'update' else op_name = nil end "Database/#{model}/#{op_name}" if op_name end end def metric_for_sql(sql) 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 "Database/model/action" this # returns an array of rollup metrics: # [ "Database/all", "Database/all", "Database/action" ] # If the metric name is in the form of "Database/action" # this returns merely: [ "Database/all", "Database/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 << "Database/allWeb" else metrics << "Database/allOther" end metrics << "Database/#{$1}" if metric =~ /Database\/[\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