lib/sidekiq_prometheus.rb in sidekiq_prometheus-0.8.1 vs lib/sidekiq_prometheus.rb in sidekiq_prometheus-0.9.0
- old
+ new
@@ -18,10 +18,25 @@
attr_accessor :base_labels
# @return [Hash] Custom labels applied to specific metrics
attr_accessor :custom_labels
+ # @return [Array] Custom metrics that will be registered on setup.
+ # @example
+ # [
+ # {
+ # name: :metric_name,
+ # type: :prometheus_metric_type,
+ # docstring: 'Description of the metric',
+ # base_labels : { label: 'value' },
+ # }
+ # ]
+ # @note Each element of the array is a hash and must have the required keys: `:name`, `:type`, and `:docstring`.
+ # The values for `:name` and `:type` should be symbols and `:docstring` should be a string.
+ # `base_labels` is optional and, if used, must be a hash of labels that will be included on every instance of this metric.
+ attr_accessor :custom_metrics
+
# @return [Boolean] Setting to control enabling/disabling GC metrics. Default: true
attr_accessor :gc_metrics_enabled
# @return [Boolean] Setting to control enabling/disabling global metrics. Default: true
attr_accessor :global_metrics_enabled
@@ -40,47 +55,46 @@
attr_writer :client
# Orverride the default Prometheus Metric Registry
# @return [Prometheus::Client::Registry]
attr_writer :registry
+
+ # @private
+ attr_writer :setup_complete
end
self.gc_metrics_enabled = true
self.periodic_metrics_enabled = true
self.global_metrics_enabled = true
self.periodic_reporting_interval = 30
self.metrics_port = 9359
self.custom_labels = {}
+ self.custom_metrics = []
module_function
##
# @return Prometheus::Client
def client
@client ||= Prometheus::Client
end
##
- # Configure SidekiqPrometheus
+ # Configure SidekiqPrometheus and setup for reporting
# @example
# SidekiqPrometheus.configure do |config|
# config.base_labels = { service: 'images_api' }
# config.custom_labels = { sidekiq_job_count: { object_klass: nil } }
# config.gc_metrics_enabled = true
# end
def configure
yield self
- end
-
- ##
- # Configure and call setup immediately after
- def configure!
- yield self
setup
end
- ##
+ alias configure! configure
+
# Helper method for +gc_metrics_enabled+ configuration setting
# @return [Boolean] defaults to true
def gc_metrics_enabled?
gc_metrics_enabled
end
@@ -115,21 +129,37 @@
##
# Prometheus client metric registry
# @return [Prometheus::Client::Registry]
def registry
- @registry ||= client.registry
+ @registry ||= client::Registry.new
end
##
+ # Register custom metrics
+ # Internal method called by +setup+. This method should not be called from application code in most cases.
+ def register_custom_metrics
+ return if custom_metrics.empty?
+
+ raise SidekiqPrometheus::Error, 'custom_metrics is not an array.' unless custom_metrics.is_a?(Array)
+
+ SidekiqPrometheus::Metrics.register_metrics(custom_metrics)
+ end
+
+ ##
# register metrics and instrument sidekiq
def setup
+ return false if @setup_complete
SidekiqPrometheus::Metrics.register_sidekiq_job_metrics
SidekiqPrometheus::Metrics.register_sidekiq_gc_metric if gc_metrics_enabled?
SidekiqPrometheus::Metrics.register_sidekiq_worker_gc_metrics if gc_metrics_enabled? && periodic_metrics_enabled?
SidekiqPrometheus::Metrics.register_sidekiq_global_metrics if global_metrics_enabled? && periodic_metrics_enabled?
+ register_custom_metrics
+
sidekiq_setup
+
+ self.setup_complete = true
end
##
# Add Prometheus instrumentation to sidekiq
def sidekiq_setup
@@ -161,16 +191,12 @@
Port: SidekiqPrometheus.metrics_port,
BindAddress: '127.0.0.1',
)
end
end
-
- private
-
- def metrics
- SidekiqPrometheus::Metrics
- end
end
+
+class SidekiqPrometheus::Error < StandardError; end
require 'sidekiq_prometheus/job_metrics'
require 'sidekiq_prometheus/metrics'
require 'sidekiq_prometheus/periodic_metrics'
require 'sidekiq_prometheus/version'