Skip to content

Commit

Permalink
split instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Nov 5, 2020
1 parent 1023ab5 commit a993445
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 121 deletions.
58 changes: 58 additions & 0 deletions lib/ddtrace/contrib/rspec/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Datadog
module Contrib
module RSpec
# Instrument RSpec::Core::Example
module Example
def self.included(base)
base.send(:prepend, InstanceMethods)
end

# Instance methods for configuration
module InstanceMethods
def run(example_group_instance, reporter)
configuration = Datadog.configuration[:rspec]
return super unless configuration[:enabled]

trace_options = {
app: Ext::APP,
resource: "#{example_group}::#{description}",
service: configuration[:service_name],
span_type: Ext::EXAMPLE_SPAN_TYPE
}

configuration[:tracer].trace(Ext::EXAMPLE_SPAN_TYPE, trace_options) do |span|
span.set_tag(Datadog::Ext::Test::TAG_FRAMEWORK, Ext::FRAMEWORK)
span.set_tag(Datadog::Ext::Test::TAG_NAME, description)
span.set_tag(Datadog::Ext::Test::TAG_SUITE, example_group)
span.set_tag(Datadog::Ext::Test::TAG_TYPE, Ext::TEST_TYPE)
span.set_tag(Datadog::Ext::Test::TAG_SPAN_KIND, Datadog::Ext::AppTypes::TEST)

# Set analytics sample rate
if Datadog::Contrib::Analytics.enabled?(configuration[:analytics_enabled])
Datadog::Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate])
end

# Measure service stats
Contrib::Analytics.set_measured(span)

super

case execution_result.status
when :passed
span.set_tag(Datadog::Ext::Test::TAG_STATUS, Datadog::Ext::Test::Status::PASS)
when :failed
span.status = 1
span.set_tag(Datadog::Ext::Test::TAG_STATUS, Datadog::Ext::Test::Status::FAIL)
span.set_error(execution_result.exception)
else
if execution_result.example_skipped?
span.set_tag(Datadog::Ext::Test::TAG_STATUS, Datadog::Ext::Test::Status::SKIP)
end
end
end
end
end
end
end
end
end
61 changes: 61 additions & 0 deletions lib/ddtrace/contrib/rspec/example_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Datadog
module Contrib
module RSpec
# Instrument RSpec::Core::ExampleGroup
module ExampleGroup
def self.included(base)
base.singleton_class.send(:prepend, ClassMethods)
end

# Class methods for configuration
module ClassMethods
def run(reporter = ::RSpec::Core::NullReporter)
configuration = Datadog.configuration[:rspec]
return super unless configuration[:enabled]

trace_options = {
app: Ext::APP,
resource: description,
service: configuration[:service_name],
span_type: Datadog::Ext::AppTypes::TEST,
tags: tags.merge(Datadog.configuration.tags)
}

configuration[:tracer].trace(configuration[:operation_name], trace_options) do |span|
span.set_tag(Datadog::Ext::Test::TAG_FRAMEWORK, Ext::FRAMEWORK)
span.set_tag(Datadog::Ext::Test::TAG_NAME, description)
span.set_tag(Datadog::Ext::Test::TAG_SUITE, file_path)
span.set_tag(Datadog::Ext::Test::TAG_TYPE, Ext::TEST_TYPE)
span.set_tag(Datadog::Ext::Test::TAG_SPAN_KIND, Datadog::Ext::AppTypes::TEST)

# Set analytics sample rate
if Datadog::Contrib::Analytics.enabled?(configuration[:analytics_enabled])
Datadog::Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate])
end

# Measure service stats
Contrib::Analytics.set_measured(span)

result = super

if ::RSpec.world.wants_to_quit
span.status = 1
span.set_tag(Datadog::Ext::Test::TAG_STATUS, Datadog::Ext::Test::Status::FAIL)
else
span.set_tag(Datadog::Ext::Test::TAG_STATUS, Datadog::Ext::Test::Status::PASS)
end

result
end
end

private

def tags
@tags ||= Datadog::Ext::CI.tags(ENV)
end
end
end
end
end
end
116 changes: 0 additions & 116 deletions lib/ddtrace/contrib/rspec/instrumentation.rb

This file was deleted.

7 changes: 4 additions & 3 deletions lib/ddtrace/contrib/rspec/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'ddtrace/contrib/patcher'
require 'ddtrace/contrib/rspec/instrumentation'
require 'ddtrace/contrib/rspec/example'
require 'ddtrace/contrib/rspec/example_group'

module Datadog
module Contrib
Expand All @@ -15,8 +16,8 @@ def target_version
end

def patch
::RSpec::Core::Example.send(:include, Instrumentation::Example)
::RSpec::Core::ExampleGroup.send(:include, Instrumentation::ExampleGroup)
::RSpec::Core::Example.send(:include, Example)
::RSpec::Core::ExampleGroup.send(:include, ExampleGroup)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/ddtrace/contrib/rspec/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

context 'is patched' do
it 'has a custom bases' do
expect(example.ancestors).to include(Datadog::Contrib::RSpec::Instrumentation::Example::InstanceMethods)
expect(example_group.ancestors).to include(Datadog::Contrib::RSpec::Instrumentation::ExampleGroup::ClassMethods)
expect(example.ancestors).to include(Datadog::Contrib::RSpec::Example::InstanceMethods)
expect(example_group.ancestors).to include(Datadog::Contrib::RSpec::ExampleGroup::ClassMethods)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@

# Helper matchers
RSpec::Matchers.define_negated_matcher :not_be, :be

Datadog.configure do |c|
c.use :rspec, {}
end

0 comments on commit a993445

Please sign in to comment.