lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.12.10 vs lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.12.11

- old
+ new

@@ -13,17 +13,19 @@ # limitations under the License. module Monitoring # Base class for the counter. class BaseCounter - def increment(*) + def increment(by: 1, labels: {}) + # No default behavior end end # Prometheus implementation of counters. class PrometheusCounter < BaseCounter def initialize(prometheus_counter) + super() @counter = prometheus_counter end def increment(by: 1, labels: {}) @counter.increment(labels, by) @@ -31,27 +33,31 @@ end # OpenCensus implementation of counters. class OpenCensusCounter < BaseCounter def initialize(recorder, measure, translator) + super() raise ArgumentError, 'measure must not be nil' if measure.nil? + @recorder = recorder @measure = measure @translator = translator end def increment(by: 1, labels: {}) labels = @translator.translate_labels(labels) tag_map = OpenCensus::Tags::TagMap.new( - labels.map { |k, v| [k.to_s, v.to_s] }.to_h) + labels.map { |k, v| [k.to_s, v.to_s] }.to_h + ) @recorder.record(@measure.create_measurement(value: by, tags: tag_map)) end end # Base class for the monitoring registry. class BaseMonitoringRegistry def initialize(_project_id, _monitored_resource, _gcm_service_address) + # no default behavior end def counter(_name, _labels, _docstring, _prefix, _aggregation) BaseCounter.new end @@ -78,13 +84,13 @@ def counter(name, _labels, docstring, _prefix, _aggregation) # When we upgrade to Prometheus client 0.10.0 or higher, pass the # labels in the metric constructor. The 'labels' field in # Prometheus client 0.9.0 has a different function and will not # work as intended. - return PrometheusCounter.new(@registry.counter(name, docstring)) + PrometheusCounter.new(@registry.counter(name, docstring)) rescue Prometheus::Client::Registry::AlreadyRegisteredError - return PrometheusCounter.new(@registry.get(name)) + PrometheusCounter.new(@registry.get(name)) end end # OpenCensus implementation of the monitoring registry. class OpenCensusMonitoringRegistry < BaseMonitoringRegistry @@ -102,30 +108,33 @@ @gcm_service_address = gcm_service_address @recorders = {} @exporters = {} @log.info( 'monitoring module: Successfully initialized Open Census monitoring ' \ - 'registry.') + 'registry.' + ) end def counter(name, labels, docstring, prefix, aggregation) translator = MetricTranslator.new(name, labels) measure = OpenCensus::Stats::MeasureRegistry.get(translator.name) if measure.nil? @log.info( 'monitoring module: Registering a new measure registry for ' \ - "#{translator.name}") + "#{translator.name}" + ) measure = OpenCensus::Stats.create_measure_int( name: translator.name, unit: OpenCensus::Stats::Measure::UNIT_NONE, description: docstring ) end unless @exporters.keys.include?(prefix) @log.info( 'monitoring module: Registering a new exporter for ' \ - "#{prefix}") + "#{prefix}" + ) @recorders[prefix] = OpenCensus::Stats::Recorder.new @exporters[prefix] = \ OpenCensus::Stats::Exporters::Stackdriver.new( project_id: @project_id, metric_prefix: prefix, @@ -133,17 +142,18 @@ resource_labels: @metrics_monitored_resource.labels, gcm_service_address: @gcm_service_address ) @log.info( 'monitoring module: Registered recorders and exporters for ' \ - "#{prefix}.\n#{@exporters[prefix]}") + "#{prefix}.\n#{@exporters[prefix]}" + ) end - if aggregation == 'GAUGE' - stats_aggregation = OpenCensus::Stats.create_last_value_aggregation - else - stats_aggregation = OpenCensus::Stats.create_sum_aggregation - end + stats_aggregation = if aggregation == 'GAUGE' + OpenCensus::Stats.create_last_value_aggregation + else + OpenCensus::Stats.create_sum_aggregation + end @recorders[prefix].register_view( OpenCensus::Stats::View.new( name: translator.name, measure: measure, aggregation: stats_aggregation, @@ -152,11 +162,12 @@ ) ) counter = OpenCensusCounter.new(@recorders[prefix], measure, translator) @log.info( 'monitoring module: Successfully initialized Open Census counter for ' \ - "#{prefix}/#{name}.") + "#{prefix}/#{name}." + ) counter rescue StandardError => e @log.warn "Failed to count metrics for #{name}.", error: e raise e end @@ -170,24 +181,24 @@ recorder = @recorders[prefix] recorder.views_data.each do |view_data| view_data.data.each_value do |aggr_data| # Apply this only to GAUGE metrics. This could fail if the metric uses # Distribution or other fancier aggregators. - if aggr_data.is_a? OpenCensus::Stats::AggregationData::LastValue - aggr_data.add aggr_data.value, new_time - end + aggr_data.add aggr_data.value, new_time if aggr_data.is_a? OpenCensus::Stats::AggregationData::LastValue end end end def export @log.debug( - "monitoring module: Exporting metrics for #{@exporters.keys}.") - @exporters.keys.each do |prefix| + "monitoring module: Exporting metrics for #{@exporters.keys}." + ) + @exporters.each_key do |prefix| @log.debug( "monitoring module: Exporting metrics for #{prefix}. " \ - "#{@recorders[prefix].views_data}") + "#{@recorders[prefix].views_data}" + ) @exporters[prefix].export @recorders[prefix].views_data end rescue StandardError => e # TODO(lingshi): Fix the error handling here. Seems like the export is # done asynchronously. So any failure happens silently. More details at @@ -221,12 +232,11 @@ # Prometheus metrics are collected by Google Kubernetes Engine's monitoring, # so we can't redefine them. # Avoid this mechanism for new metrics by defining them in their final form, # so they don't need translation. class MetricTranslator - attr_reader :name - attr_reader :view_labels + attr_reader :name, :view_labels def initialize(name, metric_labels) @legacy = true case name when :stackdriver_successful_requests_count, @@ -245,10 +255,11 @@ @view_labels = @legacy ? [:response_code] : metric_labels end def translate_labels(labels) return labels unless @legacy + translation = { code: :response_code, grpc: :grpc } - labels.map { |k, v| [translation[k], v] }.to_h + labels.transform_keys { |k| translation[k] } end end end