|
90 | 90 | end |
91 | 91 | end |
92 | 92 |
|
| 93 | + describe '#registered_view with asynchronous counters' do |
| 94 | + before { reset_metrics_sdk } |
| 95 | + |
| 96 | + it 'emits asynchronous counter metrics with no data_points if view is drop' do |
| 97 | + OpenTelemetry::SDK.configure |
| 98 | + |
| 99 | + metric_exporter = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new |
| 100 | + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) |
| 101 | + |
| 102 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 103 | + OpenTelemetry.meter_provider.add_view('async_counter', aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::Drop.new) |
| 104 | + |
| 105 | + callback = proc { 42 } |
| 106 | + meter.create_observable_counter('async_counter', unit: 'smidgen', description: 'an async counter', callback: callback) |
| 107 | + |
| 108 | + metric_exporter.pull |
| 109 | + last_snapshot = metric_exporter.metric_snapshots |
| 110 | + |
| 111 | + _(last_snapshot).wont_be_empty |
| 112 | + _(last_snapshot[0].name).must_equal('async_counter') |
| 113 | + _(last_snapshot[0].unit).must_equal('smidgen') |
| 114 | + _(last_snapshot[0].description).must_equal('an async counter') |
| 115 | + _(last_snapshot[0].instrumentation_scope.name).must_equal('test') |
| 116 | + |
| 117 | + _(last_snapshot[0].data_points[0].value).must_equal 0 |
| 118 | + _(last_snapshot[0].data_points[0].start_time_unix_nano).must_equal 0 |
| 119 | + _(last_snapshot[0].data_points[0].time_unix_nano).must_equal 0 |
| 120 | + end |
| 121 | + |
| 122 | + it 'emits asynchronous counter metrics with only last value in data_points if view is last_value' do |
| 123 | + OpenTelemetry::SDK.configure |
| 124 | + |
| 125 | + metric_exporter = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new |
| 126 | + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) |
| 127 | + |
| 128 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 129 | + OpenTelemetry.meter_provider.add_view('async_counter', aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new) |
| 130 | + |
| 131 | + # Create a callback that returns different values each time it's called |
| 132 | + call_count = 0 |
| 133 | + callback = proc do |
| 134 | + call_count += 1 |
| 135 | + final_count = call_count * 10 |
| 136 | + final_count |
| 137 | + end |
| 138 | + |
| 139 | + meter.create_observable_counter('async_counter', unit: 'smidgen', description: 'an async counter', callback: callback) |
| 140 | + |
| 141 | + # Trigger multiple collections to simulate multiple callback invocations |
| 142 | + 3.times { metric_exporter.pull } |
| 143 | + last_snapshot = metric_exporter.metric_snapshots |
| 144 | + |
| 145 | + # Reason that use 3rd from last_snapshot, because in_memory_metrics_pull exporter |
| 146 | + # will store each collected metrics into its own data store unit (special case for the type of exporter) |
| 147 | + _(last_snapshot[2].data_points).wont_be_empty |
| 148 | + _(last_snapshot[2].data_points[0].value).must_equal 30 |
| 149 | + end |
| 150 | + |
| 151 | + it 'emits asynchronous counter metrics with sum of values if view is drop but not matching to instrument' do |
| 152 | + OpenTelemetry::SDK.configure |
| 153 | + |
| 154 | + metric_exporter = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new |
| 155 | + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) |
| 156 | + |
| 157 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 158 | + # View name doesn't match the instrument name |
| 159 | + OpenTelemetry.meter_provider.add_view('retnuoc_cnysa', aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::Drop.new) |
| 160 | + |
| 161 | + callback = proc { 15 } |
| 162 | + meter.create_observable_counter('async_counter', unit: 'smidgen', description: 'an async counter', callback: callback) |
| 163 | + |
| 164 | + metric_exporter.pull |
| 165 | + last_snapshot = metric_exporter.metric_snapshots |
| 166 | + |
| 167 | + _(last_snapshot[0].data_points).wont_be_empty |
| 168 | + # Since view doesn't match, it should use default aggregation (sum for counters) |
| 169 | + _(last_snapshot[0].data_points[0].value).must_equal 15 |
| 170 | + end |
| 171 | + |
| 172 | + it 'emits asynchronous counter metrics with multiple registered views' do |
| 173 | + OpenTelemetry::SDK.configure |
| 174 | + |
| 175 | + metric_exporter = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new |
| 176 | + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) |
| 177 | + |
| 178 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 179 | + # Add multiple views for the same instrument |
| 180 | + OpenTelemetry.meter_provider.add_view('async_counter', aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::Sum.new) |
| 181 | + OpenTelemetry.meter_provider.add_view('async_counter', aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new) |
| 182 | + |
| 183 | + callback = proc { 25 } |
| 184 | + meter.create_observable_counter('async_counter', unit: 'smidgen', description: 'an async counter', callback: callback) |
| 185 | + |
| 186 | + metric_exporter.pull |
| 187 | + last_snapshot = metric_exporter.metric_snapshots |
| 188 | + |
| 189 | + # Should have multiple metric data entries (one for each view) |
| 190 | + _(last_snapshot.size).must_be :>=, 2 |
| 191 | + |
| 192 | + # All should have the same instrument metadata |
| 193 | + last_snapshot.each do |snapshot| |
| 194 | + _(snapshot.name).must_equal('async_counter') |
| 195 | + _(snapshot.unit).must_equal('smidgen') |
| 196 | + _(snapshot.description).must_equal('an async counter') |
| 197 | + _(snapshot.instrumentation_scope.name).must_equal('test') |
| 198 | + _(snapshot.data_points).wont_be_empty |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + it 'emits asynchronous counter metrics with view attribute filtering' do |
| 203 | + OpenTelemetry::SDK.configure |
| 204 | + |
| 205 | + metric_exporter = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new |
| 206 | + OpenTelemetry.meter_provider.add_metric_reader(metric_exporter) |
| 207 | + |
| 208 | + meter = OpenTelemetry.meter_provider.meter('test') |
| 209 | + |
| 210 | + # Create a view that adds specific attributes |
| 211 | + view_with_attributes = OpenTelemetry::SDK::Metrics::View::RegisteredView.new( |
| 212 | + 'async_counter', |
| 213 | + aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::Sum.new, |
| 214 | + attribute_keys: { 'environment' => 'test', 'service' => 'metrics' } |
| 215 | + ) |
| 216 | + OpenTelemetry.meter_provider.instance_variable_get(:@registered_views) << view_with_attributes |
| 217 | + |
| 218 | + callback = proc { 35 } |
| 219 | + observable_counter = meter.create_observable_counter('async_counter', unit: 'smidgen', description: 'an async counter', callback: callback) |
| 220 | + observable_counter.add_attributes({ 'original' => 'value' }) |
| 221 | + |
| 222 | + metric_exporter.pull |
| 223 | + last_snapshot = metric_exporter.metric_snapshots |
| 224 | + |
| 225 | + _(last_snapshot[0].data_points).wont_be_empty |
| 226 | + _(last_snapshot[0].data_points[0].value).must_equal 35 |
| 227 | + |
| 228 | + # Check that view attributes are merged with original attributes |
| 229 | + attributes = last_snapshot[0].data_points[0].attributes |
| 230 | + _(attributes['environment']).must_equal 'test' |
| 231 | + _(attributes['service']).must_equal 'metrics' |
| 232 | + _(attributes['original']).must_equal 'value' |
| 233 | + end |
| 234 | + end |
| 235 | + |
93 | 236 | describe '#registered_view select instrument' do |
94 | 237 | let(:registered_view) { OpenTelemetry::SDK::Metrics::View::RegisteredView.new(nil, aggregation: ::OpenTelemetry::SDK::Metrics::Aggregation::LastValue.new) } |
95 | 238 | let(:instrumentation_scope) do |
|
0 commit comments