Module: OpenCensus::Stats

Defined in:
lib/opencensus/stats.rb,
lib/opencensus/stats/view.rb,
lib/opencensus/stats/config.rb,
lib/opencensus/stats/measure.rb,
lib/opencensus/stats/exemplar.rb,
lib/opencensus/stats/recorder.rb,
lib/opencensus/stats/exporters.rb,
lib/opencensus/stats/view_data.rb,
lib/opencensus/stats/aggregation.rb,
lib/opencensus/stats/measurement.rb,
lib/opencensus/stats/aggregation/sum.rb,
lib/opencensus/stats/exporters/multi.rb,
lib/opencensus/stats/aggregation_data.rb,
lib/opencensus/stats/exporters/logger.rb,
lib/opencensus/stats/measure_registry.rb,
lib/opencensus/stats/aggregation/count.rb,
lib/opencensus/stats/aggregation_data/sum.rb,
lib/opencensus/stats/aggregation/last_value.rb,
lib/opencensus/stats/aggregation_data/count.rb,
lib/opencensus/stats/aggregation/distribution.rb,
lib/opencensus/stats/aggregation_data/last_value.rb,
lib/opencensus/stats/aggregation_data/distribution.rb

Overview

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.

Defined Under Namespace

Modules: Aggregation, AggregationData, Exporters Classes: Exemplar, Measure, MeasureRegistry, Measurement, Recorder, View, ViewData

Class Method Summary collapse

Class Method Details

.configureObject

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:

@example:

OpenCensus::Stats.configure do |config| config.exporter = OpenCensus::Stats::Exporters::Logger.new end



64
65
66
67
68
69
70
# File 'lib/opencensus/stats/config.rb', line 64

def configure
  if block_given?
    yield @config
  else
    @config
  end
end

.create_and_register_view(name:, measure:, aggregation:, columns: nil, description: nil) ⇒ Object

Create and register a view to current stats recorder context.

Parameters:

  • name (String)
  • measure (Measure)
  • aggregation (Aggregation)
  • columns (Array<String>) (defaults to: nil)
  • description (String) (defaults to: nil)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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_count_aggregationAggregation

Create aggregation defination instance with type count.

Returns:



150
151
152
# File 'lib/opencensus/stats.rb', line 150

def create_count_aggregation
  Aggregation::Count.new
end

.create_distribution_aggregation(buckets) ⇒ Aggregation

Create aggregation defination instance with type distribution. distribution.

Parameters:

  • buckets (Array<Integer>, Array<Float>)

    Value boundries for

Returns:



158
159
160
# File 'lib/opencensus/stats.rb', line 158

def create_distribution_aggregation buckets
  Aggregation::Distribution.new buckets
end

.create_last_value_aggregationAggregation

Create aggregation defination instance with type last value.

Returns:



164
165
166
# File 'lib/opencensus/stats.rb', line 164

def create_last_value_aggregation
  Aggregation::LastValue.new
end

.create_measure_double(name:, unit:, description: nil) ⇒ Measure

Create and register double type measure into measure registry.

Parameters:

  • name (String)

    Name of the measure.

  • unit (String)

    Unit of the measure. i.e "kb", "s", "ms"

  • description (String) (defaults to: nil)

    Detail description

Returns:



92
93
94
95
96
97
98
99
# 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_measure_int(name:, unit:, description: nil) ⇒ Measure

Create and register int64 type measure into measure registry.

Parameters:

  • name (String)

    Name of the measure.

  • unit (String)

    Unit of the measure. i.e "kb", "s", "ms"

  • description (String) (defaults to: nil)

    Detail description

Returns:



77
78
79
80
81
82
83
84
# 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(name:, value:, tags:) ⇒ Object

Create measurement value for registered measure.

Parameters:

  • name (String)

    Name of the registered measure

  • value (Integer, Float)

    Value of the measurement

  • tags (Hash<String,String>)

    Tags to which the value is recorded

Raises:

  • (ArgumentError)

    if givem measure is not register



113
114
115
116
117
# 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_sum_aggregationAggregation

Create aggregation defination instance with type sum.

Returns:



144
145
146
# File 'lib/opencensus/stats.rb', line 144

def create_sum_aggregation
  Aggregation::Sum.new
end

.ensure_recorderRecorder

Get recorder from the stats context. If stats context nil then create new recorder and set into stats context.

Returns:



67
68
69
# File 'lib/opencensus/stats.rb', line 67

def ensure_recorder
  self.recorder_context ||= Recorder.new
end

.recorder_contextRecorder?

Get the current thread-local stats recorder context/ Returns nil if there is no current SpanContext.

Returns:



60
61
62
# File 'lib/opencensus/stats.rb', line 60

def recorder_context
  OpenCensus::Context.get RECORDER_CONTEXT_KEY
end

.recorder_context=(context) ⇒ Object

Sets the current thread-local Recorder, which governs the behavior of the recorder creation methods of OpenCensus::Stats::Recorder.

Parameters:



45
46
47
# File 'lib/opencensus/stats.rb', line 45

def recorder_context= context
  OpenCensus::Context.set RECORDER_CONTEXT_KEY, context
end

.registered_measuresArray<Measure>

Get list of registered measures

Returns:



103
104
105
# File 'lib/opencensus/stats.rb', line 103

def registered_measures
  MeasureRegistry.measures
end

.unset_recorder_contextObject

Unsets the current thread-local SpanContext, disabling stats recorder creation methods of OpenCensus::Stats::Recorder



52
53
54
# File 'lib/opencensus/stats.rb', line 52

def unset_recorder_context
  OpenCensus::Context.unset RECORDER_CONTEXT_KEY
end