|
| 1 | +# OpenTelemetry Instrumentation Test Helpers: Metrics |
| 2 | + |
| 3 | +This Ruby gem facilitates testing instrumentation libraries with respect to the OpenTelemetry Metrics API and SDK. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +Add the gem to your instrumentation's Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +# Gemfile |
| 11 | + |
| 12 | +group :test, :development do |
| 13 | + gem 'opentelemetry-metrics-test-helpers', path: '../../helpers/metrics-test-helpers', require: false |
| 14 | +end |
| 15 | +``` |
| 16 | + |
| 17 | +It's not necessary to add this gem as a development dependency in the gemspec. |
| 18 | +`opentelemetry-metrics-test-helpers` is not currently published to RubyGems, |
| 19 | +and it is expected that it will always be bundled from the source in this |
| 20 | +repository. |
| 21 | + |
| 22 | +Note that metrics-test-helpers makes no attempt to require |
| 23 | +the metrics API or SDK. It is designed to work with or without the metrics API and SDK defined, but you may experience issues if the API or SDK gem is in the gemfile but not yet loaded when the test helpers are initialized. |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +In a test_helper.rb, after the `configure` block, |
| 28 | +require this library: |
| 29 | + |
| 30 | +```ruby |
| 31 | +OpenTelemetry::SDK.configure do |c| |
| 32 | + c.error_handler = ->(exception:, message:) { raise(exception || message) } |
| 33 | + c.add_span_processor span_processor |
| 34 | +end |
| 35 | +require 'opentelemetry-metrics-test-helpers' |
| 36 | +``` |
| 37 | + |
| 38 | +If the library uses Appraisals, it is recommended to appraise with and without the metrics api and sdk gems. Note that any metrics implementation in instrumentation libraries should be written against the API only, but for testing the SDK is required to collect metrics data - testing under all three scenarios (no metrics at all, api only, and with the sdk) helps ensure compliance with this requirement. |
| 39 | + |
| 40 | +In a test: |
| 41 | + |
| 42 | +```ruby |
| 43 | +with_metrics_sdk do |
| 44 | + let(:metric_snapshots) do |
| 45 | + metrics_exporter.tap(&:pull) |
| 46 | + .metric_snapshots.select { |snapshot| snapshot.data_points.any? } |
| 47 | + .group_by(&:name) |
| 48 | + end |
| 49 | + |
| 50 | + it "uses metrics", with_metrics_sdk: true do |
| 51 | + # do something here ... |
| 52 | + _(metric_snapshots).count.must_equal(4) |
| 53 | + end |
| 54 | +end |
| 55 | +``` |
| 56 | + |
| 57 | +- `metrics_exporter` is automatically reset before each test. |
| 58 | +- `#with_metrics_sdk` will only yield if the SDK is present. |
| 59 | +- `#with_metrics_api` will only yield if the API is present |
| 60 | + |
| 61 | +## How can I get involved? |
| 62 | + |
| 63 | +The `opentelemetry-metrics-test-helpers` gem source is [on github][repo-github], along with related gems including `opentelemetry-instrumentation-pg` and `opentelemetry-instrumentation-trilogy`. |
| 64 | + |
| 65 | +The OpenTelemetry Ruby gems are maintained by the OpenTelemetry Ruby special interest group (SIG). You can get involved by joining us on our [GitHub Discussions][discussions-url], [Slack Channel][slack-channel] or attending our weekly meeting. See the [meeting calendar][community-meetings] for dates and times. For more information on this and other language SIGs, see the OpenTelemetry [community page][ruby-sig]. |
| 66 | + |
| 67 | +## License |
| 68 | + |
| 69 | +The `opentelemetry-helpers-sql-obfuscation` gem is distributed under the Apache 2.0 license. See [LICENSE][license-github] for more information. |
| 70 | + |
| 71 | +[repo-github]: https://github.com/open-telemetry/opentelemetry-ruby |
| 72 | +[license-github]: https://github.com/open-telemetry/opentelemetry-ruby-contrib/blob/main/LICENSE |
| 73 | +[ruby-sig]: https://github.com/open-telemetry/community#ruby-sig |
| 74 | +[community-meetings]: https://github.com/open-telemetry/community#community-meetings |
| 75 | +[slack-channel]: https://cloud-native.slack.com/archives/C01NWKKMKMY |
| 76 | +[discussions-url]: https://github.com/open-telemetry/opentelemetry-ruby/discussions |
0 commit comments