lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.10.4 vs lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.10.5
- old
+ new
@@ -50,11 +50,11 @@
# Base class for the monitoring registry.
class BaseMonitoringRegistry
def initialize(_project_id, _monitored_resource, _gcm_service_address)
end
- def counter(_name, _labels, _docstring, _prefix)
+ def counter(_name, _labels, _docstring, _prefix, _aggregation)
BaseCounter.new
end
def export
nil
@@ -73,11 +73,11 @@
require 'prometheus/client'
@registry = Prometheus::Client.registry
end
# Exception-driven behavior to avoid synchronization errors.
- def counter(name, _labels, docstring, _prefix)
+ 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))
@@ -100,50 +100,81 @@
@project_id = project_id
@metrics_monitored_resource = monitored_resource
@gcm_service_address = gcm_service_address
@recorders = {}
@exporters = {}
+ @log.info(
+ 'monitoring module: Successfully initialized Open Census monitoring ' \
+ 'registry.')
end
- def counter(name, labels, docstring, prefix)
+ 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}")
measure = OpenCensus::Stats.create_measure_int(
name: translator.name,
unit: OpenCensus::Stats::Measure::UNIT_NONE,
description: docstring
)
end
unless @exporters.keys.include?(prefix)
- @recorders[prefix] = OpenCensus::Stats.ensure_recorder
+ @log.info(
+ 'monitoring module: Registering a new exporter for ' \
+ "#{prefix}")
+ @recorders[prefix] = OpenCensus::Stats::Recorder.new
@exporters[prefix] = \
OpenCensus::Stats::Exporters::Stackdriver.new(
project_id: @project_id,
metric_prefix: prefix,
resource_type: @metrics_monitored_resource.type,
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]}")
end
- OpenCensus::Stats.create_and_register_view(
- name: translator.name,
- measure: measure,
- aggregation: OpenCensus::Stats.create_sum_aggregation,
- description: docstring,
- columns: translator.view_labels.map(&:to_s)
+ if aggregation == 'GAUGE'
+ stats_aggregation = OpenCensus::Stats.create_last_value_aggregation
+ else
+ stats_aggregation = OpenCensus::Stats.create_sum_aggregation
+ end
+ @recorders[prefix].register_view(
+ OpenCensus::Stats::View.new(
+ name: translator.name,
+ measure: measure,
+ aggregation: stats_aggregation,
+ description: docstring,
+ columns: translator.view_labels.map(&:to_s)
+ )
)
- OpenCensusCounter.new(@recorders[prefix], measure, translator)
+ counter = OpenCensusCounter.new(@recorders[prefix], measure, translator)
+ @log.info(
+ 'monitoring module: Successfully initialized Open Census counter for ' \
+ "#{prefix}/#{name}.")
+ counter
rescue StandardError => e
@log.warn "Failed to count metrics for #{name}.", error: e
raise e
end
def export
+ @log.debug(
+ "monitoring module: Exporting metrics for #{@exporters.keys}.")
@exporters.keys.each do |prefix|
+ @log.debug(
+ "monitoring module: Exporting metrics for #{prefix}. " \
+ "#{@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
+ # https://github.com/census-ecosystem/opencensus-ruby-exporter-stackdriver/blob/f8de506204972548ca535eff6010d15f328df6c3/lib/opencensus/stats/exporters/stackdriver.rb#L156
@log.warn 'Failed to export some metrics.', error: e
raise e
end
end