lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.10.2 vs lib/fluent/plugin/monitoring.rb in fluent-plugin-google-cloud-0.10.3

- 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) + def counter(_name, _labels, _docstring, _prefix) 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) + def counter(name, _labels, docstring, _prefix) # 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)) @@ -95,45 +95,57 @@ def initialize(project_id, monitored_resource, gcm_service_address) super require 'opencensus' require 'opencensus-stackdriver' @log = $log # rubocop:disable Style/GlobalVars - @recorder = OpenCensus::Stats.ensure_recorder - @exporter = OpenCensus::Stats::Exporters::Stackdriver.new( - project_id: project_id, - metric_prefix: 'agent.googleapis.com/agent', - resource_type: monitored_resource.type, - resource_labels: monitored_resource.labels, - gcm_service_address: gcm_service_address - ) - OpenCensus.configure do |c| - c.stats.exporter = @exporter - end - @log.debug "OpenCensus config=#{OpenCensus.config}" + @project_id = project_id + @metrics_monitored_resource = monitored_resource + @gcm_service_address = gcm_service_address + @recorders = {} + @exporters = {} end - def counter(name, labels, docstring) + def counter(name, labels, docstring, prefix) translator = MetricTranslator.new(name, labels) measure = OpenCensus::Stats::MeasureRegistry.get(translator.name) if measure.nil? 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 + @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 + ) + 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) ) - OpenCensusCounter.new(@recorder, measure, translator) + OpenCensusCounter.new(@recorders[prefix], measure, translator) + rescue StandardError => e + @log.warn "Failed to count metrics for #{name}.", error: e + raise e end def export - @exporter.export @recorder.views_data + @exporters.keys.each do |prefix| + @exporters[prefix].export @recorders[prefix].views_data + end + rescue StandardError => e + @log.warn 'Failed to export some metrics.', error: e + raise e end end # Factory that is used to create a monitoring registry based on # the monitoring solution name.