-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1234 from DataDog/jirikuncar/rspec
feature: rspec integration
- Loading branch information
Showing
13 changed files
with
431 additions
and
0 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
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
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,38 @@ | ||
require 'ddtrace/contrib/configuration/settings' | ||
require 'ddtrace/contrib/rspec/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module RSpec | ||
module Configuration | ||
# Custom settings for the RSpec integration | ||
class Settings < Contrib::Configuration::Settings | ||
option :enabled do |o| | ||
o.default { env_to_bool(Ext::ENV_ENABLED, true) } | ||
o.lazy | ||
end | ||
|
||
option :analytics_enabled do |o| | ||
o.default { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, true) } | ||
o.lazy | ||
end | ||
|
||
option :analytics_sample_rate do |o| | ||
o.default { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) } | ||
o.lazy | ||
end | ||
|
||
option :service_name do |o| | ||
o.default { Datadog.configuration.service || Ext::SERVICE_NAME } | ||
o.lazy | ||
end | ||
|
||
option :operation_name do |o| | ||
o.default { ENV.key?(Ext::ENV_OPERATION_NAME) ? ENV[Ext::ENV_OPERATION_NAME] : Ext::OPERATION_NAME } | ||
o.lazy | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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] | ||
|
||
test_name = "#{example_group.description}::#{description}" | ||
trace_options = { | ||
app: Ext::APP, | ||
resource: test_name, | ||
service: configuration[:service_name], | ||
span_type: Datadog::Ext::AppTypes::TEST, | ||
tags: example_group.instance_variable_get(:@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, test_name) | ||
span.set_tag(Datadog::Ext::Test::TAG_SUITE, example_group.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 | ||
|
||
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 | ||
result | ||
end | ||
end | ||
end | ||
end | ||
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
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(Ext::EXAMPLE_GROUP_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 |
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,19 @@ | ||
module Datadog | ||
module Contrib | ||
module RSpec | ||
# RSpec integration constants | ||
module Ext | ||
APP = 'rspec'.freeze | ||
ENV_ANALYTICS_ENABLED = 'DD_TRACE_RSPEC_ANALYTICS_ENABLED'.freeze | ||
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_RSPEC_ANALYTICS_SAMPLE_RATE'.freeze | ||
ENV_ENABLED = 'DD_TRACE_RSPEC_ENABLED'.freeze | ||
ENV_OPERATION_NAME = 'DD_TRACE_RSPEC_OPERATION_NAME'.freeze | ||
FRAMEWORK = 'rspec'.freeze | ||
OPERATION_NAME = 'rspec.example'.freeze | ||
EXAMPLE_GROUP_OPERATION_NAME = 'rspec.example_group'.freeze | ||
SERVICE_NAME = 'rspec'.freeze | ||
TEST_TYPE = 'test'.freeze | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.