Skip to content

Commit

Permalink
instrument RSpec::Core::Runner to trace test session
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Dec 7, 2023
1 parent a6e08e2 commit 4ce7077
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/datadog/ci/contrib/rspec/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "datadog/tracing/contrib/patcher"
require_relative "example"
require_relative "runner"

module Datadog
module CI
Expand All @@ -19,6 +20,7 @@ def target_version

def patch
::RSpec::Core::Example.include(Example)
::RSpec::Core::Runner.include(Runner)
end
end
end
Expand Down
51 changes: 51 additions & 0 deletions lib/datadog/ci/contrib/rspec/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require_relative "../../ext/test"
require_relative "ext"

module Datadog
module CI
module Contrib
module RSpec
# Instrument RSpec::Core::Runner
module Runner
def self.included(base)
base.prepend(InstanceMethods)
end

module InstanceMethods
def run(err, out)
return super unless configuration[:enabled]

test_session = CI.start_test_session(
tags: {
CI::Ext::Test::TAG_FRAMEWORK => Ext::FRAMEWORK,
CI::Ext::Test::TAG_FRAMEWORK_VERSION => CI::Contrib::RSpec::Integration.version.to_s,
CI::Ext::Test::TAG_TYPE => Ext::TEST_TYPE
},
service: configuration[:service_name]
)

result = super

if result != 0
test_session.failed!
else
test_session.passed!
end
test_session.finish

result
end

private

def configuration
Datadog.configuration.ci[:rspec]
end
end
end
end
end
end
end
21 changes: 21 additions & 0 deletions sig/datadog/ci/contrib/rspec/runner.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Datadog
module CI
module Contrib
module RSpec
module Runner
def self.included: (untyped base) -> untyped

module InstanceMethods
include ::RSpec::Core::Runner

def run: (untyped err, untyped out) -> untyped

private

def configuration: () -> untyped
end
end
end
end
end
end
64 changes: 64 additions & 0 deletions spec/datadog/ci/contrib/rspec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,68 @@ def expect_failure
expect_failure
end
end

context "with rspec runner" do
def devnull
File.new("/dev/null", "w")
end

it "creates test session span" do
with_new_rspec_environment do
RSpec.describe "SomeTest" do
it "foo" do
# DO NOTHING
end
end

options = ::RSpec::Core::ConfigurationOptions.new(%w[--pattern none])
::RSpec::Core::Runner.new(options).run(devnull, devnull)
end

expect(test_session_span).not_to be_nil

expect(test_session_span.span_type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION)
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_SPAN_KIND)).to eq(
Datadog::CI::Ext::AppTypes::TYPE_TEST
)
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_TYPE)).to eq(
Datadog::CI::Contrib::RSpec::Ext::TEST_TYPE
)
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK)).to eq(
Datadog::CI::Contrib::RSpec::Ext::FRAMEWORK
)
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_FRAMEWORK_VERSION)).to eq(
Datadog::CI::Contrib::RSpec::Integration.version.to_s
)
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(
Datadog::CI::Ext::Test::Status::PASS
)
end

context "with failures" do
it "creates test session span with failed state" do
with_new_rspec_environment do
RSpec.describe "SomeTest" do
it "foo" do
# DO NOTHING
end
end

RSpec.describe "SomeTestThatFailed" do
it "fails" do
expect(1).to eq(2)
end
end

options = ::RSpec::Core::ConfigurationOptions.new(%w[--pattern none])
::RSpec::Core::Runner.new(options).run(devnull, devnull)
end

expect(test_session_span).not_to be_nil
expect(test_session_span.get_tag(Datadog::CI::Ext::Test::TAG_STATUS)).to eq(
Datadog::CI::Ext::Test::Status::FAIL
)
end
end
end
end
4 changes: 4 additions & 0 deletions vendor/rbs/rspec/0/rspec.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ module RSpec::Core::Example
def description: () -> String
def full_description: () -> String
end

module RSpec::Core::Runner
def run: (untyped err, untyped out) -> untyped
end

0 comments on commit 4ce7077

Please sign in to comment.