-
Notifications
You must be signed in to change notification settings - Fork 283
test: Add tests for MeterProvider API #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
robertlaurin
merged 7 commits into
open-telemetry:main
from
thoughtbot:elias--test-meter-provider-api
May 16, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1245d11
test: Add tests for MeterProvider API
elias19r db4f401
Merge branch 'main' into elias--test-meter-provider-api
elias19r 750877e
Number parameters (that _1 syntax) is not available in Ruby 2.6
elias19r 78c3482
Fix mock error regarding keyword args
elias19r 1c54ec3
Use version as a keyword warg in meter method
elias19r 0cc89a5
Fix linter issue
elias19r 7e61f90
Merge branch 'main' into elias--test-meter-provider-api
elias19r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
metrics_api/test/opentelemetry/metrics/meter_provider_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| describe OpenTelemetry::Metrics::MeterProvider do | ||
| describe '#meter' do | ||
| it 'requires a name' do | ||
| meter_provider = build_meter_provider | ||
|
|
||
| _(-> { meter_provider.meter }).must_raise(ArgumentError) | ||
| end | ||
|
|
||
| it 'returns an instance of Meter' do | ||
| meter_provider = build_meter_provider | ||
|
|
||
| assert(meter_provider.meter('test', version: '1.0.0').is_a?(OpenTelemetry::Metrics::Meter)) | ||
| end | ||
| end | ||
|
|
||
| def build_meter_provider | ||
| OpenTelemetry::Metrics::MeterProvider.new | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| describe OpenTelemetry do | ||
| after do | ||
| # TODO: After Metrics SDK is incoporated into OpenTelemetry SDK, move this | ||
| # to OpenTelemetry::TestHelpers.reset_opentelemetry | ||
| OpenTelemetry.instance_variable_set( | ||
| :@meter_provider, | ||
| OpenTelemetry::Internal::ProxyMeterProvider.new | ||
| ) | ||
| end | ||
|
|
||
| describe '#meter_provider and #meter_provider=' do | ||
| it 'initializes with a global instance of ProxyMeterProvider' do | ||
| assert(OpenTelemetry.meter_provider.is_a?(OpenTelemetry::Internal::ProxyMeterProvider)) | ||
| end | ||
|
|
||
| it 'sets global MeterProvider to the given meter_provider' do | ||
| new_meter_provider = OpenTelemetry::Metrics::MeterProvider.new | ||
|
|
||
| OpenTelemetry.meter_provider = new_meter_provider | ||
|
|
||
| assert(OpenTelemetry.meter_provider.object_id == new_meter_provider.object_id) | ||
| end | ||
|
|
||
| describe 'when global MeterProvider is an instance of Internal::ProxyMeterProvider' do | ||
| it 'sets ProxyMeterProvider#delegate to the given meter_provider and logs a debug message' do | ||
| proxy_meter_provider = OpenTelemetry.meter_provider | ||
| new_meter_provider = OpenTelemetry::Metrics::MeterProvider.new | ||
|
|
||
| OpenTelemetry::TestHelpers.with_test_logger do |log_stream| | ||
| OpenTelemetry.meter_provider = new_meter_provider | ||
|
|
||
| assert(proxy_meter_provider.instance_variable_get(:@delegate).object_id == new_meter_provider.object_id) | ||
| assert(OpenTelemetry.meter_provider.object_id == new_meter_provider.object_id) | ||
| assert(log_stream.string.match?(/Upgrading default proxy meter provider to #{new_meter_provider.class}/i)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to exercise that
OpenTelemetry.meter_providerinitially returns aProxyMeterProvider. This test kind of exercises that, but it is not ideal. If this test runs first, then it exercises that; otherwise, it doesn't: it just exercises the resetI was thinking if a proper reset would be calling
load 'opentelemetry-metrics-api.rbto reload the file instead of manually setting@meter_providerwithinstance_variable_setlikeOpenTelemetry::TestHelperssuggestsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's a bit tricky if you're looking to test the initial state before any tests run. I didn't put too much importance on this because in reality all you're testing with that is that this line of code contains no syntax errors.
If there was an issue in this code path, it would show up as a flakey test, which is not nothing.