# encoding: utf-8 require 'one_apm/agent/datastore/metric_helper' module OneApm module Agent module Datastore LOCALHOST = 'localhost'.freeze LOCAL_127 = '127.0.0.1'.freeze def self.oneapm_product *product return product[0] if product[1].nil? product.compact.join(":").gsub(LOCALHOST, LOCAL_127) end def self.trace(clazz, method_name, product, operation = method_name) clazz.class_eval do method_name_without_oneapm = "#{method_name}_without_oneapm" if OneApm::Helper.instance_methods_include?(clazz, method_name) && !OneApm::Helper.instance_methods_include?(clazz, method_name_without_oneapm) visibility = OneApm::Helper.instance_method_visibility(clazz, method_name) alias_method method_name_without_oneapm, method_name define_method(method_name) do |*args, &blk| metrics = MetricHelper.metrics_for(product, operation) OneApm::Support::MethodTracer.trace_execution_scoped(metrics) do send(method_name_without_oneapm, *args, &blk) end end send visibility, method_name send visibility, method_name_without_oneapm end end end def self.wrap(product, operation, collection = nil, callback = nil) return yield unless operation metrics = MetricHelper.metrics_for(product, operation, collection) scoped_metric = metrics.first OneApm::Support::MethodTracer.trace_execution_scoped(metrics) do t0 = Time.now begin result = yield ensure if callback elapsed_time = (Time.now - t0).to_f callback.call(result, scoped_metric, elapsed_time) end end end end def self.notice_sql(query, scoped_metric, elapsed) agent = OneApm::Manager.agent agent.transaction_sampler.notice_sql(query, nil, elapsed) agent.sql_sampler.notice_sql(query, scoped_metric, nil, elapsed) nil end def self.notice_statement(statement, elapsed) return unless OneApm::Agent::Database.should_record_sql? agent = OneApm::Manager.agent agent.transaction_sampler.notice_nosql_statement(statement, elapsed) nil end end end end