Skip to content

Commit

Permalink
share in-memory exporter between test cases
Browse files Browse the repository at this point in the history
instead of creating a new exporter in RumRailsTest and
RackRailsTest have EXPORTER instance created globally
and reset it in the teardown method of the testcases.

It may be useful to figure out in the future why creating
a new exporter in each testcase was resulting in no spans
making it to the exporter in the second test to run.
  • Loading branch information
tsloughter-splunk committed Jul 8, 2022
1 parent 1a5fb6c commit 53aace3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 1 addition & 5 deletions test/splunk/instrumentation/rack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ module Splunk
class RumRackTest < Test::Unit::TestCase
include Rack::Test::Methods

EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new

def setup
EXPORTER.reset

with_env("OTEL_SERVICE_NAME" => "test-service") do
span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)
Splunk::Otel.configure do |c|
Expand All @@ -26,7 +22,7 @@ def setup
end

def teardown
OpenTelemetry.tracer_provider.shutdown
reset_opentelemetry
end

def app
Expand Down
6 changes: 1 addition & 5 deletions test/splunk/instrumentation/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ module Splunk
class RumRailsTest < Test::Unit::TestCase
include Rack::Test::Methods

EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new

def setup
EXPORTER.reset

with_env("OTEL_SERVICE_NAME" => "test-service") do
span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER)
Splunk::Otel.configure do |c|
Expand All @@ -33,7 +29,7 @@ def setup
end

def teardown
OpenTelemetry.tracer_provider.shutdown
reset_opentelemetry
end

def app
Expand Down
16 changes: 16 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

require "test-unit"

EXPORTER = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new

def with_env(new_env)
env_to_reset = ENV.select { |k, _| new_env.key?(k) }
keys_to_delete = new_env.keys - ENV.keys
Expand All @@ -17,3 +19,17 @@ def with_env(new_env)
env_to_reset.each_pair { |k, v| ENV[k] = v }
keys_to_delete.each { |k| ENV.delete(k) }
end

def reset_opentelemetry
EXPORTER.reset

OpenTelemetry.instance_variable_set(
:@tracer_provider,
OpenTelemetry::Internal::ProxyTracerProvider.new
)

# OpenTelemetry will load the defaults
# on the next call to any of these methods
OpenTelemetry.error_handler = nil
OpenTelemetry.propagation = nil
end

0 comments on commit 53aace3

Please sign in to comment.