Class: Monitoring::OpenCensusMonitoringRegistry

Inherits:
BaseMonitoringRegistry show all
Defined in:
lib/fluent/plugin/monitoring.rb

Overview

OpenCensus implementation of the monitoring registry.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id, monitored_resource, gcm_service_address) ⇒ OpenCensusMonitoringRegistry

Returns a new instance of OpenCensusMonitoringRegistry.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

Class Method Details

.nameObject



97
98
99
# File 'lib/fluent/plugin/monitoring.rb', line 97

def self.name
  'opencensus'
end

Instance Method Details

#counter(name, labels, docstring, prefix, aggregation) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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

#exportObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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) ⇒ Object

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.



179
180
181
182
183
184
185
186
187
188
189
# 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