-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove gauge_with_time, add expired_stats_collector, add tests
- Loading branch information
Showing
17 changed files
with
469 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
lib/prometheus_exporter/ext/server/base_collector_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'prometheus_exporter/metric' | ||
|
||
module PrometheusExporter::Ext::Server | ||
module BaseCollectorMethods | ||
class << self | ||
private | ||
|
||
def included(klass) | ||
super | ||
klass.singleton_class.attr_accessor :type, :registered_metrics | ||
klass.registered_metrics = {} | ||
klass.extend ClassMethods | ||
end | ||
end | ||
|
||
module ClassMethods | ||
# Registers metric observer. | ||
# @param name [Symbol] metric name. | ||
# @param help [String] metric description. | ||
# @param metric_class [Class<PrometheusExporter::Metric::Base>] observer class. | ||
# @param args [Array] additional arguments for observer class. | ||
# rubocop:disable Metrics/ParameterLists | ||
def register_metric(name, help, metric_class, *args) | ||
# rubocop:enable Metrics/ParameterLists | ||
name = name.to_s | ||
raise ArgumentError, "metric #{name} is already registered" if registered_metrics.key?(name) | ||
|
||
registered_metrics[name] = { help:, metric_class:, args: } | ||
end | ||
|
||
# Registers PrometheusExporter::Metric::Counter observer. | ||
# @param name [Symbol] metric name. | ||
# @param help [String] metric description. | ||
def register_counter(name, help) | ||
register_metric(name, help, PrometheusExporter::Metric::Counter) | ||
end | ||
|
||
# Registers PrometheusExporter::Metric::Gauge observer. | ||
# @param name [Symbol] metric name. | ||
# @param help [String] metric description. | ||
def register_gauge(name, help) | ||
register_metric(name, help, PrometheusExporter::Metric::Gauge) | ||
end | ||
|
||
# Registers PrometheusExporter::Metric::Summary observer. | ||
# @param name [Symbol] metric name. | ||
# @param help [String] metric description. | ||
# @param opts [Hash] additional options, supports `quantiles` key. | ||
def register_summary(name, help, opts = {}) | ||
register_metric(name, help, PrometheusExporter::Metric::Summary, opts) | ||
end | ||
|
||
# Registers PrometheusExporter::Metric::Histogram observer. | ||
# @param name [Symbol] metric name. | ||
# @param help [String] metric description. | ||
# @param opts [Hash] additional options, supports `buckets` key. | ||
def register_histogram(name, help, opts = {}) | ||
register_metric(name, help, PrometheusExporter::Metric::Histogram, opts) | ||
end | ||
end | ||
|
||
# @return [String] | ||
def type | ||
self.class.type | ||
end | ||
|
||
private | ||
|
||
# Adds metrics to observers with matched name. | ||
# @param observers [Hash] returned by #build_observers. | ||
# @param obj [Hash] metric data. | ||
def fill_observers(observers, obj) | ||
observers.each do |name, observer| | ||
value = obj[name] | ||
observer.observe(value, obj['labels']) if value | ||
end | ||
end | ||
|
||
# Generally metrics sent via PrometheusExporter::Ext::Instrumentation::BaseStats populate labels to `labels` key. | ||
# But PrometheusExporter::Client populate it's own labels to `custom_labels` key. | ||
# Here we merge them into single `labels` key. | ||
# @param obj [Hash] | ||
# @return [Hash] | ||
def normalize_labels(obj) | ||
obj['labels'] ||= {} | ||
custom_labels = obj.delete('custom_labels') | ||
obj['labels'].merge!(custom_labels) if custom_labels | ||
obj | ||
end | ||
|
||
# @return [Hash] key is metric name, value is observer. | ||
def build_observers | ||
observers = {} | ||
self.class.registered_metrics.each do |name, metric| | ||
observers[name] = metric[:metric_class].new("#{type}_#{name}", metric[:help], *metric[:args]) | ||
end | ||
observers | ||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
lib/prometheus_exporter/ext/server/expired_stats_collector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'base_collector_methods' | ||
require 'prometheus_exporter/server/metrics_container' | ||
|
||
module PrometheusExporter::Ext::Server | ||
module ExpiredStatsCollector | ||
class << self | ||
private | ||
|
||
def included(klass) | ||
super | ||
klass.include BaseCollectorMethods | ||
klass.singleton_class.attr_accessor :filter, :ttl | ||
klass.ttl = 60 | ||
klass.extend ClassMethods | ||
end | ||
end | ||
|
||
module ClassMethods | ||
# Defines a rule how old metric will be replaced with new one. | ||
# @yield compare new metric with existing one. | ||
# @yieldparam new_metric [Hash] new metric data. | ||
# @yieldparam metric [Hash] existing metric data. | ||
# @yieldreturn [Boolean] if true existing metric will be replaced with new one. | ||
def unique_metric_by(&block) | ||
@filter = block | ||
end | ||
end | ||
|
||
def initialize | ||
super | ||
@data = PrometheusExporter::Server::MetricsContainer.new( | ||
ttl: self.class.ttl, | ||
filter: self.class.filter | ||
) | ||
end | ||
|
||
# Returns all metrics collected by this collector. | ||
# @return [Array<PrometheusExporter::Metric::Base>] | ||
def metrics | ||
observers = build_observers | ||
@data.each do |obj| | ||
fill_observers(observers, obj) | ||
end | ||
|
||
observers.values | ||
end | ||
|
||
# Collects metric data received from client. | ||
# @param obj [Hash] metric data. | ||
def collect(obj) | ||
normalize_labels(obj) | ||
@data << obj | ||
end | ||
end | ||
end |
Oops, something went wrong.