opentelemetry-metrics-sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +9 -0
  3. data/CHANGELOG.md +5 -0
  4. data/LICENSE +201 -0
  5. data/README.md +110 -0
  6. data/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb +100 -0
  7. data/lib/opentelemetry/sdk/metrics/aggregation/histogram_data_point.rb +27 -0
  8. data/lib/opentelemetry/sdk/metrics/aggregation/number_data_point.rb +19 -0
  9. data/lib/opentelemetry/sdk/metrics/aggregation/sum.rb +58 -0
  10. data/lib/opentelemetry/sdk/metrics/aggregation.rb +21 -0
  11. data/lib/opentelemetry/sdk/metrics/configuration_patch.rb +25 -0
  12. data/lib/opentelemetry/sdk/metrics/export/console_metric_pull_exporter.rb +44 -0
  13. data/lib/opentelemetry/sdk/metrics/export/in_memory_metric_pull_exporter.rb +46 -0
  14. data/lib/opentelemetry/sdk/metrics/export/metric_reader.rb +36 -0
  15. data/lib/opentelemetry/sdk/metrics/export.rb +28 -0
  16. data/lib/opentelemetry/sdk/metrics/instrument/counter.rb +51 -0
  17. data/lib/opentelemetry/sdk/metrics/instrument/histogram.rb +44 -0
  18. data/lib/opentelemetry/sdk/metrics/instrument/observable_counter.rb +26 -0
  19. data/lib/opentelemetry/sdk/metrics/instrument/observable_gauge.rb +26 -0
  20. data/lib/opentelemetry/sdk/metrics/instrument/observable_up_down_counter.rb +26 -0
  21. data/lib/opentelemetry/sdk/metrics/instrument/synchronous_instrument.rb +49 -0
  22. data/lib/opentelemetry/sdk/metrics/instrument/up_down_counter.rb +44 -0
  23. data/lib/opentelemetry/sdk/metrics/instrument.rb +22 -0
  24. data/lib/opentelemetry/sdk/metrics/meter.rb +51 -0
  25. data/lib/opentelemetry/sdk/metrics/meter_provider.rb +139 -0
  26. data/lib/opentelemetry/sdk/metrics/state/metric_data.rb +25 -0
  27. data/lib/opentelemetry/sdk/metrics/state/metric_store.rb +48 -0
  28. data/lib/opentelemetry/sdk/metrics/state/metric_stream.rb +76 -0
  29. data/lib/opentelemetry/sdk/metrics/state.rb +22 -0
  30. data/lib/opentelemetry/sdk/metrics/version.rb +14 -0
  31. data/lib/opentelemetry/sdk/metrics.rb +22 -0
  32. data/lib/opentelemetry-metrics-sdk.rb +9 -0
  33. metadata +260 -0
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Export
11
+ # The InMemoryMetricPullExporter behaves as a Metric Reader and Exporter.
12
+ # To be used for testing purposes, not production.
13
+ class InMemoryMetricPullExporter < MetricReader
14
+ attr_reader :metric_snapshots
15
+
16
+ def initialize
17
+ super
18
+ @metric_snapshots = []
19
+ @mutex = Mutex.new
20
+ end
21
+
22
+ def pull
23
+ export(collect)
24
+ end
25
+
26
+ def export(metrics)
27
+ @mutex.synchronize do
28
+ @metric_snapshots << metrics
29
+ end
30
+ SUCCESS
31
+ end
32
+
33
+ def reset
34
+ @mutex.synchronize do
35
+ @metric_snapshots.clear
36
+ end
37
+ end
38
+
39
+ def shutdown
40
+ SUCCESS
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Export
11
+ # MetricReader provides a minimal example implementation.
12
+ # It is not required to subclass this class to provide an implementation
13
+ # of MetricReader, provided the interface is satisfied.
14
+ class MetricReader
15
+ attr_reader :metric_store
16
+
17
+ def initialize
18
+ @metric_store = OpenTelemetry::SDK::Metrics::State::MetricStore.new
19
+ end
20
+
21
+ def collect
22
+ @metric_store.collect
23
+ end
24
+
25
+ def shutdown(timeout: nil)
26
+ Export::SUCCESS
27
+ end
28
+
29
+ def force_flush(timeout: nil)
30
+ Export::SUCCESS
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Export
11
+ ExportError = Class.new(OpenTelemetry::Error)
12
+
13
+ # The operation finished successfully.
14
+ SUCCESS = 0
15
+
16
+ # The operation finished with an error.
17
+ FAILURE = 1
18
+
19
+ # The operation timed out.
20
+ TIMEOUT = 2
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ require 'opentelemetry/sdk/metrics/export/metric_reader'
27
+ require 'opentelemetry/sdk/metrics/export/in_memory_metric_pull_exporter'
28
+ require 'opentelemetry/sdk/metrics/export/console_metric_pull_exporter'
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {Counter} is the SDK implementation of {OpenTelemetry::Metrics::Counter}.
12
+ class Counter < OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument
13
+ # Returns the instrument kind as a Symbol
14
+ #
15
+ # @return [Symbol]
16
+ def instrument_kind
17
+ :counter
18
+ end
19
+
20
+ # Increment the Counter by a fixed amount.
21
+ #
22
+ # @param [numeric] increment The increment amount, which MUST be a non-negative numeric value.
23
+ # @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes
24
+ # Values must be non-nil and (array of) string, boolean or numeric type.
25
+ # Array values must not contain nil elements and all elements must be of
26
+ # the same basic type (string, numeric, boolean).
27
+ def add(increment, attributes: {})
28
+ # TODO: When the metrics SDK stabilizes and is merged into the main SDK,
29
+ # we can leverage the SDK Internal validation classes to enforce this:
30
+ # https://github.com/open-telemetry/opentelemetry-ruby/blob/6bec625ef49004f364457c26263df421526b60d6/sdk/lib/opentelemetry/sdk/internal.rb#L47
31
+ if increment.negative?
32
+ OpenTelemetry.logger.warn("#{@name} received a negative value")
33
+ else
34
+ update(increment, attributes)
35
+ end
36
+ nil
37
+ rescue StandardError => e
38
+ OpenTelemetry.handle_error(exception: e)
39
+ nil
40
+ end
41
+
42
+ private
43
+
44
+ def default_aggregation
45
+ OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {Histogram} is the SDK implementation of {OpenTelemetry::Metrics::Histogram}.
12
+ class Histogram < OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument
13
+ # Returns the instrument kind as a Symbol
14
+ #
15
+ # @return [Symbol]
16
+ def instrument_kind
17
+ :histogram
18
+ end
19
+
20
+ # Updates the statistics with the specified amount.
21
+ #
22
+ # @param [numeric] amount The amount of the Measurement, which MUST be a non-negative numeric value.
23
+ # @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes
24
+ # Values must be non-nil and (array of) string, boolean or numeric type.
25
+ # Array values must not contain nil elements and all elements must be of
26
+ # the same basic type (string, numeric, boolean).
27
+ def record(amount, attributes: nil)
28
+ update(amount, attributes)
29
+ nil
30
+ rescue StandardError => e
31
+ OpenTelemetry.handle_error(exception: e)
32
+ nil
33
+ end
34
+
35
+ private
36
+
37
+ def default_aggregation
38
+ OpenTelemetry::SDK::Metrics::Aggregation::ExplicitBucketHistogram.new
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {ObservableCounter} is the SDK implementation of {OpenTelemetry::Metrics::ObservableCounter}.
12
+ class ObservableCounter < OpenTelemetry::Metrics::Instrument::ObservableCounter
13
+ attr_reader :name, :unit, :description
14
+
15
+ def initialize(name, unit, description, callback, meter)
16
+ @name = name
17
+ @unit = unit
18
+ @description = description
19
+ @callback = callback
20
+ @meter = meter
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {ObservableGauge} is the SDK implementation of {OpenTelemetry::Metrics::ObservableGauge}.
12
+ class ObservableGauge < OpenTelemetry::Metrics::Instrument::ObservableGauge
13
+ attr_reader :name, :unit, :description
14
+
15
+ def initialize(name, unit, description, callback, meter)
16
+ @name = name
17
+ @unit = unit
18
+ @description = description
19
+ @callback = callback
20
+ @meter = meter
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {ObservableUpDownCounter} is the SDK implementation of {OpenTelemetry::Metrics::ObservableUpDownCounter}.
12
+ class ObservableUpDownCounter < OpenTelemetry::Metrics::Instrument::ObservableUpDownCounter
13
+ attr_reader :name, :unit, :description
14
+
15
+ def initialize(name, unit, description, callback, meter)
16
+ @name = name
17
+ @unit = unit
18
+ @description = description
19
+ @callback = callback
20
+ @meter = meter
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {SynchronousInstrument} contains the common functionality shared across
12
+ # the synchronous instruments SDK instruments.
13
+ class SynchronousInstrument
14
+ def initialize(name, unit, description, instrumentation_scope, meter_provider)
15
+ @name = name
16
+ @unit = unit
17
+ @description = description
18
+ @instrumentation_scope = instrumentation_scope
19
+ @meter_provider = meter_provider
20
+ @metric_streams = []
21
+
22
+ meter_provider.register_synchronous_instrument(self)
23
+ end
24
+
25
+ # @api private
26
+ def register_with_new_metric_store(metric_store, aggregation: default_aggregation)
27
+ ms = OpenTelemetry::SDK::Metrics::State::MetricStream.new(
28
+ @name,
29
+ @description,
30
+ @unit,
31
+ instrument_kind,
32
+ @meter_provider,
33
+ @instrumentation_scope,
34
+ aggregation
35
+ )
36
+ @metric_streams << ms
37
+ metric_store.add_metric_stream(ms)
38
+ end
39
+
40
+ private
41
+
42
+ def update(value, attributes)
43
+ @metric_streams.each { |ms| ms.update(value, attributes) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module Instrument
11
+ # {UpDownCounter} is the SDK implementation of {OpenTelemetry::Metrics::UpDownCounter}.
12
+ class UpDownCounter < OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument
13
+ # Returns the instrument kind as a Symbol
14
+ #
15
+ # @return [Symbol]
16
+ def instrument_kind
17
+ :up_down_counter
18
+ end
19
+
20
+ # Increment or decrement the UpDownCounter by a fixed amount.
21
+ #
22
+ # @param [Numeric] amount The amount to be added, can be positive, negative or zero.
23
+ # @param [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes
24
+ # Values must be non-nil and (array of) string, boolean or numeric type.
25
+ # Array values must not contain nil elements and all elements must be of
26
+ # the same basic type (string, numeric, boolean).
27
+ def add(amount, attributes: nil)
28
+ update(amount, attributes)
29
+ nil
30
+ rescue StandardError => e
31
+ OpenTelemetry.handle_error(exception: e)
32
+ nil
33
+ end
34
+
35
+ private
36
+
37
+ def default_aggregation
38
+ OpenTelemetry::SDK::Metrics::Aggregation::Sum.new
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ # The Instrument module contains the OpenTelemetry instruments reference
10
+ # implementation.
11
+ module Instrument
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'opentelemetry/sdk/metrics/instrument/synchronous_instrument'
17
+ require 'opentelemetry/sdk/metrics/instrument/counter'
18
+ require 'opentelemetry/sdk/metrics/instrument/histogram'
19
+ require 'opentelemetry/sdk/metrics/instrument/observable_counter'
20
+ require 'opentelemetry/sdk/metrics/instrument/observable_gauge'
21
+ require 'opentelemetry/sdk/metrics/instrument/observable_up_down_counter'
22
+ require 'opentelemetry/sdk/metrics/instrument/up_down_counter'
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ # The Metrics module contains the OpenTelemetry metrics reference
10
+ # implementation.
11
+ module Metrics
12
+ # {Meter} is the SDK implementation of {OpenTelemetry::Metrics::Meter}.
13
+ class Meter < OpenTelemetry::Metrics::Meter
14
+ # @api private
15
+ #
16
+ # Returns a new {Meter} instance.
17
+ #
18
+ # @param [String] name Instrumentation package name
19
+ # @param [String] version Instrumentation package version
20
+ #
21
+ # @return [Meter]
22
+ def initialize(name, version, meter_provider)
23
+ @mutex = Mutex.new
24
+ @instrument_registry = {}
25
+ @instrumentation_scope = InstrumentationScope.new(name, version)
26
+ @meter_provider = meter_provider
27
+ end
28
+
29
+ # @api private
30
+ def add_metric_reader(metric_reader)
31
+ @instrument_registry.each do |_n, instrument|
32
+ instrument.register_with_new_metric_store(metric_reader.metric_store)
33
+ end
34
+ end
35
+
36
+ def create_instrument(kind, name, unit, description, callback)
37
+ super do
38
+ case kind
39
+ when :counter then OpenTelemetry::SDK::Metrics::Instrument::Counter.new(name, unit, description, @instrumentation_scope, @meter_provider)
40
+ when :observable_counter then OpenTelemetry::SDK::Metrics::Instrument::ObservableCounter.new(name, unit, description, callback, @instrumentation_scope, @meter_provider)
41
+ when :histogram then OpenTelemetry::SDK::Metrics::Instrument::Histogram.new(name, unit, description, @instrumentation_scope, @meter_provider)
42
+ when :observable_gauge then OpenTelemetry::SDK::Metrics::Instrument::ObservableGauge.new(name, unit, description, callback, @instrumentation_scope, @meter_provider)
43
+ when :up_down_counter then OpenTelemetry::SDK::Metrics::Instrument::UpDownCounter.new(name, unit, description, @instrumentation_scope, @meter_provider)
44
+ when :observable_up_down_counter then OpenTelemetry::SDK::Metrics::Instrument::ObservableUpDownCounter.new(name, unit, description, callback, @instrumentation_scope, @meter_provider)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ # The Metrics module contains the OpenTelemetry metrics reference
10
+ # implementation.
11
+ module Metrics
12
+ # {MeterProvider} is the SDK implementation of {OpenTelemetry::Metrics::MeterProvider}.
13
+ class MeterProvider < OpenTelemetry::Metrics::MeterProvider
14
+ Key = Struct.new(:name, :version)
15
+ private_constant(:Key)
16
+
17
+ attr_reader :resource, :metric_readers
18
+
19
+ def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create)
20
+ @mutex = Mutex.new
21
+ @meter_registry = {}
22
+ @stopped = false
23
+ @metric_readers = []
24
+ @resource = resource
25
+ end
26
+
27
+ # Returns a {Meter} instance.
28
+ #
29
+ # @param [String] name Instrumentation package name
30
+ # @param [optional String] version Instrumentation package version
31
+ #
32
+ # @return [Meter]
33
+ def meter(name, version: nil)
34
+ version ||= ''
35
+ if @stopped
36
+ OpenTelemetry.logger.warn 'calling MeterProvider#meter after shutdown, a noop meter will be returned.'
37
+ OpenTelemetry::Metrics::Meter.new
38
+ else
39
+ @mutex.synchronize { @meter_registry[Key.new(name, version)] ||= Meter.new(name, version, self) }
40
+ end
41
+ end
42
+
43
+ # Attempts to stop all the activity for this {MeterProvider}.
44
+ #
45
+ # Calls MetricReader#shutdown for all registered MetricReaders.
46
+ #
47
+ # After this is called all the newly created {Meter}s will be no-op.
48
+ #
49
+ # @param [optional Numeric] timeout An optional timeout in seconds.
50
+ # @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
51
+ # a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
52
+ def shutdown(timeout: nil)
53
+ @mutex.synchronize do
54
+ if @stopped
55
+ OpenTelemetry.logger.warn('calling MetricProvider#shutdown multiple times.')
56
+ Export::FAILURE
57
+ else
58
+ start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
59
+ results = @metric_readers.map do |metric_reader|
60
+ remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
61
+ if remaining_timeout&.zero?
62
+ Export::TIMEOUT
63
+ else
64
+ metric_reader.shutdown(timeout: remaining_timeout)
65
+ end
66
+ end
67
+
68
+ @stopped = true
69
+ results.max || Export::SUCCESS
70
+ end
71
+ end
72
+ end
73
+
74
+ # This method provides a way for provider to notify the registered
75
+ # {MetricReader} instances, so they can do as much as they could to consume
76
+ # or send the metrics. Note: unlike Push Metric Exporter which can send data on
77
+ # its own schedule, Pull Metric Exporter can only send the data when it is
78
+ # being asked by the scraper, so ForceFlush would not make much sense.
79
+ #
80
+ # @param [optional Numeric] timeout An optional timeout in seconds.
81
+ # @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
82
+ # a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
83
+ def force_flush(timeout: nil)
84
+ @mutex.synchronize do
85
+ if @stopped
86
+ Export::SUCCESS
87
+ else
88
+ start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
89
+ results = @metric_readers.map do |metric_reader|
90
+ remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
91
+ if remaining_timeout&.zero?
92
+ Export::TIMEOUT
93
+ else
94
+ metric_reader.force_flush(timeout: remaining_timeout)
95
+ end
96
+ end
97
+
98
+ results.max || Export::SUCCESS
99
+ end
100
+ end
101
+ end
102
+
103
+ # Adds a new MetricReader to this {MeterProvider}.
104
+ #
105
+ # @param metric_reader the new MetricReader to be added.
106
+ def add_metric_reader(metric_reader)
107
+ @mutex.synchronize do
108
+ if @stopped
109
+ OpenTelemetry.logger.warn('calling MetricProvider#add_metric_reader after shutdown.')
110
+ else
111
+ @metric_readers.push(metric_reader)
112
+ @meter_registry.each_value { |meter| meter.add_metric_reader(metric_reader) }
113
+ end
114
+
115
+ nil
116
+ end
117
+ end
118
+
119
+ # @api private
120
+ def register_synchronous_instrument(instrument)
121
+ @mutex.synchronize do
122
+ @metric_readers.each do |mr|
123
+ instrument.register_with_new_metric_store(mr.metric_store)
124
+ end
125
+ end
126
+ end
127
+
128
+ # The type of the Instrument(s) (optional).
129
+ # The name of the Instrument(s). OpenTelemetry SDK authors MAY choose to support wildcard characters, with the question mark (?) matching exactly one character and the asterisk character (*) matching zero or more characters.
130
+ # The name of the Meter (optional).
131
+ # The version of the Meter (optional).
132
+ # The schema_url of the Meter (optional).
133
+ def add_view
134
+ # TODO: For each meter add this view to all applicable instruments
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module State
11
+ # MetricData is a Struct containing {MetricStream} data for export.
12
+ MetricData = Struct.new(:name, # String
13
+ :description, # String
14
+ :unit, # String
15
+ :instrument_kind, # Symbol
16
+ :resource, # OpenTelemetry::SDK::Resources::Resource
17
+ :instrumentation_scope, # OpenTelemetry::SDK::InstrumentationScope
18
+ :data_points, # Hash{Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>} => Numeric}
19
+ :aggregation_temporality, # Symbol
20
+ :start_time_unix_nano, # Integer nanoseconds since Epoch
21
+ :time_unix_nano) # Integer nanoseconds since Epoch
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Metrics
10
+ module State
11
+ # @api private
12
+ #
13
+ # The MetricStore module provides SDK internal functionality that is not a part of the
14
+ # public API.
15
+ class MetricStore
16
+ def initialize
17
+ @mutex = Mutex.new
18
+ @epoch_start_time = now_in_nano
19
+ @epoch_end_time = nil
20
+ @metric_streams = []
21
+ end
22
+
23
+ def collect
24
+ @mutex.synchronize do
25
+ @epoch_end_time = now_in_nano
26
+ snapshot = @metric_streams.map { |ms| ms.collect(@epoch_start_time, @epoch_end_time) }
27
+ @epoch_start_time = @epoch_end_time
28
+ snapshot
29
+ end
30
+ end
31
+
32
+ def add_metric_stream(metric_stream)
33
+ @mutex.synchronize do
34
+ @metric_streams = @metric_streams.dup.push(metric_stream)
35
+ nil
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def now_in_nano
42
+ (Time.now.to_r * 1_000_000_000).to_i
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end