Class: OpenTelemetry::SDK::Metrics::State::MetricStream Private

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/sdk/metrics/state/metric_stream.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The MetricStream class provides SDK internal functionality that is not a part of the public API.

Direct Known Subclasses

AsynchronousMetricStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, unit, instrument_kind, meter_provider, instrumentation_scope, aggregation) ⇒ MetricStream

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MetricStream.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

def initialize(
  name,
  description,
  unit,
  instrument_kind,
  meter_provider,
  instrumentation_scope,
  aggregation
)
  @name = name
  @description = description
  @unit = unit
  @instrument_kind = instrument_kind
  @meter_provider = meter_provider
  @instrumentation_scope = instrumentation_scope
  @default_aggregation = aggregation
  @data_points = {}
  @registered_views = {}

  find_registered_view
  @mutex = Mutex.new
end

Instance Attribute Details

#data_pointsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def data_points
  @data_points
end

#descriptionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def description
  @description
end

#instrument_kindObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def instrument_kind
  @instrument_kind
end

#instrumentation_scopeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def instrumentation_scope
  @instrumentation_scope
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def name
  @name
end

#unitObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 16

def unit
  @unit
end

Instance Method Details

#aggregate_metric_data(start_time, end_time, aggregation: nil, data_points: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 75

def aggregate_metric_data(start_time, end_time, aggregation: nil, data_points: nil)
  aggregator = aggregation || @default_aggregation
  is_monotonic = aggregator.respond_to?(:monotonic?) ? aggregator.monotonic? : nil
  aggregation_temporality = aggregator.respond_to?(:aggregation_temporality) ? aggregator.aggregation_temporality : nil
  data_point = data_points || @data_points

  MetricData.new(
    @name,
    @description,
    @unit,
    @instrument_kind,
    @meter_provider.resource,
    @instrumentation_scope,
    aggregator.collect(start_time, end_time, data_point),
    aggregation_temporality,
    start_time,
    end_time,
    is_monotonic
  )
end

#collect(start_time, end_time) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 41

def collect(start_time, end_time)
  @mutex.synchronize do
    metric_data = []

    # data points are required to export over OTLP
    return metric_data if empty_data_point?

    if @registered_views.empty?
      metric_data << aggregate_metric_data(start_time, end_time)
    else
      @registered_views.each do |view, data_points|
        metric_data << aggregate_metric_data(start_time, end_time, aggregation: view.aggregation, data_points: data_points)
      end
    end

    metric_data
  end
end

#empty_data_point?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 102

def empty_data_point?
  if @registered_views.empty?
    @data_points.empty?
  else
    @registered_views.each_value do |data_points|
      return false unless data_points.empty?
    end
  end
end

#find_registered_viewObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
99
100
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 96

def find_registered_view
  return if @meter_provider.nil?

  @meter_provider.registered_views.each { |view| @registered_views[view] = {} if view.match_instrument?(self) }
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 112

def to_s
  instrument_info = +''
  instrument_info << "name=#{@name}"
  instrument_info << " description=#{@description}" if @description
  instrument_info << " unit=#{@unit}" if @unit
  @data_points.map do |attributes, value|
    metric_stream_string = +''
    metric_stream_string << instrument_info
    metric_stream_string << " attributes=#{attributes}" if attributes
    metric_stream_string << " #{value}"
    metric_stream_string
  end.join("\n")
end

#update(value, attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

view will modify the data_point that is not suitable when there are multiple views



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 61

def update(value, attributes)
  if @registered_views.empty?
    @mutex.synchronize { @default_aggregation.update(value, attributes, @data_points) }
  else
    @registered_views.each do |view, data_points|
      @mutex.synchronize do
        attributes ||= {}
        attributes.merge!(view.attribute_keys)
        view.aggregation.update(value, attributes, data_points) if view.valid_aggregation?
      end
    end
  end
end