module OpenCensus::Stats
The Stats
module contains support for OpenCensus
stats collection.
OpenCensus
allows users to create typed measures, record measurements, aggregate the collected data, and export the aggregated data.
Constants
- RECORDER_CONTEXT_KEY
Internal key for storing the current stats recorder in the thread local context.
@private
Attributes
Get the current configuration @private
Public Class Methods
Configure OpenCensus
Stats
. These configuration fields include parameters governing aggregation, exporting.
This configuration is also available as the `stats` subconfig under the main configuration `OpenCensus.configure`. If the OpenCensus
Railtie is installed in a Rails application, the configuration object is also exposed as `config.opencensus.stats`.
Generally, you should configure this once at process initialization, but it can be modified at any time.
Supported fields are:
-
`exporter` The exporter to use. Must be an exporter, an object with an export method that takes an array of
ViewData
objects. See {OpenCensus::Stats::Exporters}. The initial value is a {OpenCensus::Stats::Exporters::Logger} that logs to STDOUT.
@example:
OpenCensus::Stats.configure do |config| config.exporter = OpenCensus::Stats::Exporters::Logger.new end
# File lib/opencensus/stats/config.rb, line 64 def configure if block_given? yield @config else @config end end
Create and register a view to current stats recorder context.
@param [String] name @param [Measure] measure @param [Aggregation] aggregation @param [Array<String>] columns @param [String] description
# File lib/opencensus/stats.rb, line 126 def create_and_register_view \ name:, measure:, aggregation:, columns: nil, description: nil view = View.new( name: name, measure: measure, aggregation: aggregation, description: description, columns: columns ) ensure_recorder.register_view view end
Create aggregation defination instance with type count. @return [Aggregation]
# File lib/opencensus/stats.rb, line 150 def create_count_aggregation Aggregation::Count.new end
Create aggregation defination instance with type distribution. @param [Array<Integer>,Array<Float>] buckets Value boundries for distribution. @return [Aggregation]
# File lib/opencensus/stats.rb, line 158 def create_distribution_aggregation buckets Aggregation::Distribution.new buckets end
Create aggregation defination instance with type last value. @return [Aggregation]
# File lib/opencensus/stats.rb, line 164 def create_last_value_aggregation Aggregation::LastValue.new end
Create and register double type measure into measure registry.
@param [String] name Name of the measure. @param [String] unit Unit of the measure. i.e “kb”, “s”, “ms” @param [String] description Detail description @return [Measure]
# File lib/opencensus/stats.rb, line 92 def create_measure_double name:, unit:, description: nil MeasureRegistry.register( name: name, unit: unit, type: Measure::DOUBLE_TYPE, description: description ) end
Create and register int64 type measure into measure registry.
@param [String] name Name of the measure. @param [String] unit Unit of the measure. i.e “kb”, “s”, “ms” @param [String] description Detail description @return [Measure]
# File lib/opencensus/stats.rb, line 77 def create_measure_int name:, unit:, description: nil MeasureRegistry.register( name: name, unit: unit, type: Measure::INT64_TYPE, description: description ) end
Create measurement value for registered measure.
@param [String] name Name of the registered measure @param [Integer, Float] value Value of the measurement @param [Hash<String,String>] tags Tags
to which the value is recorded @raise [ArgumentError] if givem measure is not register
# File lib/opencensus/stats.rb, line 113 def create_measurement name:, value:, tags: measure = MeasureRegistry.get name return measure.create_measurement(value: value, tags: tags) if measure raise ArgumentError, "#{name} measure is not registered" end
Create aggregation defination instance with type sum. @return [Aggregation]
# File lib/opencensus/stats.rb, line 144 def create_sum_aggregation Aggregation::Sum.new end
Get recorder from the stats context. If stats context nil then create new recorder and set into stats context. @return [Recorder]
# File lib/opencensus/stats.rb, line 67 def ensure_recorder self.recorder_context ||= Recorder.new end
Get the current thread-local stats recorder context/ Returns `nil` if there is no current SpanContext.
@return [Recorder, nil]
# File lib/opencensus/stats.rb, line 60 def recorder_context OpenCensus::Context.get RECORDER_CONTEXT_KEY end
Sets the current thread-local Recorder
, which governs the behavior of the recorder creation methods of OpenCensus::Stats::Recorder
.
@param [Recorder] context
# File lib/opencensus/stats.rb, line 45 def recorder_context= context OpenCensus::Context.set RECORDER_CONTEXT_KEY, context end
Get list of registered measures @return [Array<Measure>]
# File lib/opencensus/stats.rb, line 103 def registered_measures MeasureRegistry.measures end
Unsets the current thread-local SpanContext, disabling stats recorder creation methods of OpenCensus::Stats::Recorder
# File lib/opencensus/stats.rb, line 52 def unset_recorder_context OpenCensus::Context.unset RECORDER_CONTEXT_KEY end