# encoding: utf-8 module OneApm module Agent module Datastore module MetricHelper OA_ROLLUP_METRIC = "Datastore/all".freeze OA_WEB_ROLLUP_METRIC = "Datastore/allWeb".freeze OA_OTHER_ROLLUP_METRIC = "Datastore/allOther".freeze OA_DEFAULT_PRODUCT_NAME = "ActiveRecord".freeze OA_OTHER = "Other".freeze OA_ALL = "all".freeze OA_ALL_WEB = "allWeb".freeze OA_ALL_OTHER = "allOther".freeze # Datastore/operation/NoSqlType:10.128.6.240:27017/update def self.operation_metric_for(product, operation) "Datastore/operation/#{product}/#{operation}" end #Datastore/operation/NoSqlType:10.128.6.240:27017/all def self.operation_product_rollup(product) "Datastore/operation/#{product}/all" end #Datastore/operation/NoSqlType def self.operation_all(product) "Datastore/operation/#{product.split(':')[0]}" end def self.product_suffixed_rollup(product, suffix) "Datastore/#{product}/#{suffix}" end def self.product_rollup(product) "Datastore//#{product}/all" end def self.suffixed_rollup(suffix) "Datastore/#{suffix}" end #Datastore/statement/NoSqlType:10.128.6.240:27017/book/insert def self.statement_metric_for(product, collection, operation) "Datastore/statement/#{product}/#{collection}/#{operation}" end #Datastore/statement/NoSqlType:10.128.6.240:27017/book/insert def self.statement_action_metric_for(product, operation) "Datastore/statement/#{product}/#{operation}" end #Datastore/statement/NoSqlType:10.128.6.240:27017/all def self.statement_all_metric_for(product) "Datastore/statement/#{product}/all" end #Datastore/statement/NoSqlType def self.statement_all(product) "Datastore/statement/#{product.split(':')[0]}" end def self.all_suffix if OneApm::Transaction.recording_web_transaction? OA_ALL_WEB else OA_ALL_OTHER end end def self.metrics_for(product, operation, collection = nil) if overrides = overridden_operation_and_collection operation, collection = overrides end suffix = all_suffix # Order of these metrics matters--the first metric in the list will # be treated as the scoped metric in a bunch of different cases. metrics = [ operation_metric_for(product, operation), operation_product_rollup(product), operation_all(product), product_suffixed_rollup(product, suffix), product_rollup(product), suffixed_rollup(suffix), OA_ROLLUP_METRIC ] metrics.unshift statement_metric_for(product, collection, operation) if collection metrics.unshift statement_action_metric_for(product,operation) metrics.unshift statement_all_metric_for(product) metrics.unshift statement_all(product) metrics end def self.metrics_from_sql(product, sql) operation = OneApm::Agent::Database.parse_operation_from_query(sql) || OA_OTHER metrics_for(product, operation) end # Allow Transaction#with_database_metric_name to override our # collection and operation def self.overridden_operation_and_collection state = OneApm::TransactionState.tl_get txn = state.current_transaction txn ? txn.instrumentation_state[:datastore_override] : nil end end end end end