-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'minitest/spec' | ||
|
||
module OpenTelemetry | ||
module TestHelpers | ||
# Convenience features and Minitest extensions to support testing | ||
# around the metrics-api or metrics-sdk libraries. | ||
module Metrics | ||
module LoadedMetricsFeatures | ||
OTEL_METRICS_API_LOADED = !Gem.loaded_specs['opentelemetry-metrics-api'].nil? | ||
OTEL_METRICS_SDK_LOADED = !Gem.loaded_specs['opentelemetry-metrics-sdk'].nil? | ||
|
||
extend self | ||
|
||
def api_loaded? | ||
OTEL_METRICS_API_LOADED | ||
end | ||
|
||
def sdk_loaded? | ||
OTEL_METRICS_SDK_LOADED | ||
end | ||
end | ||
|
||
module MinitestExtensions | ||
def self.prepended(base) | ||
base.extend(self) | ||
end | ||
|
||
def self.included(base) | ||
base.extend(self) | ||
end | ||
|
||
def before_setup | ||
super | ||
reset_metrics_exporter | ||
end | ||
|
||
def with_metrics_sdk | ||
yield if LoadedMetricsFeatures.sdk_loaded? | ||
end | ||
|
||
def without_metrics_sdk | ||
yield unless LoadedMetricsFeatures.sdk_loaded? | ||
end | ||
|
||
def metrics_exporter | ||
with_metrics_sdk { METRICS_EXPORTER } | ||
end | ||
|
||
def reset_meter_provider | ||
with_metrics_sdk do | ||
resource = OpenTelemetry.meter_provider.resource | ||
OpenTelemetry.meter_provider = OpenTelemetry::SDK::Metrics::MeterProvider.new(resource: resource) | ||
OpenTelemetry.meter_provider.add_metric_reader(METRICS_EXPORTER) | ||
end | ||
end | ||
|
||
def reset_metrics_exporter | ||
with_metrics_sdk do | ||
METRICS_EXPORTER.pull | ||
METRICS_EXPORTER.reset | ||
end | ||
end | ||
|
||
def it(desc = 'anonymous', with_metrics_sdk: false, without_metrics_sdk: false, &block) | ||
return super(desc, &block) unless with_metrics_sdk || without_metrics_sdk | ||
|
||
raise ArgumentError, 'without_metrics_sdk and with_metrics_sdk must be mutually exclusive' if without_metrics_sdk && with_metrics_sdk | ||
|
||
return if with_metrics_sdk && !LoadedMetricsFeatures.sdk_loaded? | ||
return if without_metrics_sdk && LoadedMetricsFeatures.sdk_loaded? | ||
|
||
super(desc, &block) | ||
end | ||
end | ||
|
||
if LoadedMetricsFeatures.sdk_loaded? | ||
METRICS_EXPORTER = OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter.new | ||
OpenTelemetry.meter_provider.add_metric_reader(METRICS_EXPORTER) | ||
end | ||
|
||
Minitest::Spec.prepend(MinitestExtensions) | ||
end | ||
end | ||
end |
This file contains 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
26 changes: 26 additions & 0 deletions
26
test_helpers/test/opentelemetry/test_helpers/metrics_test.rb
This file contains 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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
|
||
require 'opentelemetry/test_helpers/metrics' # not loaded by default | ||
|
||
describe OpenTelemetry::TestHelpers::Metrics do | ||
describe 'dependencies' do | ||
let(:gemspec) { Gem.loaded_specs.fetch('opentelemetry-test-helpers') } | ||
let(:dependencies) { gemspec.dependencies.map(&:name) } | ||
|
||
# NOTE: The `metrics` module here is intended to facilitate testing | ||
# for instrumentation libraries that should function with or without | ||
# the metrics-api in the bundle. Including it in this test helper | ||
# should be considered a mistake unless additional provisions are made to preserve | ||
# this feature. | ||
it 'does not include the api or sdk gems' do | ||
_(dependencies).wont_include('opentelemetry-metrics-sdk') | ||
_(dependencies).wont_include('opentelemetry-metrics-api') | ||
end | ||
end | ||
end |
This file contains 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