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.

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

AsynchronousMetricStream

Constant Summary collapse

DEFAULT_CARDINALITY_LIMIT =

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

2000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, unit, instrument_kind, meter_provider, instrumentation_scope, aggregation, exemplar_filter, exemplar_reservoir) ⇒ 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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 23

def initialize(
  name,
  description,
  unit,
  instrument_kind,
  meter_provider,
  instrumentation_scope,
  aggregation,
  exemplar_filter,
  exemplar_reservoir
)
  @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 = {}
  @exemplar_filter = exemplar_filter
  @exemplar_reservoir = exemplar_reservoir

  find_registered_view
  @mutex = Mutex.new
end

Instance Attribute Details

#cardinality_limit=(value) ⇒ Object (writeonly)

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.



19
20
21
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 19

def cardinality_limit=(value)
  @cardinality_limit = value
end

#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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



18
19
20
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 18

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 102

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 50

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)


129
130
131
132
133
134
135
136
137
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 129

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.



123
124
125
126
127
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 123

def find_registered_view
  return if @meter_provider.nil?

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

#resolve_cardinality_limit(view) ⇒ 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.



139
140
141
142
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 139

def resolve_cardinality_limit(view)
  cardinality_limit = view&.aggregation_cardinality_limit || @cardinality_limit || DEFAULT_CARDINALITY_LIMIT
  [cardinality_limit, 0].max # if cardinality_limit is negative, then give it 0
end

#should_offer_exemplar?(value, attributes) ⇒ 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)


144
145
146
147
148
149
150
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 144

def should_offer_exemplar?(value, attributes)
  return false if @exemplar_reservoir&.noop?

  context = OpenTelemetry::Context.current
  time = OpenTelemetry::Common::Utilities.time_in_nanoseconds
  @exemplar_filter&.should_sample?(value, time, attributes, context)
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.



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 152

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 has the cardinality, pass to aggregation update to determine if aggregation have the cardinality if the aggregation does not have the cardinality, then it will be default 2000 it better to move overflowed data_points during update because if do it in collect, then we need to sort the entire data_points (~ 2000) based on time, which is time-consuming view will modify the data_point that is not suitable when there are multiple views



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 79

def update(value, attributes)
  if @registered_views.empty?
    resolved_cardinality_limit = resolve_cardinality_limit(nil)
    @mutex.synchronize do
      exemplar_offer = should_offer_exemplar?(value, attributes)
      @default_aggregation.update(value, attributes, @data_points, resolved_cardinality_limit, exemplar_offer: exemplar_offer)
    end
  else
    @registered_views.each do |view, data_points|
      resolved_cardinality_limit = resolve_cardinality_limit(view)
      @mutex.synchronize do
        attributes ||= {}
        attributes.merge!(view.attribute_keys)

        if view.valid_aggregation?
          exemplar_offer = should_offer_exemplar?(value, attributes)
          view.aggregation.update(value, attributes, data_points, resolved_cardinality_limit, exemplar_offer: exemplar_offer)
        end
      end
    end
  end
end