class OpenCensus::Stats::Exporters::Stackdriver::Converter

An object that converts OpenCensus stats data objects to Monitoring service protos

@private

Public Class Methods

new(project_id) click to toggle source

Create a converter

@param [String] project_id Google project ID

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 34
def initialize project_id
  @project_id = project_id
end

Public Instance Methods

convert_labels(names) click to toggle source

Conver to lables

@param [Array<String>] names @return [Array<Google::Api::LabelDescriptor>]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 62
def convert_labels names
  names.map do |name|
    Google::Api::LabelDescriptor.new(
      key: name,
      value_type: Google::Api::LabelDescriptor::ValueType::STRING
    )
  end
end
convert_metric_descriptor(view, metric_prefix) click to toggle source

Convert view to metric descriptor

@param [OpenCensus::Stats:View] view Stats view @param [String] metric_prefix Metric prefix name @return [Google::Api::MetricDescriptor]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 44
def convert_metric_descriptor view, metric_prefix
  descriptor = Google::Api::MetricDescriptor.new(
    type: make_metric_type(metric_prefix, view.name),
    display_name: view.measure.name,
    metric_kind: convert_metric_kind(view.aggregation),
    value_type: convert_metric_value_type(view),
    unit: view.measure.unit,
    labels: convert_labels(view.columns)
  )

  descriptor.description = view.description if view.description
  descriptor
end
convert_metric_kind(aggregation) click to toggle source

Convert to metric kind

@param [OpenCensus::Stats:Aggregation::LastValue,

OpenCensus::Stats:Aggregation::Sum,
OpenCensus::Stats:Aggregation::Count,
OpenCensus::Stats:Aggregation::Distribution] aggregation
Aggregation type

@return [Symbol] Metric kind type

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 101
def convert_metric_kind aggregation
  last_value_class = OpenCensus::Stats::Aggregation::LastValue

  if aggregation.instance_of? last_value_class
    return Google::Api::MetricDescriptor::MetricKind::GAUGE
  end

  Google::Api::MetricDescriptor::MetricKind::CUMULATIVE
end
convert_metric_value_type(view) click to toggle source

Convert to metric view type.

@param [OpenCensus::Stats:View] view Stats view @return [Symbol] Metric value type

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 76
def convert_metric_value_type view
  case view.aggregation
  when OpenCensus::Stats::Aggregation::Distribution
    Google::Api::MetricDescriptor::ValueType::DISTRIBUTION
  when OpenCensus::Stats::Aggregation::Count
    Google::Api::MetricDescriptor::ValueType::INT64
  when OpenCensus::Stats::Aggregation::Sum,
      OpenCensus::Stats::Aggregation::LastValue
    if view.measure.int64?
      Google::Api::MetricDescriptor::ValueType::INT64
    else
      Google::Api::MetricDescriptor::ValueType::DOUBLE
    end
  end
end
convert_point(start_time, end_time, measure, aggr_data) click to toggle source

Convert aggr data to time series point proto

@param [Time] start_time Start time @param [Time] end_time Start time @param [OpenCensus::Stats:Measure] measure Measure details @param [OpenCensus::Stats:AggregationData] aggr_data Aggregated data @raise [TypeError] If invalid aggr data type. @return [Google::Cloud::Monitoring::V3::Point]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 156
def convert_point start_time, end_time, measure, aggr_data
  case aggr_data
  when OpenCensus::Stats::AggregationData::Distribution
    create_distribution_point start_time, end_time, aggr_data
  when OpenCensus::Stats::AggregationData::LastValue
    create_number_point(
      start_time,
      start_time,
      aggr_data.value,
      measure
    )
  when OpenCensus::Stats::AggregationData::Sum,
      OpenCensus::Stats::AggregationData::Count
    create_number_point(
      start_time,
      end_time,
      aggr_data.value,
      measure
    )
  else
    raise TypeError, "invalid aggregation type : #{aggr_data.class}"
  end
end
convert_time(time) click to toggle source

Convert time object to protobuf timestamp

@param [Time] time Ruby Time object @return [Google::Protobuf::Timestamp] The generated proto

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 238
def convert_time time
  proto = Google::Protobuf::Timestamp.new
  proto.from_time(time)
  proto
end
convert_time_series(metric_prefix, resource_type, resource_labels, view_data) click to toggle source

Convert view data to time series list

@param [String] metric_prefix Metric prefix name @param [String] resource_type Metric resource type @param [Hash<String,String>] resource_labels Metric resource labels @param [OpenCensus::Stats::ViewData] view_data Stats view data @return [Array]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 119
def convert_time_series metric_prefix, resource_type, resource_labels,
                        view_data
  view = view_data.view

  view_data.data.map do |tag_values, aggr_data|
    series = Google::Cloud::Monitoring::V3::TimeSeries.new(
      metric: {
        type: make_metric_type(metric_prefix, view.name),
        labels: Hash[view.columns.zip tag_values]
      },
      resource: {
        type: resource_type,
        labels: resource_labels
      },
      metric_kind: convert_metric_kind(view.aggregation),
      value_type: convert_metric_value_type(view)
    )

    series.points << convert_point(
      view_data.start_time,
      aggr_data.time,
      view.measure,
      aggr_data
    )

    series
  end
end
create_distribution_point(start_time, end_time, aggr_data) click to toggle source

Create a distribution point @param [Time] start_time Start time @param [Time] end_time Start time @param [OpenCensus::Stats::AggregationData::Distribution] aggr_data @return [Google::Cloud::Monitoring::V3::Point]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 186
def create_distribution_point start_time, end_time, aggr_data
  value = {
    count: aggr_data.count,
    mean: aggr_data.mean,
    sum_of_squared_deviation: aggr_data.sum_of_squared_deviation,
    bucket_options: {
      explicit_buckets: {
        bounds: [0].concat(aggr_data.buckets)
      }
    },
    bucket_counts: [0].concat(aggr_data.bucket_counts)
  }

  Google::Cloud::Monitoring::V3::Point.new(
    interval: {
      start_time: convert_time(start_time),
      end_time: convert_time(end_time)
    },
    value: {
      distribution_value: value
    }
  )
end
create_number_point(start_time, end_time, value, measure) click to toggle source

Create a number point @param [Time] start_time Start time @param [Time] end_time Start time @param [Integer, Float] value @param [OpenCensus::Stats::Measure] measure Measure defination @return [Google::Cloud::Monitoring::V3::Point]

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 217
def create_number_point start_time, end_time, value, measure
  value = if measure.int64?
            { int64_value: value }
          else
            { double_value: value }
          end

  Google::Cloud::Monitoring::V3::Point.new(
    interval: {
      start_time: convert_time(start_time),
      end_time: convert_time(end_time)
    },
    value: value
  )
end
make_metric_type(metric_prefix, name) click to toggle source

Make make metric type

@param [String] metric_prefix The metric prefix @param [String] name The name of the mertic view @return [String] The metric type path

# File lib/opencensus/stats/exporters/stackdriver/converter.rb, line 251
def make_metric_type metric_prefix, name
  "#{metric_prefix}/#{name}"
end