diff --git a/src/lib_shell/chain_validator.ml b/src/lib_shell/chain_validator.ml index 40c1eea79e85318f34f604a8f0d37d15e5ade88d..a385ac3aa89b1023eca3e64e0ecc27197964ca02 100644 --- a/src/lib_shell/chain_validator.ml +++ b/src/lib_shell/chain_validator.ml @@ -579,20 +579,19 @@ let collect_proto ~metrics (chain_store, block) = match metadata_opt with | None -> Lwt.return_unit | Some metadata -> - let open Shell_metrics.Proto_plugin in let protocol_metadata = Block_repr.block_metadata metadata in Lwt.catch (fun () -> (* Return Noop if the protocol does not exist, and UndefinedMetric if the plugin for the protocol does not exist *) - let* (module ProtoMetrics) = + let* (module Metrics_plugin) = let* protocol = Store.Block.protocol_hash_exn chain_store block in - safe_get_prevalidator_proto_metrics protocol + Shell_plugin.safe_find_metrics protocol in let fitness = Store.Block.fitness block in let* () = - ProtoMetrics.update_metrics + Metrics_plugin.update_metrics ~protocol_metadata fitness (Shell_metrics.Chain_validator.update_proto_metrics_callback diff --git a/src/lib_shell/shell_metrics.ml b/src/lib_shell/shell_metrics.ml index 70bb0de819ca631c63e9647d2fdc6400bb48f327..12c97ec0a8dd243318f7e65f83d21d6c0f2a8427 100644 --- a/src/lib_shell/shell_metrics.ml +++ b/src/lib_shell/shell_metrics.ml @@ -397,47 +397,6 @@ module Block_validator = struct } end -module Proto_plugin = struct - module type PROTOMETRICS = sig - val hash : Protocol_hash.t - - val update_metrics : - protocol_metadata:bytes -> - Fitness.t -> - (cycle:float -> consumed_gas:float -> round:float -> unit) -> - unit Lwt.t - end - - module UndefinedProtoMetrics (P : sig - val hash : Protocol_hash.t - end) = - struct - let hash = P.hash - - let update_metrics ~protocol_metadata:_ _ _ = Lwt.return_unit - end - - let proto_metrics_table : (module PROTOMETRICS) Protocol_hash.Table.t = - Protocol_hash.Table.create 5 - - let register_plugin (module ProtoMetrics : PROTOMETRICS) = - Protocol_hash.Table.replace - proto_metrics_table - ProtoMetrics.hash - (module ProtoMetrics) - - let find_plugin = Protocol_hash.Table.find proto_metrics_table - - let safe_get_prevalidator_proto_metrics hash = - match find_plugin hash with - | Some proto_metrics -> Lwt.return proto_metrics - | None -> - let module ProtoMetrics = UndefinedProtoMetrics (struct - let hash = hash - end) in - Lwt.return (module ProtoMetrics : PROTOMETRICS) -end - module Chain_validator = struct open Prometheus diff --git a/src/lib_shell/shell_metrics.mli b/src/lib_shell/shell_metrics.mli index 48380d43358588c71a020adec194d5b5c2d62cad..c1766349fefa203e986965169bbb884319869c68 100644 --- a/src/lib_shell/shell_metrics.mli +++ b/src/lib_shell/shell_metrics.mli @@ -83,37 +83,6 @@ module Block_validator : sig val set_operation_per_pass_collector : (unit -> float list) -> unit end -module Proto_plugin : sig - (* This is a protocol specific module that is used to collect all the - * protocol-specific metrics. It works using the protocol plugin system - * and it's very similar to the mempool filter plugin. This module - * allows to decode protocol data payload and provide back basic - * types that can be used as metrics. *) - module type PROTOMETRICS = sig - val hash : Protocol_hash.t - - val update_metrics : - protocol_metadata:bytes -> - Fitness.t -> - (cycle:float -> consumed_gas:float -> round:float -> unit) -> - unit Lwt.t - end - - (** Emtpy metrics module. All metrics are -1. *) - module UndefinedProtoMetrics (P : sig - val hash : Protocol_hash.t - end) : PROTOMETRICS - - (** Register a metrics plugin module *) - val register_plugin : (module PROTOMETRICS) -> unit - - (** Find a metrics plugin module associated to a protocol *) - val find_plugin : Protocol_hash.t -> (module PROTOMETRICS) option - - val safe_get_prevalidator_proto_metrics : - Protocol_hash.t -> (module PROTOMETRICS) Lwt.t -end - module Chain_validator : sig type t = { head_level : Prometheus.Gauge.t; diff --git a/src/lib_shell/shell_plugin.ml b/src/lib_shell/shell_plugin.ml index 6d40b4d69b353e0a7b65c5afe1f4fed601c3a0a8..bb159c7ce0140938507d9377d34b742f2db9ea2b 100644 --- a/src/lib_shell/shell_plugin.ml +++ b/src/lib_shell/shell_plugin.ml @@ -129,12 +129,34 @@ module No_filter (Proto : Registered_protocol.T) = struct end end +module type METRICS = sig + val hash : Protocol_hash.t + + val update_metrics : + protocol_metadata:bytes -> + Fitness.t -> + (cycle:float -> consumed_gas:float -> round:float -> unit) -> + unit Lwt.t +end + +module Undefined_metrics_plugin (Proto : sig + val hash : Protocol_hash.t +end) = +struct + let hash = Proto.hash + + let update_metrics ~protocol_metadata:_ _ _ = Lwt.return_unit +end + let filter_table : (module FILTER) Protocol_hash.Table.t = Protocol_hash.Table.create 5 let rpc_table : (module RPC) Protocol_hash.Table.t = Protocol_hash.Table.create 5 +let metrics_table : (module METRICS) Protocol_hash.Table.t = + Protocol_hash.Table.create 5 + let register_filter (module Filter : FILTER) = assert (not (Protocol_hash.Table.mem filter_table Filter.Proto.hash)) ; Protocol_hash.Table.add filter_table Filter.Proto.hash (module Filter) @@ -143,6 +165,20 @@ let register_rpc (module Rpc : RPC) = assert (not (Protocol_hash.Table.mem rpc_table Rpc.Proto.hash)) ; Protocol_hash.Table.add rpc_table Rpc.Proto.hash (module Rpc) +let register_metrics (module Metrics : METRICS) = + Protocol_hash.Table.replace metrics_table Metrics.hash (module Metrics) + let find_filter = Protocol_hash.Table.find filter_table let find_rpc = Protocol_hash.Table.find rpc_table + +let find_metrics = Protocol_hash.Table.find metrics_table + +let safe_find_metrics hash = + match find_metrics hash with + | Some proto_metrics -> Lwt.return proto_metrics + | None -> + let module Metrics = Undefined_metrics_plugin (struct + let hash = hash + end) in + Lwt.return (module Metrics : METRICS) diff --git a/src/lib_shell/shell_plugin.mli b/src/lib_shell/shell_plugin.mli index 4bf7f64aa03c16a12e0536fefe1c24c1d78ed4ef..70c7259c24c564da124aa8aa153229747a4ead3f 100644 --- a/src/lib_shell/shell_plugin.mli +++ b/src/lib_shell/shell_plugin.mli @@ -149,14 +149,42 @@ end module No_filter (Proto : Registered_protocol.T) : FILTER with module Proto = Proto +(** This is a protocol specific module that is used to collect all the + * protocol-specific metrics. This module + * allows to decode protocol data payload and provide back basic + * types that can be used as metrics. *) +module type METRICS = sig + val hash : Protocol_hash.t + + val update_metrics : + protocol_metadata:bytes -> + Fitness.t -> + (cycle:float -> consumed_gas:float -> round:float -> unit) -> + unit Lwt.t +end + +(** Emtpy metrics module. All metrics are -1. *) +module Undefined_metrics_plugin (P : sig + val hash : Protocol_hash.t +end) : METRICS + (** Registers a mempool filters plug-in for a specific protocol (according to its [Proto.hash]). *) val register_filter : (module FILTER) -> unit (** Registers a RPC plug-in for a specific protocol *) val register_rpc : (module RPC) -> unit +(** Register a metrics plugin module *) +val register_metrics : (module METRICS) -> unit + (** Looks for a mempool filter plug-in for a specific protocol. *) val find_filter : Protocol_hash.t -> (module FILTER) option (** Looks for an rpc plug-in for a specific protocol. *) val find_rpc : Protocol_hash.t -> (module RPC) option + +(** Looks for a metrics plugin module for a specific protocol *) +val find_metrics : Protocol_hash.t -> (module METRICS) option + +(** Same as [find_metrics] but returns [Undefined_metrics_plugin] if not found *) +val safe_find_metrics : Protocol_hash.t -> (module METRICS) Lwt.t diff --git a/src/proto_012_Psithaca/lib_plugin/metrics_plugin.ml b/src/proto_012_Psithaca/lib_plugin/metrics.ml similarity index 100% rename from src/proto_012_Psithaca/lib_plugin/metrics_plugin.ml rename to src/proto_012_Psithaca/lib_plugin/metrics.ml diff --git a/src/proto_012_Psithaca/lib_plugin/plugin_registerer.ml b/src/proto_012_Psithaca/lib_plugin/plugin_registerer.ml index aae179bf4c455953dd6b8ad78450643fa3cfc256..8073f4644dbc187968f65026ccfceca4384a39a2 100644 --- a/src/proto_012_Psithaca/lib_plugin/plugin_registerer.ml +++ b/src/proto_012_Psithaca/lib_plugin/plugin_registerer.ml @@ -33,8 +33,8 @@ module RPC = struct include Plugin.RPC end -module MetricsPlugin = struct - include Metrics_plugin +module Metrics = struct + include Metrics let hash = Registerer.Registered.hash end @@ -43,4 +43,4 @@ let () = Shell_plugin.register_filter (module Filter) let () = Shell_plugin.register_rpc (module RPC) -let () = Shell_metrics.Proto_plugin.register_plugin (module MetricsPlugin) +let () = Shell_plugin.register_metrics (module Metrics) diff --git a/src/proto_013_PtJakart/lib_plugin/metrics_plugin.ml b/src/proto_013_PtJakart/lib_plugin/metrics.ml similarity index 100% rename from src/proto_013_PtJakart/lib_plugin/metrics_plugin.ml rename to src/proto_013_PtJakart/lib_plugin/metrics.ml diff --git a/src/proto_013_PtJakart/lib_plugin/plugin_registerer.ml b/src/proto_013_PtJakart/lib_plugin/plugin_registerer.ml index aae179bf4c455953dd6b8ad78450643fa3cfc256..8073f4644dbc187968f65026ccfceca4384a39a2 100644 --- a/src/proto_013_PtJakart/lib_plugin/plugin_registerer.ml +++ b/src/proto_013_PtJakart/lib_plugin/plugin_registerer.ml @@ -33,8 +33,8 @@ module RPC = struct include Plugin.RPC end -module MetricsPlugin = struct - include Metrics_plugin +module Metrics = struct + include Metrics let hash = Registerer.Registered.hash end @@ -43,4 +43,4 @@ let () = Shell_plugin.register_filter (module Filter) let () = Shell_plugin.register_rpc (module RPC) -let () = Shell_metrics.Proto_plugin.register_plugin (module MetricsPlugin) +let () = Shell_plugin.register_metrics (module Metrics) diff --git a/src/proto_014_PtKathma/lib_plugin/metrics_plugin.ml b/src/proto_014_PtKathma/lib_plugin/metrics.ml similarity index 100% rename from src/proto_014_PtKathma/lib_plugin/metrics_plugin.ml rename to src/proto_014_PtKathma/lib_plugin/metrics.ml diff --git a/src/proto_014_PtKathma/lib_plugin/plugin.ml b/src/proto_014_PtKathma/lib_plugin/plugin.ml index 3483f1e674318843af5eb3fd144f7a63c5e57a34..224a977827cdfb831842683d61848024b9a12138 100644 --- a/src/proto_014_PtKathma/lib_plugin/plugin.ml +++ b/src/proto_014_PtKathma/lib_plugin/plugin.ml @@ -28,3 +28,4 @@ module Mempool = Mempool module View_helpers = View_helpers module RPC = RPC +module Metrics = Metrics diff --git a/src/proto_014_PtKathma/lib_plugin/plugin_registerer.ml b/src/proto_014_PtKathma/lib_plugin/plugin_registerer.ml index 4a65d49b58b6a458840b419e24618766b1b764cc..ad4a3eac57c074570f71ccd681cfdc449d908d00 100644 --- a/src/proto_014_PtKathma/lib_plugin/plugin_registerer.ml +++ b/src/proto_014_PtKathma/lib_plugin/plugin_registerer.ml @@ -33,8 +33,8 @@ module RPC = struct include RPC end -module MetricsPlugin = struct - include Metrics_plugin +module Metrics = struct + include Plugin.Metrics let hash = Registerer.Registered.hash end @@ -43,4 +43,4 @@ let () = Shell_plugin.register_filter (module Filter) let () = Shell_plugin.register_rpc (module RPC) -let () = Shell_metrics.Proto_plugin.register_plugin (module MetricsPlugin) +let () = Shell_plugin.register_metrics (module Metrics) diff --git a/src/proto_alpha/lib_plugin/metrics_plugin.ml b/src/proto_alpha/lib_plugin/metrics.ml similarity index 100% rename from src/proto_alpha/lib_plugin/metrics_plugin.ml rename to src/proto_alpha/lib_plugin/metrics.ml diff --git a/src/proto_alpha/lib_plugin/plugin.ml b/src/proto_alpha/lib_plugin/plugin.ml index 3483f1e674318843af5eb3fd144f7a63c5e57a34..224a977827cdfb831842683d61848024b9a12138 100644 --- a/src/proto_alpha/lib_plugin/plugin.ml +++ b/src/proto_alpha/lib_plugin/plugin.ml @@ -28,3 +28,4 @@ module Mempool = Mempool module View_helpers = View_helpers module RPC = RPC +module Metrics = Metrics diff --git a/src/proto_alpha/lib_plugin/plugin_registerer.ml b/src/proto_alpha/lib_plugin/plugin_registerer.ml index aae179bf4c455953dd6b8ad78450643fa3cfc256..1cf730f38b8d4dde6d591dd9f6890819b95fae80 100644 --- a/src/proto_alpha/lib_plugin/plugin_registerer.ml +++ b/src/proto_alpha/lib_plugin/plugin_registerer.ml @@ -33,8 +33,8 @@ module RPC = struct include Plugin.RPC end -module MetricsPlugin = struct - include Metrics_plugin +module Metrics = struct + include Plugin.Metrics let hash = Registerer.Registered.hash end @@ -43,4 +43,4 @@ let () = Shell_plugin.register_filter (module Filter) let () = Shell_plugin.register_rpc (module RPC) -let () = Shell_metrics.Proto_plugin.register_plugin (module MetricsPlugin) +let () = Shell_plugin.register_metrics (module Metrics)