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

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.



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

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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 85

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 46

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)


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

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.



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

def find_registered_view
  return if @meter_provider.nil?

  @meter_provider.registered_views.each { |view| @registered_views[view] = {} if view.match_instrument?(self) }
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)


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

def should_offer_exemplar?(value, attributes)
  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.



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

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/opentelemetry/sdk/metrics/state/metric_stream.rb', line 65

def update(value, attributes)
  if @registered_views.empty?
    @mutex.synchronize do
      exemplar_offer = should_offer_exemplar?(value, attributes)
      @default_aggregation.update(value, attributes, @data_points, exemplar_offer: exemplar_offer)
    end
  else
    @registered_views.each do |view, data_points|
      @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, exemplar_offer: exemplar_offer)
        end
      end
    end
  end
end