class Monitoring::OpenCensusMonitoringRegistry

OpenCensus implementation of the monitoring registry.

Public Class Methods

name() click to toggle source
# File lib/fluent/plugin/monitoring.rb, line 97
def self.name
  'opencensus'
end
new(project_id, monitored_resource, gcm_service_address) click to toggle source
Calls superclass method Monitoring::BaseMonitoringRegistry::new
# File lib/fluent/plugin/monitoring.rb, line 101
def initialize(project_id, monitored_resource, gcm_service_address)
  super
  require 'opencensus'
  require 'opencensus-stackdriver'
  @log = $log # rubocop:disable Style/GlobalVars
  @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

Public Instance Methods

counter(name, labels, docstring, prefix, aggregation) click to toggle source
# File lib/fluent/plugin/monitoring.rb, line 117
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)
    @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
  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,
      description: docstring,
      columns: translator.view_labels.map(&:to_s)
    )
  )
  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
export() click to toggle source
# File lib/fluent/plugin/monitoring.rb, line 191
def export
  @log.debug(
    "monitoring module: Exporting metrics for #{@exporters.keys}."
  )
  @exporters.each_key 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
update_timestamps(prefix) click to toggle source

Update timestamps for each existing AggregationData without altering tags or values. This is currently only used for config analysis metrics, because we want to repeatedly send the exact same metrics as created at start-up.

# File lib/fluent/plugin/monitoring.rb, line 179
def update_timestamps(prefix)
  new_time = Time.now.utc
  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.
      aggr_data.add aggr_data.value, new_time if aggr_data.is_a? OpenCensus::Stats::AggregationData::LastValue
    end
  end
end